Пример #1
0
    father = n.find_live_node(('localhost', 7777)).get_address()

    n.add_node('localhost', 7777, father)

    father = n.find_live_node(('localhost', 8888)).get_address()

    n.add_node('localhost', 8888, father)

    # n.turn_off_node(('localhost', 5555))

    for i in range(0, 20):
        n.increment_time()

    print(n.find_live_node(('localhost', 1111)).get_address())
    print(n.find_node('localhost', 3333).is_on)

    n.reunion_arrived(('localhost', 5555))
    n.reunion_arrived(('localhost', 6666))
    n.reunion_arrived(('localhost', 7777))
    n.reunion_arrived(('localhost', 8888))

    for i in range(0, 30):
        n.increment_time()

    print(n.find_live_node(('localhost', 1111)).get_address())

    print(n.find_node('localhost', 6666).is_on)

    for i in range(0, 80):
        n.increment_time()
Пример #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. --- done
			2. Initialise a PacketFactory object. ----- done
			3. Initialise our UserInterface for interaction with user commandline. ----- done
			4. Initialise a Thread for handling reunion daemon. ------- done

		Warnings:
			1. For root Peer, we need a NetworkGraph object. ----- done
			2. In root Peer, start reunion daemon as soon as possible. ------ done
			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. ------ done


		: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
		"""
        if root_address:
            root_address = (root_address[0], str(root_address[1]).zfill(5))
        server_port = str(server_port).zfill(5)
        self.is_root = is_root
        self.is_client_connected = False
        self.client_predecessor_address = tuple()
        self.successors_address = []
        self.client_is_waiting_for_helloback = False
        self.register_node = None
        self.client_reunion_timeout = False
        # TODO increase
        self.client_timeout_threshold = 10
        self.root_timeout_threshold = 10
        self.client_last_hello_time = 0
        self.nodes_for_root = {}  # {(address) : last_time_hello_came}
        self.address = (server_ip, server_port)
        self.stream = Stream(server_ip, server_port)
        self.user_interface = UserInterface()

        if self.is_root:
            graph_node_root = GraphNode(self.address)
            self.network_graph = NetworkGraph(graph_node_root)
            reunion_thread = threading.Thread(target=self.run_reunion_daemon)
            reunion_thread.start()
        else:
            self.root_address = root_address
            print("Set root address! " + str(self.root_address))
            self.stream.add_node(self.root_address, True)

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

		:return:
		"""
        print('Starting User Interface')
        self.user_interface.run()

    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:
		"""
        for message in self.user_interface.buffer:
            print('handling : ' + message)
            if message == 'Register':
                reg_packet = PacketFactory.new_register_packet(
                    "REQ", self.address, self.address)
                self.send_packet(reg_packet, self.root_address)
                self.stream.send_out_buf_messages(only_register=True)
            elif message == 'Advertise':
                advertise_packet = PacketFactory.new_advertise_packet(
                    type="REQ", source_server_address=self.address)
                self.send_advertise_packet(advertise_packet)
                self.stream.send_out_buf_messages(only_register=True)
            elif message.startswith('SendMessage'):
                to_be_sent = message.split()[1]
                message_packet = PacketFactory.new_message_packet(
                    message=to_be_sent, source_server_address=self.address)
                self.send_broadcast_packet(message_packet)
            else:
                continue

        self.user_interface.buffer.clear()

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

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

		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. ------- done
			2. In every situation checkout Advertise Response packets; even if Reunion in failure mode or not ------- done

		:return:
		"""

        while True:
            if not self.is_root:
                if self.is_client_connected:
                    input_buffer = self.stream.read_in_buf()
                    for buf in input_buffer:
                        packet = Packet(buf)
                        print('gonna print a received packet! ')
                        print(packet.__dict__)
                        self.handle_packet(packet)
                    self.stream.clear_in_buff(
                    )  # TODO buffer messages that use unvailable addreses
                    unavailable_addreses = self.stream.send_out_buf_messages()
                    if self.root_address in unavailable_addreses:
                        self.is_client_connected = False
                    for add in unavailable_addreses:
                        if add in self.successors_address:
                            self.successors_address.remove(add)
                else:
                    input_buffer = self.stream.read_in_buf()
                    for buf in input_buffer:
                        packet = Packet(buf)
                        if packet.type == PacketType.ADVERTISE:
                            self.__handle_advertise_packet(packet)
                    self.stream.clear_in_buff()
                self.handle_user_interface_buffer()
            else:
                input_buffer = self.stream.read_in_buf()
                for buf in input_buffer:
                    packet = Packet(buf)
                    print('gonna print a received packet! ')
                    print(packet.__dict__)
                    self.handle_packet(packet)

                self.stream.clear_in_buff()
                self.handle_user_interface_buffer()
                unavailable_addreses = self.stream.send_out_buf_messages()
                for add in unavailable_addreses:
                    if add in self.successors_address:
                        self.successors_address.remove(add)
            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 not 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!
			   --- done

		:return:
		"""
        while True:

            if self.is_root:
                now = time.time()
                to_be_deleted = []
                for peer_address, latest_reunion_msg in self.nodes_for_root.items(
                ):
                    passed_time = now - latest_reunion_msg
                    if passed_time > self.root_timeout_threshold:
                        print(
                            "I've waited more than enough! where is my hello from "
                            + str(peer_address))
                        to_be_deleted.append(peer_address)
                        self.network_graph.turn_off_node(peer_address)

                for peer_address in to_be_deleted:
                    self.nodes_for_root.pop(peer_address)
                    self.network_graph.remove_node(peer_address)
                    if peer_address in self.successors_address:
                        self.successors_address.remove(peer_address)
            else:
                if self.client_predecessor_address:
                    if not self.client_is_waiting_for_helloback:
                        reunion_packet = PacketFactory.new_reunion_packet(
                            "REQ", self.address, [self.address])
                        print("created hello packet! gonna send it! ")
                        self.forward_hello(packet=reunion_packet, is_mine=True)
                        self.client_last_hello_time = time.time()
                        self.client_is_waiting_for_helloback = True
                    elif time.time(
                    ) - self.client_last_hello_time >= self.client_timeout_threshold:
                        print(
                            "I've waited more than enough! where is my helloback "
                        )
                        self.client_is_waiting_for_helloback = False
                        self.is_client_connected = False
                        self.client_predecessor_address = None
                        adv_pckt = PacketFactory.new_advertise_packet(
                            "REQ", self.address)
                        self.send_advertise_packet(adv_pckt)

            time.sleep(4)

    def send_packet(self, packet, address):
        packet = self.change_header(packet)
        self.stream.add_message_to_out_buff(address, packet)

    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:
		"""
        sender_address = broadcast_packet.get_source_server_address()
        broadcast_packet = self.change_header(broadcast_packet)

        if self.is_root and broadcast_packet.type != PacketType.REUNION:
            for address in self.successors_address:
                if address != sender_address:
                    self.stream.add_message_to_out_buff(
                        address, broadcast_packet)
        elif self.is_root and broadcast_packet.type == PacketType.REUNION:
            dest_addr = broadcast_packet.body[-20:]
            dest_addr = (dest_addr[:15], dest_addr[15:])
            if dest_addr in self.successors_address:
                print('Finally sending hello back to ' + str(dest_addr))
                self.stream.add_message_to_out_buff(dest_addr,
                                                    broadcast_packet)
        elif broadcast_packet.type == PacketType.MESSAGE:
            all_addreses = [self.client_predecessor_address
                            ] + self.successors_address
            for address in all_addreses:
                if address != sender_address:
                    print('Going to broadcast a Message! ' + ' with body ' +
                          str(broadcast_packet.get_body()))
                    self.stream.add_message_to_out_buff(
                        address, broadcast_packet)
        else:
            return

    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.type == PacketType.REGISTER:
            self.__handle_register_packet(packet)
        elif packet.type == PacketType.MESSAGE:
            self.__handle_message_packet(packet)
        elif packet.type == PacketType.ADVERTISE:
            self.__handle_advertise_packet(packet)
        elif packet.type == PacketType.JOIN:
            self.__handle_join_packet(packet)
        elif packet.type == PacketType.REUNION:
            self.__handle_reunion_packet(packet)
        else:
            return

    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 not self.is_root:
            return False
        node = self.network_graph.find_node(source_address[0],
                                            source_address[1])
        return not node is None

    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 self.is_root:
            sender_address = packet.get_source_server_address()
            neighbour = self.__get_neighbour(sender_address)
            self.network_graph.add_node(sender_address[0], sender_address[1],
                                        neighbour.address)
            print("Someone has requested a neighbour")
            print(
                f'gave {neighbour.address} to {sender_address} as a neighbour')
            if self.__check_registered(
                    sender_address) and neighbour is not None:
                adv_packet = PacketFactory.new_advertise_packet(
                    "RES", self.address, neighbour=neighbour.address)
                self.send_packet(adv_packet, sender_address)
        elif packet.body.startswith('RES'):
            reunion_thread = threading.Thread(target=self.run_reunion_daemon)
            reunion_thread.start()
            join_pckt = PacketFactory.new_join_packet(self.address)
            self.client_predecessor_address = (packet.body[-20:-5],
                                               packet.body[-5:])
            print("I've found a father! " +
                  str(self.client_predecessor_address))
            self.stream.add_node(self.client_predecessor_address)
            self.send_packet(join_pckt, self.client_predecessor_address)
            self.is_client_connected = True
        else:
            return

    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
        else:
            sender = packet.get_source_server_address()
            if sender not in self.nodes_for_root:
                self.stream.add_node(sender)
                self.nodes_for_root.update({sender: time.time()})
            else:
                return

    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:
		"""
        self.send_broadcast_packet(packet)

    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:
		"""
        if self.is_root:
            sender_address = packet.get_first_address_hello_packet()
            print('Hello from ' + str(sender_address))
            self.nodes_for_root[sender_address] = time.time()
            self.network_graph.turn_on_node(sender_address)
            self.send_helloback(packet)
        else:
            if packet.is_reunion_hello():
                print('Forwarding Hello Packet from ' +
                      str(packet.get_first_address_hello_packet()))
                self.forward_hello(packet)
            else:
                if not self.helloback_is_mine(packet):
                    self.forward_helloback(packet)
                else:
                    print('I received hello back!')
                    self.client_is_waiting_for_helloback = False

    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:
		"""
        address = packet.get_source_server_address()
        print(f'received join from {address}')
        if address not in self.successors_address and len(
                self.successors_address) < 2:
            self.successors_address.append(address)
        self.stream.add_node(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)

    def send_helloback(self, packet):
        if not self.is_root:
            return
        print('Sending Hello back')
        packet.body = 'RES' + packet.body[3:]
        packet.source_ip = self.address[0]
        packet.source_port = self.address[1]
        packet = Packet(packet.get_buf())
        self.send_broadcast_packet(packet)

    def send_advertise_packet(self, advertise_packet):
        print('Sending advertise packet!')
        self.send_packet(advertise_packet, self.root_address)
        self.stream.send_out_buf_messages(only_register=True)

    def __check_neighbour(self, address):
        """
		It checks if the address is 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.client_predecessor_address or address in self.successors_address

    def helloback_is_mine(self, packet):
        first_address = packet.get_first_address_hello_packet()
        return first_address == self.address

    def change_header(self, packet):
        packet.source_ip, packet.source_port = self.address[0], self.address[1]
        packet = Packet(packet.get_buf())
        return packet

    def forward_hello(self, packet, is_mine=False):
        if is_mine:
            self.stream.add_message_to_out_buff(
                self.client_predecessor_address, packet)
        else:
            print('Forwarding a Hello that is not mine')
            packet = self.change_header(packet)
            packet.body += str(self.address[0]) + str(self.address[1])
            new_number_of_elements = int(packet.body[3:5]) + 1
            packet.body = 'REQ' + str(new_number_of_elements).zfill(
                2) + packet.body[5:]
            packet = Packet(packet.get_buf())
            self.stream.add_message_to_out_buff(
                self.client_predecessor_address, packet)

    def forward_helloback(self, packet):
        packet = self.change_header(packet)
        packet.body = packet.body[:-20]
        number_of_entries = str(int(packet.body[3:5]) - 1).zfill(2)
        packet.body = 'RES' + number_of_entries + packet.body[5:]
        packet = Packet(packet.get_buf())
        fw_address = packet.body[-20:]
        fw_address = (fw_address[:15], fw_address[15:])
        if fw_address in self.successors_address:
            self.stream.add_message_to_out_buff(fw_address, packet)
        else:
            return
Пример #3
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
Пример #4
0
class Peer(threading.Thread):
    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
        """
        super().__init__()
        self.server_ip = Node.parse_ip(server_ip)
        self.server_port = Node.parse_port(str(server_port))
        self.server_address = (self.server_ip, self.server_port)
        self.stream = Stream(self.server_ip, self.server_port)
        self.UI = UserInterface()

        self.is_root = is_root
        self.left_child_address, self.right_child_address, self.parent_address = None, None, None

        if self.is_root:
            self.registered_peers_address = {}
            self.root_ip, self.root_port = self.server_ip, self.server_port
            self.root_node = GraphNode((self.root_ip, self.root_port))
            self.graph = NetworkGraph(self.root_node)

        else:
            self.root_ip = Node.parse_ip(root_address[0])
            self.root_port = Node.parse_ip(str(root_address[1]))

        self.root_address = (self.root_ip, self.root_port)

        self.running = True
        self.registered = False
        self.advertised = False
        self.joined = False
        self.reunion_on_fly = False
        self.reunion_timer = 0
        self.counter = 0
        self.timer_interval = 0.2
        self.UI.start()
        self.start()

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

        :return:
        """
        self.print_function("F**K KHAJE POOR")

    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:
        """
        for command in self.UI.buffer:
            self.handle_user_command(command)
        self.UI.clear_buffer()

    def handle_user_command(self, command):
        """

        :param command:
        :type command: str
        :return:
        """
        command = command.lower()
        if command.startswith("register"):
            if self.registered:
                self.print_function(
                    "You have been registered before and F**K KHAJE POOR")
            else:
                register_req_packet = PacketFactory.new_register_packet(
                    type='REQ',
                    source_server_address=self.server_address,
                    address=self.root_address)
                # Adding root node to peer's stream
                self.stream.add_node(server_address=self.root_address,
                                     set_register_connection=True)
                self.stream.add_message_to_out_buff(
                    address=self.root_address, message=register_req_packet.buf)
                self.print_function("{} sent register request".format(
                    self.server_address))
        elif command.startswith("advertise"):
            if not self.registered:
                self.print_function(
                    "{} must register first to advertise".format(
                        str(self.server_address)))
            elif self.advertised:
                self.print_function("You have advertised before")
            else:
                advertise_req_packet = PacketFactory.new_advertise_packet(
                    type='REQ', source_server_address=self.server_address)
                self.stream.add_message_to_out_buff(
                    address=self.root_address,
                    message=advertise_req_packet.buf)
                self.print_function("{} sent advertise packet".format(
                    str(self.server_address)))

        elif command.startswith("message"):
            if self.joined:
                message = command[8:]
                broadcast_message_packet = PacketFactory.new_message_packet(
                    message=message, source_server_address=self.server_address)
                self.print_function("{} is broadcasting message: {}".format(
                    str(self.server_address), message))
                self.send_broadcast_packet(broadcast_message_packet,
                                           "F**K KHAJE POOR")
            else:
                self.print_function(
                    "You must join the network before broadcasting a message")
        elif command.startswith('disconnect'):
            self.running = False
        else:
            self.print_function("Unknown command received: {}".format(command))

    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:
                pass
            else:
                pass

            if self.counter > 0 or True:
                received_bufs = self.stream.read_in_buf()
                received_packets = [
                    PacketFactory.parse_buffer(buf) for buf in received_bufs
                ]
                for packet in received_packets:
                    self.handle_packet(packet)

                self.stream.clear_in_buff()
                self.stream.send_out_buf_messages()
                self.handle_user_interface_buffer()

            if self.reunion_timer == 0 and self.joined and not self.reunion_on_fly:
                self.reunion_on_fly = True
                reunion_req_packet = PacketFactory.new_reunion_packet(
                    type='REQ',
                    source_address=self.server_address,
                    nodes_array=[self.server_address])
                self.stream.add_message_to_out_buff(
                    address=self.parent_address,
                    message=reunion_req_packet.buf)
                self.print_function("Sent Reunion Packet to Parent: " +
                                    str(self.parent_address))

            if not self.reunion_on_fly and self.reunion_timer > 0:
                self.reunion_timer -= self.timer_interval * 10

            self.counter += self.timer_interval * 10
            if self.counter == 4 * 10:
                self.counter = 0
            if self.is_root:
                for gnode in self.graph.nodes:
                    gnode.reunion_timer += self.timer_interval * 10
            time.sleep(self.timer_interval)

    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:
        """
        self.print_function("F**K KHAJE POOR")

    def send_broadcast_packet(self, broadcast_packet, exclude_address):
        """

        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:
        """
        if self.parent_address is not None and self.parent_address != exclude_address:
            self.stream.add_message_to_out_buff(self.parent_address,
                                                broadcast_packet.buf)
        if self.left_child_address is not None and self.left_child_address != exclude_address:
            self.stream.add_message_to_out_buff(self.left_child_address,
                                                broadcast_packet.buf)
        if self.right_child_address is not None and self.right_child_address != exclude_address:
            self.stream.add_message_to_out_buff(self.right_child_address,
                                                broadcast_packet.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 self.is_root:
        #     self.print_function("root received type {} message".format(packet_type))
        if packet_type == 1:
            self.__handle_register_packet(packet)
        elif packet_type == 2:
            self.__handle_advertise_packet(packet)
        elif packet_type == 3:
            self.__handle_join_packet(packet)
        elif packet_type == 4:
            self.__handle_message_packet(packet)
        elif packet_type == 5:
            self.__handle_reunion_packet(packet)
        else:
            self.print_function("Unknown type of packet received {}".format(
                packet.get_body()))

    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 source_address in self.registered_peers_address:
            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 register packet

        :type packet Packet

        :return:
        """
        if self.is_root:
            self.__handle_advertise_packet_root(packet)
        else:
            self.__handle_advertise_packet_client(packet)

    def __handle_advertise_packet_root(self, packet):
        if packet.get_body()[:3] == "REQ":
            packet_source_address = packet.get_source_server_address()
            if not self.__check_registered(packet_source_address):
                self.print_function(
                    "Advertise request received from unregistered client {}".
                    format(str(packet_source_address)))
            else:
                if self.graph.find_node(packet_source_address[0],
                                        packet_source_address[1]) is not None:
                    self.print_function(
                        "Advertise request received from a client in graph {}".
                        format(str(packet_source_address)))
                else:
                    father_node = self.graph.find_live_node("F**K KHAJE POOR")
                    print("Father found", father_node.address)
                    self.graph.add_node(ip=packet_source_address[0],
                                        port=packet_source_address[1],
                                        father_address=father_node.address)
                    self.graph.turn_on_node(packet_source_address)
                    register_response_packet = PacketFactory.new_advertise_packet(
                        type='RES',
                        source_server_address=self.server_address,
                        neighbour=father_node.address)
                    self.stream.add_message_to_out_buff(
                        address=packet_source_address,
                        message=register_response_packet.buf)
                    self.registered_peers_address[packet_source_address] = 1
                    self.print_function(
                        "Advertise request received from {} and register response sent"
                        .format(packet_source_address))
        else:
            self.print_function(
                "Root received advertise response. Wtf? F**K KHAJE POOR")

    def __handle_advertise_packet_client(self, packet):
        if packet.get_body()[:3] == 'RES':
            parent_address = (packet.get_body()[3:18],
                              packet.get_body()[18:23])

            self.advertised = True
            if parent_address != self.root_address:
                self.stream.add_node(server_address=parent_address,
                                     set_register_connection=False)
            join_packet = PacketFactory.new_join_packet(
                source_server_address=self.server_address)
            self.stream.add_message_to_out_buff(address=parent_address,
                                                message=join_packet.buf)
            self.parent_address = parent_address
            self.joined = True
            self.print_function(
                "Advertise response received from root. Client {} sent join request to {}"
                .format(str(self.server_address), str(parent_address)))
        else:
            self.print_function(
                "Client received advertise request. Wtf? F**K KHAJE POOR")

    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 self.is_root:
            self.__handle_register_packet_root(packet)
        else:
            self.__handle_register_packet_client(packet)

    def __handle_register_packet_root(self, packet):
        if packet.get_body()[:3] == "REQ":
            packet_source_address = packet.get_source_server_address()
            if self.__check_registered(packet_source_address):
                self.print_function(
                    "Duplicate register request received from {}".format(
                        str(packet_source_address)))
            else:
                self.stream.add_node(server_address=packet_source_address,
                                     set_register_connection=True)
                register_response_packet = PacketFactory.new_register_packet(
                    type='RES', source_server_address=self.server_address)
                self.stream.add_message_to_out_buff(
                    address=packet.get_source_server_address(),
                    message=register_response_packet.buf)
                self.registered_peers_address[packet_source_address] = 1
                self.print_function(
                    "Register request received from {} and register response sent"
                    .format(packet_source_address))
        else:
            self.print_function(
                "Root received register response. Wtf? F**K KHAJE POOR")

    def __handle_register_packet_client(self, packet):
        if packet.get_body()[:3] == "RES":
            self.registered = True
            self.print_function("Client {} received register response".format(
                self.server_address))
        else:
            self.print_function(
                "Client received register request. Wtf? F**K KHAJE POOR")

    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 address == self.parent_address or address == self.left_child_address or address == self.right_child_address

    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:
        """
        source_address = packet.get_source_server_address()
        if self.__check_neighbour(source_address):
            self.print_function("{} received broadcast message: {}".format(
                self.server_address, packet.get_body()))
            broadcast_message_packet = PacketFactory.new_message_packet(
                message=packet.get_body(),
                source_server_address=self.server_address)
            self.send_broadcast_packet(broadcast_message_packet,
                                       exclude_address=source_address)
        else:
            self.print_function(
                "Broadcast packet received from KHAJE POOR. wut??")

    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:
        """
        source_address = packet.get_source_server_address()
        if not self.__check_neighbour(source_address):
            self.print_function(
                "Reunion packet received from KHAJE POOR. wut??")

    def __handle_reunion_packet_root(self, packet):
        body = packet.get_body()
        if body[:3] == "REQ":
            packet_source_address = packet.get_source_server_address()
            if self.graph.find_node(packet_source_address[0],
                                    packet_source_address[1]) is None:
                self.print_function(
                    "Root received reunion from {} which is not in graph".
                    format(packet_source_address))
            nodes_address_list = []
            n_nodes = int(body[3:5])
            for i in range(n_nodes):
                ip = body[5 + 20 * i:5 + 15 + 20 * i]
                port = body[20 + 20 * i:20 + 5 + 20 * i]
                nodes_address_list.append((ip, port))
            reunion_sender_node = self.graph.find_node(
                nodes_address_list[0][0], nodes_address_list[0][1])
            self.print_function(
                "Root received Reunion from {} and reunion response sent".
                format(str(reunion_sender_node)))
            nodes_address_list.reverse()
            reunion_sender_node.reunion_timer = 0
            reunion_response_packet = PacketFactory.new_reunion_packet(
                type="RES",
                source_address=self.root_address,
                nodes_array=nodes_address_list)
            self.stream.add_message_to_out_buff(
                packet_source_address, message=reunion_response_packet.buf)

        else:
            self.print_function(
                "Root received reunion response. Wtf? F**K KHAJE POOR")

    def __handle_reunion_packet_client(self, packet):
        body = packet.get_body()
        if not self.joined:
            self.print_function(
                "{} has no parent to redirect reunion request to".format(
                    self.server_address))
            return
        if body[:3] == "REQ":
            nodes_address_list = []
            n_nodes = int(body[3:5])
            for i in range(n_nodes):
                ip = body[5 + 20 * i:5 + 15 + 20 * i]
                port = body[20 + 20 * i:20 + 5 + 20 * i]
                nodes_address_list.append((ip, port))
            reunion_sender_node = self.graph.find_node(
                nodes_address_list[0][0], nodes_address_list[0][1])
            nodes_address_list.append(self.server_address)
            reunion_response_packet = PacketFactory.new_reunion_packet(
                type="REQ",
                source_address=self.server_address,
                nodes_array=nodes_address_list)
            self.stream.add_message_to_out_buff(
                self.parent_address, message=reunion_response_packet.buf)
            self.print_function(
                "{} received Reunion from {} and reunion request sent to parent"
                .format(str(self.server_address), str(reunion_sender_node)))
        else:
            n_nodes = int(body[3:5])
            if n_nodes == 1:
                target_ip = body[5:20]
                target_port = body[20:25]
                if (target_ip, target_port) == self.server_address:
                    self.reunion_on_fly = False
                    self.reunion_timer = 4 * 10
            else:
                nodes_address_list = []
                n_nodes = int(body[3:5])
                for i in range(1, n_nodes):
                    nodes_address_list.append((body[5 + 20 * i:20 + 20 * i],
                                               body[20 + 20 * i:25 + 20 * i]))

                reunion_response_packet = PacketFactory.new_reunion_packet(
                    type='RES',
                    source_address=self.server_address,
                    nodes_array=nodes_address_list)
                target_ip = body[25:40]
                target_port = body[40:45]
                target_address = (target_ip, target_port)
                self.stream.add_message_to_out_buff(
                    target_address, reunion_response_packet.buf)
                self.print_function(
                    "{} redirected reunion response to {}".format(
                        self.server_address, target_address))

    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:
        """
        join_request_source_address = packet.get_source_server_address()
        if self.right_child_address == join_request_source_address or self.left_child_address == join_request_source_address:
            self.print_function(
                "{} received join request from {} but they have been joined before"
                .format(self.server_address, join_request_source_address))

        if self.right_child_address is None:
            self.right_child_address = join_request_source_address
        elif self.left_child_address is None:
            self.left_child_address = join_request_source_address
        else:
            self.print_function(
                "Client {} received join from {} but has no free room for a new child"
                .format(self.server_address, join_request_source_address))
            return
        self.print_function("{} received join request from {}".format(
            self.server_address, join_request_source_address))

        if not self.is_root:
            self.stream.add_node(join_request_source_address,
                                 set_register_connection=False)

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

    def print_function(self, message):
        print(message)
        self.UI.peer_log.log += "{}:\n\t{}\n".format(datetime.datetime.now(),
                                                     message)