1
0
mirror of https://github.com/Rogiel/torrent4j synced 2025-12-05 23:22:49 +00:00

Moved peer model related classes to com.torrent4j.model.peer

This commit is contained in:
2012-04-22 18:53:17 -03:00
parent b3e6b47cb6
commit 749c2c9558
28 changed files with 232 additions and 50 deletions

3
.gitignore vendored
View File

@@ -1,3 +1,6 @@
/.project
/.classpath
/.settings
target/
test.torrent

View File

@@ -6,7 +6,7 @@ import java.net.InetSocketAddress;
import java.nio.file.Paths;
import com.torrent4j.model.Torrent;
import com.torrent4j.model.TorrentPeer;
import com.torrent4j.model.peer.TorrentPeer;
import com.torrent4j.net.peerwire.PeerWireProtocol;
import com.torrent4j.storage.InMemoryTorrentStorage;

View File

@@ -5,6 +5,8 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import com.torrent4j.model.peer.TorrentPeer;
public class TorrentSwarm {
private final Torrent torrent;
private final List<TorrentPeer> peers = new ArrayList<>();

View File

@@ -1,11 +1,12 @@
package com.torrent4j.model;
package com.torrent4j.model.peer;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import com.torrent4j.model.TorrentPeerState.TorrentPeerChoking;
import com.torrent4j.model.TorrentPeerState.TorrentPeerInterest;
import com.torrent4j.model.Torrent;
import com.torrent4j.model.TorrentPiece;
import com.torrent4j.model.TorrentPieceBlock;
import com.torrent4j.net.TorrentProtocolPeer;
/**
@@ -80,8 +81,8 @@ public class TorrentPeer {
}
/**
* Tries to detect the peer's client. If unknown, {@link TorrentPeerClient#UNKNOWN} is
* returned.
* Tries to detect the peer's client. If unknown,
* {@link TorrentPeerClient#UNKNOWN} is returned.
*
* @return the peer torrent client
*/
@@ -89,6 +90,20 @@ public class TorrentPeer {
return peerID.getClient();
}
/**
* @return <code>true</code> if the client version is known
*/
public boolean isClientVersionKnown() {
return peerID.isClientVersionKnown();
}
/**
* @return the client version as by the PeerID
*/
public String getClientVersion() {
return peerID.getClientVersion();
}
public TorrentProtocolPeer getProtocolPeer() {
return protocolPeer;
}

View File

@@ -0,0 +1,97 @@
package com.torrent4j.model.peer;
import com.torrent4j.net.peerwire.messages.HandshakeMessage;
/**
* Enlists all recognized extensions for the bittorrent protocol
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public enum TorrentPeerCapability {
/**
* These extensions serve multiple purposes. They allow a peer to more
* quickly bootstrap into a swarm by giving a peer a specific set of pieces
* which they will be allowed download regardless of choked status. They
* reduce message overhead by adding HaveAll and HaveNone messages and allow
* explicit rejection of piece requests whereas previously only implicit
* rejection was possible meaning that a peer might be left waiting for a
* piece that would never be delivered.
*/
FAST_PEERS(62),
/**
* This extension is to allow for the tracking of peers downloading torrents
* without the use of a standard tracker. A peer implementing this protocol
* becomes a "tracker" and stores lists of other nodes/peers which can be
* used to locate new peers.
*/
DHT(64),
/**
* A protocol in its own right - if two clients indicate they support the
* protocol, then they should switch over to using it. It allows normal
* BitTorrent as well extension messages to be sent over it. Currently
* implemented by Azureus and Transmission.
* <p>
* It is not possible to use both this protocol and the LibTorrent extension
* protocol at the same time - if both clients indicate they support both,
* then they should follow the semantics defined by the Extension
* Negotiation Protocol.
*/
AZUREUS_MESSAGING_PROTOCOL(1),
/**
* This is a protocol for exchanging extension information and was derived
* from an early version of Azureus' extension protocol. It adds one message
* for exchanging arbitrary handshake information including defined
* extension messages, mapping extensions to specific message IDs.
*/
EXTENSION_PROTOCOL(44),
/**
* These bits are used to allow two clients that support both the Azureus
* Messaging Protocol and LibTorrent's extension protocol to decide which of
* the two extensions should be used for communication.
* <p>
* This bit should always be set along with this
* {@link TorrentPeerCapability#EXTENSION_NEGOTIATION_PROTOCOL_2}
*
* @see TorrentPeerCapability#EXTENSION_NEGOTIATION_PROTOCOL_2
*/
EXTENSION_NEGOTIATION_PROTOCOL_1(47),
/**
* These bits are used to allow two clients that support both the Azureus
* Messaging Protocol and LibTorrent's extension protocol to decide which of
* the two extensions should be used for communication.
* <p>
* This bit should always be set along with this
* {@link TorrentPeerCapability#EXTENSION_NEGOTIATION_PROTOCOL_1}
*
* @see TorrentPeerCapability#EXTENSION_NEGOTIATION_PROTOCOL_1
*/
EXTENSION_NEGOTIATION_PROTOCOL_2(48),
/**
* A Protocol, considering peers location (in geographical terms) for better
* performance.
*/
LOCATION_AWARE_PROTOCOL(48);
/**
* This bit is stored on the {@link HandshakeMessage#reserved} long. Those
* set of bits specify which BitTorrent extensions are known and compatible
* with the client. If any given extension is supported by both clients, it
* is enabled by default.
*/
public final int bit;
/**
* Creates a new enum instance
*
* @param bit
* the bit
*/
private TorrentPeerCapability(int bit) {
this.bit = bit;
}
}

View File

@@ -0,0 +1,10 @@
package com.torrent4j.model.peer;
/**
* Determines whether the peer is choked or unchocked.
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public enum TorrentPeerChoking {
CHOKED, UNCHOKED;
}

View File

@@ -1,4 +1,4 @@
package com.torrent4j.model;
package com.torrent4j.model.peer;
/**
* The list of known torrent clients
@@ -9,17 +9,17 @@ public enum TorrentPeerClient {
/**
* µTorrent for Mac (<strong>-UM</strong>)
*/
UTORRENT_FOR_MAC("µTorrent for Mac", "-UM"),
UTORRENT_FOR_MAC("µTorrent for Mac", "-UM", true),
/**
* µTorrent (<strong>-UT</strong>)
*/
UTORRENT("µTorrent", "-UT"),
UTORRENT("µTorrent", "-UT", true),
/**
* Transmission (<strong>-TR</strong>)
*/
TRANSMISSION("Transmission", "-TR"),
TRANSMISSION("Transmission", "-TR", true),
UNKNOWN("Unknown", "");
UNKNOWN("Unknown", "", false);
/**
* An friendly name for the client. Can be used on an UI.
@@ -29,6 +29,10 @@ public enum TorrentPeerClient {
* The PeerID prefix for the client
*/
public final String prefix;
/**
* Whether the PeerID has an version attribute
*/
public final boolean versioned;
/**
* Initializes a new enum value
@@ -39,7 +43,24 @@ public enum TorrentPeerClient {
* the PeerID prefix
*/
private TorrentPeerClient(String friendlyName, String prefix) {
this(friendlyName, prefix, false);
}
/**
* Initializes a new enum value
*
* @param friendlyName
* an friendly name for the client
* @param prefix
* the PeerID prefix
* @param versioned
* whether the PeerID has an version attribute
*
*/
private TorrentPeerClient(String friendlyName, String prefix,
boolean versioned) {
this.friendlyName = friendlyName;
this.prefix = prefix;
this.versioned = versioned;
}
}

View File

@@ -1,4 +1,6 @@
package com.torrent4j.model;
package com.torrent4j.model.peer;
import com.torrent4j.model.Torrent;
/**
* This ID represents an PeerID on the BitTorrent network
@@ -36,21 +38,39 @@ public class TorrentPeerID {
}
/**
* Tries to detect the peer's client. If unknown, {@link TorrentPeerClient#UNKNOWN} is
* returned.
* Tries to detect the peer's client. If unknown,
* {@link TorrentPeerClient#UNKNOWN} is returned.
*
* @return the peer torrent client
*/
public TorrentPeerClient getClient() {
for(TorrentPeerClient client : TorrentPeerClient.values()) {
if(client == TorrentPeerClient.UNKNOWN)
for (TorrentPeerClient client : TorrentPeerClient.values()) {
if (client == TorrentPeerClient.UNKNOWN)
continue;
if(peerID.startsWith(client.prefix))
if (peerID.startsWith(client.prefix))
return client;
}
return TorrentPeerClient.UNKNOWN;
}
/**
* @return <code>true</code> if the client version is known
*/
public boolean isClientVersionKnown() {
return getClient().versioned;
}
/**
* @return the client version as by the PeerID
*/
public String getClientVersion() {
if (isClientVersionKnown()) {
return this.peerID.substring(3, 7);
} else {
return null;
}
}
/**
* @return the peer
*/

View File

@@ -0,0 +1,10 @@
package com.torrent4j.model.peer;
/**
* Determines whether there are interest on each other pieces
*
* @author <a href="http://www.rogiel.com">Rogiel</a>
*/
public enum TorrentPeerInterest {
INTERESTED, NOT_INTERESTED;
}

View File

@@ -1,7 +1,9 @@
package com.torrent4j.model;
package com.torrent4j.model.peer;
import java.util.BitSet;
import com.torrent4j.model.AbstractTorrentPiecesContainer;
public class TorrentPeerPieces extends AbstractTorrentPiecesContainer {
private final TorrentPeer peer;

View File

@@ -1,24 +1,19 @@
package com.torrent4j.model;
package com.torrent4j.model.peer;
import java.util.Date;
import com.torrent4j.model.Torrent;
import com.torrent4j.model.TorrentPieceBlock;
public class TorrentPeerState {
private final TorrentPeer peer;
private TorrentPeerInterest remoteInterest = TorrentPeerInterest.NOT_INTERESTED;
private TorrentPeerInterest localInterest = TorrentPeerInterest.NOT_INTERESTED;
public enum TorrentPeerInterest {
INTERESTED, NOT_INTERESTED;
}
private TorrentPeerChoking remoteChoked = TorrentPeerChoking.CHOKED;
private TorrentPeerChoking locallyChoked = TorrentPeerChoking.CHOKED;
public enum TorrentPeerChoking {
CHOKED, UNCHOKED;
}
private TorrentPieceBlock downloadRequestedBlock;
private Date downloadRequestedDate;
private TorrentPieceBlock lastDownloadedBlock;

View File

@@ -1,4 +1,6 @@
package com.torrent4j.model;
package com.torrent4j.model.peer;
import com.torrent4j.model.TorrentTrafficControl;
public class TorrentPeerTrafficControl extends TorrentTrafficControl {
private final TorrentPeer peer;

View File

@@ -1,7 +1,7 @@
package com.torrent4j.net;
import com.torrent4j.TorrentController;
import com.torrent4j.model.TorrentPeer;
import com.torrent4j.model.peer.TorrentPeer;
public interface TorrentProtocol {
void start(TorrentController controller, int listenPort);

View File

@@ -4,7 +4,7 @@ import java.nio.ByteBuffer;
import java.util.BitSet;
import com.torrent4j.model.Torrent;
import com.torrent4j.model.TorrentPeer;
import com.torrent4j.model.peer.TorrentPeer;
public interface TorrentProtocolPeer {
public TorrentPeer getTorrentPeer();

View File

@@ -11,9 +11,9 @@ import java.util.Date;
import com.torrent4j.TorrentController;
import com.torrent4j.model.Torrent;
import com.torrent4j.model.TorrentPeer;
import com.torrent4j.model.TorrentPeerState.TorrentPeerChoking;
import com.torrent4j.model.TorrentPeerState.TorrentPeerInterest;
import com.torrent4j.model.peer.TorrentPeer;
import com.torrent4j.model.peer.TorrentPeerChoking;
import com.torrent4j.model.peer.TorrentPeerInterest;
import com.torrent4j.model.TorrentPiece;
import com.torrent4j.model.TorrentPieceBlock;
import com.torrent4j.net.peerwire.codec.PeerWireFrameDecoder;

View File

@@ -14,7 +14,7 @@ import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import com.torrent4j.TorrentController;
import com.torrent4j.model.TorrentPeer;
import com.torrent4j.model.peer.TorrentPeer;
import com.torrent4j.net.TorrentProtocol;
public class PeerWireProtocol implements TorrentProtocol {

View File

@@ -7,7 +7,7 @@ import java.nio.ByteBuffer;
import java.util.BitSet;
import com.torrent4j.model.Torrent;
import com.torrent4j.model.TorrentPeer;
import com.torrent4j.model.peer.TorrentPeer;
import com.torrent4j.net.TorrentProtocolPeer;
import com.torrent4j.net.peerwire.messages.BitFieldMessage;
import com.torrent4j.net.peerwire.messages.BlockMessage;

View File

@@ -5,8 +5,8 @@ import io.netty.handler.traffic.TrafficCounter;
import java.util.concurrent.Executor;
import com.torrent4j.model.TorrentPeer;
import com.torrent4j.model.TorrentPeerTrafficControl;
import com.torrent4j.model.peer.TorrentPeer;
import com.torrent4j.model.peer.TorrentPeerTrafficControl;
public class PeerTrafficShapingHandler extends ChannelTrafficShapingHandler {
private long writeLimit;

View File

@@ -9,8 +9,8 @@ import com.torrent4j.model.Torrent;
import com.torrent4j.util.Range;
/**
* An simple {@link TorrentStorage} implementation that stores data into huge
* in-memory direct buffers. Please note that though it provides really fast
* An simple {@link TorrentStorage} implementation that stores data into a huge
* in-memory direct buffer. Please note that though it provides really fast
* write and reading, it should not be used in real world implementations.
* Storing data in-memory consumes a lot of memory and may cause the JVM to be
* terminated by some OSes.

View File

@@ -1,9 +1,9 @@
package com.torrent4j.strategy;
import com.torrent4j.model.Torrent;
import com.torrent4j.model.TorrentPeer;
import com.torrent4j.model.TorrentPiece;
import com.torrent4j.model.TorrentPieceBlock;
import com.torrent4j.model.peer.TorrentPeer;
/**
* Determines the actions that should be taken regarding downloads

View File

@@ -1,8 +1,8 @@
package com.torrent4j.strategy;
import com.torrent4j.model.Torrent;
import com.torrent4j.model.TorrentPeer;
import com.torrent4j.model.TorrentPiece;
import com.torrent4j.model.peer.TorrentPeer;
/**
* Determine actions that the client should take regarding peers and their

View File

@@ -1,8 +1,8 @@
package com.torrent4j.strategy;
import com.torrent4j.model.Torrent;
import com.torrent4j.model.TorrentPeer;
import com.torrent4j.model.TorrentPieceBlock;
import com.torrent4j.model.peer.TorrentPeer;
/**
* Determines the actions that should be taken regarding uploads

View File

@@ -1,7 +1,7 @@
package com.torrent4j.strategy.standard;
import com.torrent4j.model.TorrentPeer;
import com.torrent4j.model.TorrentPiece;
import com.torrent4j.model.peer.TorrentPeer;
/**
* Selects an suitable piece for download
@@ -13,6 +13,11 @@ public interface PieceSelector {
* Applies an algorithm to determine the most suitable piece for download
* from the peer. If no pieces are available or none are worth to download
* from this peer, <code>null</code> should be returned.
* <p>
* Also note that further interest decisions can be performed on the
* {@link StandardTorrentDownloadStrategy#blockReceived(com.torrent4j.model.Torrent, com.torrent4j.model.TorrentPieceBlock, TorrentPeer)}
* that can set to no interest even if this algorithm has interest into
* downloading an piece.
*
* @param peer
* the peer to download the piece from

View File

@@ -2,8 +2,8 @@ package com.torrent4j.strategy.standard;
import java.util.List;
import com.torrent4j.model.TorrentPeer;
import com.torrent4j.model.TorrentPiece;
import com.torrent4j.model.peer.TorrentPeer;
/**
* Randomly selects an piece from the available peer pieces

View File

@@ -1,10 +1,10 @@
package com.torrent4j.strategy.standard;
import com.torrent4j.model.Torrent;
import com.torrent4j.model.TorrentPeer;
import com.torrent4j.model.TorrentPiece;
import com.torrent4j.model.TorrentPieceBlock;
import com.torrent4j.model.TorrentSwarm.SwarmBroadcastHandler;
import com.torrent4j.model.peer.TorrentPeer;
import com.torrent4j.strategy.TorrentDownloadStrategy;
public class StandardTorrentDownloadStrategy implements TorrentDownloadStrategy {

View File

@@ -1,8 +1,8 @@
package com.torrent4j.strategy.standard;
import com.torrent4j.model.Torrent;
import com.torrent4j.model.TorrentPeer;
import com.torrent4j.model.TorrentPiece;
import com.torrent4j.model.peer.TorrentPeer;
import com.torrent4j.strategy.TorrentPeerStrategy;
public class StandardTorrentPeerStrategy implements TorrentPeerStrategy {

View File

@@ -3,7 +3,7 @@ package com.torrent4j.strategy.standard;
import java.util.ArrayList;
import java.util.List;
import com.torrent4j.model.TorrentPeer;
import com.torrent4j.model.peer.TorrentPeer;
import com.torrent4j.strategy.TorrentDownloadStrategy;
import com.torrent4j.strategy.TorrentPeerStrategy;
import com.torrent4j.strategy.TorrentStrategy;

View File

@@ -3,8 +3,8 @@ package com.torrent4j.strategy.standard;
import java.io.IOException;
import com.torrent4j.model.Torrent;
import com.torrent4j.model.TorrentPeer;
import com.torrent4j.model.TorrentPieceBlock;
import com.torrent4j.model.peer.TorrentPeer;
import com.torrent4j.storage.TorrentStorage;
import com.torrent4j.strategy.TorrentUploadStrategy;