Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Remove non-standard/non-common fields from TrackerMessage definitions

Remove fields from the TrackerMessage definitions that are not shared
among implementations -- they are still free to implement them, but they
are not part of each message's interface.

Change also contains a few improvements to the Announce and HTTPAnnounce
code.

Signed-off-by: Maxime Petazzoni <mpetazzoni@turn.com>
  • Loading branch information...
commit 86a43de7e479eb99cf4f64f3357f9a4c9e2b3acb 1 parent f9dcb2a
Maxime Petazzoni authored
View
13 src/main/java/com/turn/ttorrent/client/announce/Announce.java
@@ -23,7 +23,6 @@
import java.net.SocketException;
import java.net.UnknownHostException;
import java.net.UnknownServiceException;
-
import java.util.List;
import java.util.HashSet;
import java.util.Set;
@@ -193,11 +192,9 @@ public abstract void announce(AnnounceRequestMessage.RequestEvent event,
*/
protected String formatAnnounceEvent(
AnnounceRequestMessage.RequestEvent event) {
- if (!AnnounceRequestMessage.RequestEvent.NONE.equals(event)) {
- return String.format(" %s", event.name());
- }
-
- return "";
+ return AnnounceRequestMessage.RequestEvent.NONE.equals(event)
+ ? ""
+ : String.format(" %s", event.name());
}
/**
@@ -271,7 +268,7 @@ public void run() {
}
/**
- * Handle the response from the tracker.
+ * Handle the announce response from the tracker.
*
* <p>
* Analyzes the response from the tracker and acts on it. If the response
@@ -283,7 +280,7 @@ public void run() {
* @param message The incoming {@link TrackerMessage}.
* @param inhibitEvents Whether or not to prevent events from being fired.
*/
- protected void handleTrackerResponse(TrackerMessage message,
+ protected void handleTrackerAnnounceResponse(TrackerMessage message,
boolean inhibitEvents) throws AnnounceException {
if (message instanceof ErrorMessage) {
ErrorMessage error = (ErrorMessage)message;
View
5 src/main/java/com/turn/ttorrent/client/announce/HTTPAnnounce.java
@@ -101,7 +101,7 @@ public void announce(AnnounceRequestMessage.RequestEvent event,
// Parse and handle the response
HTTPTrackerMessage message =
HTTPTrackerMessage.parse(ByteBuffer.wrap(baos.toByteArray()));
- this.handleTrackerResponse(message, inhibitEvents);
+ this.handleTrackerAnnounceResponse(message, inhibitEvents);
} catch (MalformedURLException mue) {
throw new AnnounceException("Invalid announce URL (" +
mue.getMessage() + ")", mue);
@@ -138,7 +138,6 @@ private HTTPAnnounceRequestMessage buildAnnounceRequest(
this.torrent.getLeft(),
true, false, event,
this.peer.getIp(),
- AnnounceRequestMessage.DEFAULT_NUM_WANT,
- null, null);
+ AnnounceRequestMessage.DEFAULT_NUM_WANT);
}
}
View
40 src/main/java/com/turn/ttorrent/common/protocol/TrackerMessage.java
@@ -121,9 +121,6 @@ public MessageValidationException(String s, Throwable cause) {
*/
public interface ConnectionRequestMessage {
- public long getConnectionId();
- public int getActionId();
- public int getTransactionId();
};
@@ -139,9 +136,6 @@ public MessageValidationException(String s, Throwable cause) {
*/
public interface ConnectionResponseMessage {
- public int getActionId();
- public int getTransactionId();
- public long getConnectionId();
};
@@ -178,18 +172,36 @@ public MessageValidationException(String s, Throwable cause) {
* </p>
*/
public enum RequestEvent {
- NONE,
- STARTED,
- STOPPED,
- COMPLETED;
+ NONE(0),
+ COMPLETED(1),
+ STARTED(2),
+ STOPPED(3);
+
+ private final int id;
+ RequestEvent(int id) {
+ this.id = id;
+ }
public String getEventName() {
return this.name().toLowerCase();
}
- public static RequestEvent get(String event) {
+ public int getId() {
+ return this.id;
+ }
+
+ public static RequestEvent getByName(String name) {
+ for (RequestEvent type : RequestEvent.values()) {
+ if (type.name().equalsIgnoreCase(name)) {
+ return type;
+ }
+ }
+ return null;
+ }
+
+ public static RequestEvent getById(int id) {
for (RequestEvent type : RequestEvent.values()) {
- if (type.name().equalsIgnoreCase(event)) {
+ if (type.getId() == id) {
return type;
}
}
@@ -211,8 +223,6 @@ public static RequestEvent get(String event) {
public String getIp();
public int getNumWant();
- public String getKey();
- public String getTrackerId();
};
@@ -229,8 +239,6 @@ public static RequestEvent get(String event) {
public interface AnnounceResponseMessage {
public int getInterval();
- public int getMinInterval();
- public String getTrackerId();
public int getComplete();
public int getIncomplete();
public List<Peer> getPeers();
View
54 src/main/java/com/turn/ttorrent/common/protocol/http/HTTPAnnounceRequestMessage.java
@@ -57,13 +57,11 @@
private final boolean noPeerId;
private final RequestEvent event;
private final int numWant;
- private final String key;
- private final String trackerId;
private HTTPAnnounceRequestMessage(ByteBuffer data,
byte[] infoHash, Peer peer, long uploaded, long downloaded,
long left, boolean compact, boolean noPeerId, RequestEvent event,
- int numWant, String key, String trackerId) {
+ int numWant) {
super(Type.ANNOUNCE_REQUEST, data);
this.infoHash = infoHash;
this.peer = peer;
@@ -74,8 +72,6 @@ private HTTPAnnounceRequestMessage(ByteBuffer data,
this.noPeerId = noPeerId;
this.event = event;
this.numWant = numWant;
- this.key = key;
- this.trackerId = trackerId;
}
@Override
@@ -143,16 +139,6 @@ public int getNumWant() {
return this.numWant;
}
- @Override
- public String getKey() {
- return this.key;
- }
-
- @Override
- public String getTrackerId() {
- return this.trackerId;
- }
-
/**
* Build the announce request URL for the given tracker announce URL.
*
@@ -188,14 +174,6 @@ public URL buildAnnounceURL(URL trackerAnnounceURL)
url.append("&ip=").append(this.getIp());
}
- if (this.getKey() != null) {
- url.append("&key=").append(this.getKey());
- }
-
- if (this.getTrackerId() != null) {
- url.append("&trackerid=").append(this.getTrackerId());
- }
-
return new URL(url.toString());
}
@@ -270,25 +248,14 @@ public static HTTPAnnounceRequestMessage parse(ByteBuffer data)
RequestEvent event = RequestEvent.NONE;
if (params.containsKey("event")) {
- event = RequestEvent.get(params.get("event")
+ event = RequestEvent.getByName(params.get("event")
.getString(Torrent.BYTE_ENCODING));
}
- String key = null;
- if (params.containsKey("key")) {
- key = params.get("key").getString(Torrent.BYTE_ENCODING);
- }
-
- String trackerId = null;
- if (params.containsKey("trackerid")) {
- trackerId = params.get("trackerid")
- .getString(Torrent.BYTE_ENCODING);
- }
-
return new HTTPAnnounceRequestMessage(data, infoHash,
new Peer(ip, port, ByteBuffer.wrap(peerId)),
downloaded, uploaded, left, compact, noPeerId,
- event, numWant, key, trackerId);
+ event, numWant);
} catch (InvalidBEncodingException ibee) {
throw new MessageValidationException(
"Invalid HTTP tracker request!", ibee);
@@ -298,7 +265,7 @@ public static HTTPAnnounceRequestMessage parse(ByteBuffer data)
public static HTTPAnnounceRequestMessage craft(byte[] infoHash,
byte[] peerId, int port, long uploaded, long downloaded, long left,
boolean compact, boolean noPeerId, RequestEvent event,
- String ip, int numWant, String key, String trackerId)
+ String ip, int numWant)
throws IOException, MessageValidationException,
UnsupportedEncodingException {
Map<String, BEValue> params = new HashMap<String, BEValue>();
@@ -325,20 +292,9 @@ public static HTTPAnnounceRequestMessage craft(byte[] infoHash,
params.put("numwant", new BEValue(numWant));
}
- if (key != null) {
- params.put("key",
- new BEValue(key, Torrent.BYTE_ENCODING));
- }
-
- if (trackerId != null) {
- params.put("trackerid",
- new BEValue(trackerId, Torrent.BYTE_ENCODING));
- }
-
return new HTTPAnnounceRequestMessage(
BEncoder.bencode(params),
infoHash, new Peer(ip, port, ByteBuffer.wrap(peerId)),
- uploaded, downloaded, left, compact, noPeerId, event,
- numWant, key, trackerId);
+ uploaded, downloaded, left, compact, noPeerId, event, numWant);
}
}
View
36 src/main/java/com/turn/ttorrent/common/protocol/http/HTTPAnnounceResponseMessage.java
@@ -26,6 +26,7 @@
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
+import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.util.HashMap;
@@ -43,19 +44,14 @@
implements AnnounceResponseMessage {
private final int interval;
- private final int minInterval;
- private final String trackerId;
private final int complete;
private final int incomplete;
private final List<Peer> peers;
private HTTPAnnounceResponseMessage(ByteBuffer data,
- int interval, int minInterval, String trackerId, int complete,
- int incomplete, List<Peer> peers) {
+ int interval, int complete, int incomplete, List<Peer> peers) {
super(Type.ANNOUNCE_RESPONSE, data);
this.interval = interval;
- this.minInterval = minInterval;
- this.trackerId = trackerId;
this.complete = complete;
this.incomplete = incomplete;
this.peers = peers;
@@ -67,16 +63,6 @@ public int getInterval() {
}
@Override
- public int getMinInterval() {
- return this.minInterval;
- }
-
- @Override
- public String getTrackerId() {
- return this.trackerId;
- }
-
- @Override
public int getComplete() {
return this.complete;
}
@@ -116,13 +102,6 @@ public static HTTPAnnounceResponseMessage parse(ByteBuffer data)
return new HTTPAnnounceResponseMessage(data,
params.get("interval").getInt(),
- params.containsKey("min interval")
- ? params.get("min interval").getInt()
- : -1,
- params.containsKey("tracker id")
- ? params.get("tracker id")
- .getString(Torrent.BYTE_ENCODING)
- : null,
params.get("complete").getInt(),
params.get("incomplete").getInt(),
peers);
@@ -169,7 +148,6 @@ public static HTTPAnnounceResponseMessage parse(ByteBuffer data)
*/
private static List<Peer> toPeerList(byte[] data)
throws InvalidBEncodingException, UnknownHostException {
- int nPeers = data.length / 6;
if (data.length % 6 != 0) {
throw new InvalidBEncodingException("Invalid peers " +
"binary information string!");
@@ -178,14 +156,14 @@ public static HTTPAnnounceResponseMessage parse(ByteBuffer data)
List<Peer> result = new LinkedList<Peer>();
ByteBuffer peers = ByteBuffer.wrap(data);
- for (int i=0; i < nPeers ; i++) {
+ for (int i=0; i < data.length / 6 ; i++) {
byte[] ipBytes = new byte[4];
peers.get(ipBytes);
InetAddress ip = InetAddress.getByAddress(ipBytes);
int port =
(0xFF & (int)peers.get()) << 8 |
(0xFF & (int)peers.get());
- result.add(new Peer(ip.getHostAddress(), port));
+ result.add(new Peer(new InetSocketAddress(ip, port)));
}
return result;
@@ -206,9 +184,6 @@ public static HTTPAnnounceResponseMessage craft(int interval,
List<Peer> peers) throws IOException, UnsupportedEncodingException {
Map<String, BEValue> response = new HashMap<String, BEValue>();
response.put("interval", new BEValue(interval));
- response.put("minInterval", new BEValue(minInterval));
- response.put("trackerid",
- new BEValue(trackerId, Torrent.BYTE_ENCODING));
response.put("complete", new BEValue(complete));
response.put("incomplete", new BEValue(incomplete));
@@ -225,7 +200,6 @@ public static HTTPAnnounceResponseMessage craft(int interval,
return new HTTPAnnounceResponseMessage(
BEncoder.bencode(response),
- interval, minInterval, trackerId, complete, incomplete,
- peers);
+ interval, complete, incomplete, peers);
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.