def test_set_xmsg_data(self):
     data = xMsgData()
     data.type = xMsgData.T_FLSINT32A
     data.FLSINT32A.append(1)
     data.FLSINT32A.append(2)
     data.FLSINT32A.append(3)
     msg = xMsgMessage.create_with_xmsg_data(self.topic, data)
     self.assertEqual("a:b:c", self.message.topic)
     ds_data = xMsgData()
     ds_data.ParseFromString(msg.data)
     self.assertEqual(data, ds_data)
     self.assertEqual(data.T_FLSINT32A, ds_data.T_FLSINT32A)
     self.assertIsInstance(msg, xMsgMessage)
def main():
    """Publisher usage:
    ::
        "Usage: python xmsg/examples/Publisher <array_size>
    """
    # Create an xMsg actor
    publisher = xMsg("test_publisher")

    # Create a socket connections to the xMsg node
    connection = publisher.connect()

    # Build Topic
    topic = xMsgTopic.build("test_domain", "test_subject", "test_type")

    # Register at xMsg Registrar
    reg_address = RegAddress()
    publisher.register_as_publisher(reg_address, topic)

    # Publish data for ever...
    while True:
        data = [float(randint(1, 10)) for _ in range(int(5))]

        # Create transient data
        t_msg_data = xMsgData_pb2.xMsgData()
        t_msg_data.type = xMsgData_pb2.xMsgData.T_FLOATA
        t_msg_data.FLOATA.extend(data)
        t_msg = xMsgMessage.from_xmsg_data(topic, t_msg_data)

        # Publishing
        publisher.publish(connection, t_msg)
        print "publishing : T_FLOATA"
        xMsgUtil.sleep(1)
Beispiel #3
0
def main(array_size, proxy_host):
    """Publisher usage:
    ::
        "Usage: python xmsg/examples/Publisher
    """
    # Create an xMsg actor
    publisher = xMsg("test_publisher")

    # Create a socket connections to the xMsg node
    connection = publisher.get_connection(ProxyAddress(proxy_host))

    # Build Topic
    topic = xMsgTopic.build("test_domain", "test_subject", "test_type")

    # Publish data for ever...
    try:
        while True:
            data = [float(randint(1, 10)) for _ in range(int(array_size))]

            # Create transient data
            t_msg_data = xMsgData()
            t_msg_data.type = xMsgData.T_FLOATA
            t_msg_data.FLOATA.extend(data)
            t_msg = xMsgMessage.from_xmsg_data(topic, t_msg_data)

            # Publishing
            publisher.publish(connection, t_msg)

            print "publishing : T_FLOATA"
            xMsgUtil.sleep(1)
    except KeyboardInterrupt:
        publisher.destroy()
Beispiel #4
0
def main(array_size, proxy_host, alert_every_n):
    """Publisher usage:
    ::
        Usage: python xmsg/examples/Publisher <array_size> <fe_host>
    """

    sync_publisher = xMsg("test_publisher")

    # Create a socket connections to the xMsg node
    connection = sync_publisher.get_connection(ProxyAddress(proxy_host))

    # Build Topic
    topic = xMsgTopic.build("test_domain", "test_subject", "test_type")

    # Publish data for ever...
    count = 0
    start_time = time.time()
    while True:
        try:
            t_msg_data = xMsgData()
            t_msg_data.type = xMsgData.T_FLOATA
            data = [float(random.randint(1, 10)) for _ in range(int(array_size))]
            # Create transient data

            t_msg_data.FLOATA.extend(data)
            transient_message = xMsgMessage.from_xmsg_data(topic, t_msg_data)

            # Publishing
            sync_publisher.sync_publish(connection, transient_message, 10)
            count += 1

            if count % alert_every_n == 0:
                now = time.time()
                elapsed = now - start_time
                xMsgUtil.log("With %d messages: %s" % (alert_every_n, elapsed))
                start_time = time.time()

        except KeyboardInterrupt:
            print ""
            xMsgUtil.log("Removing Registration and terminating thread pool.")
            sync_publisher.destroy()
            return
Beispiel #5
0
    def __init__(self, n_messages, proxy_port):
        proxy_address = ProxyAddress("localhost", proxy_port)
        super(Producer, self).__init__(name="the_producer",
                                       proxy_address=proxy_address)
        connection = self.get_connection(proxy_address)

        class _CallBack(xMsgCallBack):
            def __init__(self):
                super(_CallBack, self).__init__()
                self.count = 0
                self.alert_count = n_messages
                self.start_time = 0

            def callback(self, msg):
                if self.count == 0:
                    self.start_time = time.time()
                else:
                    if self.count % self.alert_count == 0:
                        now = time.time()
                        elapsed = now - self.start_time
                        xMsgUtil.log("With %d messages: %s" %
                                     (n_messages, elapsed))
                        self.start_time = time.time()

                self.count += 1

        subscription = self.subscribe(proxy_address, "the_reply", _CallBack(),
                                      1)
        try:
            while True:
                t_msg_data = xMsgData()
                t_msg_data.type = xMsgData.T_FLOATA
                data = [float(random.randint(1, 10)) for _ in range(int(10))]

                # Create transient data
                t_msg_data.FLOATA.extend(data)
                t_msg = xMsgMessage.from_xmsg_data(self.myname, t_msg_data)
                self.publish(connection, t_msg)
        except KeyboardInterrupt:
            self.unsubscribe(subscription)
            self.destroy()
Beispiel #6
0
    def write(self, data):
        s_data = xMsgData()
        if self.__mimetype == Mimetype.SINT32:
            s_data.VLSINT32 = data
        elif self.__mimetype == Mimetype.SINT64:
            s_data.VLSINT64 = data
        elif self.__mimetype == Mimetype.SFIXED32:
            s_data.FLSINT32 = data
        elif self.__mimetype == Mimetype.SFIXED64:
            s_data.FLSINT64 = data
        elif self.__mimetype == Mimetype.DOUBLE:
            s_data.DOUBLE = data
        elif self.__mimetype == Mimetype.FLOAT:
            s_data.FLOAT = float(data)
        elif self.__mimetype == Mimetype.STRING:
            s_data.STRING = str(data)
        elif self.__mimetype == Mimetype.BYTES:
            s_data.BYTES = bytes(data)
        elif self.__mimetype == Mimetype.ARRAY_SINT32:
            s_data.VLSINT32A.extend(data)
        elif self.__mimetype == Mimetype.ARRAY_SINT64:
            s_data.VLSINT64A.extend(data)
        elif self.__mimetype == Mimetype.ARRAY_SFIXED32:
            s_data.FLSINT32A.extend(data)
        elif self.__mimetype == Mimetype.ARRAY_SFIXED64:
            s_data.FLSINT64A.extend(data)
        elif self.__mimetype == Mimetype.ARRAY_DOUBLE:
            s_data.DOUBLEA.extend(data)
        elif self.__mimetype == Mimetype.ARRAY_FLOAT:
            s_data.FLOATA.extend(data)
        elif self.__mimetype == Mimetype.ARRAY_STRING:
            s_data.STRINGA.extend(data)
        else:
            raise ValueError("Received invalid mimetype %s" % self.__mimetype)

        return self.__native_serializer.write(s_data)
Beispiel #7
0
 def write(self, data):
     s_data = xMsgData()
     s_data.MergeFrom(data)
     return s_data.SerializeToString()
Beispiel #8
0
 def read(self, data):
     s_data = xMsgData()
     s_data.ParseFromString(data)
     return s_data
Beispiel #9
0
 def callback(self, msg):
     # User takes care of the proper de-serialization
     ds_msg = xMsgData()
     ds_msg.ParseFromString(msg.data)
     return msg
 def callback(self, msg):
     # User takes care of the proper de-serialization
     ds_msg = xMsgData_pb2.xMsgData()
     ds_msg.ParseFromString(msg.get_data())
     print "Subscriber received data : \n%s" % ds_msg
     return msg