def log(self, msg_type, msg): """logging method with custom type tag Args: msg_type (String): type of message to log msg (String): message to log """ prefix = msg_type log_msg = " <%s> %s" % (self._class_name, msg) xMsgUtil.log(prefix + log_msg + Style.RESET_ALL)
def _closing(self, conn): try: yield conn except Exception as e: xMsgUtil.log(e.message) raise e finally: conn.close()
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
def process_request(self, msg): topic_frame, type_frame, id_frame = msg if type_frame == xMsgConstants.CTRL_CONNECT: self._router_socket.send_multipart([id_frame, type_frame]) elif type_frame == xMsgConstants.CTRL_SUBSCRIBE: self._pub_socket.send_multipart([id_frame, type_frame]) elif type_frame == xMsgConstants.CTRL_REPLY: self._router_socket.send_multipart([id_frame, type_frame]) else: xMsgUtil.log("unexpected request: " + str(type_frame))
def start(self): """Starts the proxy server of the xMsgNode on a local host. It will launch the xmsg pub-sub proxy, it will exit if another node running with the same address """ try: self._controller = self._Controller(self.context, self.proxy_address) self._proxy = self._Proxy(self.context, self.proxy_address) self._controller.start() self._proxy.start() except zmq.error.ZMQError: raise AddressInUseException("Proxy address already being used") except KeyboardInterrupt: xMsgUtil.log("Exiting proxy...") self.stop()
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 run(self): """xMsgRegService thread method to process registration requests During its execution will receive the registration request and will process the petition, sending a response. Method description: :: while thread is alive: reg_service_response = process_request(registration_request) send_back(reg_service_response) """ registrar_socket = None try: registrar_socket = self.context.socket(zmq.REP) registrar_socket.bind(self.address) except zmq.ZMQError: raise AddressInUseException("Registrar address already being used") else: while threading.currentThread().is_alive() and not self.stopped(): try: registrar_request = registrar_socket.recv_multipart() if not registrar_request: continue response = self.process_request(registrar_request) registrar_socket.send_multipart(response.msg()) except zmq.error.ContextTerminated: break except KeyboardInterrupt: break except Exception as e: xMsgUtil.log("xMsgRegService received: %s" % e.message) break finally: return
def request(self, request, timeout): """Sends a request to the given registrar server and waits the response. Args: socket (zmq.Socket): zmq socket for communication request (xMsgRegRequest): xMsg request object timeout (int): timeout for processing request in seconds Returns: bytes[]: serialized registration information """ try: self._connection.send_multipart(request.msg()) except: raise RegistrationException("Error sending registration message") poller = zmq.Poller() poller.register(self._connection, zmq.POLLIN) try: if poller.poll(timeout * 1000): # timeout in milliseconds request = self._connection.recv_multipart() response = xMsgRegResponse.create_from_multipart_request( request) if response.get_status() != xMsgConstants.SUCCESS: raise RegistrationException(response.get_status()) xMsgUtil.log("Info: xMsg actor has been registered in node") return response.get_data() else: raise TimeoutReached("Timeout processing registration request") except KeyboardInterrupt: raise RegistrationException("Keyboard Interrupt while trying to \ register")
def run(self): try: while True: try: s_msg = self._queue.get() if s_msg: self._queue.task_done() if s_msg == u"STOP": xMsgUtil.log("Worker received stop message") self._queue_interr.put("excp") return msg = xMsgMessage.from_serialized_data(s_msg) self._callback.callback(msg) except KeyboardInterrupt: # executor process catches ctrl-c from subscriber self._queue_interr.put("excp") return except IOError: pass return
def main(): import argparse parser = argparse.ArgumentParser() parser.add_argument("--host", help="Proxy address", type=str, default="localhost") parser.add_argument("--port", help="Proxy port", type=int, default=xMsgConstants.DEFAULT_PORT) args = parser.parse_args() host = args.host port = args.port try: proxy = xMsgProxy(zmq.Context(), host, port) proxy.start() except AddressInUseException as e: xMsgUtil.log(e.message) return
def start(self): try: """Starts the registrar services""" xMsgUtil.log("Local ip: %s" % xMsgUtil.get_local_ip()) self.registrar_service.start() xMsgUtil.log("Local registration and discovery server starting") self.proxy.start() self.registrar_service.join() except AddressInUseException as e: xMsgUtil.log(e.message)
def process_request(self, registration_request): """Method to process the registration request and interact with the Registration DB The process_request method will receive a request from a xMsg actor to register or for obtaining registration info from the database. The method is able to respond to the following requests (this request is taken from the topic of the request message): * *REMOVE_ALL_REGISTRATION* * *REGISTER_PUBLISHER* * *REGISTER_SUBSCRIBER* * *REMOVE_PUBLISHER* * *REMOVE_SUBSCRIBER* * *FIND_PUBLISHER* * *FIND_SUBSCRIBER* * *REMOVE_ALL_REGISTRATION* Any other request, not listed among the previous request topics is considered unknown, and the method will return *xMsgContants.ERROR* Args: registration_request (bytes[]): Multipart message received by the xMsgRegService thread. Returns: xMsgResponse: response to the received registration request """ try: request = xMsgRegRequest.create_from_multipart( registration_request) sender = "%s:%s" % (xMsgUtil.get_local_ip(), xMsgConstants.REGISTRAR) registration = Set([]) s_host = xMsgConstants.UNDEFINED msg = "Received a request from %s to %s" % (request.sender, request.topic) xMsgUtil.log(msg) if request.topic == xMsgConstants.REMOVE_ALL_REGISTRATION: s_host = request.get_data().domain if request.topic == xMsgConstants.REGISTER_PUBLISHER: self.publishers_db.register(request.get_data()) elif request.topic == xMsgConstants.REGISTER_SUBSCRIBER: self.subscribers_db.register(request.get_data()) elif request.topic == xMsgConstants.REMOVE_PUBLISHER: self.publishers_db.remove(request.get_data()) elif request.topic == xMsgConstants.REMOVE_SUBSCRIBER: self.subscribers_db.remove(request.get_data()) elif request.topic == xMsgConstants.REMOVE_ALL_REGISTRATION: self.subscribers_db.remove_by_host(s_host) self.publishers_db.remove_by_host(s_host) elif request.topic == xMsgConstants.FIND_PUBLISHER: register = request.get_data() registration = self.publishers_db.find(register.domain, register.subject, register.type) elif request.topic == xMsgConstants.FIND_SUBSCRIBER: register = request.get_data() registration = self.subscribers_db.find( register.domain, register.subject, register.type) else: xMsgUtil.log("Warning: unknown registration request type...") xMsgUtil.log("Warning: got message %s" % request.topic) registration = xMsgConstants.ERROR # send a response to request return xMsgRegResponse(request.topic, sender, registration) except zmq.error.ContextTerminated: xMsgUtil.log("xMsgRegService: Context terminated") return