java.lang.Object
org.klomp.snark.PeerAcceptor
Accepts incoming connections from peers. The ConnectionAcceptor will call the
connection(I2PSocket, InputStream, OutputStream) method when it detects an incoming BT protocol connection. The PeerAcceptor will then create a new peer if the PeerCoordinator wants more peers.Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classA unique exception so we can tell the ConnectionAcceptor about non-BT connections.Field Summary
FieldsModifier and TypeFieldDescriptionprivate final LogLogger for this class.private final PeerCoordinatorTold about the peers that connect in.(package private) final PeerCoordinatorSetThe coordinators this acceptor feeds.private static final longShorten timeout while reading handshake.private static final intHandshake read timeout, in seconds.private static final int[]The protocol string as bytes, for matching.private static final intLength including the length byte.private static final StringProtocol string expected in a handshake.private static final intLength of that protocol string.Constructor Summary
ConstructorsConstructorDescriptionPeerAcceptor(PeerCoordinator coordinator) Creates a single-torrent PeerAcceptor.PeerAcceptor(PeerCoordinatorSet coordinators) Creates a multi-torrent PeerAcceptor.Method Summary
Modifier and TypeMethodDescriptionvoidconnection(I2PSocket socket, InputStream in, OutputStream out) Handle an incoming connection from a peer.private static byte[]readHash(InputStream in) Read ahead to the infohash, throwing an exception if there isn't enough data.
Field Details
_log
Logger for this class.coordinator
Told about the peers that connect in.coordinators
The coordinators this acceptor feeds.HASH_READ_TIMEOUT
private static final long HASH_READ_TIMEOUTShorten timeout while reading handshake.- See Also:
PROTO_STR
Protocol string expected in a handshake.- See Also:
PROTO_STR_LEN
private static final int PROTO_STR_LENLength of that protocol string.PROTO_LEN
private static final int PROTO_LENLength including the length byte.PROTO
private static final int[] PROTOThe protocol string as bytes, for matching.LOOKAHEAD_SIZE
private static final int LOOKAHEAD_SIZEHandshake read timeout, in seconds.
Constructor Details
PeerAcceptor
Creates a single-torrent PeerAcceptor.- Parameters:
coordinator- the coordinator for the one torrent this acceptor serves
PeerAcceptor
Creates a multi-torrent PeerAcceptor.- Parameters:
coordinators- the set of coordinators for all active torrents
Method Details
connection
Handle an incoming connection from a peer.- Parameters:
socket- the incoming socketin- the input streamout- the output stream- Throws:
IOException- if the connection fails or the infohash is unknown
readHash
Read ahead to the infohash, throwing an exception if there isn't enough data. Also check the first 20 bytes for the correct protocol here and throw IOE if bad, so we don't hang waiting for 48 bytes if it's not a bittorrent client. The 20 bytes are checked again inPeer.handshake().- Parameters:
in- the stream to read the handshake prefix from- Returns:
- the 20-byte infohash following the protocol header
- Throws:
IOException- if the leading bytes are not the bittorrent protocol string, or the hash cannot be fully read