def disconnect_from_the_splitter(self): # {{{ # Close the TCP socket Peer_DBS.disconnect_from_the_splitter(self) # Use UDP to create a working NAT entry self.say_hello(self.splitter) self.say_hello(self.splitter) self.say_hello(self.splitter)
def disconnect_from_the_splitter(self): # {{{ self.start_send_hello_thread() # Receive the generated ID for this peer from splitter self.receive_id() # There are currently no other peers in the team self.initial_peer_list = [] # Close the TCP socket Peer_DBS.disconnect_from_the_splitter(self)
def try_to_disconnect_from_the_splitter(self): # {{{ self.start_send_hello_thread() # Receive the generated ID for this peer from splitter self.receive_id() # Note: This peer is *not* the monitor peer. # Send UDP packets to splitter and monitor peers # to create working NAT entries and to determine the # source port allocation type of the NAT of this peer for peer in self.peer_list[:self.number_of_monitors]: self.say_hello(peer) self.say_hello(self.splitter) # Directly start packet sending self.hello_messages_event.set() # A list of peer_ids that contains the peers that were in the team when # starting incorporation and that are not connected yet self.initial_peer_list = [] # Receive the list of peers, except the monitor peer, with their peer # IDs and send hello messages self.receive_the_list_of_peers_2() # Wait for getting connected to all currently known peers incorporation_time = time.time() # A timeout < MAX_PEER_ARRIVING_TIME has to be set for self.team_socket # The monitor is not in initial_peer_list while len(self.initial_peer_list) > 0: if time.time( ) - incorporation_time > Common.MAX_PEER_ARRIVING_TIME: # Retry incorporation into the team _p_("Retrying incorporation with %d peers left: %s" \ % (len(self.initial_peer_list), self.initial_peer_list)) incorporation_time = time.time() # Cleaning hello messages with self.hello_messages_lock: self.hello_messages_times.clear() self.hello_messages_ports.clear() del self.hello_messages[:] # Resetting peer lists del self.initial_peer_list[:] del self.peer_list[self.number_of_monitors:] # Leave monitors # Recreate the socket # Similar to Peer_DBS.listen_to_the_team, binds to a random port self.team_socket.close() self.create_team_socket() try: self.team_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) except Exception as e: _print_(Common.NTS_COLOR + "NTS:" + Color.none, e) self.team_socket.bind(('', 0)) self.team_socket.settimeout(1) # Say hello to splitter again, to retry incorporation # 'N' for 'not incorporated' self.send_message( (self.peer_id.encode() + b'N', self.splitter)) # Say hello to monitors again, to keep the NAT entry alive for peer in self.peer_list[:self.number_of_monitors]: self.send_message((self.peer_id.encode() + b'N', peer)) # Receive all peer endpoints and send hello messages self.receive_the_list_of_peers_2() # Process messages to establish connections to peers try: message, sender = self.team_socket.recvfrom( \ struct.calcsize(self.message_format)) self.process_message(message, sender) except socket.timeout: pass # Close the TCP socket Peer_DBS.disconnect_from_the_splitter(self) # The peer is now successfully incorporated; inform the splitter self.send_message((self.peer_id.encode() + b'Y', self.splitter)) _p_("Incorporation successful")
def try_to_disconnect_from_the_splitter(self): # {{{ self.start_send_hello_thread() # Receive the generated ID for this peer from splitter self.receive_id() # Note: This peer is *not* the monitor peer. # Send UDP packets to splitter and monitor peers # to create working NAT entries and to determine the # source port allocation type of the NAT of this peer for peer in self.peer_list[:self.number_of_monitors]: self.say_hello(peer) self.say_hello(self.splitter) # Directly start packet sending self.hello_messages_event.set() # A list of peer_ids that contains the peers that were in the team when # starting incorporation and that are not connected yet self.initial_peer_list = [] # Receive the list of peers, except the monitor peer, with their peer # IDs and send hello messages self.receive_the_list_of_peers_2() # Wait for getting connected to all currently known peers incorporation_time = time.time() # A timeout < MAX_PEER_ARRIVING_TIME has to be set for self.team_socket # The monitor is not in initial_peer_list while len(self.initial_peer_list) > 0: if time.time() - incorporation_time > Common.MAX_PEER_ARRIVING_TIME: # Retry incorporation into the team _p_("Retrying incorporation with %d peers left: %s" \ % (len(self.initial_peer_list), self.initial_peer_list)) incorporation_time = time.time() # Cleaning hello messages with self.hello_messages_lock: self.hello_messages_times.clear() self.hello_messages_ports.clear() del self.hello_messages[:] # Resetting peer lists del self.initial_peer_list[:] del self.peer_list[self.number_of_monitors:] # Leave monitors # Recreate the socket # Similar to Peer_DBS.listen_to_the_team, binds to a random port self.team_socket.close() self.create_team_socket() try: self.team_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) except Exception as e: _print_(Common.NTS_COLOR + "NTS:" + Color.none, e) self.team_socket.bind(('', 0)) self.team_socket.settimeout(1) # Say hello to splitter again, to retry incorporation # 'N' for 'not incorporated' self.send_message((self.peer_id.encode() + b'N', self.splitter)) # Say hello to monitors again, to keep the NAT entry alive for peer in self.peer_list[:self.number_of_monitors]: self.send_message((self.peer_id.encode() + b'N', peer)) # Receive all peer endpoints and send hello messages self.receive_the_list_of_peers_2() # Process messages to establish connections to peers try: message, sender = self.team_socket.recvfrom( \ struct.calcsize(self.message_format)) self.process_message(message, sender) except socket.timeout: pass # Close the TCP socket Peer_DBS.disconnect_from_the_splitter(self) # The peer is now successfully incorporated; inform the splitter self.send_message((self.peer_id.encode() + b'Y', self.splitter)) _p_("Incorporation successful")