def main(): global server simulation = Simulation() #interrompe l'esecuzione se da tastiera arriva la sequenza (CTRL + C) try: while True: print('Ready to serve...') connection_socket, addr = server.get_tcp_sock().accept() print('Connection accepted') try: print("Waiting to receive data...") message = connection_socket.recv(server.get_buffer_size()) utils.print_pkt_size(message) arrival_time = time.time_ns() pkt = pickle.loads(message) time_for_receipt = arrival_time - pkt.get_sending_time() utils.print_divider() print("DATA RECEIVED") utils.print_packet_header(pkt, "GATEWAY") print( "Time to receive data from gateway over a TCP connection: ", time_for_receipt, "ns") # controllare destination_ip if pkt.get_source_ip( ) == server.get_correct_gateway_ip_address(): connection_socket.send( pickle.dumps( Packet(simulation.get_server_mac(), simulation.get_gateway_send_mac(), simulation.get_server_ip(), simulation.get_gateway_send_ip(), "Measurements received"))) print("Data received from GATEWAY") data = pkt.get_payload() print_measurements(data) else: print("Permission denied") connection_socket.send( pickle.dumps( Packet( simulation.get_server_mac(), simulation.get_gateway_send_mac(), simulation.get_server_ip(), simulation.get_gateway_send_ip(), "Were you really looking for this IP_address?") )) utils.print_divider() except Exception as error: print(error) connection_socket.close() except KeyboardInterrupt: pass server.close_server_socket()
class TcpServer: def __init__(self): self.sim = Simulation() self.server_port = 3001 self.server_socket = sk.socket(sk.AF_INET, sk.SOCK_STREAM) self.server_socket.bind(('localhost', self.server_port)) self.server_socket.listen(1) self.buffer_size = 4096 def get_tcp_sock(self): return self.server_socket def close_server_socket(self): self.server_socket.close() def get_correct_gateway_ip_address(self): return self.sim.get_gateway_send_ip() def get_buffer_size(self): return self.buffer_size
class Gateway: DHCP_PORT = 1075 devices = {} # {ip_address: measurements} def __init__(self): self.sim = Simulation() self.rcv_sock = sk.socket(sk.AF_INET, sk.SOCK_DGRAM) # socket per invio delle misurazioni self.dhcp_sock = sk.socket(sk.AF_INET, sk.SOCK_DGRAM) self.dhcp_sock.bind(nc.dhcp_address) self.rcv_sock.bind(nc.gateway_address) self.dhcp = SimpleDhcp(self.dhcp_sock) self.dhcp.start() def get_rcv_ip_address(self): return self.sim.get_gateway_recv_ip() def get_udp_sock(self): return self.rcv_sock def get_devices(self): return self.devices def close_gateway(): self.dhcp.stop_thread() self.dhcp_sock.close() self.recv_sock.close() def send_data_to_server(self): try: server_socket = sk.socket(sk.AF_INET, sk.SOCK_STREAM) server_socket.connect(nc.server_address) print("SENDING DEVICES DATA TO SERVER") server_socket.send( pickle.dumps(Packet(self.sim.get_gateway_send_mac(), self.sim.get_server_mac(), self.sim.get_gateway_send_ip(), self.sim.get_server_ip(), self.get_devices() )) ) message = server_socket.recv(1024) utils.print_pkt_size(message) pkt = pickle.loads(message) utils.print_packet_header(pkt, "SERVER") print("Server answer:", pkt.get_payload()) utils.print_divider() print() except Exception as error: print(error) finally: server_socket.close() self.clear_measurments() def clear_measurments(self): self.get_devices().clear() def __send_answer(self, address, pkt, message): self.get_udp_sock().sendto( pickle.dumps(Packet(self.sim.get_gateway_recv_mac(), pkt.get_source_mac(), self.sim.get_gateway_recv_ip(), pkt.get_source_ip(), message ) ), address) def confirm_reception(self, address, pkt): self.__send_answer(address, pkt, "Measurements received") def discard_reception(self, address, pkt): self.__send_answer(address, pkt, "Wrong Ip address")