def __listen_loop(self): """ This function run a infinite loop that get messages. """ if not self.address: return while True: message = Message() message.receive() thread( self.__decode_and_buffer_message, (message,) )
def __listen_local_loop(self): """ This function run a infinite loop that get messages form local networks. """ while 1: if not self.__listening: break message = Message(destination=self.local_address) message.receive() thread(self.analyze, (message,) ) return
def ask_connections(self, address): """ This functions "share" connections with another node """ daemon_address = self.__return_origin_to_use(address) message = Message(destination=address, origin=daemon_address) message.content = { "intent": DAEMON_NODE_CONN_SHR_ASK } message.send()
def connect(self, address): """ This function send a connection request to a specific siderus address """ if address in self.connections: return daemon_address = self.__return_origin_to_use(address) message = Message(destination=address, origin=daemon_address) message.content = {"intent": DAEMON_NODE_CONN_REQ} message.send()
def __send_app_connections(self, app): if not app in self.applications.keys(): return dest = from_arg_to_addr(app, "127.0.0.1", self.applications[app]) orig = from_arg_to_addr() message = Message(destination=dest, origin=orig) message.content = { "intent": DAEMON_APP_CONN_LST_ANS, "connections": self.connections } message.send()
def is_node_alive(self, address): """ This function check if the connection is still alive """ if not address in self.connections: return daemon_address = self.__return_origin_to_use(address) message = Message(destination=address, origin=daemon_address) message.content = {"intent": DAEMON_NODE_CONN_CHK} try: message.send() except: return False return True
def disconnect(self, address): """ This function send a disconnection request to a node """ if not address in self.connections: return self.connections.pop(self.connections.index(address)) daemon_address = self.__return_origin_to_use(address) message = Message(destination=address, origin=daemon_address) message.content = {"intent": DAEMON_NODE_CONN_REF} try: message.send() except: # The message is not received, so the connection will drop. pass
def add_application(self, application): """ This function send to the application the port to use establishing a connection. """ port = get_random_port(exclude_list=self.applications.values()) dest = from_arg_to_addr(application, "127.0.0.1", DEFAULT_APP_TMP_PORT) orig = return_daemon_address("127.0.0.1") app_address = return_application_address(application, port) message = Message(destination=dest, origin=orig) message.content = {"intent": DAEMON_APP_LCCN_REQ_PRT, "address": app_address } message.send() self.applications[application] = port
def __register_app(self): """ This function ask the Siderus Daemon to register the app """ self.address = from_arg_to_addr(app=self.name, port=DEFAULT_APP_TMP_PORT) daemon_address = return_daemon_address("127.0.0.1",DAEMON_LOC_PORT) message_ask = Message(origin=self.address, destination=daemon_address) message_ask.content = {"intent": DAEMON_APP_LCCN_REQ} message_port = Message(destination=self.address) message_ask.send() message_port.receive_and_decode() self.address = message_port.content['address']
def __forward_message(self, received_message): connection = return_daemon_address_by_giving_address(received_message.origin) if not connection in self.connections: return destination_dict = from_addr_to_dict(received_message.destination) if destination_dict['app'] in self.applications.keys(): destination_dict['port'] = int(self.applications[destination_dict['app']]) else: # If app "installed" but not active: cache the forwarded message! return destination_dict['addr'] = "127.0.0.1" new_destination = from_dict_to_addr(destination_dict) forward = Message() forward.content = received_message.content forward.origin = received_message.origin forward.destination = new_destination #if app is listening then send. Otherwise cache the message. forward.send()
#!/usr/bin/env python import sys sys.path.append("..") sys.path.append("../..") from siderus.message import Message from siderus.common import return_application_address, from_arg_to_addr import sys destination = from_arg_to_addr(app="MessageTester", addr=sys.argv[1], port=52225) origin = return_application_address("MessageTester", 52225) m = Message(" ".join(sys.argv[2:]), destination, origin) m.send()
#!/usr/bin/env python import sys sys.path.append("..") sys.path.append("../..") from siderus.message import Message from siderus.common import return_application_address, from_arg_to_addr destination = return_application_address("MessageTester" , 52225) m = Message(destination=destination) m.receive_and_decode() print(m.content)