def run(self): """ Loop forever sending data that is sent in on the queue """ # Set our handler to close gracefully G.set_exit_handler(self.cleanup) # Connect to remote host self.connect() if G.VERBOSE: print "Starting RemoteQueue Client..." # loop forever while self.RUNNING: # Get our data try: data = self.INPUT_QUEUE.get() except: if G.VERBOSE: print "WARNING: Could not get data from queue!" pass if data == G.CTRL_CMD_KILL: break ### ## TODO : Optimization!!! ### # Extract index info # machine = data['MACHINE'] # name = data['SUA_NAME'] # profile = data['SUA_PROFILE'] module = data['MODULE_NAME'] pb2_data = ProtoBuf.pack_sensor_output(data) # Is this something new? If not lets not waste the bandwidth if module not in self.cache or self.cache[module] != data: self.cache[module] = data else: continue # Try to write it to our socket while True: try: G.send_socket_data(self.SOCK, pb2_data) break except: if G.VERBOSE: print "RemoteQueueClient: Socket Closed." # Clear our cache and try to reconnect del self.cache[module] self.connect() # Close our socket nicely self.SOCK.close()
def run(self): """ Loop forever sending data that is sent in on the queue """ # Set our handler to close gracefully G.set_exit_handler(self.cleanup) # Connect to remote host self.connect() if G.VERBOSE: print "Starting RemoteQueue Client..." # loop forever while self.RUNNING: # Get our data try: data = self.INPUT_QUEUE.get() except: if G.VERBOSE: print "WARNING: Could not get data from queue!" pass if data == G.CTRL_CMD_KILL: break ### ## TODO : Optimization!!! ### # Extract index info # machine = data['MACHINE'] # name = data['SUA_NAME'] # profile = data['SUA_PROFILE'] module = data['MODULE_NAME'] pb2_data = ProtoBuf.pack_sensor_output(data) # Is this something new? If not lets not waste the bandwidth if module not in self.cache or self.cache[module] != data: self.cache[module] = data else: continue # Try to write it to our socket while True: try: G.send_socket_data(self.SOCK, pb2_data) break except: if G.VERBOSE: print "RemoteQueueClient: Socket Closed." # Clear our cache and try to reconnect del self.cache[module] self.connect() # Close our socket nicely self.SOCK.close()
def send_cmd(self, command): """ Send arbitrary message """ while 1: try: # Send our command to start the analysis G.send_socket_data(self.SOCK, str(command)) # Get our return status self.status = G.read_socket_data(self.SOCK) if self.status is None: raise Exception("Controller Disconnected.") return self.status except: self.connect()
def proccess_input(self, clientsock): """" Continuously read and process commands from our socket """ COMMANDS = { G.CTRL_CMD_START: self.command_start, G.CTRL_CMD_STOP: self.command_analysis, G.CTRL_CMD_PAUSE: self.command_analysis, G.CTRL_CMD_UNPAUSE: self.command_analysis, G.CTRL_CMD_LIST: self.command_list, G.CTRL_CMD_PICKLE: self.command_pickle } try: # Loop forever while self.RUNNING: # Get the data try: data = G.read_socket_data(clientsock) except socket.timeout: # self._cleanup_pointers() continue if not data: break # cleanup our pointers to give a # self._cleanup_pointers() # Split up our command cmd = LophiCommand.from_data(data) #.rstrip().split(" ") logger.debug("Got command: %s" % cmd) # See if it's valid command if cmd.cmd == "help": G.send_socket_data( clientsock, "The available commands are: %s\n" % (COMMANDS.keys())) elif cmd.cmd not in COMMANDS.keys(): G.send_socket_data(clientsock, "Invalid Command: %s\n" % cmd.cmd) else: rtn = COMMANDS[cmd.cmd](cmd, clientsock) if rtn == True: G.send_socket_data(clientsock, "Success.") elif rtn == False: G.send_socket_data(clientsock, "Failure.") except socket.error: logger.warning("Looks like our socket closed...")
def send_analysis(self, filename, cmd): """ Sends start message and our JSON config """ while 1: try: # Send our command to start the analysis G.send_socket_data(self.SOCK, str(cmd)) # read our analysis file f = open(filename) script = f.read() f.close() # Send the json config G.send_socket_data(self.SOCK, script) status = G.read_socket_data(self.SOCK) return status except: self.connect()
def command_list(self, args, sock): """ Generic command to list statuses of the server """ # See if the list exists and return results if len(args) > 1 and args[1] in self.LISTS: output = "" # Print out our available machines if args[1] == "machines": output = G.print_machines(self.machine_list) # Print out our running analyses if args[1] == "analysis": output = G.print_analyses(self.ANALYSIS_DICT) G.send_socket_data(sock, output) return True else: return False
def command_list(self, args, sock): """ Generic command to list statuses of the server """ # See if the list exists and return results if len(args) > 1 and args[1] in self.LISTS: output = "" # Print out our available machines if args[1] == "machines": output = G.print_machines(self.machine_list) # Print out our running analyses if args[1] == "analysis": output = G.print_analyses(self.ANALYSIS_DICT) G.send_socket_data(sock, output) return True else: return False
def proccess_input(self, clientsock): """" Continuously read and process commands from our socket """ COMMANDS = {G.CTRL_CMD_START:self.command_start, G.CTRL_CMD_STOP:self.command_analysis, G.CTRL_CMD_PAUSE:self.command_analysis, G.CTRL_CMD_UNPAUSE:self.command_analysis, G.CTRL_CMD_LIST:self.command_list, G.CTRL_CMD_PICKLE:self.command_pickle} try: # Loop forever while self.RUNNING: # Get the data try: data = G.read_socket_data(clientsock) except socket.timeout: # self._cleanup_pointers() continue if not data: break # cleanup our pointers to give a # self._cleanup_pointers() # Split up our command cmd = LophiCommand.from_data(data) #.rstrip().split(" ") logger.debug("Got command: %s" % cmd) # See if it's valid command if cmd.cmd == "help": G.send_socket_data(clientsock, "The available commands are: %s\n" % (COMMANDS.keys())) elif cmd.cmd not in COMMANDS.keys(): G.send_socket_data(clientsock,"Invalid Command: %s\n" % cmd.cmd) else: rtn = COMMANDS[cmd.cmd](cmd, clientsock) if rtn == True: G.send_socket_data(clientsock, "Success.") elif rtn == False: G.send_socket_data(clientsock, "Failure.") except socket.error: logger.warning("Looks like our socket closed...")
def command_pickle(self, command, sock): """ Will pack our config lists and send them accross the socket """ # See if the list exists and return results if len(command.args) > 0 and command.args[0] in self.LISTS: # Print out our available machines if command.args[0] == "machines": # Pack out list in a proto buf machine_buf = ProtoBuf.pack_machine_list(self.machine_list) # Send our serialized protocol buffer G.send_socket_data(sock, machine_buf) logger.debug("Sent list of machines.") return True # Print out our running analyses elif command.args[0] == "analysis": # Pack up our analysis list analysis_buf = ProtoBuf.pack_analysis_list(self.ANALYSIS_DICT) # Send it across the network G.send_socket_data(sock, analysis_buf) logger.debug("Sent analysis list.") return True else: return False else: G.send_socket_data( sock, "ERROR: No such list.\n" " Available lists are: %s\n" % self.LISTS) return False
def command_pickle(self, command, sock): """ Will pack our config lists and send them accross the socket """ # See if the list exists and return results if len(command.args) > 0 and command.args[0] in self.LISTS: # Print out our available machines if command.args[0] == "machines": # Pack out list in a proto buf machine_buf = ProtoBuf.pack_machine_list(self.machine_list) # Send our serialized protocol buffer G.send_socket_data(sock, machine_buf) logger.debug("Sent list of machines.") return True # Print out our running analyses elif command.args[0] == "analysis": # Pack up our analysis list analysis_buf = ProtoBuf.pack_analysis_list(self.ANALYSIS_DICT) # Send it across the network G.send_socket_data(sock, analysis_buf) logger.debug("Sent analysis list.") return True else: return False else: G.send_socket_data(sock, "ERROR: No such list.\n" " Available lists are: %s\n" % self.LISTS) return False