java.lang.Object
org.klomp.snark.ExtensionHandler
REF: BEP 10 Extension Protocol.
Field Summary
Modifier and TypeFieldDescriptionprivate static final byte[]Filter saying comment requests are not wanted.private static final intLength of the piece hashes, in bytes.static final intComment extension message ID (4).static final intDHT extension message ID (3).static final intHandshake extension message ID (0).static final intBEP 54 lt_donthave extension message ID (5).static final intMetadata extension message ID (1).static final intPEX extension message ID (2).private static final intPieces * SHA1 Hash length, + 25% extra for file names, bencoding overhead, etc.private static final intExtension requests in flight per peer.static final StringComment extension type string, carried in the handshake's type map.private static final intExtension message carrying data.static final StringNot using the option bit since the compact format is different.static final StringBEP 54 lt_donthave extension type string.static final StringMetadata extension type string.static final StringNot ut_pex since the compact format is different.private static final intExtension message refusing a request.private static final intExtension message asking for data.Constructor Summary
ConstructorsConstructorDescriptionConstructor for subclasses, each of which handles one BEP 10 extension protocol.Method Summary
Modifier and TypeMethodDescriptionstatic byte[]getHandshake(int metasize, boolean pexAndMetadata, boolean dht, boolean uploadOnly, boolean comment, String clientName) Creates a bencoded extension handshake message.private static voidhandleComment(Peer peer, PeerListener listener, byte[] bs, Log log) Handles an incoming comment request or response message.private static voidhandleDHT(Peer peer, PeerListener listener, byte[] bs, Log log) Handles an incoming DHT port message.private static voidhandleHandshake(Peer peer, PeerListener listener, byte[] bs, Log log) Handles an incoming extension handshake message.static voidhandleMessage(Peer peer, PeerListener listener, int id, byte[] bs) Handles an incoming extension message from a peer.private static voidhandleMetadata(Peer peer, PeerListener listener, byte[] bs, Log log) Handles an incoming metadata extension message (BEP 9).private static voidhandlePEX(Peer peer, PeerListener listener, byte[] bs, Log log) Handles an incoming PEX message.static voidlocked_sendComments(Peer peer, int num, CommentSet comments) Sends comments to a peer.static voidsendCommentReq(Peer peer, int num) Sends a comment request to a peer.static voidSends DHT port numbers to a peer.static voidsendDontHave(Peer peer, int piece) Send a BEP 54 lt_donthave message to a peer that advertised support: the piece is no longer available.private static voidsendExtension(Peer peer, String type, byte[] payload) Send an extension message to a peer, looking up its message code for the given type in the handshake.private static voidsendMessage(Peer peer, int type, int piece) Sends a metadata request or reject message to a peer.static voidSends a PEX message with the given peer list.private static voidSends a metadata chunk data message to a peer.private static voidsendRequest(Peer peer, int piece) Sends a metadata chunk request to a peer.
Field Details
ID_HANDSHAKE
public static final int ID_HANDSHAKEHandshake extension message ID (0).- See Also:
ID_METADATA
TYPE_METADATA
Metadata extension type string.- See Also:
ID_PEX
TYPE_PEX
Not ut_pex since the compact format is different.- See Also:
ID_DHT
TYPE_DHT
Not using the option bit since the compact format is different.- See Also:
ID_COMMENT
TYPE_COMMENT
Comment extension type string, carried in the handshake's type map.- See Also:
ID_LT_DONTHAVE
public static final int ID_LT_DONTHAVEBEP 54 lt_donthave extension message ID (5).- See Also:
TYPE_LT_DONTHAVE
BEP 54 lt_donthave extension type string.- See Also:
MAX_METADATA_SIZE
private static final int MAX_METADATA_SIZEPieces * SHA1 Hash length, + 25% extra for file names, bencoding overhead, etc.- See Also:
PARALLEL_REQUESTS
private static final int PARALLEL_REQUESTSExtension requests in flight per peer.- See Also:
TYPE_REQUEST
private static final int TYPE_REQUESTExtension message asking for data.- See Also:
TYPE_DATA
TYPE_REJECT
private static final int TYPE_REJECTExtension message refusing a request.- See Also:
HASH_LENGTH
private static final int HASH_LENGTHLength of the piece hashes, in bytes.- See Also:
COMMENTS_FILTER
private static final byte[] COMMENTS_FILTERFilter saying comment requests are not wanted.
Constructor Details
ExtensionHandler
ExtensionHandler()Constructor for subclasses, each of which handles one BEP 10 extension protocol.
Method Details
sendExtension
Send an extension message to a peer, looking up its message code for the given type in the handshake. Silently ignores peers that did not advertise the extension.- Parameters:
peer- the peer to send totype- the extension type name (TYPE_* constant)payload- the bencoded message payload
getHandshake
public static byte[] getHandshake(int metasize, boolean pexAndMetadata, boolean dht, boolean uploadOnly, boolean comment, String clientName) Creates a bencoded extension handshake message.- Parameters:
metasize- the metadata size in bytes, or -1 if unknownpexAndMetadata- true to advertise PEX and metadata extensionsdht- true to advertise DHT capabilityuploadOnly- true to advertise upload-only mode (BEP 21)comment- true to advertise ut_comment extensionclientName- the "v" value, or null for the default "I2PSnark"- Returns:
- bencoded outgoing handshake message
sendDontHave
Send a BEP 54 lt_donthave message to a peer that advertised support: the piece is no longer available. The payload is the raw 4-byte piece index.- Parameters:
peer- the peer to send topiece- the piece index
handleMessage
Handles an incoming extension message from a peer.- Parameters:
peer- the peer the message arrived fromlistener- the peer listener for callbacksid- the extension message IDbs- the raw message bytes
handleHandshake
Handles an incoming extension handshake message.- Parameters:
peer- the peer the message arrived fromlistener- the peer listener for callbacksbs- the raw message byteslog- the logger instance
handleMetadata
Handles an incoming metadata extension message (BEP 9).- Parameters:
peer- the peer the message arrived fromlistener- the peer listener for callbacksbs- the raw message byteslog- the logger instance
sendRequest
Sends a metadata chunk request to a peer.- Parameters:
peer- the peer the message arrived frompiece- the chunk number to request
sendMessage
Sends a metadata request or reject message to a peer.- Parameters:
peer- the peer the message arrived fromtype- the message type (TYPE_REQUEST or TYPE_REJECT)piece- the chunk number
sendPiece
Sends a metadata chunk data message to a peer.- Parameters:
peer- the peer the message arrived frompiece- the chunk numberdata- the chunk data bytestotalSize- the total metadata size
handlePEX
Handles an incoming PEX message. Uses the "added" key as a single string of concatenated 32-byte peer hashes. added.f and dropped are unsupported.- Parameters:
peer- the peer the message arrived fromlistener- the peer listener for callbacksbs- the raw message byteslog- the logger instance
handleDHT
Handles an incoming DHT port message.- Parameters:
peer- the peer the message arrived fromlistener- the peer listener for callbacksbs- the raw message byteslog- the logger instance
sendPEX
sendDHT
Sends DHT port numbers to a peer.- Parameters:
peer- the peer the message arrived fromqport- the query portrport- the response port
handleComment
Handles an incoming comment request or response message.- Parameters:
peer- the peer the message arrived fromlistener- the peer listener for callbacksbs- the raw message byteslog- the logger instance
sendCommentReq
Sends a comment request to a peer.- Parameters:
peer- the peer the message arrived fromnum- the maximum number of comments to request
locked_sendComments
Sends comments to a peer. Caller must synchronize on comments.- Parameters:
peer- the peer the message arrived fromnum- the maximum number of comments to sendcomments- non-null set of comments to send