/// The method adds the given peer to the list of current peers.
The method adds the given peer to the list of current peers.
The method adds the given peer to the list of current peers .
fn add_peer(
        &self,
        node_id: NodeId,
        node_record: &NodeRecord,
        registry_version: RegistryVersion,
        event_handler: &Arc<dyn P2PEventHandlerControl>,
    ) -> P2PResult<()> {
        // Only add other peers to the peer list.
        if node_id == self.node_id {
            return Err(P2PError {
                p2p_error_code: P2PErrorCode::Failed,
            });
        }
        // Add the peer to the list of current peers and the event handler, and drop the
        // lock before calling into transport.
        {
            let mut current_peers = self.current_peers.lock().unwrap();
            if current_peers.contains_key(&node_id) {
                Err(P2PError {
                    p2p_error_code: P2PErrorCode::Exists,
                })
            } else {
                current_peers
                    .entry(node_id)
                    .or_insert_with(|| PeerContext::from(node_id.to_owned()));
                event_handler.add_node(node_id);
                info!(self.log, "Nodes {:0} added", node_id);
                Ok(())
            }?;
        }
        // If starting a transport connection failed, remove the
        // node from current peer list. This removal makes it possible to attempt a
        // re-connection on the next registry refresh.
        //
        // TODO: P2P-511 transport.start_connection() should be non-fallible.
        // Instead, connection failures should be retried internally in
        // transport.
        self.transport
            .start_connections(
                self.transport_client_type,
                &node_id,
                node_record,
                registry_version,
            )
            .map_err(|e| {
                let mut current_peers = self.current_peers.lock().unwrap();
                current_peers.remove(&node_id);
                warn!(self.log, "start connections failed {:?} {:?}", node_id, e);
                P2PError {
                    p2p_error_code: P2PErrorCode::InitFailed,
                }
            })
    }
fn add_peer ( & self , node_id : NodeId , node_record : & NodeRecord , registry_version : RegistryVersion , event_handler : & Arc < dyn P2PEventHandlerControl > , ) -> P2PResult < ( ) > { // Only add other peers to the peer list. if node_id == self . node_id { return Err ( P2PError { p2p_error_code : P2PErrorCode :: Failed , } ) ; } // Add the peer to the list of current peers and the event handler, and drop the // lock before calling into transport. { let mut current_peers = self . current_peers . lock ( ) . unwrap ( ) ; if current_peers . contains_key ( & node_id ) { Err ( P2PError { p2p_error_code : P2PErrorCode :: Exists , } ) } else { current_peers . entry ( node_id ) . or_insert_with ( | | PeerContext :: from ( node_id . to_owned ( ) ) ) ; event_handler . add_node ( node_id ) ; info ! ( self log " " node_id ) ; Ok ( ( ) ) } ? ; } // If starting a transport connection failed, remove the // node from current peer list. This removal makes it possible to attempt a // re-connection on the next registry refresh. // // TODO: P2P-511 transport.start_connection() should be non-fallible. // Instead, connection failures should be retried internally in // transport. self . transport . start_connections ( self . transport_client_type , & node_id , node_record , registry_version , ) . map_err ( | e | { let mut current_peers = self . current_peers . lock ( ) . unwrap ( ) ; current_peers . remove ( & node_id ) ; warn ! ( self log " " node_id e ) ; P2PError { p2p_error_code : P2PErrorCode :: InitFailed , } } ) }
The method adds the given peer to the list of current peers.
The method adds the given peer to the list of current peers .
&self
node_id
NodeId
node_record
&NodeRecord
registry_version
RegistryVersion
event_handler
&Arc<dyn P2PEventHandlerControl>