src/pyams_zmq/handler.py
changeset 25 9352acb9366b
parent 0 11f0f97d508f
equal deleted inserted replaced
24:36b9e48666ce 25:9352acb9366b
     8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
     9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
    10 # FOR A PARTICULAR PURPOSE.
    10 # FOR A PARTICULAR PURPOSE.
    11 #
    11 #
    12 
    12 
       
    13 """PyAMS_zmq.handler module
       
    14 
       
    15 This module provides a default implementation of a 0MQ messages handler.
       
    16 
       
    17 These handlers are simple classes which delegate the real message handling
       
    18 to their "handler" attribute, which is a simple class which may have a method matching the
       
    19 message type.
       
    20 """
       
    21 
       
    22 from zmq.utils import jsonapi
       
    23 from zope.interface import implementer
       
    24 
       
    25 from pyams_zmq.interfaces import IZMQMessageHandler
       
    26 
       
    27 
    13 __docformat__ = 'restructuredtext'
    28 __docformat__ = 'restructuredtext'
    14 
    29 
    15 
    30 
    16 # import standard library
       
    17 from zmq.utils import jsonapi
       
    18 
       
    19 # import interfaces
       
    20 from pyams_zmq.interfaces import IZMQMessageHandler
       
    21 
       
    22 # import packages
       
    23 from zope.interface import implementer
       
    24 
       
    25 
       
    26 @implementer(IZMQMessageHandler)
    31 @implementer(IZMQMessageHandler)
    27 class ZMQMessageHandler(object):
    32 class ZMQMessageHandler:
    28     """Base class for message handlers for a :class:`pyams_zmq.process.Process`.
    33     """Base class for message handlers for a :class:`pyams_zmq.process.Process`.
    29 
    34 
    30     Inheriting classes only need to implement a handler function for each
    35     Inheriting classes only need to implement a handler function for each
    31     message type.
    36     message type.
    32     """
    37     """
    33 
    38 
    34     handler = None
    39     handler = None
    35 
    40 
    36     def __init__(self, process, stream, stop, handler=None, json_load=-1):
    41     def __init__(self, process, stream, stop, handler=None, json_load=-1):
       
    42         # pylint: disable=too-many-arguments
    37         # ZMQ parent process
    43         # ZMQ parent process
    38         self.process = process
    44         self.process = process
    39         self._json_load = json_load
    45         self._json_load = json_load
    40         # Response stream
    46         # Response stream
    41         self.rep_stream = stream
    47         self.rep_stream = stream
    42         self._stop = stop
    48         self._stop = stop
    43         # Response handler
    49         # Response handler
    44         self.rep_handler = handler or self.handler()
    50         self.rep_handler = handler or self.handler()  # pylint: disable=not-callable
    45         self.rep_handler.process = process
    51         self.rep_handler.process = process
    46 
    52 
    47     def __call__(self, msg):
    53     def __call__(self, msg):
    48         """
    54         """Gets called when a message is received by the stream this handler is
    49         Gets called when a messages is received by the stream this handlers is
    55         registered with.
    50         registered at. *msg* is a list as returned by
    56 
    51         :meth:`zmq.core.socket.Socket.recv_multipart`.
    57         :param msg: message content; it's a list as returned by
       
    58             :meth:`zmq.core.socket.Socket.recv_multipart`.
    52         """
    59         """
    53         # Try to JSON-decode the index "self._json_load" of the message
    60         # Try to JSON-decode the index "self._json_load" of the message
    54         i = self._json_load
    61         i = self._json_load
    55         msg_type, data = jsonapi.loads(msg[i])
    62         msg_type, data = jsonapi.loads(msg[i])
    56         msg[i] = data
    63         msg[i] = data