* </p>
*
* @param search The {@link Peer} specification.
*/
private SharingPeer getOrCreatePeer(Peer search) {
SharingPeer peer;
synchronized (this.peers) {
logger.trace("Searching for {}...", search);
if (search.hasPeerId()) {
peer = this.peers.get(search.getHexPeerId());
if (peer != null) {
logger.trace("Found peer (by peer ID): {}.", peer);
this.peers.put(peer.getHostIdentifier(), peer);
this.peers.put(search.getHostIdentifier(), peer);
return peer;
}
}
peer = this.peers.get(search.getHostIdentifier());
if (peer != null) {
if (search.hasPeerId()) {
logger.trace("Recording peer ID {} for {}.",
search.getHexPeerId(), peer);
peer.setPeerId(search.getPeerId());
this.peers.put(search.getHexPeerId(), peer);
}
logger.debug("Found peer (by host ID): {}.", peer);
return peer;
}
peer = new SharingPeer(search.getIp(), search.getPort(),
search.getPeerId(), this.torrent);
logger.trace("Created new peer: {}.", peer);
this.peers.put(peer.getHostIdentifier(), peer);
if (peer.hasPeerId()) {
this.peers.put(peer.getHexPeerId(), peer);
}
return peer;
}
}