コード例 #1
0
ファイル: Publisher.py プロジェクト: JeffersonLab/xmsg-python
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()
コード例 #2
0
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)
コード例 #3
0
def main():
    subscriber = xMsg("test_subscriber")

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

    # Register at xMsg Registrar
    reg_address = RegAddress()
    subscriber.register_as_subscriber(reg_address, topic)

    # Connect
    connection = subscriber.connect()

    try:
        if subscriber.find_publisher(reg_address, topic):
            callback = ExampleSubscriberCallback()
            subscription = subscriber.subscribe(topic, connection, callback)

            xMsgUtil.keep_alive()

    except KeyboardInterrupt:
        subscriber.unsubscribe(subscription)
        subscriber.remove_as_subscriber(reg_address, topic)
        subscriber.destroy(10)
        return

    except RegistrationException:
        print "Unable to register"
        return -1
コード例 #4
0
def main(pool_size, proxy_host):
    # Create an xMsg actor
    subscriber = xMsg("test_subscriber", pool_size=pool_size)

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

    subscription = None
    try:
        subscription = subscriber.subscribe(ProxyAddress(proxy_host), topic,
                                            ExampleSubscriberCallback())
        xMsgUtil.keep_alive()

    except KeyboardInterrupt:
        subscriber.unsubscribe(subscription)
        subscriber.destroy()
コード例 #5
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
コード例 #6
0
class TestXMsgRegDatabase(unittest.TestCase):
    topic0 = xMsgTopic.build("test_domain_0", "test_subject_0", "test_type_0")
    topic1 = xMsgTopic.build("test_domain_1", "test_subject_1", "test_type_1")
    topic2 = xMsgTopic.build("test_domain_2", "test_subject_2", "test_type_2")
    topic3 = xMsgTopic.build("test_domain_3", "test_subject_3", "test_type_3")

    reg1 = new_registration(topic0, "name0", "10.2.2.1")
    asimov1 = new_registration(topic0, "asimov", "10.2.2.1")
    asimov2 = new_registration(topic0, "asimov", "10.2.2.2")
    bradbury1 = new_registration(topic0, "bradbury", "10.2.2.1")
    bradbury2 = new_registration(topic0, "bradbury", "10.2.2.2")
    twain1 = new_registration(topic1, "twain", "10.2.2.1")
    twain2 = new_registration(topic1, "twain", "10.2.2.2")
    brando = new_registration(topic2, "brando", "10.2.2.2")
    tolkien = new_registration(topic3, "tolkien", "10.2.2.1")

    topic = "test_domain:test_subject:test_type"

    def setUp(self):
        self.db = xMsgRegDatabase()

    def test_empty_db(self):
        self.assertIsInstance(self.db.all(), list) and not self.db.all()

    def test_register_first_topic_creates_first_topic(self):
        self.db.register(self.reg1)

        self.assertEqual(self.db.get(self.topic0),
                         Set([self.reg1.SerializeToString()]))

    def test_add_next_registration_of_first_topic(self):
        self.db.register(self.twain1)
        self.db.register(self.twain2)
        test_case = self.db.get(self.topic1)

        self.assertEqual(len(test_case), 2)
        self.assertEqual(test_case,
                         Set([self.twain1.SerializeToString(),
                              self.twain2.SerializeToString()]))

    def test_add_first_registration_new_topic(self):
        self.db.register(self.asimov1)
        self.db.register(self.bradbury1)
        self.db.register(self.twain1)
        self.db.register(self.tolkien)

        self.assertEqual(len(self.db.all()), 3)
        self.assertEqual(self.db.all(),
                         [str(self.topic0),
                          str(self.topic1),
                          str(self.topic3)])

        test_case = self.db.get(self.topic0)
        self.assertEqual(test_case, Set([self.asimov1.SerializeToString(),
                                         self.bradbury1.SerializeToString()]))

        test_case = self.db.get(self.topic1)
        self.assertEqual(test_case, Set([self.twain1.SerializeToString()]))

        test_case = self.db.get(self.topic3)
        self.assertEqual(test_case, Set([self.tolkien.SerializeToString()]))

    def add_duplicated_registration_does_nothing(self):
        self.db.clear()
        self.db.register(self.asimov1)
        self.db.register(self.bradbury1)
        self.db.register(self.bradbury1)

        test_case = self.db.get(self.topic0)
        self.assertEqual(test_case,
                         Set([self.asimov1.SerializeToString(),
                              self.bradbury1.SerializeToString()]))

    def test_remove_register_with_only_topic_db_one_element(self):
        self.db.clear()
        self.db.register(self.reg1)
        self.db.remove(self.reg1)

        self.assertIsNone(self.db.get(self.topic0))
        self.assertEqual(self.db.all(), [])

    def test_remove_register_with_only_topic_with_several_element(self):
        self.db.clear()
        self.db.register(self.asimov1)
        self.db.register(self.asimov2)
        self.db.register(self.bradbury1)
        self.db.remove(self.asimov2)

        test_case = self.db.find("test_domain_0", "test_subject_0", "test_type_0")

        self.assertEqual(test_case, Set([self.asimov1.SerializeToString(),
                                         self.bradbury1.SerializeToString()]))

    def test_remove_register_with_topic_with_one_element(self):
        self.db.clear()
        self.db.register(self.asimov1)
        self.db.register(self.twain1)
        self.db.register(self.twain2)
        self.db.remove(self.asimov1)

        test_case = self.db.find("test_domain_1", "test_subject_1", "test_type_1")
        self.assertEqual(self.db.all(), [str(self.topic1)])
        self.assertEqual(test_case, Set([self.twain1.SerializeToString(),
                                         self.twain2.SerializeToString()]))

    def test_remove_register_with_topic_db_several_element(self):
        self.db.clear()
        self.db.register(self.asimov1)
        self.db.register(self.asimov2)
        self.db.register(self.bradbury1)
        self.db.register(self.twain1)
        self.db.register(self.twain2)
        self.db.remove(self.bradbury1)

        self.assertEqual(self.db.all(), [str(self.topic0), str(self.topic1)])

        self.assertEqual(self.db.get("test_domain_0:test_subject_0:test_type_0"),
                         Set([self.asimov1.SerializeToString(),
                              self.asimov2.SerializeToString()]))

        self.assertEqual(self.db.get("test_domain_1:test_subject_1:test_type_1"),
                         Set([self.twain1.SerializeToString(),
                              self.twain2.SerializeToString()]))

    def test_remove_missing_registration_does_nothing(self):
        self.db.clear()
        self.db.register(self.asimov1)
        self.db.register(self.asimov2)

        self.db.remove(self.bradbury1)

        self.assertEqual(self.db.get(str(self.topic0)),
                         Set([self.asimov1.SerializeToString(),
                              self.asimov2.SerializeToString()]))

    def test_remove_by_host(self):
        self.db.clear()
        self.db.register(self.asimov1)
        self.db.register(self.asimov2)
        self.db.register(self.bradbury1)
        self.db.register(self.bradbury2)
        self.db.register(self.twain1)
        self.db.register(self.twain2)

        self.db.remove_by_host("10.2.2.1")

        self.assertEqual(self.db.get("test_domain_0:test_subject_0:test_type_0"),
                         Set([self.asimov2.SerializeToString(),
                              self.bradbury2.SerializeToString()]))
        self.assertEqual(self.db.get("test_domain_1:test_subject_1:test_type_1"),
                         Set([self.twain2.SerializeToString()]))

    def test_get_by_domain(self):
        self.db.clear()
        self.db.register(self.reg1)

        test_case = self.db.find(self.reg1.domain)
        self.assertEqual(test_case, Set([self.reg1.SerializeToString()]))

    def test_get_by_domain_and_subject(self):
        self.db.clear()
        self.db.register(self.reg1)

        test_case = self.db.find(self.reg1.domain, self.reg1.subject)
        self.assertEqual(test_case, Set([self.reg1.SerializeToString()]))

    def test_find_Set_by_topic(self):
        self.db.clear()
        self.db.register(self.reg1)

        self.assertEqual(self.db.get(self.topic0),
                         Set([self.reg1.SerializeToString()]))
        self.assertIsNone(self.db.get(self.topic1))

    def test_find_unregistered_topic_returns_none(self):
        self.db.clear()
        self.db.register(self.asimov1)
        self.db.register(self.bradbury2)
        self.db.register(self.brando)
        self.db.register(self.tolkien)

        self.assertIsNone(self.db.find(str(self.topic1)))
コード例 #7
0
 def test_get_type(self):
     for case in BUILD_TOPIC_CASES:
         test_case = xMsgTopic.build(**case["args"])
         self.assertIsNotNone(test_case.type())
コード例 #8
0
 def test_build_topic(self):
     for case in BUILD_TOPIC_CASES:
         test_case = xMsgTopic.build(**case["args"])
         self.assertEqual(str(test_case), case["result"])