def sender(self): while not self.stopped: try: req = self._outgoing_queue.get(True, 1) data = ser_data(req.command, req.data) self._requests[req.id] = req self.socket.send(ser_destination(req.destination), zmq.SNDMORE) self.socket.send(req.command, zmq.SNDMORE) self.socket.send(ser_uint32(req.id), zmq.SNDMORE) self.socket.send(data, zmq.SNDMORE) self.socket.send(checksum(data)) self._outgoing_queue.task_done() except Queue.Empty: #print "queue empty" #msleep(500) pass
def poller(self): poller = zmq.Poller() poller.register(self.socket, zmq.POLLIN) while not self.stopped: socks = dict(poller.poll(1000)) if socks: if socks.get(self.socket) == zmq.POLLIN: #message = self.socket.recv(zmq.NOBLOCK) #resp_frame.append(message) resp_frame = self.socket.recv_multipart(zmq.NOBLOCK) #print resp_frame if len(resp_frame) == 5: response = ObeliskResponse() response.origin = resp_frame[0] response.command = resp_frame[1] response.id = deser_uint32(resp_frame[2]) response.error_code, response.data = deser_data(response.command, resp_frame[3]) response.checksum = resp_frame[4] #print "got command, ", response.command #print "got data, ", response.data # validate data if it fails, try and send the request again and del this response # if the request is no longer in _requests ignore it. if checksum(resp_frame[3]) != response.checksum: try: self.send_request(self._requests[response.id]) del response except KeyError: pass # Message is fine, remove the request from _requests and add our response to _response else: # If there isn't a callback related to this id, pop it from the requests and insert # the result into responses. if response.id not in self._callbacks: self._requests.pop(response.id) self._responses[response.id] = response else: print "callbacked" # Try and pop the old request out of the request dict. # It doesn't need to exist any more as the server has received the message try: self._requests.pop(response.id) except KeyError: pass # Call the callback function in _callbacks try: if response.data is not None: self._callbacks[response.id](response.data) except KeyError: pass