Example #1
0
    def __ping_peers(self):
        """
        Ping all peers
        Sends 'ping' sys message to all peers, checking for 'pong' response
        """
        # create ping message instance
        message = Message('', 'SYSTEM', msg_body='ping')
        # save time of ping
        ping_time = math.floor(time.time())
        # ping each connected client
        for connection in self.connections:
            # send ping message
            connection.send(message.get_encoded())

        # wait ping timeout seconds
        time.sleep(1)

        # check which peers pong'ed back
        dead_peers = []
        for peer_id, peer_data in self.peer.get_chat_peers().items():
            # get the last successful ping time of loop peer
            last_successful_ping = peer_data['last_successful_ping']
            # if the last successful ping response was earlier than this ping started
            if last_successful_ping < ping_time:
                # they didn't pong back, so they're inactive
                dead_peers.append(peer_id)

        # for each inactive peer
        for peer_id in dead_peers:
            # remove them from the chat peers list
            self.peer.remove_chat_peer(peer_id)

            # broadcast disconnect message (including remaining chat members)
            msg_body = 'output:' + (
                Style.error('- ' + peer_data['username'] + ' disconnected') +
                Style.info(' [Chat Members: ' + self.peer.get_chat_peers_str() + ']')
            )
            disconnect_msg = Message('', 'SYSTEM', msg_body)
            for connection in self.connections:
                # send disconnect message to each connected client
                connection.send(disconnect_msg.get_encoded())

        # send the remaining peers list to each client
        self.__send_peers()
Example #2
0
    if existing_ip and existing_port:
        # existing member address given, so attempt to connect to their server
        print('Connecting to existing members server...')

        try:
            client = Client(peer, existing_ip, existing_port)
            peer.connected = True
        except ConnectionRefusedError:
            # failed to connect to existing member, so try become coordinator of own Server
            print(Style.warning('Failed to connect to existing member! \n'))
    else:
        try:
            client = Client(peer, listen_ip, listen_port)
            peer.connected = True
        except ConnectionRefusedError:
            print(Style.error('Failed to connect to own server!'))

    try:
        while True:
            if not peer.connected:
                peer.connected = True
                print(Style.error('Coordinator disconnected, finding new coordinator...'))

                chat_peers = peer.get_chat_peers()

                earliest_join_id = None
                for peer_id, peer_data in chat_peers.items():
                    # loop peer isn't the old chat coordinator
                    if not peer_data['is_coord']:
                        if not earliest_join_id:
                            earliest_join_id = peer_id