class Node: def __init__(self, server_address: Address, set_register: bool = False) -> None: """ The Node object constructor. This object is our low-level abstraction for other peers in the network. Every node has a ClientSocket that should bind to the Node TCPServer address. Warnings: 1. Insert an exception handler when initializing the ClientSocket; when a socket closed here we will face to an exception and we should detach this Node and clear its output buffer. :param server_address: :param set_register: """ self.server_ip = parse_ip(server_address[0]) self.server_port = server_address[1] log(f"Node({server_address}): Initialized.") self.out_buff: List[Packet] = [] self.is_register = set_register self.__initialize_client_socket() def __initialize_client_socket(self): formatted_ip = ".".join( str(int(part)) for part in self.server_ip.split(".")) self.client = ClientSocket(formatted_ip, self.server_port, single_use=False) def send_message(self) -> None: """ Final function to send buffer to the client's socket. :return: """ for packet in self.out_buff: response = self.client.send(packet.get_buf()) if response != b'ACK': log(f"Node({self.get_server_address()}): Message of type {packet.get_type()} not ACKed." ) self.out_buff.clear() def add_message_to_out_buff(self, message: Packet) -> None: """ Here we will add a new message to the server out_buff, then in 'send_message' will send them. :param message: The message we want to add to out_buff :return: """ self.out_buff.append(message) def close(self) -> None: """ Closing client's object. :return: """ self.client.close() def get_server_address(self) -> Address: """ :return: Server address in a pretty format. :rtype: Address """ return self.server_ip, self.server_port def __eq__(self, other) -> bool: return self.server_ip == other.server_ip and self.server_port == other.server_port \ and self.is_register == other.is_register
class Node: def __init__(self, server_address, set_register=False): """ The Node object constructor. This object is our low-level abstraction for other peers in the network. Every node has a ClientSocket that should bind to the Node TCPServer address. Warnings: 1. Insert an exception handler when initializing the ClientSocket; when a socket closed here we will face to an exception and we should detach this Node and clear its output buffer. :param server_address: :param set_root: :param set_register: """ self.server_ip = Node.parse_ip(server_address[0]) self.server_port = Node.parse_port(server_address[1]) self.register = set_register self.out_buff = [] try: # self.client = ClientSocket(self.server_ip, self.server_port, single_use=False) self.client = ClientSocket(server_address[0], server_address[1], single_use=False) print("from client connecting to the server: " + server_address) #print("client address",server_address) except: "heree" self.out_buff.clear() def send_message(self): """ Final function to send buffer to the client's socket. :return: """ ans = 'not send' if len(self.out_buff) == 1 and not self.out_buff[0] == '': ans = self.client.send(self.out_buff[0]) else: s = "".join(self.out_buff) if not s == '': ans = self.client.send(s) self.out_buff.clear() return ans def add_message_to_out_buff(self, message): """ Here we will add a new message to the server out_buff, then in 'send_message' will send them. :param message: The message we want to add to out_buff :return: """ self.out_buff.append(message) def close(self): """ Closing client's object. :return: """ self.client.close() def get_server_address(self): """ :return: Server address in a pretty format. :rtype: tuple """ return self.server_ip, self.server_port def is_register(self): return self.register @staticmethod def parse_ip(ip): """ Automatically change the input IP format like '192.168.001.001'. :param ip: Input IP :type ip: str :return: Formatted IP :rtype: str """ print(ip) return '.'.join(str(int(part)).zfill(3) for part in ip.split('.')) @staticmethod def parse_port(port): """ Automatically change the input IP format like '05335'. :param port: Input IP :type port: str :return: Formatted IP :rtype: str """ return str(int(port)).zfill(5) @staticmethod def parse_address(address): return Node.parse_ip(address[0]), Node.parse_port(address[1])
class Node: def __init__(self, server_address, set_root=False, set_register=False): """ The Node object constructor. This object is our low-level abstraction for other peers in the network. Every node has a ClientSocket that should bind to the Node TCPServer address. Warnings: 1. Insert an exception handler when initializing the ClientSocket; when a socket closed here we will face to an exception and we should detach this Node and clear its output buffer. :param server_address: :param set_root: :param set_register: """ self.is_root = set_root self.is_registered = set_register self.server_ip = Node.parse_ip(server_address[0]) self.server_port = Node.parse_port(server_address[1]) print("New Node Created\n\tServer Address: ", server_address) print(self.server_ip, self.server_port) try: self.client = ClientSocket(mode=server_address[0], port=int(server_address[1]), single_use=False) except: raise ConnectionRefusedError self.out_buff = [] self.in_buff = [] def send_message(self): """ Final function to send buffer to the client's socket. :return: """ for data in self.out_buff: rcvd_data = self.client.send(data) self.in_buff.append(rcvd_data) self.out_buff = [] def add_message_to_out_buff(self, message): """ Here we will add a new message to the server out_buff, then in 'send_message' will send them. :param message: The message we want to add to out_buff :return: """ self.out_buff.append(message) def close(self): """ Closing client's object. :return: """ self.client.close() def get_server_address(self): """ :return: Server address in a pretty format. :rtype: tuple """ return self.server_ip, self.server_port @staticmethod def parse_ip(ip): """ Automatically change the input IP format like '192.168.001.001'. :param ip: Input IP :type ip: str :return: Formatted IP :rtype: str """ return '.'.join(str(int(part)).zfill(3) for part in ip.split('.')) @staticmethod def parse_port(port): """ Automatically change the input IP format like '05335'. :param port: Input IP :type port: str :return: Formatted IP :rtype: str """ return str(int(port)).zfill(5)
class Node: def __init__(self, server_address, set_root=False, set_register=False): """ The Node object constructor. This object is our low level abstraction for other peers in the network. Every node has a ClientSocket that should bind to the Node TCPServer address. Warnings: 1. Insert an exception handler when initialising the ClientSocket; when a socket closed here we will face to an exception and we should detach this Node and clear it's output buffer. :param server_address: :param set_root: :param set_register: """ self.server_ip = Node.parse_ip(server_address[0]) self.server_port = Node.parse_port(server_address[1]) print("Server Address: ", server_address) self.out_buff = [] self.is_root = set_root self.is_register_connection = set_register try: self.client = ClientSocket(self.server_ip, int(self.server_port, 10), single_use=False) except: print("Node was detached.") self.out_buff.clear() def send_message(self): """ Final function to send buffer to the clients socket. :return: """ for b in self.out_buff: response = self.client.send(bytes(b)) print("Response: ", response) if response != b'ACK': print("The ", self.get_server_address()[0], ": ", self.get_server_address()[1], " did not response with b'ACK'. ", response) self.out_buff.clear() def add_message_to_out_buff(self, message): """ Here we will add new message to the server out_buff, then in 'send_message' will send them. :param message: The message we want to add to out_buff :return: """ self.out_buff.append(message) def close(self): """ Closing client object. :return: """ self.client.close() def get_server_address(self): """ :return: Server address in a pretty format. :rtype: tuple """ return self.server_ip, self.server_port def get_standard_server_address(self): """ :return: Server address in standard format. :rtype: tuple """ port_prime = int(self.server_port) return self.server_ip, port_prime @staticmethod def parse_ip(ip): """ Automatically change the input IP format like '192.168.001.001'. :param ip: Input IP :type ip: str :return: Formatted IP :rtype: str """ return '.'.join(str(int(part)).zfill(3) for part in ip.split('.')) @staticmethod def parse_port(port): """ Automatically change the input IP format like '05335'. :param port: Input IP :type port: str :return: Formatted IP :rtype: str """ return str(int(port)).zfill(5)