java.lang.Object
org.klomp.snark.PeerAcceptor

class PeerAcceptorextends Object
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.
  • Field Details

    • _log

      private final Log _log
      Logger for this class.
    • coordinator

      private final PeerCoordinator coordinator
      Told about the peers that connect in.
    • coordinators

      final PeerCoordinatorSet coordinators
      The coordinators this acceptor feeds.
    • HASH_READ_TIMEOUT

      private static final long HASH_READ_TIMEOUT
      Shorten timeout while reading handshake.
      See Also:
    • PROTO_STR

      private static final String PROTO_STR
      Protocol string expected in a handshake.
      See Also:
    • PROTO_STR_LEN

      private static final int PROTO_STR_LEN
      Length of that protocol string.
    • PROTO_LEN

      private static final int PROTO_LEN
      Length including the length byte.
    • PROTO

      private static final int[] PROTO
      The protocol string as bytes, for matching.
    • LOOKAHEAD_SIZE

      private static final int LOOKAHEAD_SIZE
      Handshake read timeout, in seconds.
  • Constructor Details

    • PeerAcceptor

      public PeerAcceptor(PeerCoordinator coordinator)
      Creates a single-torrent PeerAcceptor.
      Parameters:
      coordinator - the coordinator for the one torrent this acceptor serves
    • PeerAcceptor

      public PeerAcceptor(PeerCoordinatorSet coordinators)
      Creates a multi-torrent PeerAcceptor.
      Parameters:
      coordinators - the set of coordinators for all active torrents
  • Method Details

    • connection

      public void connection(I2PSocket socket, InputStream in, OutputStream out) throws IOException
      Handle an incoming connection from a peer.
      Parameters:
      socket - the incoming socket
      in - the input stream
      out - the output stream
      Throws:
      IOException - if the connection fails or the infohash is unknown
    • readHash

      private static byte[] readHash(InputStream in) throws IOException
      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 in Peer.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