def test__equal__operator_false(self): reg_1 = ProxyAddress("1.1.1.2", 80) reg_2 = ProxyAddress("1.1.1.1", 80) self.assertNotEqual(reg_1, reg_2) reg_1 = ProxyAddress("1.1.1.1", 80) reg_2 = ProxyAddress("1.1.1.1", 81) self.assertNotEqual(reg_1, reg_2)
def __init__(self, name, proxy_host="localhost", proxy_port=xMsgConstants.DEFAULT_PORT, frontend_host="localhost", frontend_port=xMsgConstants.DEFAULT_PORT): self._proxy_address = ProxyAddress(proxy_host, proxy_port) self._fe_address = ProxyAddress(frontend_host, frontend_port) super(ClaraBase, self).__init__(name, self._proxy_address, RegAddress(frontend_host, frontend_port)) self.clara_home = os.environ.get('PCLARA_HOME') or ""
def __init__(self, name, proxy_address=None, registrar_address=None, **kwargs): """xMsg Constructor Constructor, requires the name of the FrontEnd host that is used to create to the registrar service running within the xMsgFE. Creates the zmq context object and thread pool for servicing received messages in a separate threads. Args: name (String): name of the xMsg actor instance local_address (String): local hostname frontend_address (String): frontend hostname Keyword arguments: pool_size (int): size of the actors thread pool Returns: xMsg: xmsg object """ # Name of xMsg Actor self.myname = name # Set of subscriptions self.my_subscriptions = [] self.context = zmq.Context.instance() self.pool_size = kwargs.pop("pool_size", False) or 2 if proxy_address: if isinstance(proxy_address, basestring): self.default_proxy_address = ProxyAddress(proxy_address) elif isinstance(proxy_address, ProxyAddress): self.default_proxy_address = proxy_address else: self.default_proxy_address = ProxyAddress() if registrar_address: if isinstance(registrar_address, basestring): self.default_registrar_address = RegAddress(registrar_address) elif isinstance(registrar_address, RegAddress): self.default_registrar_address = registrar_address else: self.default_registrar_address = RegAddress() # Initialize registration driver self.connection_manager = ConnectionManager(self.context)
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()
def test_ProxyAddress_constructor_only_with_hostname_and_pub_port_and_sub_port( self): proxy_addr = ProxyAddress("localhost", 1111, 2222) self.assertEqual(proxy_addr.host, self.host) self.assertEqual(proxy_addr.pub_port, 1111) self.assertEqual(proxy_addr.sub_port, 2222) self.assertIsInstance(proxy_addr, ProxyAddress)
def __init__(self, pool_size, proxy_port): proxy_address = ProxyAddress("localhost", proxy_port) super(Consumer, self).__init__(name="the_consumer", proxy_address=proxy_address, pool_size=pool_size) self.connection = self.get_connection(proxy_address) self.queue = mp.Queue()
def send(self, msg): """Sends xMsgMessage object to a Clara component Args: msg (xMsgMessage): xMsg transient message object """ proxy_address = ProxyAddress(ClaraUtils.get_dpe_host(msg.topic), ClaraUtils.get_dpe_port(msg.topic)) conn = self.get_connection(proxy_address) self.publish(conn, msg)
def sync_send(self, msg, timeout): """Sends xMsgMessage object to an xMsg actor synchronously Args: msg (xMsgMessage): xMsg transient message object timeout (int): response message timeout in seconds """ proxy_address = ProxyAddress(ClaraUtils.get_dpe_host(msg.topic), ClaraUtils.get_dpe_port(msg.topic)) conn = self.get_connection(proxy_address) self.sync_publish(conn, msg, timeout)
def __init__(self, context, host, port): """xMsgProxy Constructor Args: context (zmq.Context): zmq context object host (String): proxy hostname port (int): proxy port """ self.context = context self.proxy_address = ProxyAddress(host, port) self._proxy = None self._controller = None
def listen(self, topic, callback): """This method simply calls xMsg subscribe method passing the reference to user provided callback method. Args: topic (xMsgTopic): Topic of subscription callback (xMsgCallBack): User provided callback object Returns: xMsgSubscription """ proxy_address = ProxyAddress(ClaraUtils.get_dpe_host(topic)) handler = self.subscribe(proxy_address, topic, callback) return handler
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()
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
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()
def test_get_proxy_connection(self): connection_manager = CM(zmq.Context.instance()) proxy = connection_manager.get_proxy_connection( ProxyAddress(), xMsgConnectionSetup()) self.assertIsInstance(proxy, xMsgConnection)
def test_ProxyAddress_constructor_only_with_hostname(self): proxy_addr = ProxyAddress("localhost") self.assertEqual(proxy_addr.host, self.host) self.assertEqual(proxy_addr.pub_port, int(constants.DEFAULT_PORT)) self.assertEqual(proxy_addr.sub_port, int(constants.DEFAULT_PORT) + 1) self.assertIsInstance(proxy_addr, ProxyAddress)
def test_ProxyAddress_empty_constructor(self): proxy_addr = ProxyAddress() self.assertEqual(proxy_addr.host, self.host) self.assertEqual(proxy_addr.pub_port, int(constants.DEFAULT_PORT)) self.assertEqual(proxy_addr.sub_port, int(constants.DEFAULT_PORT) + 1) self.assertIsInstance(proxy_addr, ProxyAddress)