def __init__(self, factory, *endpoints): """ Constructor. @param factory: ZeroMQ Twisted factory @type factory: L{ZmqFactory} @param endpoints: ZeroMQ addresses for connect/bind @type endpoints: C{list} of L{ZmqEndpoint} """ self.factory = factory self.endpoints = endpoints self.socket = Socket(factory.context, self.socketType) self.queue = deque() self.recv_parts = [] self.fd = self.socket.getsockopt(constants.FD) self.socket.setsockopt(constants.LINGER, factory.lingerPeriod) self.socket.setsockopt(constants.MCAST_LOOP, int(self.allowLoopbackMulticast)) self.socket.setsockopt(constants.RATE, self.multicastRate) self.socket.setsockopt(constants.HWM, self.highWaterMark) if self.identity is not None: self.socket.setsockopt(constants.IDENTITY, self.identity) self._connectOrBind() self.factory.connections.add(self) self.factory.reactor.addReader(self)
def __init__(self, factory, endpoint=None, identity=None): """ Constructor. One endpoint is passed to the constructor, more could be added via call to C{addEndpoints}. @param factory: ZeroMQ Twisted factory @type factory: L{ZmqFactory} @param endpoint: ZeroMQ address for connect/bind @type endpoint: C{list} of L{ZmqEndpoint} @param identity: socket identity (ZeroMQ) @type identity: C{str} """ self.factory = factory self.endpoints = [] self.identity = identity self.socket = Socket(factory.context, self.socketType) self.queue = deque() self.recv_parts = [] self.read_scheduled = None self.fd = self.socket_get(constants.FD) self.socket_set(constants.LINGER, factory.lingerPeriod) if not ZMQ3: self.socket_set( constants.MCAST_LOOP, int(self.allowLoopbackMulticast)) self.socket_set(constants.RATE, self.multicastRate) if not ZMQ3: self.socket_set(constants.HWM, self.highWaterMark) else: self.socket_set(constants.SNDHWM, self.highWaterMark) self.socket_set(constants.RCVHWM, self.highWaterMark) if ZMQ3 and self.tcpKeepalive: self.socket_set( constants.TCP_KEEPALIVE, self.tcpKeepalive) self.socket_set( constants.TCP_KEEPALIVE_CNT, self.tcpKeepaliveCount) self.socket_set( constants.TCP_KEEPALIVE_IDLE, self.tcpKeepaliveIdle) self.socket_set( constants.TCP_KEEPALIVE_INTVL, self.tcpKeepaliveInterval) if self.identity is not None: self.socket_set(constants.IDENTITY, self.identity) if endpoint: self.addEndpoints([endpoint]) self.factory.connections.add(self) self.factory.reactor.addReader(self) self.doRead()
def __init__(self, factory, *endpoints): """ Constructor. @param factory: ZeroMQ Twisted factory @type factory: L{ZmqFactory} @param endpoints: ZeroMQ addresses for connect/bind @type endpoints: C{list} of L{ZmqEndpoint} """ self.factory = factory self.endpoints = endpoints self.socket = Socket(factory.context, self.socketType) self.queue = deque() self.recv_parts = [] self.fd = self.socket.getsockopt(constants.FD) self.socket.setsockopt(constants.LINGER, factory.lingerPeriod) self.socket.setsockopt( constants.MCAST_LOOP, int(self.allowLoopbackMulticast)) self.socket.setsockopt(constants.RATE, self.multicastRate) self.socket.setsockopt(constants.HWM, self.highWaterMark) if self.identity is not None: self.socket.setsockopt(constants.IDENTITY, self.identity) self._connectOrBind() self.factory.connections.add(self) self.factory.reactor.addReader(self)
def __init__(self, socketType=None): """ @param context: Context this socket is to be associated with @type factory: L{ZmqFactory} @param socketType: Type of socket to create @type socketType: C{int} """ if socketType is not None: self.socketType = socketType assert self.socketType is not None self._ctx = getContext() self._zsock = Socket(getContext()._zctx, self.socketType) self._queue = deque() self.fd = self._zsock.getsockopt(constants.FD) self._ctx._sockets.add(self) self._ctx.reactor.addReader(self)
def __init__(self, factory, endpoint=None, identity=None): """ Constructor. One endpoint is passed to the constructor, more could be added via call to C{addEndpoints}. @param factory: ZeroMQ Twisted factory @type factory: L{ZmqFactory} @param endpoint: ZeroMQ address for connect/bind @type endpoint: C{list} of L{ZmqEndpoint} @param identity: socket identity (ZeroMQ) @type identity: C{str} """ self.factory = factory self.endpoints = [] self.identity = identity self.socket = Socket(factory.context, self.socketType) self.queue = deque() self.recv_parts = [] self.scheduled_doRead = None self.fd = self.socket.getsockopt(constants.FD) self.socket.setsockopt(constants.LINGER, factory.lingerPeriod) try: self.socket.setsockopt( constants.MCAST_LOOP, int(self.allowLoopbackMulticast)) self.socket.setsockopt(constants.HWM, self.highWaterMark) except: self.socket.setsockopt(constants.SNDHWM, self.highWaterMark) self.socket.setsockopt(constants.RCVHWM, self.highWaterMark) self.socket.setsockopt(constants.RATE, self.multicastRate) if self.identity is not None: self.socket.setsockopt(constants.IDENTITY, self.identity) if endpoint: self.addEndpoints([endpoint]) self.factory.connections.add(self) self.factory.reactor.addReader(self)
class ZmqConnection(object): """ Connection through ZeroMQ, wraps up ZeroMQ socket. @cvar socketType: socket type, from ZeroMQ @cvar allowLoopbackMulticast: is loopback multicast allowed? @type allowLoopbackMulticast: C{boolean} @cvar multicastRate: maximum allowed multicast rate, kbps @type multicastRate: C{int} @cvar highWaterMark: hard limit on the maximum number of outstanding messages 0MQ shall queue in memory for any single peer @type highWaterMark: C{int} @ivar factory: ZeroMQ Twisted factory reference @type factory: L{ZmqFactory} @ivar socket: ZeroMQ Socket @type socket: L{Socket} @ivar endpoints: ZeroMQ addresses for connect/bind @type endpoints: C{list} of L{ZmqEndpoint} @ivar fd: file descriptor of zmq mailbox @type fd: C{int} @ivar queue: output message queue @type queue: C{deque} """ implements(IReadDescriptor, IFileDescriptor) socketType = None allowLoopbackMulticast = False multicastRate = 100 highWaterMark = 0 identity = None def __init__(self, factory, *endpoints): """ Constructor. @param factory: ZeroMQ Twisted factory @type factory: L{ZmqFactory} @param endpoints: ZeroMQ addresses for connect/bind @type endpoints: C{list} of L{ZmqEndpoint} """ self.factory = factory self.endpoints = endpoints self.socket = Socket(factory.context, self.socketType) self.queue = deque() self.recv_parts = [] self.fd = self.socket.getsockopt(constants.FD) self.socket.setsockopt(constants.LINGER, factory.lingerPeriod) self.socket.setsockopt( constants.MCAST_LOOP, int(self.allowLoopbackMulticast)) self.socket.setsockopt(constants.RATE, self.multicastRate) self.socket.setsockopt(constants.HWM, self.highWaterMark) if self.identity is not None: self.socket.setsockopt(constants.IDENTITY, self.identity) self._connectOrBind() self.factory.connections.add(self) self.factory.reactor.addReader(self) def shutdown(self): """ Shutdown connection and socket. """ self.factory.reactor.removeReader(self) self.factory.connections.discard(self) self.socket.close() self.socket = None self.factory = None def __repr__(self): return "%s(%r, %r)" % ( self.__class__.__name__, self.factory, self.endpoints) def fileno(self): """ Part of L{IFileDescriptor}. @return: The platform-specified representation of a file descriptor number. """ return self.fd def connectionLost(self, reason): """ Called when the connection was lost. Part of L{IFileDescriptor}. This is called when the connection on a selectable object has been lost. It will be called whether the connection was closed explicitly, an exception occurred in an event handler, or the other end of the connection closed it first. @param reason: A failure instance indicating the reason why the connection was lost. L{error.ConnectionLost} and L{error.ConnectionDone} are of special note, but the failure may be of other classes as well. """ log.err(reason, "Connection to ZeroMQ lost in %r" % (self)) if self.factory: self.factory.reactor.removeReader(self) def _readMultipart(self): """ Read multipart in non-blocking manner, returns with ready message or raising exception (in case of no more messages available). """ while True: self.recv_parts.append(self.socket.recv(constants.NOBLOCK)) if not self.socket.getsockopt(constants.RCVMORE): result, self.recv_parts = self.recv_parts, [] return result def doRead(self): """ Some data is available for reading on your descriptor. ZeroMQ is signalling that we should process some events. Part of L{IReadDescriptor}. """ events = self.socket.getsockopt(constants.EVENTS) if (events & constants.POLLIN) == constants.POLLIN: while True: if self.factory is None: # disconnected return try: message = self._readMultipart() except error.ZMQError as e: if e.errno == constants.EAGAIN: break raise e log.callWithLogger(self, self.messageReceived, message) if (events & constants.POLLOUT) == constants.POLLOUT: self._startWriting() def _startWriting(self): """ Start delivering messages from the queue. """ while self.queue: try: self.socket.send( self.queue[0][1], constants.NOBLOCK | self.queue[0][0]) except error.ZMQError as e: if e.errno == constants.EAGAIN: break self.queue.popleft() raise e self.queue.popleft() def logPrefix(self): """ Part of L{ILoggingContext}. @return: Prefix used during log formatting to indicate context. @rtype: C{str} """ return 'ZMQ' def send(self, message): """ Send message via ZeroMQ. @param message: message data """ if not hasattr(message, '__iter__'): self.queue.append((0, message)) else: self.queue.extend([(constants.SNDMORE, m) for m in message[:-1]]) self.queue.append((0, message[-1])) # this is crazy hack: if we make such call, zeromq happily signals # available events on other connections self.socket.getsockopt(constants.EVENTS) self._startWriting() def messageReceived(self, message): """ Called on incoming message from ZeroMQ. @param message: message data """ raise NotImplementedError(self) def _connectOrBind(self): """ Connect and/or bind socket to endpoints. """ for endpoint in self.endpoints: if endpoint.type == ZmqEndpointType.connect: self.socket.connect(endpoint.address) elif endpoint.type == ZmqEndpointType.bind: self.socket.bind(endpoint.address) else: assert False, "Unknown endpoint type %r" % endpoint
class ZmqConnection(object): """ Connection through ZeroMQ, wraps up ZeroMQ socket. @cvar socketType: socket type, from ZeroMQ @cvar allowLoopbackMulticast: is loopback multicast allowed? @type allowLoopbackMulticast: C{boolean} @cvar multicastRate: maximum allowed multicast rate, kbps @type multicastRate: C{int} @cvar highWaterMark: hard limit on the maximum number of outstanding messages 0MQ shall queue in memory for any single peer @type highWaterMark: C{int} @ivar factory: ZeroMQ Twisted factory reference @type factory: L{ZmqFactory} @ivar socket: ZeroMQ Socket @type socket: L{Socket} @ivar endpoints: ZeroMQ addresses for connect/bind @type endpoints: C{list} of L{ZmqEndpoint} @ivar fd: file descriptor of zmq mailbox @type fd: C{int} @ivar queue: output message queue @type queue: C{deque} """ implements(IReadDescriptor, IFileDescriptor) socketType = None allowLoopbackMulticast = False multicastRate = 100 highWaterMark = 0 identity = None def __init__(self, factory, *endpoints): """ Constructor. @param factory: ZeroMQ Twisted factory @type factory: L{ZmqFactory} @param endpoints: ZeroMQ addresses for connect/bind @type endpoints: C{list} of L{ZmqEndpoint} """ self.factory = factory self.endpoints = endpoints self.socket = Socket(factory.context, self.socketType) self.queue = deque() self.recv_parts = [] self.fd = self.socket.getsockopt(constants.FD) self.socket.setsockopt(constants.LINGER, factory.lingerPeriod) self.socket.setsockopt(constants.MCAST_LOOP, int(self.allowLoopbackMulticast)) self.socket.setsockopt(constants.RATE, self.multicastRate) self.socket.setsockopt(constants.HWM, self.highWaterMark) if self.identity is not None: self.socket.setsockopt(constants.IDENTITY, self.identity) self._connectOrBind() self.factory.connections.add(self) self.factory.reactor.addReader(self) def shutdown(self): """ Shutdown connection and socket. """ self.factory.reactor.removeReader(self) self.factory.connections.discard(self) self.socket.close() self.socket = None self.factory = None def __repr__(self): return "%s(%r, %r)" % (self.__class__.__name__, self.factory, self.endpoints) def fileno(self): """ Part of L{IFileDescriptor}. @return: The platform-specified representation of a file descriptor number. """ return self.fd def connectionLost(self, reason): """ Called when the connection was lost. Part of L{IFileDescriptor}. This is called when the connection on a selectable object has been lost. It will be called whether the connection was closed explicitly, an exception occurred in an event handler, or the other end of the connection closed it first. @param reason: A failure instance indicating the reason why the connection was lost. L{error.ConnectionLost} and L{error.ConnectionDone} are of special note, but the failure may be of other classes as well. """ log.err(reason, "Connection to ZeroMQ lost in %r" % (self)) if self.factory: self.factory.reactor.removeReader(self) def _readMultipart(self): """ Read multipart in non-blocking manner, returns with ready message or raising exception (in case of no more messages available). """ while True: self.recv_parts.append(self.socket.recv(constants.NOBLOCK)) if not self.socket.getsockopt(constants.RCVMORE): result, self.recv_parts = self.recv_parts, [] return result def doRead(self): """ Some data is available for reading on your descriptor. ZeroMQ is signalling that we should process some events. Part of L{IReadDescriptor}. """ events = self.socket.getsockopt(constants.EVENTS) if (events & constants.POLLIN) == constants.POLLIN: while True: if self.factory is None: # disconnected return try: message = self._readMultipart() except error.ZMQError as e: if e.errno == constants.EAGAIN: break raise e log.callWithLogger(self, self.messageReceived, message) if (events & constants.POLLOUT) == constants.POLLOUT: self._startWriting() def _startWriting(self): """ Start delivering messages from the queue. """ while self.queue: try: self.socket.send(self.queue[0][1], constants.NOBLOCK | self.queue[0][0]) except error.ZMQError as e: if e.errno == constants.EAGAIN: break self.queue.popleft() raise e self.queue.popleft() def logPrefix(self): """ Part of L{ILoggingContext}. @return: Prefix used during log formatting to indicate context. @rtype: C{str} """ return 'ZMQ' def send(self, message): """ Send message via ZeroMQ. @param message: message data """ if not hasattr(message, '__iter__'): self.queue.append((0, message)) else: self.queue.extend([(constants.SNDMORE, m) for m in message[:-1]]) self.queue.append((0, message[-1])) # this is crazy hack: if we make such call, zeromq happily signals # available events on other connections self.socket.getsockopt(constants.EVENTS) self._startWriting() def messageReceived(self, message): """ Called on incoming message from ZeroMQ. @param message: message data """ raise NotImplementedError(self) def _connectOrBind(self): """ Connect and/or bind socket to endpoints. """ for endpoint in self.endpoints: if endpoint.type == ZmqEndpointType.connect: self.socket.connect(endpoint.address) elif endpoint.type == ZmqEndpointType.bind: self.socket.bind(endpoint.address) else: assert False, "Unknown endpoint type %r" % endpoint
class ZmqConnection(object): """ Connection through ZeroMQ, wraps up ZeroMQ socket. @cvar socketType: socket type, from ZeroMQ @cvar allowLoopbackMulticast: is loopback multicast allowed? @type allowLoopbackMulticast: C{boolean} @cvar multicastRate: maximum allowed multicast rate, kbps @type multicastRate: C{int} @cvar highWaterMark: hard limit on the maximum number of outstanding messages 0MQ shall queue in memory for any single peer @type highWaterMark: C{int} @ivar factory: ZeroMQ Twisted factory reference @type factory: L{ZmqFactory} @ivar socket: ZeroMQ Socket @type socket: L{Socket} @ivar endpoints: ZeroMQ addresses for connect/bind @type endpoints: C{list} of L{ZmqEndpoint} @ivar fd: file descriptor of zmq mailbox @type fd: C{int} @ivar queue: output message queue @type queue: C{deque} """ implements(IReadDescriptor, IFileDescriptor) socketType = None allowLoopbackMulticast = False multicastRate = 100 highWaterMark = 0 # Only supported by zeromq3 and pyzmq>=2.2.0.1 tcpKeepalive = 0 tcpKeepaliveCount = 0 tcpKeepaliveIdle = 0 tcpKeepaliveInterval = 0 def __init__(self, factory, endpoint=None, identity=None): """ Constructor. One endpoint is passed to the constructor, more could be added via call to C{addEndpoints}. @param factory: ZeroMQ Twisted factory @type factory: L{ZmqFactory} @param endpoint: ZeroMQ address for connect/bind @type endpoint: C{list} of L{ZmqEndpoint} @param identity: socket identity (ZeroMQ) @type identity: C{str} """ self.factory = factory self.endpoints = [] self.identity = identity self.socket = Socket(factory.context, self.socketType) self.queue = deque() self.recv_parts = [] self.read_scheduled = None self.fd = self.socket.getsockopt(constants.FD) self.socket.setsockopt(constants.LINGER, factory.lingerPeriod) if not ZMQ3: self.socket.setsockopt(constants.MCAST_LOOP, int(self.allowLoopbackMulticast)) self.socket.setsockopt(constants.RATE, self.multicastRate) if not ZMQ3: self.socket.setsockopt(constants.HWM, self.highWaterMark) else: self.socket.setsockopt(constants.SNDHWM, self.highWaterMark) self.socket.setsockopt(constants.RCVHWM, self.highWaterMark) if ZMQ3 and self.tcpKeepalive: self.socket.setsockopt(constants.TCP_KEEPALIVE, self.tcpKeepalive) self.socket.setsockopt(constants.TCP_KEEPALIVE_CNT, self.tcpKeepaliveCount) self.socket.setsockopt(constants.TCP_KEEPALIVE_IDLE, self.tcpKeepaliveIdle) self.socket.setsockopt(constants.TCP_KEEPALIVE_INTVL, self.tcpKeepaliveInterval) if self.identity is not None: self.socket.setsockopt(constants.IDENTITY, self.identity) if endpoint: self.addEndpoints([endpoint]) self.factory.connections.add(self) self.factory.reactor.addReader(self) self.doRead() def addEndpoints(self, endpoints): """ Add more connection endpoints. Connection may have many endpoints, mixing protocols and types. @param endpoints: list of endpoints to add @type endpoints: C{list} """ self.endpoints.extend(endpoints) self._connectOrBind(endpoints) def shutdown(self): """ Shutdown connection and socket. """ self.factory.reactor.removeReader(self) self.factory.connections.discard(self) self.socket.close() self.socket = None self.factory = None if self.read_scheduled is not None: self.read_scheduled.cancel() self.read_scheduled = None def __repr__(self): return "%s(%r, %r)" % (self.__class__.__name__, self.factory, self.endpoints) def fileno(self): """ Part of L{IFileDescriptor}. @return: The platform-specified representation of a file descriptor number. """ return self.fd def connectionLost(self, reason): """ Called when the connection was lost. Part of L{IFileDescriptor}. This is called when the connection on a selectable object has been lost. It will be called whether the connection was closed explicitly, an exception occurred in an event handler, or the other end of the connection closed it first. @param reason: A failure instance indicating the reason why the connection was lost. L{error.ConnectionLost} and L{error.ConnectionDone} are of special note, but the failure may be of other classes as well. """ if self.factory: self.factory.reactor.removeReader(self) def _readMultipart(self): """ Read multipart in non-blocking manner, returns with ready message or raising exception (in case of no more messages available). """ while True: self.recv_parts.append(self.socket.recv(constants.NOBLOCK)) if not self.socket.getsockopt(constants.RCVMORE): result, self.recv_parts = self.recv_parts, [] return result def doRead(self): """ Some data is available for reading on your descriptor. ZeroMQ is signalling that we should process some events, we're starting to to receive incoming messages. Part of L{IReadDescriptor}. """ if self.read_scheduled is not None: if not self.read_scheduled.called: self.read_scheduled.cancel() self.read_scheduled = None while True: if self.factory is None: # disconnected return events = self.socket.getsockopt(constants.EVENTS) if (events & constants.POLLIN) != constants.POLLIN: return try: message = self._readMultipart() except error.ZMQError as e: if e.errno == constants.EAGAIN: continue raise e log.callWithLogger(self, self.messageReceived, message) def logPrefix(self): """ Part of L{ILoggingContext}. @return: Prefix used during log formatting to indicate context. @rtype: C{str} """ return "ZMQ" def send(self, message): """ Send message via ZeroMQ. Sending is performed directly to ZeroMQ without queueing. If HWM is reached on ZeroMQ side, sending operation is aborted with exception from ZeroMQ (EAGAIN). @param message: message data @type message: message could be either list of parts or single part (str) """ if not hasattr(message, "__iter__"): self.socket.send(message, constants.NOBLOCK) else: for m in message[:-1]: self.socket.send(m, constants.NOBLOCK | constants.SNDMORE) self.socket.send(message[-1], constants.NOBLOCK) if self.read_scheduled is None: self.read_scheduled = reactor.callLater(0, self.doRead) def messageReceived(self, message): """ Called on incoming message from ZeroMQ. @param message: message data """ raise NotImplementedError(self) def _connectOrBind(self, endpoints): """ Connect and/or bind socket to endpoints. """ for endpoint in endpoints: if endpoint.type == ZmqEndpointType.connect: self.socket.connect(endpoint.address) elif endpoint.type == ZmqEndpointType.bind: self.socket.bind(endpoint.address) else: assert False, "Unknown endpoint type %r" % endpoint
def _createSocket(self, factory): """ Create a socket and assign the fd. """ socket = Socket(factory.context, self.socketType) self.fd = socket.getsockopt(constants.FD) socket.setsockopt(constants.LINGER, factory.lingerPeriod) socket.setsockopt(constants.MCAST_LOOP, int(self.allowLoopbackMulticast)) socket.setsockopt(constants.RATE, self.multicastRate) socket.setsockopt(constants.HWM, self.highWaterMark) if self.identity is not None: socket.setsockopt(constants.IDENTITY, self.identity) return socket
class ZmqConnection(object): """ Connection through ZeroMQ, wraps up ZeroMQ socket. @cvar socketType: socket type, from ZeroMQ @cvar allowLoopbackMulticast: is loopback multicast allowed? @type allowLoopbackMulticast: C{boolean} @cvar multicastRate: maximum allowed multicast rate, kbps @type multicastRate: C{int} @cvar highWaterMark: hard limit on the maximum number of outstanding messages 0MQ shall queue in memory for any single peer @type highWaterMark: C{int} @ivar factory: ZeroMQ Twisted factory reference @type factory: L{ZmqFactory} @ivar socket: ZeroMQ Socket @type socket: L{Socket} @ivar endpoints: ZeroMQ addresses for connect/bind @type endpoints: C{list} of L{ZmqEndpoint} @ivar fd: file descriptor of zmq mailbox @type fd: C{int} @ivar queue: output message queue @type queue: C{deque} """ implements(IReadDescriptor, IFileDescriptor) socketType = None allowLoopbackMulticast = False multicastRate = 100 highWaterMark = 0 # Only supported by zeromq3 and pyzmq>=2.2.0.1 tcpKeepalive = 0 tcpKeepaliveCount = 0 tcpKeepaliveIdle = 0 tcpKeepaliveInterval = 0 def __init__(self, factory, endpoint=None, identity=None): """ Constructor. One endpoint is passed to the constructor, more could be added via call to C{addEndpoints}. @param factory: ZeroMQ Twisted factory @type factory: L{ZmqFactory} @param endpoint: ZeroMQ address for connect/bind @type endpoint: C{list} of L{ZmqEndpoint} @param identity: socket identity (ZeroMQ) @type identity: C{str} """ self.factory = factory self.endpoints = [] self.identity = identity self.socket = Socket(factory.context, self.socketType) self.queue = deque() self.recv_parts = [] self.read_scheduled = None self.fd = self.socket_get(constants.FD) self.socket_set(constants.LINGER, factory.lingerPeriod) if not ZMQ3: self.socket_set( constants.MCAST_LOOP, int(self.allowLoopbackMulticast)) self.socket_set(constants.RATE, self.multicastRate) if not ZMQ3: self.socket_set(constants.HWM, self.highWaterMark) else: self.socket_set(constants.SNDHWM, self.highWaterMark) self.socket_set(constants.RCVHWM, self.highWaterMark) if ZMQ3 and self.tcpKeepalive: self.socket_set( constants.TCP_KEEPALIVE, self.tcpKeepalive) self.socket_set( constants.TCP_KEEPALIVE_CNT, self.tcpKeepaliveCount) self.socket_set( constants.TCP_KEEPALIVE_IDLE, self.tcpKeepaliveIdle) self.socket_set( constants.TCP_KEEPALIVE_INTVL, self.tcpKeepaliveInterval) if self.identity is not None: self.socket_set(constants.IDENTITY, self.identity) if endpoint: self.addEndpoints([endpoint]) self.factory.connections.add(self) self.factory.reactor.addReader(self) self.doRead() def addEndpoints(self, endpoints): """ Add more connection endpoints. Connection may have many endpoints, mixing protocols and types. @param endpoints: list of endpoints to add @type endpoints: C{list} """ self.endpoints.extend(endpoints) self._connectOrBind(endpoints) def shutdown(self): """ Shutdown connection and socket. """ self.factory.reactor.removeReader(self) self.factory.connections.discard(self) self.socket.close() self.socket = None self.factory = None if self.read_scheduled is not None: self.read_scheduled.cancel() self.read_scheduled = None def __repr__(self): return "%s(%r, %r)" % ( self.__class__.__name__, self.factory, self.endpoints) def fileno(self): """ Part of L{IFileDescriptor}. @return: The platform-specified representation of a file descriptor number. """ return self.fd def connectionLost(self, reason): """ Called when the connection was lost. Part of L{IFileDescriptor}. This is called when the connection on a selectable object has been lost. It will be called whether the connection was closed explicitly, an exception occurred in an event handler, or the other end of the connection closed it first. @param reason: A failure instance indicating the reason why the connection was lost. L{error.ConnectionLost} and L{error.ConnectionDone} are of special note, but the failure may be of other classes as well. """ if self.factory: self.factory.reactor.removeReader(self) def _readMultipart(self): """ Read multipart in non-blocking manner, returns with ready message or raising exception (in case of no more messages available). """ while True: self.recv_parts.append(self.socket.recv(constants.NOBLOCK)) if not self.socket_get(constants.RCVMORE): result, self.recv_parts = self.recv_parts, [] return result def doRead(self): """ Some data is available for reading on your descriptor. ZeroMQ is signalling that we should process some events, we're starting to to receive incoming messages. Part of L{IReadDescriptor}. """ if self.read_scheduled is not None: if not self.read_scheduled.called: self.read_scheduled.cancel() self.read_scheduled = None while True: if self.factory is None: # disconnected return events = self.socket_get(constants.EVENTS) if (events & constants.POLLIN) != constants.POLLIN: return try: message = self._readMultipart() except error.ZMQError as e: if e.errno == constants.EAGAIN: continue raise e log.callWithLogger(self, self.messageReceived, message) def logPrefix(self): """ Part of L{ILoggingContext}. @return: Prefix used during log formatting to indicate context. @rtype: C{str} """ return 'ZMQ' def send(self, message): """ Send message via ZeroMQ. Sending is performed directly to ZeroMQ without queueing. If HWM is reached on ZeroMQ side, sending operation is aborted with exception from ZeroMQ (EAGAIN). @param message: message data @type message: message could be either list of parts or single part (str) """ if not hasattr(message, '__iter__'): self.socket.send(message, constants.NOBLOCK) else: for m in message[:-1]: self.socket.send(m, constants.NOBLOCK | constants.SNDMORE) self.socket.send(message[-1], constants.NOBLOCK) if self.read_scheduled is None: self.read_scheduled = reactor.callLater(0, self.doRead) def messageReceived(self, message): """ Called on incoming message from ZeroMQ. @param message: message data """ raise NotImplementedError(self) def _connectOrBind(self, endpoints): """ Connect and/or bind socket to endpoints. """ for endpoint in endpoints: if endpoint.type == ZmqEndpointType.connect: self.socket.connect(endpoint.address) elif endpoint.type == ZmqEndpointType.bind: self.socket.bind(endpoint.address) else: assert False, "Unknown endpoint type %r" % endpoint # Compatibility shims def _socket_get_pyzmq2(self, constant): return self.socket.getsockopt(constant) def _socket_get_pyzmq13(self, constant): return self.socket.get(constant) def _socket_set_pyzmq2(self, constant, value): return self.socket.setsockopt(constant, value) def _socket_set_pyzmq13(self, constant, value): return self.socket.set(constant, value) if PYZMQ13: socket_get = _socket_get_pyzmq13 socket_set = _socket_set_pyzmq13 else: socket_get = _socket_get_pyzmq2 socket_set = _socket_set_pyzmq2
def _createSocket(self, factory): """ Create a socket and assign the fd. """ socket = Socket(factory.context, self.socketType) self.fd = socket.getsockopt(constants.FD) socket.setsockopt(constants.LINGER, factory.lingerPeriod) if not ZMQ3: socket.setsockopt( constants.MCAST_LOOP, int(self.allowLoopbackMulticast)) socket.setsockopt(constants.RATE, self.multicastRate) if not ZMQ3: socket.setsockopt(constants.HWM, self.highWaterMark) else: socket.setsockopt(constants.SNDHWM, self.highWaterMark) socket.setsockopt(constants.RCVHWM, self.highWaterMark) if self.identity is not None: socket.setsockopt(constants.IDENTITY, self.identity) return socket
class ZmqSocket(object): """ Wraps a ZeroMQ socket and integrates it into the Twisted reactor @ivar zsock: ZeroMQ Socket @type zsock: L{zmq.core.socket.Socket} @ivar queue: output message queue @type queue: C{deque} """ implements(IReadDescriptor, IFileDescriptor) socketType = None def __init__(self, socketType=None): """ @param context: Context this socket is to be associated with @type factory: L{ZmqFactory} @param socketType: Type of socket to create @type socketType: C{int} """ if socketType is not None: self.socketType = socketType assert self.socketType is not None self._ctx = getContext() self._zsock = Socket(getContext()._zctx, self.socketType) self._queue = deque() self.fd = self._zsock.getsockopt(constants.FD) self._ctx._sockets.add(self) self._ctx.reactor.addReader(self) def _sockopt_property(i, totype=int): return property(lambda zs: zs._zsock.getsockopt(i), lambda zs, v: zs._zsock.setsockopt(i, totype(v))) linger = _sockopt_property(constants.LINGER) rate = _sockopt_property(constants.RATE) identity = _sockopt_property(constants.IDENTITY, str) subscribe = _sockopt_property(constants.SUBSCRIBE, str) # Removed in 3.2 # mcast_loop = _sockopt_property( constants.MCAST_LOOP ) # hwm = _sockopt_property( constants.HWM ) def close(self): self._ctx.reactor.removeReader(self) self._ctx._sockets.discard(self) self._zsock.close() self._zsock = None self._ctx = None def __repr__(self): t = _type_map[self.socketType].lower() t = t[0].upper() + t[1:] return "Zmq%sSocket(%s)" % (t, repr(self._zsock)) def logPrefix(self): """ Part of L{ILoggingContext}. @return: Prefix used during log formatting to indicate context. @rtype: C{str} """ return 'ZMQ' def fileno(self): """ Part of L{IFileDescriptor}. @return: The platform-specified representation of a file descriptor number. """ return self.fd def connectionLost(self, reason): """ Called when the connection was lost. This will only be called during reactor shutdown with active ZeroMQ sockets. Part of L{IFileDescriptor}. """ if self._ctx: self._ctx.reactor.removeReader(self) def doRead(self): """ Some data is available for reading on your descriptor. ZeroMQ is signalling that we should process some events, we're starting to send queued messages and to receive incoming messages. Note that the ZeroMQ FD is used in an edge-triggered manner. Consequently, this function must read all pending messages before returning. Part of L{IReadDescriptor}. """ if self._ctx is None: # disconnected return while self._queue and self._zsock is not None: try: self._zsock.send_multipart(self._queue[0], constants.NOBLOCK) self._queue.popleft() except error.ZMQError as e: if e.errno == constants.EAGAIN: break raise e while self._zsock is not None: try: msg_list = self._zsock.recv_multipart(constants.NOBLOCK) log.callWithLogger(self, self.messageReceived, msg_list) except error.ZMQError as e: if e.errno == constants.EAGAIN: break # This exception can be thrown during socket closing process if e.errno == 156384763 or str( e ) == 'Operation cannot be accomplished in current state': break # Seen in 3.2 for an unknown reason if e.errno == 95: break raise e def send(self, *message_parts): """ Sends a ZeroMQ message. Each positional argument is converted into a message part """ if len(message_parts) == 1 and isinstance(message_parts[0], (list, tuple)): message_parts = message_parts[0] self._queue.append(message_parts) self.doRead() def connect(self, addr): return self._zsock.connect(addr) def bind(self, addr): return self._zsock.bind(addr) def bindToRandomPort(self, addr, min_port=49152, max_port=65536, max_tries=100): return self._zsock.bind_to_random_port(addr, min_port, max_port, max_tries) def messageReceived(self, message_parts): """ Called on incoming message from ZeroMQ. @param message_parts: list of message parts """ raise NotImplementedError(self)
class ZmqSocket(object): """ Wraps a ZeroMQ socket and integrates it into the Twisted reactor @ivar zsock: ZeroMQ Socket @type zsock: L{zmq.core.socket.Socket} @ivar queue: output message queue @type queue: C{deque} """ implements(IReadDescriptor, IFileDescriptor) socketType = None def __init__(self, socketType=None): """ @param context: Context this socket is to be associated with @type factory: L{ZmqFactory} @param socketType: Type of socket to create @type socketType: C{int} """ if socketType is not None: self.socketType = socketType assert self.socketType is not None self._ctx = getContext() self._zsock = Socket(getContext()._zctx, self.socketType) self._queue = deque() self.fd = self._zsock.getsockopt(constants.FD) self._ctx._sockets.add(self) self._ctx.reactor.addReader(self) def _sockopt_property( i, totype=int): return property( lambda zs: zs._zsock.getsockopt(i), lambda zs,v: zs._zsock.setsockopt(i,totype(v)) ) linger = _sockopt_property( constants.LINGER ) mcast_loop = _sockopt_property( constants.MCAST_LOOP ) rate = _sockopt_property( constants.RATE ) hwm = _sockopt_property( constants.HWM ) identity = _sockopt_property( constants.IDENTITY, str ) subscribe = _sockopt_property( constants.SUBSCRIBE, str ) def close(self): self._ctx.reactor.removeReader(self) self._ctx._sockets.discard(self) self._zsock.close() self._zsock = None self._ctx = None def __repr__(self): return "ZmqSocket(%s)" % repr(self._zsock) def fileno(self): """ Part of L{IFileDescriptor}. @return: The platform-specified representation of a file descriptor number. """ return self.fd def connectionLost(self, reason): """ Called when the connection was lost. This will only be called during reactor shutdown with active ZeroMQ sockets. Part of L{IFileDescriptor}. """ if self._ctx: self._ctx.reactor.removeReader(self) def doRead(self): """ Some data is available for reading on your descriptor. ZeroMQ is signalling that we should process some events, we're starting to send queued messages and to receive incoming messages. Note that the ZeroMQ FD is used in an edge-triggered manner. Consequently, this function must read all pending messages before returning. Part of L{IReadDescriptor}. """ events = self._zsock.getsockopt(constants.EVENTS) #print 'doRead()', events while self._queue and (events & constants.POLLOUT) == constants.POLLOUT: try: self._zsock.send_multipart( self._queue[0], constants.NOBLOCK ) self._queue.popleft() events = self._zsock.getsockopt(constants.EVENTS) except error.ZMQError as e: if e.errno == constants.EAGAIN: break self._queue.popleft() # Failed to send, discard message raise e while (events & constants.POLLIN) == constants.POLLIN: if self._ctx is None: # disconnected return try: msg_list = self._zsock.recv_multipart( constants.NOBLOCK ) except error.ZMQError as e: if e.errno == constants.EAGAIN: break raise e log.callWithLogger(self, self.messageReceived, msg_list) # Callback can cause the socket to be closed if self._zsock is not None: events = self._zsock.getsockopt(constants.EVENTS) def logPrefix(self): """ Part of L{ILoggingContext}. @return: Prefix used during log formatting to indicate context. @rtype: C{str} """ return 'ZMQ' def send(self, *message_parts): """ Sends a ZeroMQ message. Each positional argument is converted into a message part """ if len(message_parts) == 1 and isinstance(message_parts[0], (list, tuple)): message_parts = message_parts[0] if self._zsock.getsockopt(constants.EVENTS) & constants.POLLOUT == constants.POLLOUT: self._zsock.send_multipart( message_parts, constants.NOBLOCK ) else: self._queue.append( message_parts ) # The following call is requried to ensure that the socket's file descriptor # will signal new data as being available. self._zsock.getsockopt(constants.EVENTS) # #if self._zsock.getsockopt(constants.EVENTS) & POLL_IN_OUT: # self.doRead() def connect(self, addr): return self._zsock.connect(addr) def bind(self, addr): return self._zsock.bind(addr) def bindToRandomPort(self, addr, min_port=49152, max_port=65536, max_tries=100): return self._zsock.bind_to_random_port(addr, min_port, max_port, max_tries) def messageReceived(self, message_parts): """ Called on incoming message from ZeroMQ. @param message_parts: list of message parts """ raise NotImplementedError(self)