def _get_analysis(self, sock): """ Download and import our analysis class @param sock: Socket to download analysis file from @return: Subclass of LophiAnalysis downloaded from the network to be used for analysis """ # Get our script to execute script = G.read_socket_data(sock) # generate a temporary filename to store this script import random import string tmp_name = ''.join( random.choice(string.ascii_uppercase) for x in range(10)) tmp_name_file = os.path.join(SCRIPTS_TMP_PATH, tmp_name + ".py") try: logger.debug("Creating tmp file: %s" % tmp_name_file) # Import our script so that we can use it f = open(tmp_name_file, "w+") f.write(script) f.close() except: logger.error("Could not create temporary script file.") return tmp_name_file
def _get_analysis(self,sock): """ Download and import our analysis class @param sock: Socket to download analysis file from @return: Subclass of LophiAnalysis downloaded from the network to be used for analysis """ # Get our script to execute script = G.read_socket_data(sock) # generate a temporary filename to store this script import random import string tmp_name = ''.join(random.choice(string.ascii_uppercase) for x in range(10)) tmp_name_file = os.path.join(SCRIPTS_TMP_PATH, tmp_name + ".py") try: logger.debug("Creating tmp file: %s"%tmp_name_file) # Import our script so that we can use it f = open(tmp_name_file,"w+") f.write(script) f.close() except: logger.error("Could not create temporary script file.") return tmp_name_file
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 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_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 run(self): """ Loop until we fail relaying objects """ # Set our handler to close gracefully G.set_exit_handler(self.cleanup) if G.VERBOSE: print "Relaying data from socket to queue." while self.RUNNING: # Try to unpack it try: # Get our data data = G.read_socket_data(self.SOCK) # Unpack our sensor output data = ProtoBuf.unpack_sensor_output(data) except EOFError: if G.VERBOSE: print "RemoteQueue: Looks like our socket closed." break except: # Just die! if self.RUNNING: print "ERROR/RemoteQueue: Could not unpickle network data." break # update our machine name to indicate its origin data['MACHINE'] = self.address[0] + "-" + data['MACHINE'] # Write the data to our queue, if we can try: self.OUTPUT_QUEUE.put(data, False) except: if self.RUNNING: print "ERROR/RemoteQueue: Could not write to output queue." G.print_traceback() break # Close socket self.SOCK.close()
def run(self): """ Loop until we fail relaying objects """ # Set our handler to close gracefully G.set_exit_handler(self.cleanup) if G.VERBOSE: print "Relaying data from socket to queue." while self.RUNNING: # Try to unpack it try: # Get our data data = G.read_socket_data(self.SOCK) # Unpack our sensor output data = ProtoBuf.unpack_sensor_output(data) except EOFError: if G.VERBOSE: print "RemoteQueue: Looks like our socket closed." break except: # Just die! if self.RUNNING: print "ERROR/RemoteQueue: Could not unpickle network data." break # update our machine name to indicate its origin data['MACHINE'] = self.address[0] + "-" + data['MACHINE'] # Write the data to our queue, if we can try: self.OUTPUT_QUEUE.put(data, False) except: if self.RUNNING: print "ERROR/RemoteQueue: Could not write to output queue." G.print_traceback() break # Close socket self.SOCK.close()
def get_analysis(self): """ Get protocol buffer version of remote analysis """ while 1: try: logger.debug("Getting analysis list for Controller/%s" % self.name) # Get reply cmd = LophiCommand(G.CTRL_CMD_PICKLE, args=["analysis"]) analysis_buf = self.send_cmd(cmd) status = G.read_socket_data(self.SOCK) # unpack protocol buffer self.analysis = ProtoBuf.unpack_analysis_list(analysis_buf) return status except: self.connect()
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 get_machines(self): """ Get protocol buffer version of remote machines """ while 1: try: logger.debug("Getting machine list for Controller/%s" % self.name) # Get response cmd = LophiCommand(G.CTRL_CMD_PICKLE, args=["machines"]) data = self.send_cmd(cmd) status = G.read_socket_data(self.SOCK) # Unpack our machine list # (WARNING: This a subset of the objects at the server if data is not None: self.machines = ProtoBuf.unpack_machine_list(data) else: self.machines = [] return status except: G.print_traceback() self.connect()