def eventloop(self): """ The eventloop runs in its own thread. It listens for inbound messages on the control socket (a DEALER socket). These messages are replies to outbound requests (control messages). When a reply is received, a thread is created and a user specified callback is called in that thread. """ while True: ti = str(threading.currentThread().ident) if self.debug > 2: print ti + "] eventloop: Waiting for a reply" r = self.req.recv_multipart() if self.debug > 2: print ti + "] eventloop: Got reply: "#, r if len(r) > 0: msg = r[1] decoded_msg = control_pb2.ControlType() decoded_msg.ParseFromString(msg) control_command = 0 if decoded_msg.type == control_pb2.ControlType.COMMAND: control_command = decoded_msg.command if self.debug > 3: print ti + "] eventloop: msg: src=", decoded_msg.src, " dst=", decoded_msg.dst try: cifsupport.versionCheck(decoded_msg) msgid = decoded_msg.seq self.callback_registry_lock.acquire() if msgid in self.callback_registry: if self.debug > 2: print ti + "] eventloop: Callback specified. Calling it.", self.callback_registry # create a separate thread so the callback doesn't influence the event loop # these threads should be short lived cbthread = threading.Thread(target = self.callback_registry[msgid], name="callback:" + str(control_command), args=(decoded_msg,)) cbthread.start() # thread_tracking is done in the thread itself, not here del self.callback_registry[msgid] else: if self.defaultcallback != None: dcbthread = threading.Thread(target = self.defaultcallback, name="defaultcallback:" + str(control_command), args=(decoded_msg,self.defaultcallback_params)) dcbthread.start() # thread_tracking is done in the thread itself, not here else: if self.debug > 2: print ti + "] eventloop: Reply is bad (unexpected, no callback available). Discarding." self.callback_registry_lock.release() except Exception as e: print "ERROR: eventloop: Received message was bad: ", e
#print " Got ", rawmsg msg = control_pb2.ControlType() try: msg.ParseFromString(rawmsg[2]) except Exception as e: print "Received message isn't a protobuf: ", e mystats.setbad() else: from_zmqid = rawmsg[0] # save the ZMQ identity of who sent us this message #print "Got msg: "#, msg.seq try: cifsupport.versionCheck(msg) except Exception as e: print "\tReceived message has incompatible version: ", e mystats.setbadversion(1, msg.version) else: if cifsupport.isControl(msg): msgfrom = msg.src msgto = msg.dst msgcommand = msg.command msgcommandtext = control_pb2._CONTROLTYPE_COMMANDTYPE.values_by_number[msg.command].name msgid = msg.seq if msgfrom != '' and msg.apikey != '': if msgto == myname and msg.type == control_pb2.ControlType.REPLY: print "\tREPLY for me: ", msgcommand