def ireply_handler(self): msg_id = self.msg['id'] if msg_id in self.pit.keys(): for key, value in self.pit[msg_id].items(): if value: try: value['conn'].sendall(dumps(self.msg['data']).encode('utf-8')) value['conn'].close() except Exception as e: print(e) print('Unable to send message back') else: udp_msg = { 'type': 'DATA', 'id': uuid4(), 'data': { 'type': 'INTEREST_REPLY', 'id': msg_id, 'source': self.localhost, 'data': self.msg['data'] }, 'dest': key, 'ttl': 30, 'source': self.localhost } udp_data(dumps(udp_msg).encode('utf-8'), self.localhost, self.udp_port) self.cs[msg_id] = self.msg['data'] del self.pit[msg_id]
def ndn_hello_sender(self): ''' Envia Messagem do tipo "HELLO" com informação em CS e FIB ''' try: # Hello message to be sent self.msg = { "type": "HELLO", } if self.cs: for named_data in self.cs.keys(): self.msg[named_data] = [self.localhost] for key, value in self.fib.items(): self.msg[key] = value udp_msg = { 'type': 'DATA', 'id': str(uuid4()), 'data': self.msg, 'dest': self.mcast_group, 'ttl': 30, 'source': self.localhost } send_data.udp_data(udp_msg, '::1', 9999) except Exception as e: print('Sending error: {}'.format(e))
def rreply_handler(self): if not len(self.msg['path']) and self.msg['id'] in self.queue.keys(): print(dumps(self.msg)) self.route_table['dest'] = { 'timestamp': time(), 'next_hop': self.addr } data_msg = self.queue.pop(self.msg['id']) udp_data(data_msg, self.addr, self.mcast_port) elif self.msg['ttl'] - 1 and self.msg[ 'dest'] not in self.route_table.keys(): print(dumps(self.msg)) self.msg['ttl'] = self.msg['ttl'] - 1 self.msg['source'] = self.localhost self.route_table[self.msg['dest']] = { 'timestamp': time(), 'next_hop': self.addr } target = self.msg['path'].pop(-1) udp_data(self.msg, target, self.mcast_port)
def irequest_handler(self): msg_id = self.msg['id'] if msg_id in self.cs.keys(): udp_msg = { 'type': 'DATA', 'id': uuid4(), 'data': { 'type': 'INTEREST_REPLY', 'id': msg_id, 'source': self.localhost, 'data': self.cs[msg_id] }, 'dest': 'multicast', 'ttl': 30, 'source': self.localhost } udp_data( dumps(udp_msg).encode('utf-8'), self.localhost, self.udp_port) elif msg_id in self.pit.keys(): if not self.msg['source'] == self.localhost: self.pit[msg_id][self.msg['source']] = {} elif msg_id in self.fib.keys(): udp_msg = { 'type': 'DATA', 'id': uuid4(), 'data': { 'type': 'INTEREST_REQUEST', 'id': msg_id, 'source': self.localhost }, 'dest': self.fib[msg_id], 'ttl': 30, 'source': self.localhost } self.pit[msg_id][self.msg['source']] = {} udp_data( dumps(udp_msg).encode('utf-8'), self.localhost, self.udp_port) self.conn.close()
def send_data(self): if self.msg['dest'] == self.localhost: """ Enviar a mensagem para o server TCP localhost """ send_data.tcp_data(self.msg['data'], self.localhost, self.mcast_port) elif self.msg['dest'] == 'multicast': if not self.msg['id'] in self.queue.keys(): self.msg['ttl'] = self.msg['ttl'] - 1 if not self.msg['ttl']: return self.queue[self.msg['id']] = None send_data.udp_data(self.msg, self.mcast_addr, self.mcast_port) send_data.tcp_data(self.msg['data'], self.localhost, self.mcast_port) elif self.msg['dest'] in self.route_table.keys(): self.msg['ttl'] = self.msg['ttl'] - 1 if not self.msg['ttl']: return target = self.msg['dest'] if self.route_table[target]['next_hop'] is None: send_data.udp_data(self.msg, target, self.mcast_port) else: next_hop = self.route_table[target]['next_hop'] send_data.udp_data(self.msg, next_hop, self.mcast_port) send_data.tcp_data(self.msg['data'], self.localhost, self.mcast_port) elif self.msg['dest'] not in self.route_table.keys(): self.queue[self.msg['id']] = self.msg rrequest = { 'type': 'ROUTE_REQUEST', 'id': self.msg['id'], 'dest': self.msg['dest'], 'ttl': 30, 'path': [self.localhost], 'source': self.localhost } send_data.udp_data(rrequest, self.mcast_addr, self.mcast_port) send_data.tcp_data(self.msg['data'], self.localhost, self.mcast_port)
def rrequest_handler(self): if self.localhost in self.msg['path']: return elif self.msg['dest'] in self.route_table.keys( ) or self.msg['dest'] == self.localhost: self.msg['type'] = 'ROUTE_REPLY' self.msg['ttl'] = len(self.msg['path']) * 3 self.msg['source'] = self.localhost target = self.msg['path'].pop(-1) send_data.udp_data(self.msg, target, self.mcast_port) elif self.msg['ttl'] - 1: self.msg['ttl'] = self.msg['ttl'] - 1 self.msg['path'].append(self.localhost) self.msg['source'] = self.localhost send_data.udp_data(self.msg, self.mcast_addr, self.mcast_port)
def send_data(self): if self.msg['dest'] == self.localhost: """ Enviar a mensagem para o server TCP localhost """ tcp_sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM) tcp_sock.connect((self.localhost, self.mcast_port)) tcp_sock.sendall(dumps(self.msg['data']).encode("utf-8")) tcp_sock.close() elif self.msg['dest'] in self.route_table.keys(): self.msg['ttl'] = self.msg['ttl'] - 1 if not self.msg['ttl']: return target = self.msg['dest'] if self.route_table[target]['next_hop'] is None: udp_data(self.msg, target, self.mcast_port) else: next_hop = self.route_table[target]['next_hop'] udp_data(self.msg, next_hop, self.mcast_port) elif self.msg['dest'] not in self.route_table.keys(): self.queue[self.msg['id']] = self.msg rrequest = { 'type': 'ROUTE_REQUEST', 'id': self.msg['id'], 'dest': self.msg['dest'], 'ttl': 30, 'path': [self.localhost], 'source': self.localhost } udp_data(rrequest, self.mcast_addr, self.mcast_port)
def get_handler(self): msg_id = self.msg['id'] if msg_id in self.cs.keys(): self.conn.sendall(dumps(self.cs[msg_id]).encode('utf-8')) self.conn.close() elif msg_id in self.pit.keys(): pit_entry = {'conn': self.conn} self.pit[msg_id][self.msg['source']] = pit_entry self.conn.sendall( dumps({ 'status': 'we are still looking for the data' }).encode('utf-8')) elif msg_id in self.fib.keys(): udp_msg = { 'type': 'DATA', 'id': uuid4(), 'data': { 'type': 'INTEREST_REQUEST', 'id': msg_id, 'source': self.localhost }, 'dest': self.fib[msg_id], 'ttl': 30, 'source': self.localhost } pit_entry = {'conn': self.conn} self.pit[msg_id][self.msg['source']] = pit_entry udp_data( dumps(udp_msg).encode('utf-8'), self.localhost, self.udp_port) self.conn.sendall( dumps({ 'status': 'we have requested the data' }).encode('utf-8')) else: udp_msg = { 'type': 'DATA', 'id': uuid4(), 'data': { 'type': 'INTEREST_REQUEST', 'id': msg_id, 'source': self.localhost }, 'dest': 'multicast', 'ttl': 30, 'source': self.localhost } pit_entry = {'conn': self.conn} self.pit[msg_id][self.msg['source']] = pit_entry udp_data( dumps(udp_msg).encode('utf-8'), self.localhost, self.udp_port) self.conn.sendall( dumps({ 'status': 'we are looking for the data' }).encode('utf-8'))