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 consuming data """ if len(self.OUTPUT_QUEUES) == 0: return # Wait for output to start returning, and handle appropriately while True: # Read our input try: output = self.INPUT_QUEUE.get() except: print "ERROR/DataHandler: Queue failed to get data." break if output == G.CTRL_CMD_KILL: output_packed = output else: output_packed = protobuf.pack_sensor_output(output) # If its a kill command, die if output == G.CTRL_CMD_KILL: if G.VERBOSE: print "Killing Data Handler..." for q in self.OUTPUT_QUEUES: q.put(None) q.close() # self.INPUT_QUEUE.close() break # Forward it for q in self.OUTPUT_QUEUES: q.put(output_packed) logger.debug("Data Handler Closed") # self.INPUT_QUEUE.close() import sys sys.exit(0)