def __init__(self, server_ip, server_port, is_root=False, root_address=None): """ The Peer object constructor. Code design suggestions: 1. Initialise a Stream object for our Peer. 2. Initialise a PacketFactory object. 3. Initialise our UserInterface for interaction with user commandline. 4. Initialise a Thread for handling reunion daemon. Warnings: 1. For root Peer we need a NetworkGraph object. 2. In root Peer start reunion daemon as soon as possible. 3. In client Peer we need to connect to the root of the network, Don't forget to set this connection as a register_connection. :param server_ip: Server IP address for this Peer that should be pass to Stream. :param server_port: Server Port address for this Peer that should be pass to Stream. :param is_root: Specify that is this Peer root or not. :param root_address: Root IP/Port address if we are a client. :type server_ip: str :type server_port: int :type is_root: bool :type root_address: tuple """ self._is_root = is_root self.stream = Stream(server_ip, server_port) self.parent = None self.packets = [] self.neighbours = [] self.flagg = True self.reunion_accept = True self.reunion_daemon_thread = threading.Thread( target=self.run_reunion_daemon) self.reunion_sending_time = time.time() self.reunion_pending = False self._user_interface = UserInterface() self.packet_factory = PacketFactory() if self._is_root: self.network_nodes = [] self.registered_nodes = [] self.network_graph = NetworkGraph( GraphNode((server_ip, str(server_port).zfill(5)))) self.reunion_daemon_thread.start() else: self.root_address = root_address self.stream.add_node(root_address, set_register_connection=True)
def __init__(self, server_ip, server_port, is_root=False, root_address=None): """ The Peer object constructor. Code design suggestions: 1. Initialise a Stream object for our Peer. 2. Initialise a PacketFactory object. 3. Initialise our UserInterface for interaction with user commandline. 4. Initialise a Thread for handling reunion daemon. Warnings: 1. For root Peer, we need a NetworkGraph object. 2. In root Peer, start reunion daemon as soon as possible. 3. In client Peer, we need to connect to the root of the network, Don't forget to set this connection as a register_connection. :param server_ip: Server IP address for this Peer that should be pass to Stream. :param server_port: Server Port address for this Peer that should be pass to Stream. :param is_root: Specify that is this Peer root or not. :param root_address: Root IP/Port address if we are a client. :type server_ip: str :type server_port: int :type is_root: bool :type root_address: tuple """ # self.root_address = (SemiNode.parse_ip(root_address[0]),SemiNode.parse_port(root_address[1])) self.root_address = root_address self.stream = Stream(server_ip, server_port) self.packet_factory = PacketFactory() self.user_interfarce = UserInterface() self.server_ip = SemiNode.parse_ip(server_ip) self.server_port = SemiNode.parse_port(str(server_port)) self.is_root = is_root self.flag = False # self.root_address = (SemiNode.parse_ip(root_address[0]), SemiNode.parse_port(root_address[1])) self.neighbours = [] if self.is_root: print("from root in init") self.root_node = GraphNode(self.stream.get_server_address()) self.network_graph = NetworkGraph(self.root_node) self.reunions_arrival_time = dict() else: print("from peer in init") self.stream.add_node(root_address, set_register_connection=True) # self.graph_node = GraphNode((server_ip, server_port)) self.reunion_mode = None self.last_reunion_sent_time = None self.t = threading.Thread(target=self.run_reunion_daemon)
def __init__(self, server_ip, server_port, is_root=False, root_address=None, gui=False, interface=None): """ The Peer object constructor. Code design suggestions: 1. Initialise a Stream object for our Peer. 2. Initialise a PacketFactory object. 3. Initialise our UserInterface for interaction with user commandline. 4. Initialise a Thread for handling reunion daemon. Warnings: 1. For root Peer, we need a NetworkGraph object. 2. In root Peer, start reunion daemon as soon as possible. 3. In client Peer, we need to connect to the root of the network, Don't forget to set this connection as a register_connection. :param server_ip: Server IP address for this Peer that should be pass to Stream. :param server_port: Server Port address for this Peer that should be pass to Stream. :param is_root: Specify that is this Peer root or not. :param root_address: Root IP/Port address if we are a client. :type server_ip: str :type server_port: int :type is_root: bool :type root_address: tuple """ self.has_gui = gui self.ip = Node.parse_ip(server_ip) self.port = server_port self.stream = Stream(server_ip, server_port) self.packet_factory = PacketFactory() self.interface = interface self.parse_interface_thread = threading.Thread(target=self.handle_user_interface_buffer, daemon=True) self.is_root = is_root self.root_address = root_address self.reunion_daemon = threading.Thread(target=self.run_reunion_daemon, daemon=True) if is_root: root_node = GraphNode((server_ip, server_port)) self.network_graph = NetworkGraph(root_node) if not is_root: try: self.stream.add_node(root_address, True) except LostConnection as lc: print("Couldn't connect to root") self.father_address = None self.send_reunion_timer = 4 self.last_reunion_back = 0 self.is_alive = False
def __init__(self, server_ip, server_port, is_root=False, root_address=None): """ The Peer object constructor. Code design suggestions: 1. Initialise a Stream object for our Peer. 2. Initialise a PacketFactory object. 3. Initialise our UserInterface for interaction with user commandline. 4. Initialise a Thread for handling reunion daemon. Warnings: 1. For root Peer, we need a NetworkGraph object. 2. In root Peer, start reunion daemon as soon as possible. 3. In client Peer, we need to connect to the root of the network, Don't forget to set this connection as a register_connection. :param server_ip: Server IP address for this Peer that should be pass to Stream. :param server_port: Server Port address for this Peer that should be pass to Stream. :param is_root: Specify that is this Peer root or not. :param root_address: Root IP/Port address if we are a client. :type server_ip: str :type server_port: int :type is_root: bool :type root_address: tuple """ self.address = (Node.parse_ip(server_ip), Node.parse_port(str(server_port))) self.root_address = None if root_address is None else Node.parse_address( root_address) self.stream = Stream(server_ip, server_port, root_address) self.packet_factory = PacketFactory() self.ui = UserInterface() self.ui.daemon = True self.is_root = is_root self.parent_address = None self.children = [] self.network_graph = None self.registered_peers = None self.waiting_for_hello_back = False self.last_sent_hello_time = None self.reunion_daemon = threading.Thread(target=self.run_reunion_daemon) self.reunion_daemon.daemon = True if is_root: root_graph_node = GraphNode(self.address) root_graph_node.depth = 0 self.network_graph = NetworkGraph(root_graph_node) self.registered_peers = dict() self.last_received_hello_times = dict() self.reunion_daemon.start() elif root_address is not None: self.stream.add_node(root_address, set_register_connection=True) self.start_user_interface() print('Peer initialized.')