def test_socket_manager_publish_safe(): ( "SocketManager().publish_safe() should serialize " "before sending, using the configured backend" ) # Given a zmq mock zmq = Mock() # And a context context = Mock() # And a serializer serializer = Mock(name="serializer") serializer.pack.side_effect = lambda x: "<pac({})ked>".format(repr(x)) # And a socket manager manager = SocketManager(zmq, context, serialization_backend=serializer) # And a socket socket = manager.create("foobar", zmq.REP) # When I call .publish_safe() manager.publish_safe("foobar", "topic", "PAYLOAD") # Then it should have packed the payload before sending serializer.pack.assert_called_once_with("PAYLOAD") serializer.pack.return_value = "packed" socket.send_multipart.assert_called_once_with( [cast_bytes("topic"), cast_bytes("<pac('PAYLOAD')ked>")] )
def emit(self, record): """Emit a log message on my socket.""" try: topic, record.msg = record.msg.split(TOPIC_DELIM, 1) except Exception: topic = "untopic" record.__dict__['hostname'] = HOSTNAME try: bmsg = cast_bytes(self.format(record)) except Exception: self.handleError(record) return topic_list = [self.root_topic, topic] btopic = b'.'.join(cast_bytes(t) for t in topic_list) blevel = cast_bytes(record.levelname) if not self.is_connected(): self.connect() time.sleep(0.1) self.socket.send_multipart([btopic, blevel, bmsg])
def emit(self, record): """Emit a log message on my socket.""" try: topic, record.msg = record.msg.split(TOPIC_DELIM,1) except Exception: topic = "" try: bmsg = cast_bytes(self.format(record)) except Exception: self.handleError(record) return topic_list = [] if self.root_topic: topic_list.append(self.root_topic) topic_list.append(record.levelname) if topic: topic_list.append(topic) btopic = b'.'.join(cast_bytes(t) for t in topic_list) self.socket.send_multipart([btopic, bmsg])
def emit(self, record): """Emit a log message on my socket.""" try: bmsg = cast_bytes(record.msg) except Exception: self.handleError(record) return retry = 2 while retry > 0: try: self.socket.send(bmsg) poller = zmq.Poller() poller.register(self.socket, flags=zmq.POLLIN) # 0.5s超时 polled = poller.poll(500) if len(polled) > 0: rep = self.socket.recv() if rep != "OK": retry -= 1 continue else: return else: retry -= 1 continue except Exception as e: do_stdout("log send error {}".format(e)) self.handleError(record) self.socket.close() self.socket = self.ctx.socket(zmq.REQ) self.socket.connect(self.address) return
def __init__(self, channel_type, endpoint=None, context=None, identity=None): """Initalise the IpcChannel object. :param channel_type: ZeroMQ socket type, using CHANNEL_TYPE_xxx constants :param endpoint: URI of channel endpoint, can be specified later :param context: ZeroMQ context, will be initialised if not given :param identity: channel identity for DEALER type sockets """ # Initalise channel type and endpoint if given self.channel_type = channel_type if endpoint: self.endpoint = endpoint # Initialise the ZeroMQ context or obtain the current instance self.context = context or zmq.Context().instance() # Create the socket self.socket = self.context.socket(self.channel_type) # If the socket type is DEALER, set the identity, chosing a random # UUID4 value if not specified if self.channel_type == self.CHANNEL_TYPE_DEALER: if identity is None: identity = "{:04x}-{:04x}".format(random.randrange(0x10000), random.randrange(0x10000)) self.identity = identity self.socket.setsockopt(zmq.IDENTITY, cast_bytes(identity)) # pylint: disable=no-member
def form_ipc_msg(self, msgType, msgVal, msgDevice, msgConfig, blink_timeout, blink_rate): """ Forms and returns an encoded IPC Message :param msgtype: The type of message i.e CMD :param msgVal: The value of the request i.e STATUS/CONFIG :param msgDevice: The device alias name :param msgConfig: The configuration parameter Returns the encoded ipc message for sending over zmq socket """ request = IpcMessage(msgType, msgVal) request.set_param("DEVICE", msgDevice) if msgVal == "CONFIG": request.set_param("CONFIG", msgConfig) if msgConfig == "BLINK": request.set_param("TIMEOUT", blink_timeout) request.set_param("RATE", blink_rate) print("%s Configuring service request..." % self.identity) # Encode the message to be sent request = request.encode() if isinstance(request, unicode): request = cast_bytes(request) return request
def emit(self, record): msg = cast_bytes(self.format(record)) # except Exception: # self.handleError(record) # # return data = {'msg': msg, 'args': record.args, 'level': record.levelno} self.sockets.publish_safe(self.socket_name, self.topic_name, data)
def emit(self, record): """Emit a log message on my socket.""" try: topic, record.msg = record.msg.split(TOPIC_DELIM,1) except Exception: topic = "" try: bmsg = cast_bytes(self.format(record)) except Exception: self.handleError(record) return if isinstance(topic, str): btopic = cast_bytes(topic) else: print("Exception: topic is not string:{topic}".format(topic=topic)) btopic = b'Debug' self.socket.send_multipart([btopic, bmsg])
def send(self, data): """Send data to the IpcChannel. :param: data to send on channel """ # If the data is unicode (like all Python3 native strings), convert to a # byte stream to be sent on the socket if isinstance(data, unicode): data = cast_bytes(data) # Send the data self.socket.send(data)
def emit(self, record): """Emit a log message on my socket.""" try: topic, record.msg = record.msg.split(TOPIC_DELIM, 1) except Exception: topic = "untopic" record.__dict__['hostname'] = HOSTNAME try: bmsg = cast_bytes(self.format(record)) except Exception: self.handleError(record) return topic_list = [self.root_topic, topic] btopic = b'.'.join(cast_bytes(t) for t in topic_list) blevel = cast_bytes(record.levelname) self.socket.send_multipart([btopic, blevel, bmsg])
def send_multipart(self, data): """ Send data to the IpcChannel, in multiple parts. :param: data to send, as an iterable object """ for idx, part in enumerate(data): if isinstance(part, unicode): data[idx] = cast_bytes(part) if self._stream: self._stream.send_multipart(data) else: super(IpcTornadoChannel, self).send_multipart(data)
def emit(self, record): """Emit a log message on my socket.""" try: bmsg = cast_bytes(self.format(record)) except Exception: self.handleError(record) return try: self.socket.send_multipart([record.levelname.encode(), bmsg], flags=zmq.DONTWAIT) except zmq.error.Again: # Raised if the SNDHWM is reached. self.handleError(record) print("HWM reached, dropping messages") return
def send(self, data): """Send data to the IpcChannel. :param: data to send on channel """ # If the data are unicode (like all Python3 native strings), convert to a # byte stream to be sent on the socket if isinstance(data, unicode): data = cast_bytes(data) # If a Stream is registered send the data out on the tornado IO Loop if self._stream: self._stream.send(data) else: super(IpcTornadoChannel, self).send(data)
def __init__(self, topic_name, host=None, pub_port=None, serializer='DEFAULT'): topic_name = get_remapped_topic_name(topic_name) if topic_name.count(' '): raise Error('you can\'t use " " for topic_name') if topic_name == '': raise Error('empty topic name is not supported') if host is None: host = get_master_host() if pub_port is None: pub_port = get_pub_port() if serializer is 'DEFAULT': serializer = get_default_serializer() self._serializer = serializer context = zmq.Context() self._socket = context.socket(zmq.PUB) self._socket.connect( 'tcp://{host}:{port}'.format(host=host, port=pub_port)) self._topic = cast_bytes(topic_name + get_topic_suffix())
def publish(self, payload): '''Publish payload to the topic .. note:: If you publishes just after creating Publisher instance, it will causes lost of message. You have to add sleep if you just want to publish once. >>> pub = jps.Publisher('topic') >>> time.sleep(0.1) >>> pub.publish('{data}') :param payload: data to be published. This is ok if the data is not json. ''' if self._serializer is not None: payload = self._serializer(payload) if self._topic == '*': # special case for publish everything msg = payload else: msg = '{topic} {data}'.format(topic=self._topic, data=payload) self._socket.send(cast_bytes(msg))
def __init__(self, topic_name, callback=None, host=None, sub_port=None, deserializer='DEFAULT'): topic_name = get_remapped_topic_name(topic_name) if topic_name.count(' '): raise Error('you can\'t use " " for topic_name') if topic_name == '': raise Error('empty topic name is not supported') if host is None: host = get_master_host() if sub_port is None: sub_port = get_sub_port() if deserializer is 'DEFAULT': deserializer = get_default_deserializer() self._deserializer = deserializer context = zmq.Context() self._socket = context.socket(zmq.SUB) self._socket.connect('tcp://{host}:{port}'.format(host=host, port=sub_port)) self._topic = cast_bytes(topic_name + get_topic_suffix()) self._topic_without_star = self._topic.rstrip('*') self._socket.setsockopt(zmq.SUBSCRIBE, self._topic_without_star) self._user_callback = callback if type(callback) == types.MethodType: # arg=[self, message, topic_name] self._user_callback_takes_topic_name = callback.im_func.func_code.co_argcount == 3 elif type(callback) == types.FunctionType: # arg=[message, topic_name] self._user_callback_takes_topic_name = callback.func_code.co_argcount == 2 elif hasattr(callback, '__call__'): # arg=[self, message, topic_name] self._user_callback_takes_topic_name = callback.__call__.im_func.func_code.co_argcount == 3 else: self._user_callback_takes_topic_name = False if type(callback) == types.InstanceType: print 'argcoutn = ' + callback.func_code.co_argcount self._thread = None self._poller = zmq.Poller() self._poller.register(self._socket, zmq.POLLIN)
def run_rep(self): ''' sends a request, waits for a reply, returns response ''' while True: try: client_address, request = self.socket.recv_multipart() request = IpcMessage(from_str=request) print("received request : %s from %s" % (request, client_address.decode())) # Get the alias device name used in the request req_alias = request.get_param("DEVICE") # get the address of the device req_address = self.process_address(request) # get the message value (CONFIG/STATUS/READ) req_msg_val = request.get_msg_val() req_device = None req_config = None reply_message = IpcMessage(msg_type="CMD", msg_val="NOTIFY") # Find the device attached to that request address for device in self.devices: if req_address == device.get_addr(): req_device = device if req_msg_val == "CONFIG": req_config = request.get_param("CONFIG") if req_config == "BLINK": req_timeout = request.get_param("TIMEOUT") req_rate = request.get_param("RATE") req_device.set_config(req_config, req_timeout, req_rate) reply_string = "Processed request from %s. %s at address %s blinked for %s seconds. \ Current status is %s." % (client_address.decode(),req_alias, req_address, req_timeout, req_device.get_config()) else: req_device.set_config(req_config) reply_string = "Processed Request from %s. Set %s at \ address %s to: %s." % (client_address.decode(), req_alias, req_address, req_device.get_config()) if req_msg_val == "STATUS": rep_status = req_device.get_status() reply_string = "Processed Request from %s. Status of %s at \ address %s is: %s." % (client_address.decode(), req_alias, req_address, rep_status) if req_msg_val == "READ": rep_value = req_device.get_data() reply_string = "Processed Request from %s. Value of %s at \ address %s is: %s." % (client_address.decode(), req_alias, req_address, rep_value) reply_message.set_param("REPLY", reply_string) # Encode the message for sending reply_message = reply_message.encode() # check if its unicode, if so covert to bytes if isinstance(reply_message, unicode): reply_message = cast_bytes(reply_message) # send a multipart back to the client self.socket.send_multipart([client_address, b"", reply_message,]) except IpcMessageException as err: print("IPC MESSAGE Error Found %s: " % str(err))
import colorama colorama.init() colors = { 'DEBUG': colorama.Fore.LIGHTCYAN_EX, 'INFO': colorama.Fore.LIGHTWHITE_EX, 'WARNING': colorama.Fore.YELLOW, 'ERROR': colorama.Fore.LIGHTRED_EX, 'CRITICAL': colorama.Fore.LIGHTRED_EX, '__RESET__': colorama.Fore.RESET, } else: colors = {} ctx = zmq.Context() sub = ctx.socket(zmq.SUB) sub.subscribe(cast_bytes(args.topic)) sub.connect(args.zmq_pub_url) topic_widths = {} while (1): try: if sub.poll(10, zmq.POLLIN): topic, msg = sub.recv_multipart() topics = topic.decode('utf8').strip().split('.') if args.align: topics.extend( ' ' for extra in range(len(topics), len(topic_widths))) aligned_parts = [] for key, part in enumerate(topics):
def __init__(self, socket_manager, socket_name='logs', topic_name='logs'): logging.Handler.__init__(self) self.sockets = socket_manager self.socket_name = socket_name self.topic_name = cast_bytes(topic_name)
def spin_once(self): with self._lock: request = self._socket.recv() self._socket.send(cast_bytes(self._callback(request)))