def run(self): super(ConsoleModule,self).run() self._console_thread = ConsoleThread(processing_function=self._process_client_data,log=self.log,in_socket_path=self.parameters['in_socket_path']) # To process data received from controller that must be sent to client #TODO: not sure if needed / main thread receives commands from controller that might be used to send data to client self._receive_controller_thread = ReceiveQueueThread(processing_function=self._process_controller_data,queue=self._output_text_queue) self._receive_controller_thread.start() #TODO: wait for threads to end? while not self.stopping(): self._console_thread.start() # waiting for commands from controller while not self.stopping() and self._console_thread.is_alive(): try: #TODO: check timeout cmd = self._commands_queue.get(block=True, timeout=3) if cmd: self.log.debug('Received command %s' % cmd.tostring()) try: arguments = cmd.arguments exec(self._commands[cmd.name]) except KeyError: self.log.debug('Unknown command %s ' % cmd.tostring()) except Empty: continue except IOError,e: if e.errno == 4: continue self._console_thread.stop() self._receive_controller_thread.stop() self._console_thread.join(self._console_thread.STOP_TIMEOUT_SECS) self._receive_controller_thread.join(self._receive_controller_thread.STOP_TIMEOUT_SECS)
class ConsoleModule(BotModule): _default_args = {'in_socket_path':'console_socket'} def __init__(self,name='console',parameters={}): super(ConsoleModule,self).__init__(name=name,parameters=parameters) self._receive_output_text = True # setup to receive output_text from controller to send to client for arg in self._default_args: if not self.parameters.has_key(arg): self.parameters[arg] = self._default_args[arg] self._commands['send']='self.send_command(arguments)' def _process_client_data(self,data): self.log.debug('Client data: %s' % data) self.output_command(data) # send command line received from client to controller def _process_controller_data(self,data): self.log.debug('Controller data: %s' % data) cmd = BotCommand(name='output',command=None,arguments=data) self._console_thread.send_command(cmd) # send to client data sent by controller def send_text(self,arguments): if self._console_thread.out_socket: self._console_thread.out_socket.sendall(str(arguments)) def run(self): super(ConsoleModule,self).run() self._console_thread = ConsoleThread(processing_function=self._process_client_data,log=self.log,in_socket_path=self.parameters['in_socket_path']) # To process data received from controller that must be sent to client #TODO: not sure if needed / main thread receives commands from controller that might be used to send data to client self._receive_controller_thread = ReceiveQueueThread(processing_function=self._process_controller_data,queue=self._output_text_queue) self._receive_controller_thread.start() #TODO: wait for threads to end? while not self.stopping(): self._console_thread.start() # waiting for commands from controller while not self.stopping() and self._console_thread.is_alive(): try: #TODO: check timeout cmd = self._commands_queue.get(block=True, timeout=3) if cmd: self.log.debug('Received command %s' % cmd.tostring()) try: arguments = cmd.arguments exec(self._commands[cmd.name]) except KeyError: self.log.debug('Unknown command %s ' % cmd.tostring()) except Empty: continue except IOError,e: if e.errno == 4: continue self._console_thread.stop() self._receive_controller_thread.stop() self._console_thread.join(self._console_thread.STOP_TIMEOUT_SECS) self._receive_controller_thread.join(self._receive_controller_thread.STOP_TIMEOUT_SECS)