示例#1
0
文件: client.py 项目: alphatb/domogik
class MQSyncReq(object):

    """Class for the MDP client side.

    Thin asynchronous encapsulation of a zmq.REQ socket.
    Provides a :func:`request` method with optional timeout.

    Objects of this class are ment to be integrated into the
    asynchronous IOLoop of pyzmq.

    :param context:  the ZeroMQ context to create the socket in.
    :type context:   zmq.Context
    :param endpoint: the enpoint to connect to.
    :type endpoint:  str
    :param service:  the service the client should use
    :type service:   str
    """

    _proto_version = b'MDPC01'

    def __init__(self, context):
        """Initialize the MDPClient.
        """
        if ("domogik.common.configloader" in sys.modules):
            cfg = Loader('mq').load()
            config = dict(cfg[1])
            endpoint = "tcp://{0}:{1}".format(config['ip'], config['req_rep_port'])
        else:
            ip = Parameter.objects.get(key='mq-ip')
            port = Parameter.objects.get(key='mq-req_rep_port')
            endpoint = "tcp://{0}:{1}".format(ip.value, port.value)
        self.socket = ZmqSocket(context, zmq.REQ)
        self.socket.connect(endpoint)         
        return

    def shutdown(self):
        """Method to deactivate the client connection completely.

        Will delete the stream and the underlying socket.

        .. warning:: The instance MUST not be used after :func:`shutdown` has been called.

        :rtype: None
        """
        if not self.socket:
            return
        self.socket.setsockopt(zmq.LINGER, 0)
        self.socket.close()
        self.socket = None
        return

    def rawrequest(self, service, msg, timeout=None):
        if not timeout or timeout < 0.0:
            timeout = None
	if type(msg) in (bytes, str):
            msg = [msg]
	to_send = [self._proto_version, service]
	to_send.extend(msg)
	self.socket.send_multipart(to_send)
	ret = None
        rlist, _, _ = select([self.socket], [], [], timeout)
        if rlist and rlist[0] == self.socket:
            ret = self.socket.recv_multipart()
            ret.pop(0) # remove service from reply
            ret.pop(0)
	return ret

    def request(self, service, msg, timeout=None):
        """Send the given message.

        :param msg:     message parts to send.
        :type msg:      list of str
        :param timeout: time to wait in milliseconds.
        :type timeout:  int
        
        :rtype : message parts
        """
	ret = self.rawrequest( service, msg, timeout)
	msg = None
	if ret:
	    msg = MQMessage()
            msg.set(ret)
        return msg
示例#2
0
class MQSyncReq(object):

    """Class for the MDP client side.

    Thin asynchronous encapsulation of a zmq.REQ socket.
    Provides a :func:`request` method with optional timeout.

    Objects of this class are ment to be integrated into the
    asynchronous IOLoop of pyzmq.

    :param context:  the ZeroMQ context to create the socket in.
    :type context:   zmq.Context
    :param endpoint: the enpoint to connect to.
    :type endpoint:  str
    :param service:  the service the client should use
    :type service:   str
    """

    _proto_version = b'MDPC01'

    def __init__(self, context):
        """Initialize the MDPClient.
        """
        if ("domogik.common.configloader" in sys.modules):
            cfg = Loader('mq').load()
            config = dict(cfg[1])
            endpoint = "tcp://{0}:{1}".format(config['ip'], config['req_rep_port'])
        else:
            ip = Parameter.objects.get(key='mq-ip')
            port = Parameter.objects.get(key='mq-req_rep_port')
            endpoint = "tcp://{0}:{1}".format(ip.value, port.value)
        self.socket = ZmqSocket(context, zmq.REQ)
        self.socket.connect(endpoint)         
        return

    def shutdown(self):
        """Method to deactivate the client connection completely.

        Will delete the stream and the underlying socket.

        .. warning:: The instance MUST not be used after :func:`shutdown` has been called.

        :rtype: None
        """
        if not self.socket:
            return
        self.socket.setsockopt(zmq.LINGER, 0)
        self.socket.close()
        self.socket = None
        return

    def rawrequest(self, service, msg, timeout=None):
        if not timeout or timeout < 0.0:
            timeout = None
	if type(msg) in (bytes, str):
            msg = [msg]
	to_send = [self._proto_version, service]
	to_send.extend(msg)
	self.socket.send_multipart(to_send)
	ret = None
        rlist, _, _ = select([self.socket], [], [], timeout)
        if rlist and rlist[0] == self.socket:
            ret = self.socket.recv_multipart()
            ret.pop(0) # remove service from reply
            ret.pop(0)
	return ret

    def request(self, service, msg, timeout=None):
        """Send the given message.

        :param msg:     message parts to send.
        :type msg:      list of str
        :param timeout: time to wait in milliseconds.
        :type timeout:  int
        
        :rtype : message parts
        """
	ret = self.rawrequest( service, msg, timeout)
	msg = None
	if ret:
	    msg = MQMessage()
            msg.set(ret)
        return msg