public Connection connect(OutRequest outRequest) { SubPeer peer = choosePeer(outRequest); if (peer == null) { return null; } Connection conn = peer.getPreferredConnection(); if (conn == null) { conn = peer.connectTo(); } return conn; }
public SubPeer choosePeer(OutRequest outRequest) { SubPeer res = null; if (peers.size() == 0) { return null; } int start = new Random().nextInt(peers.size()); int i = start; boolean stop = false; do { i = (i + 1) % peers.size(); SubPeer peer = peers.get(i); stop = peer.updateScore(outRequest); if (stop || res == null) { res = peer; } else if (peer.getScore() > res.getScore()) { res = peer; } } while (!stop && i != start); outRequest.setUsedPeer(res.getRemoteAddress()); return res; }