コード例 #1
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
コード例 #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
コード例 #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.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)