class Router: def __init__(self): self.nodes = {} self.input_queue = InputQueue(self) self.output_queue = OutputQueue(self) def init(self, state, condition_engine): self.state = state self.condition_engine = condition_engine self.load_nodes() self.state.prune() self.input_queue.start() #start input queue self.output_queue.start() #start output queue def load_nodes(self): #load nodes from file node_json = [] try: node_json = json.load(open('nodes.json','r+')) except: pass node_list = {} for node in node_json: #for each node in the configuration file... node_module = node['module'] node_class = node['class'] node_label = node['label'] new_node = getattr(globals()[node_module], node_class)(node_label, self.state, self) # create the node object... node_list[node_label] = new_node #and append it to the list of nodes logger.info('Created new node: Module: ' + node_module + ' Class: ' + node_class + ' Label: ' + node_label) self.nodes = node_list def push(self, node_label, message): #push logger.info('Received push from: ' + node_label + '. Message: ' + str(message)) self.input_queue.add_message((node_label, message)) def send_to_node(self, node_label, message): #send logger.info('Sending to node: ' + node_label + '. Message: ' + str(message)) self.nodes[node_label].send(message) def get_from_node(self, node_label, arguments): #get logger.info('Getting from node: ' + node_label + '. Arguments: ' + str(arguments)) self.nodes[node_label].get(arguments) def send_to_condition_engine(self, message): logger.info('Sending message to condition engine: ' + str(message)) self.condition_engine.add_message(message) def stop(self): self.input_queue.stop() self.output_queue.stop()
def __init__(self): self.nodes = {} self.input_queue = InputQueue(self) self.output_queue = OutputQueue(self)