@ Override public void handleDiscoveredPeers ( List < Peer > peers , String hexInfoHash ) { if ( peers . size ( ) == 0 ) return ; SharedTorrent torrent = torrentsStorage . getTorrent ( hexInfoHash ) ; if ( torrent != null && torrent . isFinished ( ) ) return ; final LoadedTorrent announceableTorrent = torrentsStorage . getLoadedTorrent ( hexInfoHash ) ; if ( announceableTorrent == null ) { logger . info ( "announceable torrent {} is not found in storage. Maybe it was removed" , hexInfoHash ) ; return ; } if ( announceableTorrent . getPieceStorage ( ) . isFinished ( ) ) return ; logger . debug ( "Got {} peer(s) ({}) for {} in tracker response" , new Object [ ] { peers . size ( ) , Arrays . toString ( peers . toArray ( ) ) , hexInfoHash } ) ; Map < PeerUID , Peer > uniquePeers = new HashMap < PeerUID , Peer > ( ) ; for ( Peer peer : peers ) { final PeerUID peerUID = new PeerUID ( peer . getAddress ( ) , hexInfoHash ) ; if ( uniquePeers . containsKey ( peerUID ) ) continue ; uniquePeers . put ( peerUID , peer ) ; } for ( Map . Entry < PeerUID , Peer > e : uniquePeers . entrySet ( ) ) { PeerUID peerUID = e . getKey ( ) ; Peer peer = e . getValue ( ) ; boolean alreadyConnectedToThisPeer = peersStorage . getSharingPeer ( peerUID ) != null ; if ( alreadyConnectedToThisPeer ) { logger . debug ( "skipping peer {}, because we already connected to this peer" , peer ) ; continue ; } ConnectionListener connectionListener = new OutgoingConnectionListener ( this , announceableTorrent . getTorrentHash ( ) , peer . getIp ( ) , peer . getPort ( ) ) ; logger . debug ( "trying to connect to the peer {}" , peer ) ; boolean connectTaskAdded = this . myConnectionManager . offerConnect ( new ConnectTask ( peer . getIp ( ) , peer . getPort ( ) , connectionListener , new SystemTimeService ( ) . now ( ) , Constants . DEFAULT_CONNECTION_TIMEOUT_MILLIS ) , 1 , TimeUnit . SECONDS ) ; if ( ! connectTaskAdded ) { logger . info ( "can not connect to peer {}. Unable to add connect task to connection manager" , peer ) ; } } }
Handle the discovery of new peers .