def __init__(self, workers_count=3): self.__process_threads = [] self.__operations_queue = Queue() self.__boot_event_sender = BootEventSenderThread() self.__boot_event_sender.start() #register service on bus dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) #self.loop = gobject.MainLoop() bus = dbus.SystemBus() name = dbus.service.BusName(NODE_AGENT, bus) self.dbus_client = NodeAgentEventService(bus, '/events') for i in xrange(workers_count): thread = ProcessOperationThread(self.__operations_queue, self.dbus_client) thread.setName('ProcessOperationThread#%i'%i) self.__process_threads.append( thread ) thread.start() FriServer.__init__(self, hostname='0.0.0.0', port=NODE_AGENT_BIND_PORT, workers_count=1)
class NodeAgent(FriServer): def __init__(self, workers_count=3): self.__process_threads = [] self.__operations_queue = Queue() self.__boot_event_sender = BootEventSenderThread() self.__boot_event_sender.start() #register service on bus dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) #self.loop = gobject.MainLoop() bus = dbus.SystemBus() name = dbus.service.BusName(NODE_AGENT, bus) self.dbus_client = NodeAgentEventService(bus, '/events') for i in xrange(workers_count): thread = ProcessOperationThread(self.__operations_queue, self.dbus_client) thread.setName('ProcessOperationThread#%i'%i) self.__process_threads.append( thread ) thread.start() FriServer.__init__(self, hostname='0.0.0.0', port=NODE_AGENT_BIND_PORT, workers_count=1) def onDataReceive( self, json_object ): #get session_id session_id = json_object.get('id', None) if session_id is None: raise Exception('Element <id> is not found in FRI packet!') try: session_id = int(session_id) except: raise Exception('Session is should be integer! But: %s'%session_id) if session_id == 0: #LIVE packet return #get node node = json_object.get('node', None) if not node: raise Exception('Element <node> is not found in FRI packet!') node = node.strip() #get operation operation = json_object.get('operation', None) if not operation: raise Exception('Element <operation> is not found in FRI packet') operation = operation.strip() #get parameters parameters = json_object.get('parameters', None) #send message to D-BUS op_args = Dictionary(signature='ss') op_args.update(parameters) self.dbus_client.operationReceivedEvent(str(session_id), str(node), str(operation), op_args) #check appropriate plugin can_process = PluginManager.can_process_operation( operation ) if not can_process: raise Exception('NodeAgent can not process operation %s. No appropriate plugin found!'%operation) op = Operation(session_id, node, operation, parameters) self.__operations_queue.put(op) def stop(self): self.__boot_event_sender.stop() FriServer.stop(self) for i in self.__process_threads: self.__operations_queue.put(FINISH_FLAG) self.__operations_queue.join()