Example #1
0
class Peer:
    INITIAL_TIME_FOR_REUNION = 8
    SEND_REUNION_INTERVAL = 4
    REUNION_BACK_TIMEOUT = 4 + 2 * (8 * 2.5) + 2

    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 handle_user_interface_buffer(self):
        """
        In every interval, we should parse user command that buffered from our UserInterface.
        All of the valid commands are listed below:
            1. Register:  With this command, the client send a Register Request packet to the root of the network.
            2. Advertise: Send an Advertise Request to the root of the network for finding first hope.
            3. SendMessage: The following string will be added to a new Message packet and broadcast through the network.

        Warnings:
            1. Ignore irregular commands from the user.
            2. Don't forget to clear our UserInterface buffer.
        :return:
        """

        if self.is_root:
            while True:
                while len(self.interface.buffer) > 0:
                    args = self.interface.buffer[0].split()
                    command = args[0]
                    self.interface.buffer = self.interface.buffer[1:]
                    if command.lower() == "showmap" or command.lower() == "sm":
                        self.network_graph.print_all()
                    elif command.lower() == "sendmessage":
                        if len(args) >= 1:
                            self.send_message(args[1])
                time.sleep(0.1)
        else:
            while True:
                while len(self.interface.buffer) > 0:
                    args = self.interface.buffer[0].split()
                    command = args[0]
                    self.interface.buffer = self.interface.buffer[1:]
                    if command.lower() == "register":
                        self.register()
                    elif command.lower() == "advertise":
                        self.advertise()
                    elif command.lower() == "sendmessage":
                        if len(args) >= 1:
                            self.send_message(args[1])
                time.sleep(0.1)

    def run(self):
        """
        The main loop of the program.

        Code design suggestions:
            1. Parse server in_buf of the stream.
            2. Handle all packets were received from our Stream server.
            3. Parse user_interface_buffer to make message packets.
            4. Send packets stored in nodes buffer of our Stream object.
            5. ** sleep the current thread for 2 seconds **

        Warnings:
            1. At first check reunion daemon condition; Maybe we have a problem in this time
               and so we should hold any actions until Reunion acceptance.
            2. In every situation checkout Advertise Response packets; even is Reunion in failure mode or not

        :return:
        """

        self.reunion_daemon.start()
        self.parse_interface_thread.start()
        # TODO Warning 1?
        while True:
            packet = self.parse_in_buf()
            while packet is not None:
                print("PACKET HEADER:", packet.get_header())
                self.handle_packet(packet)
                packet = self.parse_in_buf()
            try:
                self.stream.send_out_buf_messages()
            except LostConnection as lc:
                self.handle_lost_connection(lc.node)
            time.sleep(2)

    def run_reunion_daemon(self):
        """

        In this function, we will handle all Reunion actions.

        Code design suggestions:
            1. Check if we are the network root or not; The actions are identical.
            2. If it's the root Peer, in every interval check the latest Reunion packet arrival time from every node;
               If time is over for the node turn it off (Maybe you need to remove it from our NetworkGraph).
            3. If it's a non-root peer split the actions by considering whether we are waiting for Reunion Hello Back
               Packet or it's the time to send new Reunion Hello packet.

        Warnings:
            1. If we are the root of the network in the situation that we want to turn a node off, make sure that you will not
               advertise the nodes sub-tree in our GraphNode.
            2. If we are a non-root Peer, save the time when you have sent your last Reunion Hello packet; You need this
               time for checking whether the Reunion was failed or not.
            3. For choosing time intervals you should wait until Reunion Hello or Reunion Hello Back arrival,
               pay attention that our NetworkGraph depth will not be bigger than 8. (Do not forget main loop sleep time)
            4. Suppose that you are a non-root Peer and Reunion was failed, In this time you should make a new Advertise
               Request packet and send it through your register_connection to the root; Don't forget to send this packet
               here, because in the Reunion Failure mode our main loop will not work properly and everything will be got stock!

        :return:
        """
        interval = 0.1
        if self.is_root:
            while True:
                self.network_graph.remove_all_expired_nodes()
                time.sleep(interval)
        else:
            while True:
                self.send_reunion_timer -= interval
                if self.send_reunion_timer <= 0 and self.is_alive:
                    self.send_reunion()
                    self.send_reunion_timer = Peer.SEND_REUNION_INTERVAL
                if time.time() - self.last_reunion_back > Peer.REUNION_BACK_TIMEOUT and self.is_alive:
                    print("Time:", time.time(), "Last:", self.last_reunion_back, "Timeout!!")
                    self.timeout()
                time.sleep(interval)

    def send_broadcast_packet(self, broadcast_packet):
        """

        For setting broadcast packets buffer into Nodes out_buff.

        Warnings:
            1. Don't send Message packets through register_connections.

        :param broadcast_packet: The packet that should be broadcast through the network.
        :type broadcast_packet: Packet

        :return:
        """
        for node in self.stream.get_nodes():
            if not node.is_register:
                self.stream.add_message_to_out_buff(node.get_server_address(), broadcast_packet.get_buf())

    def handle_packet(self, packet):
        """

        This function act as a wrapper for other handle_###_packet methods to handle the packet.

        Code design suggestion:
            1. It's better to check packet validation right now; For example Validation of the packet length.

        :param packet: The arrived packet that should be handled.

        :type packet Packet

        """
        # print("A New Packet Received!")
        # print("Header: ", packet.get_header())
        # print("Body: ", packet.get_body())

        # TODO: packet validation

        if packet.get_type() == Type.register:
            self.__handle_register_packet(packet)
        elif packet.get_type() == Type.advertise:
            self.__handle_advertise_packet(packet)
        elif packet.get_type() == Type.join:
            self.__handle_join_packet(packet)
        elif packet.get_type() == Type.message:
            self.__handle_message_packet(packet)
        elif packet.get_type() == Type.reunion:
            self.__handle_reunion_packet(packet)

    def __check_registered(self, source_address):
        """
        If the Peer is the root of the network we need to find that is a node registered or not.

        :param source_address: Unknown IP/Port address.
        :type source_address: tuple

        :return:
        """
        # TODO: ba rakhsha check konam
        return self.network_graph.is_registered(source_address)

    def __handle_advertise_packet(self, packet):
        """
        For advertising peers in the network, It is peer discovery message.

        Request:
            We should act as the root of the network and reply with a neighbour address in a new Advertise Response packet.

        Response:
            When an Advertise Response packet type arrived we should update our parent peer and send a Join packet to the
            new parent.

        Code design suggestion:
            1. Start the Reunion daemon thread when the first Advertise Response packet received.
            2. When an Advertise Response message arrived, make a new Join packet immediately for the advertised address.

        Warnings:
            1. Don't forget to ignore Advertise Request packets when you are a non-root peer.
            2. The addresses which still haven't registered to the network can not request any peer discovery message.
            3. Maybe it's not the first time that the source of the packet sends Advertise Request message. This will happen
               in rare situations like Reunion Failure. Pay attention, don't advertise the address to the packet sender
               sub-tree.
            4. When an Advertise Response packet arrived update our Peer parent for sending Reunion Packets.

        :param packet: Arrived advertise packet

        :type packet Packet

        :return:
        """
        body = packet.get_body()
        type = body[0:3]
        print("RCVD packet:", packet.body)
        if type == "REQ" and self.is_root:
            ip = packet.get_source_server_ip()
            port = int(packet.get_source_server_port())
            parent_address = self.network_graph.find_parent_and_assign(ip, port)
            if parent_address is None:
                # Ignore, the node is not registered
                return
            else:
                print("Answering Ad REQ of", ip, port, "Parent:", parent_address[0], parent_address[1])
                res = self.packet_factory.new_advertise_packet("RES", (self.ip, self.port), parent_address)
                self.stream.add_message_to_out_buff((ip, port), res.get_buf())
        elif type == "RES" and (not self.is_root):
            server_ip = body[3:18]
            server_port = int(body[18:23])
            print("Proposed Parent:", server_ip, server_port)

            # remove former parent node
            if self.father_address is not None:
                try:
                    parent_node = self.stream.get_node_by_server(self.father_address[0], self.father_address[1],
                                                                 only_not_registers=True)
                    self.stream.remove_node(parent_node)
                except ValueError as e:
                    print(repr(e))
                    traceback.print_exc()
            self.father_address = (server_ip, server_port)
            try:
                self.stream.add_node(self.father_address)
                res = self.packet_factory.new_join_packet((self.ip, self.port))
                self.stream.add_message_to_out_buff(self.father_address, res.get_buf())
                self.is_alive = True
                if self.has_gui:
                    self.interface.set_alive(self.is_alive)
                self.last_reunion_back = time.time() + Peer.INITIAL_TIME_FOR_REUNION
            except LostConnection as lc:
                print("Coudn't connect to father")

    def __handle_register_packet(self, packet):
        """
        For registration a new node to the network at first we should make a Node with stream.add_node for'sender' and
        save it.

        Code design suggestion:
            1.For checking whether an address is registered since now or not you can use SemiNode object except Node.

        Warnings:
            1. Don't forget to ignore Register Request packets when you are a non-root peer.

        :param packet: Arrived register packet
        :type packet Packet
        :return:
        """
        body = packet.get_body()
        type = body[0:3]
        if type == "REQ" and self.is_root:
            ip = body[3:18]
            port = int(body[18:23])
            print("Registering ", ip, port)
            self.network_graph.register_node(ip, port)
            try:
                self.stream.add_node((ip, port), set_register_connection=True)
                res = self.packet_factory.new_register_packet("RES", (self.ip, self.port))
                self.stream.add_message_to_out_buff((ip, port), res.get_buf())
            except LostConnection as lc:
                print("Coudn't connect to registered node")
        elif type == "RES" and (not self.is_root):
            if body[3:6] == "ACK":
                print("Register ACKed")

    def __check_neighbour(self, address):
        """
        It checks is the address in our neighbours array or not.

        :param address: Unknown address

        :type address: tuple

        :return: Whether is address in our neighbours or not.
        :rtype: bool
        """
        return self.stream.get_node_by_server(address[0], address[1]) is not None

    def __handle_message_packet(self, packet):
        """
        Only broadcast message to the other nodes.

        Warnings:
            1. Do not forget to ignore messages from unknown sources.
            2. Make sure that you are not sending a message to a register_connection.

        :param packet: Arrived message packet

        :type packet Packet

        :return:
        """
        sender = packet.get_source_server_address()
        if not self.__check_neighbour(sender):
            print("Ignored a Message from unknown source")
            return
        body = packet.get_body()
        if self.has_gui:
            self.interface.append_message(body)
        else:
            print("Message Received:\n", body)
        packet = self.packet_factory.new_message_packet(body, (self.ip, self.port))
        for node in self.stream.get_nodes():
            if node.get_server_address() != sender and (not node.is_register):
                self.stream.add_message_to_out_buff(node.get_server_address(), packet.get_buf())

    def __handle_reunion_packet(self, packet):
        """
        In this function we should handle Reunion packet was just arrived.

        Reunion Hello:
            If you are root Peer you should answer with a new Reunion Hello Back packet.
            At first extract all addresses in the packet body and append them in descending order to the new packet.
            You should send the new packet to the first address in the arrived packet.
            If you are a non-root Peer append your IP/Port address to the end of the packet and send it to your parent.

        Reunion Hello Back:
            Check that you are the end node or not; If not only remove your IP/Port address and send the packet to the next
            address, otherwise you received your response from the root and everything is fine.

        Warnings:
            1. Every time adding or removing an address from packet don't forget to update Entity Number field.
            2. If you are the root, update last Reunion Hello arrival packet from the sender node and turn it on.
            3. If you are the end node, update your Reunion mode from pending to acceptance.


        :param packet: Arrived reunion packet
        :return:
        """
        body = packet.get_body()
        type = body[0:3]
        number_of_entries = int(body[3:5])
        entries = []
        for i in range(number_of_entries):
            entry_ip = body[5 + i * 20: 5 + i * 20 + 15]
            entry_port = int(body[5 + i * 20 + 15: 5 + i * 20 + 20])
            entries.append((entry_ip, entry_port))
        if number_of_entries < 1:
            print("Warning: Invalid Reunion Packet")
            return
        if type == "REQ":
            if self.is_root:
                self.network_graph.update_latest_reunion_time(entries[0])
                first_hop = entries[-1]
                entries.reverse()
                res = self.packet_factory.new_reunion_packet("RES", (self.ip, self.port), entries)
                self.stream.add_message_to_out_buff(first_hop, res.get_buf())
            else:
                if not self.is_alive:
                    return
                entries.append((self.ip, self.port))
                res = self.packet_factory.new_reunion_packet("REQ", (self.ip, self.port), entries)
                self.stream.add_message_to_out_buff(self.father_address, res.get_buf())

        if type == "RES":
            if self.is_root:
                print("Warning: Invalid Reunion Packet")
                return
            if (self.ip, self.port) == entries[0]:
                self.last_reunion_back = time.time()
                return
            if (self.ip, self.port) != entries[-1]:
                print("Warning: Invalid Reunion Packet")
                return
            entries = entries[0:-1]
            next_hop = entries[-1]
            res = self.packet_factory.new_reunion_packet("RES", (self.ip, self.port), entries)
            self.stream.add_message_to_out_buff(next_hop, res.get_buf())

    def __handle_join_packet(self, packet):
        """
        When a Join packet received we should add a new node to our nodes array.
        In reality, there is a security level that forbids joining every node to our network.

        :param packet: Arrived register packet.


        :type packet Packet

        :return:
        """
        new_address = packet.get_source_server_address()
        try:
            self.stream.add_node(new_address, set_register_connection=False)
        except LostConnection as lc:
            print("Coudn't connect to joined node from", new_address)

    def __get_neighbour(self, sender):
        """
        Finds the best neighbour for the 'sender' from the network_nodes array.
        This function only will call when you are a root peer.

        Code design suggestion:
            1. Use your NetworkGraph find_live_node to find the best neighbour.

        :param sender: Sender of the packet
        :return: The specified neighbour for the sender; The format is like ('192.168.001.001', '05335').
        """
        return self.network_graph.find_parent_and_assign(sender[0], sender[1])

    def parse_in_buf(self):
        buffer = self.stream.read_in_buf()
        header_size = 20
        if len(buffer) < header_size:
            return None

        packet = Packet(buf=buffer[0:20])
        packet_length = packet.get_length()
        if len(buffer) < packet_length:
            return None
        packet = Packet(buf=buffer[0:packet_length])
        self.stream.delete_buffer(packet_length)
        return packet

    def register(self):
        print("Register Command!")
        if self.is_root:
            return
        req = self.packet_factory.new_register_packet("REQ", (self.ip, self.port), (self.ip, self.port))
        self.stream.add_message_to_out_buff(self.root_address, req.get_buf())

    def advertise(self):
        print("Advertisement Command")
        req = self.packet_factory.new_advertise_packet("REQ", (self.ip, self.port))
        self.stream.add_message_to_out_buff(self.root_address, req.get_buf())
        pass

    def send_message(self, message):
        print("Message Command! Message:", message)
        packet = self.packet_factory.new_message_packet(message, (self.ip, self.port))
        self.send_broadcast_packet(packet)

    def send_reunion(self):
        reunion_packet = self.packet_factory.new_reunion_packet("REQ", (self.ip, self.port), [(self.ip, self.port)])
        self.stream.add_message_to_out_buff(self.father_address, reunion_packet.get_buf())

    def timeout(self):
        self.is_alive = False
        if self.has_gui:
            self.interface.set_alive(self.is_alive)
        self.advertise()

    def handle_lost_connection(self, node):
        self.stream.remove_node(node)
        print("Connection with ", node.get_server_address(), " lost")
        if node.get_server_address() == self.father_address:
            print("Connection with father lost")
            self.timeout()
Example #2
0
class Peer:
    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 start_user_interface(self):
        """
        For starting UserInterface thread.

        :return:
        """
        print("Starting UI")
        print('Available commands: ' 'Register', 'Advertise', 'SendMessage')

        self._user_interface.start()

    def handle_user_interface_buffer(self):
        """
        In every interval we should parse user command that buffered from our UserInterface.
        All of the valid commands are listed below:
            1. Register:  With this command client send a Register Request packet to the root of the network.
            2. Advertise: Send an Advertise Request to the root of the network for finding first hope.
            3. SendMessage: The following string will be add to a new Message packet and broadcast through network.

        Warnings:
            1. Ignore irregular commands from user.
            2. Don't forget to clear our UserInterface buffer.
        :return:
        """
        available_commands = ['Register', 'Advertise', 'SendMessage']
        #
        # print("user interface handler ", self._user_interface.buffer)
        for buffer in self._user_interface.buffer:
            if len(buffer) == 0:
                continue

            print('User interface handler buffer:', buffer)

            if buffer.split(' ', 1)[0] == available_commands[0]:
                # print("Handling buffer/register in UI")
                self.stream.add_message_to_out_buff(
                    self.root_address,
                    self.packet_factory.new_register_packet(
                        "REQ", self.stream.get_server_address(),
                        self.root_address).get_buf())
            elif buffer.split(' ', 1)[0] == available_commands[1]:
                # print("Handling buffer/advertise in UI")
                self.stream.add_message_to_out_buff(
                    self.root_address,
                    self.packet_factory.new_advertise_packet(
                        "REQ", self.stream.get_server_address()).get_buf())

            elif buffer.split(' ', 1)[0] == available_commands[2]:
                # print("Handling buffer/SendMessage in UI")
                self.send_broadcast_packet(
                    self.packet_factory.new_message_packet(
                        buffer.split(' ', 1)[1],
                        self.stream.get_server_address()).get_buf())
            else:
                print('Unknown Command!!!')
        self._user_interface.buffer = []

    def run(self):
        """
        Main loop of the program.

        Code design suggestions:
            1. Parse server in_buf of the stream.
            2. Handle all packets were received from our Stream server.
            3. Parse user_interface_buffer to make message packets.
            4. Send packets stored in nodes buffer of our Stream object.
            5. ** sleep the current thread for 2 seconds **

        Warnings:
            1. At first check reunion daemon condition; Maybe we have a problem in this time
               and so we should hold any actions until Reunion acceptance.
            2. In every situations checkout Advertise Response packets; even is Reunion in failure mode or not

        :return:
        """

        print("Running the peer...")
        while True:

            time.sleep(2)
            temp_array = self.stream.read_in_buf()

            if not self.reunion_accept:
                print("Reunion failure")

                for b in self.stream.read_in_buf():
                    # print("In main while: ", b)
                    p = self.packet_factory.parse_buffer(b)
                    # print("In for: ", p.get_type())
                    if p.get_type() == 2:
                        self.handle_packet(p)
                        temp_array.remove(b)
                        # self.stream.send_out_buf_messages(only_register=True)
                        break
                continue

            for b in temp_array:
                # print("In main while: ", b)
                p = self.packet_factory.parse_buffer(b)
                self.handle_packet(p)
                # self.packets.remove(p)
            self.stream.clear_in_buff()

            self.handle_user_interface_buffer()
            # print("Main while before user_interface handler")
            # self.send_broadcast_packets()
            self.stream.send_out_buf_messages()

    def run_reunion_daemon(self):
        """

        In this function we will handle all Reunion actions.

        Code design suggestions:
            1. Check if we are the network root or not; The actions are identical.
            2. If it's the root Peer, in every interval check the latest Reunion packet arrival time from every nodes;
               If time is over for the node turn it off (Maybe you need to remove it from our NetworkGraph).
            3. If it's a non-root peer split the actions by considering whether we are waiting for Reunion Hello Back
               Packet or it's the time to send new Reunion Hello packet.

        Warnings:
            1. If we are root of the network in the situation that want to turn a node off, make sure that you will not
               advertise the nodes sub-tree in our GraphNode.
            2. If we are a non-root Peer, save the time when you have sent your last Reunion Hello packet; You need this
               time for checking whether the Reunion was failed or not.
            3. For choosing time intervals you should wait until Reunion Hello or Reunion Hello Back arrival,
               pay attention that our NetworkGraph depth will not be bigger than 8. (Do not forget main loop sleep time)
            4. Suppose that you are a non-root Peer and Reunion was failed, In this time you should make a new Advertise
               Request packet and send it through your register_connection to the root; Don't forget to send this packet
               here, because in Reunion Failure mode our main loop will not work properly and everything will got stock!

        :return:
        """
        if self._is_root:
            while True:
                for n in self.network_graph.nodes:
                    if time.time(
                    ) > n.latest_reunion_time + 36 and n is not self.network_graph.root:
                        print("We have lost a node!", n.address)
                        for child in n.children:
                            #   TODO    Turn all sub-tree off; not only children.
                            self.network_graph.turn_off_node(child.address)
                        self.network_graph.turn_off_node(n.address)
                        self.network_graph.remove_node(n.address)
                #   TODO    Handle this section
                time.sleep(2)

        else:
            while True:
                if not self.reunion_pending:
                    self.reunion_accept = True
                    time.sleep(4)
                    packet = self.packet_factory.new_reunion_packet(
                        "REQ", self.stream.get_server_address(),
                        [self.stream.get_server_address()])
                    self.stream.add_message_to_out_buff(
                        self.parent.get_server_address(), packet.get_buf())
                    self.reunion_pending = True
                    self.reunion_sending_time = time.time()
                    self.flagg = True
                else:
                    if time.time(
                    ) > self.reunion_sending_time + 38 and self.flagg:

                        print("Ooops, Reunion was failed.")

                        self.reunion_accept = False
                        advertise_packet = self.packet_factory.new_advertise_packet(
                            "REQ", self.stream.get_server_address())
                        self.stream.add_message_to_out_buff(
                            self.root_address, advertise_packet.get_buf())
                        self.flagg = False
                        self.stream.send_out_buf_messages(only_register=True)
                        # Reunion failed.
                        #   TODO    Make sure that parent will completely detach from our clients

    def send_broadcast_packet(self, broadcast_packet):
        """

        For setting broadcast packets buffer into Nodes out_buff.

        Warnings:
            1. Don't send Message packets through register_connections.

        :param broadcast_packet: The packet that should be broadcast through network.
        :type broadcast_packet: Packet

        :return:
        """

        for n in self.stream.nodes:
            # print(n.get_standard_server_address())
            # print(self.root_address)
            # if n.get_standard_server_address() != self.root_address:
            if not n.is_register_connection:
                self.stream.add_message_to_out_buff(n.get_server_address(),
                                                    broadcast_packet)

    def handle_packet(self, packet):
        """

        This function act as a wrapper for other handle_###_packet methods to handle the packet.

        Code design suggestion:
            1.It's better to check packet validation right now; For example: Validation of the packet length.

        :param packet: The arrived packet that should be handled.

        :type packet Packet

        """
        if packet.get_length() != len(packet.get_body()):
            # print("packet.get_length() = ", packet.get_length(), packet.get_body(), packet.get_source_server_ip(),packet.get_source_server_port())
            return print('Packet Length is incorrect.')
        # print("Handling the packet...")
        if packet.get_version() == 1:
            if packet.get_type() == 1:
                self.__handle_register_packet(packet)
            elif packet.get_type() == 2:
                self.__handle_advertise_packet(packet)
            elif packet.get_type() == 3:
                self.__handle_join_packet(packet)
            elif packet.get_type() == 4:
                self.__handle_message_packet(packet)
            elif packet.get_type() == 5:
                self.__handle_reunion_packet(packet)
            else:
                print('Unexpected type:\t', packet.get_type())
                # self.packets.remove(packet)

    def __check_registered(self, source_address):
        """
        If the Peer is root of the network we need to find that is a node registered or not.

        :param source_address: Unknown IP/Port address.
        :type source_address: tuple

        :return:
        """

        for n in self.registered_nodes:
            if n.get_address() == source_address:
                return True
        return False

    def __handle_advertise_packet(self, packet):
        """
        For advertising peers in network, It is peer discovery message.

        Request:
            We should act as root of the network and reply with a neighbour address in a new Advertise Response packet.

        Response:
            When a Advertise Response packet type arrived we should update our parent peer and send a Join packet to the
            new parent.

        Code design suggestion:
            1. Start the Reunion daemon thread when the first Advertise Response packet received.
            2. When an Advertise Response message arrived  make a new Join packet immediately for advertised address.

        Warnings:
            1. Don't forget to ignore Advertise Request packets when you are non-root peer.
            2. The addresses which still haven't registered to the network can not request any peer discovery message.
            3. Maybe it's not the first time that source of the packet send Advertise Request message. This will happen
               in rare situations like Reunion Failure. Pay attention that don't advertise the address in packet sender
               sub-tree.
            4. When an Advertise Response packet arrived update our Peer parent for sending Reunion Packets.

        :param packet: Arrived register packet

        :type packet Packet

        :return:
        """

        # print("Handling advertisement packet...")

        if packet.get_body()[0:3] == 'REQ':
            if not self._is_root:
                # return print("Register request packet send to a non root node!")
                return

            # print("Packet is in Request type")

            if not self.__check_registered(packet.get_source_server_address()):
                # print(packet.get_source_server_address(), " trying to advertise before registering.")
                return

            # TODO Here we should check that is the node was advertised in past then update our GraphNode

            neighbor = self.__get_neighbour(packet.get_source_server_address())
            # print("Neighbor: \t", neighbor)
            p = self.packet_factory.new_advertise_packet(
                type='RES',
                source_server_address=self.stream.get_server_address(),
                neighbor=neighbor)

            server_address = packet.get_source_server_address()
            node = self.network_graph.find_node(server_address[0],
                                                server_address[1])

            if node is not None:
                self.network_graph.turn_on_node(server_address)
            #
            # else:
            #     self.network_graph.add_node(node, neighbor.address)
            #
            self.network_graph.add_node(server_address[0], server_address[1],
                                        neighbor)

            # print("We want to add this to the node: ", packet.get_source_server_address())
            self.network_nodes.append(
                SemiNode(packet.get_source_server_ip(),
                         packet.get_source_server_port()))

            self.stream.add_message_to_out_buff(
                packet.get_source_server_address(), p.get_buf())
            # print()

        elif packet.get_body()[0:3] == 'RES':
            # print("Packet is in Response type")
            self.stream.add_node(
                (packet.get_body()[3:18], packet.get_body()[18:23]))
            if self.parent:
                if self.stream.get_node_by_server(
                        self.parent.server_ip,
                        self.parent.server_port) in self.stream.nodes:
                    self.stream.remove_node(self.parent)
            self.parent = self.stream.get_node_by_server(
                packet.get_body()[3:18],
                packet.get_body()[18:23])

            addr = self.stream.get_server_address()
            join_packet = self.packet_factory.new_join_packet(addr)
            self.stream.add_message_to_out_buff(
                self.parent.get_server_address(), join_packet.get_buf())
            self.reunion_pending = False

            if not self.reunion_daemon_thread.isAlive():
                # print("Reunion thread started")
                self.reunion_daemon_thread.start()
        else:
            raise print('Unexpected Type.')

    def __handle_register_packet(self, packet):
        """
        For registration a new node to the network at first we should make a Node with stream.add_node for'sender' and
        save it.

        Code design suggestion:
            1.For checking whether an address is registered since now or not you can use SemiNode object except Node.

        Warnings:
            1. Don't forget to ignore Register Request packets when you are non-root peer.

        :param packet: Arrived register packet
        :type packet Packet
        :return:
        """
        # print("Handling register packet body: ", packet.get_body())
        pbody = packet.get_body()
        if pbody[0:3] == 'REQ':
            # print("Packet is in Request type")
            if not self._is_root:
                raise print('Register request packet send to a root node!')
            else:

                if self.__check_registered(packet.get_source_server_address()):
                    print(packet.get_source_server_address(),
                          'Trying to register again')
                    return

                res = self.packet_factory.new_register_packet(
                    type='RES',
                    source_server_address=self.stream.get_server_address(),
                    address=self.stream.get_server_address())
                self.stream.add_node((packet.get_source_server_ip(),
                                      packet.get_source_server_port()),
                                     set_register_connection=True)
                # self.stream.add_client(pbody[3:18], pbody[18:23])
                self.registered_nodes.append(
                    SemiNode(packet.get_body()[3:18],
                             packet.get_body()[18:23]))
                self.stream.add_message_to_out_buff(
                    packet.get_source_server_address(), res.get_buf())
                # self.stream.add_message_to_out_buf((pbody[3:18], pbody[18:23]), res)

        if pbody[0:3] == 'RES':
            print("Register Response sent")
            if pbody[3:6] == "ACK":
                print('ACK! Registered accomplished')
            else:
                raise Exception(
                    'Root did not send ack in the register response packet!')

    def __check_neighbour(self, address):
        """
        Checks is the address in our neighbours array or not.

        :param address: Unknown address

        :type address: tuple

        :return: Whether is address in our neighbours or not.
        :rtype: bool
        """
        if address in self.neighbours:
            return True
        print(self.parent.get_server_address(), " ", address)
        if address == self.parent.get_server_address():
            return True
        return False

    def __handle_message_packet(self, packet):
        """
        Only broadcast message to the other nodes.

        Warnings:
            1. Do not forget to ignore messages from unknown sources.
            2. Make sure that you are not sending a message to a register_connection.

        :param packet:

        :type packet Packet

        :return:
        """
        # print("Handling message packet...")
        # print("The message was just arrived is: ", packet.get_body(), " and source of the packet is: ",
        #       packet.get_source_server_address())
        new_packet = self.packet_factory.new_message_packet(
            packet.get_body(), self.stream.get_server_address())
        # for n in self.stream.nodes:
        #     print("From here:\t", n.get_server_address(), " ", n.is_register_connection)

        if not self.__check_neighbour(packet.get_source_server_address()):
            # print("The message is from an unknown source.")
            return

        for n in self.stream.nodes:
            if not n.is_register_connection:
                if n.get_server_address() != packet.get_source_server_address(
                ):
                    # print("Node considered to send message to: ", n.get_server_address())
                    self.stream.add_message_to_out_buff(
                        n.get_server_address(), new_packet.get_buf())
                    print('Message sent to', n.get_server_address)

    def __handle_reunion_packet(self, packet):
        """
        In this function we should handle Reunion packet was just arrived.

        Reunion Hello:
            If you are root Peer you should answer with a new Reunion Hello Back packet.
            At first extract all addresses in the packet body and append them in descending order to the new packet.
            You should send the new packet to the first address in arrived packet.
            If you are a non-root Peer append your IP/Port address to the end of the packet and send it to your parent.

        Reunion Hello Back:
            Check that you are the end node or not; If not only remove your IP/Port address and send packet to the next
            address, otherwise you received your response from root and everything is fine.

        Warnings:
            1. Every time adding or removing an address from packet don't forget to update Entity Number field.
            2. If you are the root, update last Reunion Hello arrival packet from the sender node and turn it on.
            3. If you are the end node, update your Reunion mode from pending to acceptance.


        :param packet:
        :return:
        """
        # print("Handling reunion packet")
        if packet.get_body()[0:3] == "REQ":
            # print("Packet is in Request type")
            if self._is_root:
                number_of_entity = int(packet.get_body()[3:5])
                node_array = []
                ip_and_ports = packet.get_body()[5:]
                sender_ip = ip_and_ports[(number_of_entity - 1) *
                                         20:(number_of_entity - 1) * 20 + 15]
                sender_port = ip_and_ports[(number_of_entity - 1) * 20 +
                                           15:(number_of_entity - 1) * 20 + 20]
                for i in range(number_of_entity):
                    ip = ip_and_ports[i * 20:i * 20 + 15]
                    port = ip_and_ports[i * 20 + 15:i * 20 + 20]
                    node_array.insert(0, (ip, port))
                self.network_graph.turn_on_node(
                    packet.get_source_server_address())
                node = self.network_graph.find_node(
                    packet.get_source_server_ip(),
                    packet.get_source_server_port())
                node.latest_reunion_time = time.time()
                p = self.packet_factory.new_reunion_packet(
                    type='RES',
                    source_address=self.stream.get_server_address(),
                    nodes_array=node_array)
                self.stream.add_message_to_out_buff((sender_ip, sender_port),
                                                    p.get_buf())
            else:
                number_of_entity = int(packet.get_body()[3:5])
                node_array = []
                ip_and_ports = packet.get_body()[5:]
                for i in range(number_of_entity):
                    ip = ip_and_ports[i * 20:i * 20 + 15]
                    port = ip_and_ports[i * 20 + 15:i * 20 + 20]
                    node_array.append((ip, port))
                self_address = self.stream.get_server_address()
                node_array.append((self_address[0], self_address[1]))

                p = self.packet_factory.new_reunion_packet(
                    type='REQ',
                    source_address=self.stream.get_server_address(),
                    nodes_array=node_array)
                parent_address = self.parent.get_server_address()
                self.stream.add_message_to_out_buff(parent_address,
                                                    p.get_buf())
        elif packet.get_body()[0:3] == 'RES':
            # print("Packet is in Response type")
            number_of_entity = int(packet.get_body()[3:5])
            # print("Entity number is: ", number_of_entity)
            node_array = []
            ip_and_ports = packet.get_body()[5:]
            first_ip = ip_and_ports[0:15]
            first_port = ip_and_ports[15:20]
            self_address = self.stream.get_server_address()
            # print("Packet address: ", ip_and_ports[:20], ' address: ', self_address)

            if first_ip == self_address[0] and first_port == self_address[1]:

                if number_of_entity == 1:
                    print('Reunion Hello Back Packet Received')
                    self.reunion_pending = False
                    return

                sender_ip = ip_and_ports[20:35]
                sender_port = ip_and_ports[35:40]
                for i in range(1, number_of_entity):
                    ip = ip_and_ports[i * 20:i * 20 + 15]
                    port = ip_and_ports[i * 20 + 15:i * 20 + 20]
                    node_array.append((ip, port))
                p = self.packet_factory.new_reunion_packet(
                    type='RES',
                    source_address=self.stream.get_server_address(),
                    nodes_array=node_array)
                self.stream.add_message_to_out_buff((sender_ip, sender_port),
                                                    p.get_buf())
        else:
            raise print('Unexpected type')

    def __handle_join_packet(self, packet):
        """
        When a Join packet received we should add new node to our nodes array.
        In reality there is a security level that forbid joining every nodes to our network.

        :param packet: Arrived register packet.


        :type packet Packet

        :return:
        """
        print('Join packet sent')
        self.stream.add_node(packet.get_source_server_address())
        self.neighbours.append(packet.get_source_server_address())

        pass

    def __get_neighbour(self, sender):
        """
        Finds the best neighbour for the 'sender' from network_nodes array.
        This function only will call when you are a root peer.

        Code design suggestion:
            1.Use your NetworkGraph find_live_node to find the best neighbour.

        :param sender: Sender of the packet
        :return: The specified neighbor for the sender; The format is like ('192.168.001.001', '05335').
        """

        return self.network_graph.find_live_node(sender).address
Example #3
0
class Peer:
    DAEMON_THREAD_WAIT_TIME = 4
    MAXIMUM_WAIT_TIME = 2 * 2 * 8 + 4

    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.')

    def start_user_interface(self):
        """
        For starting UserInterface thread.

        :return:
        """

        self.ui.start()

    def handle_user_interface_buffer(self):
        """
        In every interval, we should parse user command that buffered from our UserInterface.
        All of the valid commands a re listed below:
            1. Register:  With this command, the client send a Register Request packet to the root of the network.
            2. Advertise: Send an Advertise Request to the root of the network for finding first hope.
            3. SendMessage: The following string will be added to a new Message packet and broadcast through the network.

        Warnings:
            1. Ignore irregular commands from the user.
            2. Don't forget to clear our UserInterface buffer.
        :return:
        """
        i = 0
        ui_buffer_snapshot_size = len(self.ui.buffer)
        while i < ui_buffer_snapshot_size:
            cmd = self.ui.buffer[i]
            if cmd == 'sendMessage':
                msg = self.ui.buffer[i + 1]
                broadcast_packet = self.packet_factory.new_message_packet(
                    msg, self.address)
                self.send_broadcast_packet(broadcast_packet)
                i += 2

            if self.is_root:
                continue
            if cmd == 'register':
                register_packet = self.packet_factory.new_register_packet(
                    Packet.BODY_REQ, self.address, self.address)
                print(register_packet.get_buf())
                self.stream.add_message_to_out_buff(self.root_address,
                                                    register_packet.get_buf(),
                                                    is_register_node=True)
                # print('register packet created')
            elif cmd == 'advertise':
                advertise_packet = self.packet_factory.new_advertise_packet(
                    Packet.BODY_REQ, self.address)
                print('sending', advertise_packet.get_buf())
                self.stream.add_message_to_out_buff(self.root_address,
                                                    advertise_packet.get_buf(),
                                                    is_register_node=True)
            elif cmd == 'suicide':
                exit(1)
            i += 1

        self.ui.buffer = self.ui.buffer[ui_buffer_snapshot_size:]

    def run(self):
        """
        The main loop of the program.

        Code design suggestions:
            1. Parse server in_buf of the stream.
            2. Handle all packets were received from our Stream server.
            3. Parse user_interface_buffer to make message packets.
            4. Send packets stored in nodes buffer of our Stream object.
            5. ** sleep the current thread for 2 seconds **

        Warnings:
            1. At first check reunion daemon condition; Maybe we have a problem in this time
               and so we should hold any actions until Reunion acceptance.
            2. In every situation checkout Advertise Response packets; even is Reunion in failure mode or not

        :return:
        """
        # TODO warnings handling

        while True:
            self.handle_user_interface_buffer()
            stream_in_buff_snapshot = self.stream.read_in_buf()
            snapshot_size = len(stream_in_buff_snapshot)
            if snapshot_size != 0:
                print(stream_in_buff_snapshot)
            for message in stream_in_buff_snapshot:
                packet = self.packet_factory.parse_buffer(message)
                # print('packet:', packet.get_buf())
                self.handle_packet(packet)

            self.stream.clear_in_buff(snapshot_size)
            self.stream.send_out_buf_messages()
            time.sleep(2)

    def run_reunion_daemon(self):
        """

        In this function, we will handle all Reunion actions.

        Code design suggestions:
            1. Check if we are the network root or not; The actions are identical.
            2. If it's the root Peer, in every interval check the latest Reunion packet arrival time from every node;
               If time is over for the node turn it off (Maybe you need to remove it from our NetworkGraph).
            3. If it's a non-root peer split the actions by considering whether we are waiting for Reunion Hello Back
               Packet or it's the time to send new Reunion Hello packet.

        Warnings:
            1. If we are the root of the network in the situation that we want to turn a node off, make sure that you will not
               advertise the nodes sub-tree in our GraphNode.
            2. If we are a non-root Peer, save the time when you have sent your last Reunion Hello packet; You need this
               time for checking whether the Reunion was failed or not.
            3. For choosing time intervals you should wait until Reunion Hello or Reunion Hello Back arrival,
               pay attention that our NetworkGraph depth will not be bigger than 8. (Do not forget main loop sleep time)
            4. Suppose that you are a non-root Peer and Reunion was failed, In this time you should make a new Advertise
               Request packet and send it through your register_connection to the root; Don't forget to send this packet
               here, because in the Reunion Failure mode our main loop will not work properly and everything will be got stock!

        :return:
        """

        while True:
            if self.is_root:
                for peer_address, last_time in list(
                        self.last_received_hello_times.items()):
                    elapsed_time = time.time() - last_time
                    if elapsed_time > self.MAXIMUM_WAIT_TIME:
                        self.network_graph.remove_node(peer_address)
            elif self.parent_address is not None:
                if not self.waiting_for_hello_back:
                    hello_packet = self.packet_factory.new_reunion_packet(
                        Packet.BODY_REQ, self.address, [self.address])
                    self.stream.add_message_to_out_buff(
                        self.parent_address, hello_packet.get_buf())
                    self.last_sent_hello_time = time.time()
                    self.waiting_for_hello_back = True
                else:
                    elapsed_time = time.time() - self.last_sent_hello_time
                    if elapsed_time > self.MAXIMUM_WAIT_TIME:
                        advertise_packet = self.packet_factory.new_advertise_packet(
                            Packet.BODY_REQ, self.address, self.address)
                        self.stream.add_message_to_out_buff(
                            self.root_address,
                            advertise_packet.get_buf(),
                            is_register_node=True)
                        self.stream.remove_node(
                            self.stream.get_node_by_server(
                                self.parent_address[0],
                                self.parent_address[1]))
                        self.parent_address = None
                        for child in self.children:
                            self.stream.remove_node(
                                self.stream.get_node_by_server(
                                    child[0], child[1]))
                        self.waiting_for_hello_back = False

            time.sleep(self.DAEMON_THREAD_WAIT_TIME)

    def send_broadcast_packet(self, broadcast_packet):
        """

        For setting broadcast packets buffer into Nodes out_buff.

        Warnings:
            1. Don't send Message packets through register_connections.

        :param broadcast_packet: The packet that should be broadcast through the network.
        :type broadcast_packet: Packet

        :return:
        """

        print('Sending new broadcast message: ', broadcast_packet.get_body())
        for child in self.children:
            self.stream.add_message_to_out_buff(child,
                                                broadcast_packet.get_buf())
        if not self.is_root:
            self.stream.add_message_to_out_buff(self.parent_address,
                                                broadcast_packet.get_buf())

    def handle_packet(self, packet):
        """
        This function act as a wrapper for other handle_###_packet methods to handle the packet.

        Code design suggestion:
            1. It's better to check packet validation right now; For example Validation of the packet length.

        :param packet: The arrived packet that should be handled.

        :type packet Packet

        """
        packet_type = packet.get_type()
        if packet.get_version() != Packet.VERSION:
            print('Error in packet: incorrect version')
            return
        if packet_type not in [
                Packet.REGISTER, Packet.ADVERTISE, Packet.JOIN, Packet.MESSAGE,
                Packet.REUNION
        ]:
            print('Error in packet: Unknown type')
            return
        if packet.get_length() != len(packet.get_body()):
            print('Error in packet: inconsistent body length')
            print('body length in header:', packet.get_length())
            print('real body length:', len(packet.get_body()))
            return
        print(time.time(), end=' ')
        if packet_type == Packet.REGISTER:
            print('register packet received')
            self.__handle_register_packet(packet)
        elif packet_type == Packet.ADVERTISE:
            print('advertise packet received')
            self.__handle_advertise_packet(packet)
        elif packet_type == Packet.JOIN:
            print('join packet received')
            self.__handle_join_packet(packet)
        elif packet_type == Packet.MESSAGE:
            print('message packet received')
            self.__handle_message_packet(packet)
        elif packet_type == Packet.REUNION:
            print('reunion packet received')
            self.__handle_reunion_packet(packet)
        else:
            print('unknown packet type')

    def __check_registered(self, source_address):
        """
        If the Peer is the root of the network we need to find that is a node registered or not.

        :param source_address: Unknown IP/Port address.
        :type source_address: tuple

        :return:
        """
        if self.registered_peers is not None:
            if str(
                (Node.parse_ip(source_address[0]),
                 Node.parse_port(source_address[1]))) in self.registered_peers:
                return True
            return False

    def __handle_advertise_packet(self, packet):
        """
        For advertising peers in the network, It is peer discovery message.

        Request:
            We should act as the root of the network and reply with a neighbour address in a new Advertise Response packet.

        Response:
            When an Advertise Response packet type arrived we should update our parent peer and send a Join packet to the
            new parent.

        Code design suggestion:
            1. Start the Reunion daemon thread when the first Advertise Response packet received.
            2. When an Advertise Response message arrived, make a new Join packet immediately for the advertised address.

        Warnings:
            1. Don't forget to ignore Advertise Request packets when you are a non-root peer.
            2. The addresses which still haven't registered to the network can not request any peer discovery message.
            3. Maybe it's not the first time that the source of the packet sends Advertise Request message. This will happen
               in rare situations like Reunion Failure. Pay attention, don't advertise the address to the packet sender
               sub-tree.
            4. When an Advertise Response packet arrived update our Peer parent for sending Reunion Packets.

        :param packet: Arrived advertise packet

        :type packet Packet

        :return:
        """
        body_str = packet.get_body()
        if self.is_root:
            if len(body_str) != 3 or body_str != Packet.BODY_REQ:
                return
            if not self.__check_registered(packet.get_source_server_address()):
                print(
                    'Peer that has sent request advertise has not registered before.'
                )
                return
            neighbour_address = self.__get_neighbour(
                packet.get_source_server_address())
            print('neighbour for', packet.get_source_server_address(), 'is',
                  neighbour_address)
            response_packet = self.packet_factory.new_advertise_packet(
                Packet.BODY_RES, packet.get_source_server_address(),
                Node.parse_address(neighbour_address))
            message = response_packet.get_buf()
            self.stream.add_message_to_out_buff(
                packet.get_source_server_address(),
                message,
                is_register_node=True)
            self.network_graph.add_node(packet.get_source_server_ip(),
                                        packet.get_source_server_port(),
                                        neighbour_address)
            self.network_graph.turn_on_node(packet.get_source_server_address())

            self.last_received_hello_times[
                packet.get_source_server_address()] = time.time()

        else:
            if len(body_str) != 23 or body_str[:3] != Packet.BODY_RES:
                return
            parent_ip = body_str[3:18]
            parent_port = body_str[18:23]
            self.parent_address = (parent_ip, parent_port)
            self.stream.add_node(self.parent_address)
            join_packet = self.packet_factory.new_join_packet(self.address)
            message = join_packet.get_buf()
            self.stream.add_message_to_out_buff(self.parent_address, message)

            self.waiting_for_hello_back = False
            if not self.reunion_daemon.is_alive():
                self.reunion_daemon.start()

    def __handle_register_packet(self, packet):
        """
        For registration a new node to the network at first we should make a Node with stream.add_node for'sender' and
        save it.

        Code design suggestion:
            1.For checking whether an address is registered since now or not you can use SemiNode object except Node.

        Warnings:
            1. Don't forget to ignore Register Request packets when you are a non-root peer.

        :param packet: Arrived register packet
        :type packet Packet
        :return:
        """
        if not self.is_root:
            return

        body_str = packet.get_body()
        if len(body_str) != 23:
            print('register request packet length is not 23')

        body_type = body_str[:3]
        if body_type == Packet.BODY_REQ:
            source_ip = body_str[3:18]
            source_port = body_str[18:23]
            source_address = (source_ip, source_port)
            if self.__check_registered(source_address):
                print('peer is already been registered!')
                return
            self.registered_peers[str(source_address)] = True
            self.stream.add_node(source_address, set_register_connection=True)
            response_packet = self.packet_factory.new_register_packet(
                Packet.BODY_RES, source_address)
            message = response_packet.get_buf()
            self.stream.add_message_to_out_buff(source_address,
                                                message,
                                                is_register_node=True)
            print('registered peers', self.registered_peers)
        else:
            print('register body type is not REQ')

    def __check_neighbour(self, address):
        """
        It checks if the address in our neighbours array or not.

        :param address: Unknown address

        :type address: tuple

        :return: Whether is address in our neighbours or not.
        :rtype: bool
        """
        return address == self.parent_address or address in self.children

    def __handle_message_packet(self, packet):
        """
        Only broadcast message to the other nodes.

        Warnings:
            1. Do not forget to ignore messages from unknown sources.
            2. Make sure that you are not sending a message to a register_connection.

        :param packet: Arrived message packet

        :type packet Packet

        :return:

        """

        if not self.__check_neighbour(packet.get_source_server_address()):
            print(
                'received message from unknown source, not found in neighbours'
            )
            return

        if self.stream.get_node_by_server(packet.get_source_server_ip(),
                                          packet.get_source_server_port()
                                          ) not in self.stream.nodes.values():
            print('source not found in stream nodes')
            return
        message = packet.get_body()
        print('New message received from', packet.get_source_server_address(),
              ':', message)
        message_packet = self.packet_factory.new_message_packet(
            message, self.address)
        for child in self.children:
            if child != packet.get_source_server_address():
                self.stream.add_message_to_out_buff(child,
                                                    message_packet.get_buf())
        if not self.is_root and self.parent_address != packet.get_source_server_address(
        ):
            self.stream.add_message_to_out_buff(self.parent_address,
                                                message_packet.get_buf())

    def __handle_reunion_packet(self, packet):
        """
        In this function we should handle Reunion packet was just arrived.

        Reunion Hello:
            If you are root Peer you should answer with a new Reunion Hello Back packet.
            At first extract all addresses in the packet body and append them in descending order to the new packet.
            You should send the new packet to the first address in the arrived packet.
            If you are a non-root Peer append your IP/Port address to the end of the packet and send it to your parent.

        Reunion Hello Back:
            Check that you are the end node or not; If not only remove your IP/Port address and send the packet to the next
            address, otherwise you received your response from the root and everything is fine.

        Warnings:
            1. Every time adding or removing an address from packet don't forget to update Entity Number field.
            2. If you are the root, update last Reunion Hello arrival packet from the sender node and turn it on.
            3. If you are the end node, update your Reunion mode from pending to acceptance.


        :param packet: Arrived reunion packet
        :return:
        """
        body_str = packet.get_body()
        num_of_entries = int(body_str[3:5])

        if num_of_entries != len(body_str[5:]) // 20:
            return

        path_peers_str = body_str[5:]
        path_peers = []

        for i in range(0, len(path_peers_str), 20):
            ip = path_peers_str[i:i + 15]
            port = path_peers_str[i + 15:i + 20]
            path_peers.append((ip, port))

        if self.is_root:
            if body_str[0:3] != Packet.BODY_REQ:
                return

            self.last_received_hello_times[
                packet.get_source_server_address()] = time.time()

            path_peers.reverse()
            hello_back_packet = self.packet_factory.new_reunion_packet(
                Packet.BODY_RES, self.address, path_peers)
            message = hello_back_packet.get_buf()
            self.stream.add_message_to_out_buff(path_peers[0], message)

        else:
            if body_str[0:3] == Packet.BODY_REQ:
                path_peers.append(self.address)
                hello_packet = self.packet_factory.new_reunion_packet(
                    Packet.BODY_REQ, packet.get_source_server_address(),
                    path_peers)
                message = hello_packet.get_buf()
                self.stream.add_message_to_out_buff(self.parent_address,
                                                    message)
            elif body_str[0:3] == Packet.BODY_RES:
                if path_peers[0] != self.address:
                    return
                if len(path_peers) == 1 and self.waiting_for_hello_back:
                    self.waiting_for_hello_back = False
                    return

                path_peers = path_peers[1:]
                hello_back_packet = self.packet_factory.new_reunion_packet(
                    Packet.BODY_RES, packet.get_source_server_address(),
                    path_peers)
                message = hello_back_packet.get_buf()
                self.stream.add_message_to_out_buff(path_peers[0], message)

    def __handle_join_packet(self, packet):
        """
        When a Join packet received we should add a new node to our nodes array.
        In reality, there is a security level that forbids joining every node to our network.

        :param packet: Arrived register packet.
        :type packet Packet

        :return:
        """
        body_str = packet.get_body()
        if len(body_str) != 4 or body_str != Packet.BODY_JOIN:
            return
        source_address = packet.get_source_server_address()
        self.stream.add_node(source_address)
        self.children.append(source_address)
        # if len(self.children) > 2:
        #     raise Exception('protection forgotten. excessive child!')

    def __get_neighbour(self, sender):
        """
        Finds the best neighbour for the 'sender' from the network_nodes array.
        This function only will call when you are a root peer.

        Code design suggestion:
            1. Use your NetworkGraph find_live_node to find the best neighbour.

        :param sender: Sender of the packet
        :return: The specified neighbour for the sender; The format is like ('192.168.001.001', '05335').
        """
        return self.network_graph.find_live_node(sender).address
Example #4
0
class Peer:
    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 start_user_interface(self):
        """
        For starting UserInterface thread.

        :return:
        """
        self.user_interfarce.start()
        t = threading.Thread(target=self.handle_user_interface_buffer)
        t.start()

    def handle_user_interface_buffer(self):
        """
        In every interval, we should parse user command that buffered from our UserInterface.
        All of the valid commands are listed below:
            1. Register:  With this command, the client send a Register Request packet to the root of the network.
            2. Advertise: Send an Advertise Request to the root of the network for finding first hope.
            3. SendMessage: The following string will be added to a new Message packet and broadcast through the network.

        Warnings:
            1. Ignore irregular commands from the user.
            2. Don't forget to clear our UserInterface buffer.
        :return:
        """
        while 1:
            if "".join(self.user_interfarce.buffer) == 'Register':
                print("Register")
                register_packet = self.packet_factory.new_register_packet(
                    "REQ", self.stream.get_server_address(), self.root_address)
                self.stream.add_message_to_out_buff(self.root_address,
                                                    register_packet.get_buf())
                self.stream.print_out_buffs()
            elif "".join(self.user_interfarce.buffer) == 'Advertise':
                print("Advertise")
                advertise_packet = self.packet_factory.new_advertise_packet(
                    "REQ", self.stream.get_server_address())
                self.stream.add_message_to_out_buff(self.root_address,
                                                    advertise_packet.get_buf())
                self.stream.print_out_buffs()
            elif "".join(self.user_interfarce.buffer)[:11] == 'SendMessage':
                print("SendMessage")
                print("".join(self.user_interfarce.buffer)[11:])
                message_packet = self.packet_factory.new_message_packet(
                    "".join(self.user_interfarce.buffer)[11:],
                    self.stream.get_server_address())
                print(message_packet.get_buf())
                self.send_broadcast_packet(message_packet)
            else:
                if len(self.user_interfarce.buffer) != 0:
                    print("command not supported or empty")

            self.user_interfarce.buffer.clear()
            time.sleep(0.5)

    def run(self):
        """
        The main loop of the program.

        Code design suggestions:
            1. Parse server in_buf of the stream.
            2. Handle all packets were received from our Stream server.
            3. Parse user_interface_buffer to make message packets.
            4. Send packets stored in nodes buffer of our Stream object.
            5. ** sleep the current thread for 2 seconds **

        Warnings:
            1. At first check reunion daemon condition; Maybe we have a problem in this time
               and so we should hold any actions until Reunion acceptance.
            2. In every situation checkout Advertise Response packets; even is Reunion in failure mode or not

        :return:


        """
        while True:

            if self.is_root or (
                    not self.is_root and
                    not (self.reunion_mode == "pending" and datetime.now() -
                         self.last_reunion_sent_time > timedelta(seconds=4))):
                for buffer in self.stream.read_in_buf():
                    packet = self.packet_factory.parse_buffer(buffer)
                    self.handle_packet(packet)
                self.stream.clear_in_buff()

                # TODO: user interface buffer parse
                if not self.flag:
                    self.start_user_interface()
                    self.flag = True
                # print(self.stream._server_in_buf)
                # print(self.stream.print_out_buffs())
                print(self.stream.send_out_buf_messages())
            elif not self.is_root and self.reunion_mode == "pending" and datetime.now(
            ) - self.last_reunion_sent_time > timedelta(seconds=4):
                for buffer in self.stream.read_in_buf():
                    packet = self.packet_factory.parse_buffer(buffer)
                    if packet.get_type() == 2 and packet.get_res_or_req(
                    ) == "RES":
                        self.__handle_advertise_packet(packet)
            time.sleep(5)

        pass

    def run_reunion_daemon(self):
        """

        In this function, we will handle all Reunion actions.

        Code design suggestions:
            1. Check if we are the network root or not; The actions are identical.
            2. If it's the root Peer, in every interval check the latest Reunion packet arrival time from every node;
               If time is over for the node turn it off (Maybe you need to remove it from our NetworkGraph).
            3. If it's a non-root peer split the actions by considering whether we are waiting for Reunion Hello Back
               Packet or it's the time to send new Reunion Hello packet.

        Warnings:
            1. If we are the root of the network in the situation that we want to turn a node off, make sure that you will not
               advertise the nodes sub-tree in our GraphNode.
            2. If we are a non-root Peer, save the time when you have sent your last Reunion Hello packet; You need this
               time for checking whether the Reunion was failed or not.
            3. For choosing time intervals you should wait until Reunion Hello or Reunion Hello Back arrival,
               pay attention that our NetworkGraph depth will not be bigger than 8. (Do not forget main loop sleep time)
            4. Suppose that you are a non-root Peer and Reunion was failed, In this time you should make a new Advertise
               Request packet and send it through your register_connection to the root; Don't forget to send this packet
               here, because in the Reunion Failure mode our main loop will not work properly and everything will be got stock!

        :return:
        """

        # TODO: this part is definitely gonna cause a lot of bugs,im not sure with the delays and 4 seconds

        while True:
            if self.is_root:
                for address, last_reunion_time in self.reunions_arrival_time.items(
                ):
                    if (datetime.now() -
                            last_reunion_time) > timedelta(seconds=16):
                        self.network_graph.turn_off_node(address)
                        self.network_graph.turn_off_subtree(address)
                        self.network_graph.remove_node(address)
            else:
                if self.reunion_mode == "acceptance":
                    if (datetime.now() -
                            self.last_reunion_sent_time) >= timedelta(
                                seconds=4):
                        nodes_array = [(self.server_ip, self.server_port)]
                        new_packet = self.packet_factory.new_reunion_packet(
                            "REQ", (self.server_ip, self.server_port),
                            nodes_array)
                        self.stream.add_message_to_out_buff(
                            self.stream.get_parent_node().get_server_address(),
                            new_packet.get_buf())
                        self.last_reunion_sent_time = datetime.now()
                        self.reunion_mode = "pending"
                elif self.reunion_mode == "pending":
                    if (datetime.now() -
                            self.last_reunion_sent_time) > timedelta(
                                seconds=16):
                        advertise_packet = self.packet_factory.new_advertise_packet(
                            "REQ", (self.server_ip, self.server_port))
                        self.stream.add_message_to_out_buff(
                            self.root_address, advertise_packet.get_buf())

    def send_broadcast_packet(self, broadcast_packet):
        """

        For setting broadcast packets buffer into Nodes out_buff.

        Warnings:
            1. Don't send Message packets through register_connections.

        :param broadcast_packet: The packet that should be broadcast through the network.
        :type broadcast_packet: Packet

        :return:
        """
        print("Send broadcast message: " + str(broadcast_packet.get_buf()))
        message = broadcast_packet.get_buf()
        self.stream.broadcast_to_none_registers(
            message, self.stream.get_server_address())

    def handle_packet(self, packet):
        """

        This function act as a wrapper for other handle_###_packet methods to handle the packet.

        Code design suggestion:
            1. It's better to check packet validation right now; For example Validation of the packet length.

        :param packet: The arrived packet that should be handled.

        :type packet Packet

        """

        type = packet.get_type()
        if packet.get_version() != 1:
            print("unsupported version", file=sys.stderr)
            raise ValueError
        if type < 1 or type > 5:
            print("unknown packet type", file=sys.stderr)
            raise ValueError

        if type == 1:
            self.__handle_register_packet(packet)
        elif type == 2:
            self.__handle_advertise_packet(packet)
        elif type == 3:
            self.__handle_join_packet(packet)
        elif type == 4:
            self.__handle_message_packet(packet)
        elif type == 5:
            self.__handle_reunion_packet(packet)

    def __check_registered(self, source_address):
        """
        If the Peer is the root of the network we need to find that is a node registered or not.

        :param source_address: Unknown IP/Port address.
        :type source_address: tuple

        :return:
        """
        if self.is_root:
            if self.stream.get_node_by_server(source_address[0],
                                              source_address[1]):
                if self.stream.get_node_by_server(
                        source_address[0], source_address[1]).is_register():
                    return True

    def __handle_advertise_packet(self, packet):
        """
        For advertising peers in the network, It is peer discovery message.

        Request:
            We should act as the root of the network and reply with a neighbour address in a new Advertise Response packet.

        Response:
            When an Advertise Response packet type arrived we should update our parent peer and send a Join packet to the
            new parent.

        Code design suggestion:
            1. Start the Reunion daemon thread when the first Advertise Response packet received.
            2. When an Advertise Response message arrived, make a new Join packet immediately for the advertised address.

        Warnings:
            1. Don't forget to ignore Advertise Request packets when you are a non-root peer.
            2. The addresses which still haven't registered to the network can not request any peer discovery message.
            3. Maybe it's not the first time that the source of the packet sends Advertise Request message. This will happen
               in rare situations like Reunion Failure. Pay attention, don't advertise the address to the packet sender
               sub-tree.
            4. When an Advertise Response packet arrived update our Peer parent for sending Reunion Packets.

        :param packet: Arrived register packet

        :type packet Packet

        :return:
        """

        if packet.get_res_or_req() == "REQ":
            if self.is_root:
                print("advertise REQ in root:" + str(packet.get_buf()))
                if self.__check_registered(packet.get_source_server_address()):
                    parent = self.__get_neighbour(
                        packet.get_source_server_address())
                    new_packet = self.packet_factory.new_advertise_packet(
                        "RES", self.stream.get_server_address(), parent)
                    print("packet from advertise root" +
                          str(new_packet.get_buf()))
                    self.stream.add_message_to_out_buff(
                        packet.get_source_server_address(),
                        new_packet.get_buf())
                    self.network_graph.add_node(
                        packet.get_source_server_ip(),
                        packet.get_source_server_port(), parent)

        else:
            if not self.is_root:
                print("advertise RES in peer:" + str(packet.get_buf()))
                buff = packet.get_buf()[23:]
                buff = str(buff)
                buff = buff[2:]
                buff = buff[:len(buff) - 1]
                ip = buff[:15]
                port = buff[15:20]
                print(ip, port)
                self.stream.add_parent_node(server_address=(ip, int(port)))
                self.stream.get_parent_node().add_message_to_out_buff(
                    PacketFactory.new_join_packet(
                        self.stream.get_server_address()).get_buf())
                print("join", self.stream.get_parent_node().out_buff)
                self.stream.get_parent_node().send_message()
                print("hogyttg")
                # self.t.run()
                print("adkofg")

    def __handle_register_packet(self, packet):
        """
        For registration a new node to the network at first we should make a Node with stream.add_node for'sender' and
        save it.

        Code design suggestion:
            1.For checking whether an address is registered since now or not you can use SemiNode object except Node.

        Warnings:
            1. Don't forget to ignore Register Request packets when you are a non-root peer.

        :param packet: Arrived register packet
        :type packet Packet
        :return:
        """
        # TODO:check this again
        if self.is_root:
            print("register in root:" + str(packet.get_buf()))
            if not self.__check_registered(packet.get_source_server_address()):
                print("node address ", packet.get_source_server_address())
                self.stream.add_node(packet.get_source_server_address(),
                                     set_register_connection=True)
                response_packet = self.packet_factory.new_register_packet(
                    "RES", self.stream.get_server_address())
                self.stream.add_message_to_out_buff(
                    packet.get_source_server_address(),
                    response_packet.get_buf())

    #   else:
    #      if packet.get_res_or_req() == "RES":

    #advertise_packet = self.packet_factory.new_advertise_packet("REQ", self.stream.get_server_address())
    #self.stream.add_message_to_out_buff(packet.get_source_server_address(), advertise_packet)

    def __check_neighbour(self, address):
        """
        It checks is the address in our neighbours array or not.

        :param address: Unknown address

        :type address: tuple

        :return: Whether is address in our neighbours or not.
        :rtype: bool
        """
        print("neighbour checked!")
        if self.stream.get_node_by_server(address[0], address[1]):
            if not (self.stream.get_node_by_server(address[0],
                                                   address[1]).is_register()):
                return True

        pass

    #Tested
    def __handle_message_packet(self, packet):
        """
        Only broadcast message to the other nodes.

        Warnings:
            1. Do not forget to ignore messages from unknown sources.
            2. Make sure that you are not sending a message to a register_connection.

        :param packet: Arrived message packet

        :type packet Packet

        :return:
        """
        print("from handle message: " + str(packet.get_buf()))

        body = str(packet.get_buf()[20:])

        new_packet = self.packet_factory.new_message_packet(
            body[2:len(body) - 1], self.stream.get_server_address())

        print(
            self.stream.broadcast_to_none_registers(
                new_packet.get_buf(), packet.get_source_server_address()))

    #Tested packet builds not send!
    def __handle_reunion_packet(self, packet):
        """
        In this function we should handle Reunion packet was just arrived.

        Reunion Hello:
            If you are root Peer you should answer with a new Reunion Hello Back packet.
            At first extract all addresses in the packet body and append them in descending order to the new packet.
            You should send the new packet to the first address in the arrived packet.
            If you are a non-root Peer append your IP/Port address to the end of the packet and send it to your parent.

        Reunion Hello Back:
            Check that you are the end node or not; If not only remove your IP/Port address and send the packet to the next
            address, otherwise you received your response from the root and everything is fine.

        Warnings:
            1. Every time adding or removing an address from packet don't forget to update Entity Number field.
            2. If you are the root, update last Reunion Hello arrival packet from the sender node and turn it on.
            3. If you are the end node, update your Reunion mode from pending to acceptance.


        :param packet: Arrived reunion packet
        :return:
        """
        # TODO: update number of entire
        res = packet.get_res_or_req()
        body = str(packet.get_buf()[25:])
        ips_str = body[2:len(body) - 1]
        ips = []
        ports = []
        for i in range(0, len(ips_str), 20):
            ips.append(ips_str[i:i + 15])
            ports.append(ips_str[i + 15:i + 20])

        if res == "REQ":
            if self.is_root:
                print("reunion REQ in root:" + str(packet.get_buf()))
                # updating reunions arrival time
                adds = []
                for i in range(len(ips)):
                    adds.append((SemiNode.parse_ip(ips[i]),
                                 SemiNode.parse_port(ports[i])))

                for address in adds:
                    self.reunions_arrival_time[address] = datetime.now()

                reversed_ips = ips[::-1]
                reversed_ports = ports[::-1]

                nodes_array = []

                for i in range(len(reversed_ips)):
                    nodes_array.append((reversed_ips[i], reversed_ports[i]))

                new_packet = self.packet_factory.new_reunion_packet(
                    "RES", self.stream.get_server_address(), nodes_array)
                node_address = (SemiNode.parse_ip(nodes_array[0][0]),
                                SemiNode.parse_port(nodes_array[0][1]))
                print(new_packet.get_buf())
                self.stream.add_message_to_out_buff(node_address, new_packet)

            else:
                print("reunion REQ in peer:" + str(packet.get_buf()))
                ips.append(self.stream.get_server_address()[0])
                ports.append(self.stream.get_server_address()[1])

                nodes_array = []

                for i in range(len(ips)):
                    nodes_array.append((ips[i], ports[i]))

                new_packet = self.packet_factory.new_reunion_packet(
                    "REQ", self.stream.get_server_address(), nodes_array)
                print(new_packet.get_buf())
                parent_address = self.stream.get_parent_node(
                ).get_server_address()
                self.stream.add_message_to_out_buff(parent_address, new_packet)

        elif res == "RES":
            print("reunion RES in peer:" + str(packet.get_buf()))
            if ips[len(ips) - 1] == self.stream.get_server_address()[0] \
                    and \
                    ports[len(ports) - 1] == self.stream.get_server_address()[1]:
                ips.pop(len(ips) - 1)
                ports.pop(len(ports) - 1)

                nodes_array = []

                for i in range(len(ips)):
                    nodes_array.append((ips[i], ports[i]))

                new_packet = self.packet_factory.new_reunion_packet(
                    "REQ", self.stream.get_server_address(), nodes_array)

                if len(ips) == 0:
                    self.reunion_mode = "acceptance"
                    print("accepted")
                else:
                    node_address = (SemiNode.parse_ip(nodes_array[0][0]),
                                    SemiNode.parse_port(nodes_array[0][1]))
                    print(new_packet.get_buf())
                    self.stream.add_message_to_out_buff(
                        node_address, new_packet)

    #Tested
    def __handle_join_packet(self, packet):
        """
        When a Join packet received we should add a new node to our nodes array.
        In reality, there is a security level that forbids joining every node to our network.

        :param packet: Arrived register packet.


        :type packet Packet

        :return:
        """
        print("recv join: " + str(packet.get_buf()))
        self.stream.add_node(packet.get_source_server_address())

    def __get_neighbour(self, sender):
        """
        Finds the best neighbour for the 'sender' from the network_nodes array.
        This function only will call when you are a root peer.

        Code design suggestion:
            1. Use your NetworkGraph find_live_node to find the best neighbour.

        :param sender: Sender of the packet
        :return: The specified neighbour for the sender; The format is like ('192.168.001.001', '05335').
        """
        return self.network_graph.find_live_node(sender)