def select_router(self): """ Select a router from the given ones, avoids the null and not correct answers """ print("Finding Available Subnets...") message = Message.empty() message.message_type = MessageType.ROUTER_LIST_REQUEST message.source_mac = self.mac message.destination_ip = "195.1.10.10" self.sock.send(Utilities.serializeClass(message)) message = Utilities.deserializeClass( self.sock.recv(Utilities.getDefaultBufferSize())) if message.message_type == MessageType.ROUTER_LIST_EMPTY: print('No router found on server') self.exit() connections = {} for line in message.text.splitlines(): connections[line[:line.find('-')]] = line[line.find(',') + 1:] print('Select a router to connect (type the IP):') for key in connections.keys(): print("- " + key) selection = "" while selection not in connections.keys(): selection = input('-> ') self.init_socket() self.sock.connect(('127.0.0.1', int(connections[selection]))) message = Message.empty() message.message_type = MessageType.DHCP_REQUEST message.source_mac = self.mac message.destination_ip = "195.1.10.10" message.text = self.id self.sock.send(Utilities.serializeClass(message))
def id_to_router(self): """ Send a message to the now connected router, tell them your IP """ message = Message.empty() message.source_ip = self.ip message.source_mac = self.mac message.message_type = MessageType.CLIENT_IDENTIFY self.sock.send(Utilities.serializeClass(message))
def accept_incoming_client(self): while True: print("Attendo prossima connessione ...") client, clientAddress = self.serverSocket.accept() print("Benvenuto %s:%s !" % clientAddress) message = Message.empty() message.source_mac = self.serverMacAddress message.source_ip = self.serverIPAddress message.message_type = MessageType.WELCOME client.send(util.serializeClass(message)) Thread(target=self.client_management, args=(client, )).start()
def exit(self): """ Exit the client gracefully, closing everything. """ print("Shutting down the client...") message = Message.empty() message.source_ip = self.ip message.source_mac = self.mac message.message_type = MessageType.CLIENT_EXIT self.sock.send(Utilities.serializeClass(message)) self.sock.shutdown(SHUT_RDWR) self.sock.close() sys.exit(0)
def prepare_for_messaging(self): """ Prepare the Client for messaging, fetch che clients connected at the moment then send to the target IP input """ message = Message.empty() message.message_type = MessageType.CLIENT_LIST_REQUEST message.source_ip = self.ip message.source_mac = self.mac self.sock.send(Utilities.serializeClass(message)) self.connected_clients = Utilities.deserializeClass( self.sock.recv(Utilities.getDefaultBufferSize())) self.connected_clients = self.connected_clients.text.split('-') self.connected_clients.remove(self.ip) self.fetcher = Thread(target=self.send_message) self.fetcher.start()
def __init__(self, target_ip: str, target_port: str): """ Init the Client that targets the server for a connection Args: target_ip (str): ip of the server target_port (str): port of the server """ self.ip = "" self.mac = Utilities.mac_gen() self.connected_clients = list() self.received_message = Message.empty() self.id = str(datetime.now().timestamp()) self.sock = socket(AF_INET, SOCK_STREAM) self.sock.connect((target_ip, target_port)) self.fetcher = Thread self.receive_thread = Thread(target=self.receive) self.receive_thread.start()
def send_message(self): """ Loop that manages the messaging through clients """ print("Clients connected now:") for value in self.connected_clients: print('- ' + value) while True: selection = input( 'Input the IP of the client do you want to talk to (q to exit):' ) if selection == 'q': break message = Message.empty() message.source_ip = self.ip message.source_mac = self.mac message.destination_ip = selection message.message_type = MessageType.CLIENT_SEND_MESSAGE text_to_send = input('Text to send: ') message.text = text_to_send self.sock.send(Utilities.serializeClass(message)) self.exit()
def client_exit(self, message: Message, client: socket): print("Il SERVER riceve uscita da parte di client") content = message.text self.clientConnectedInRouter[client].remove(message.source_ip) return message.empty()