i2psnark: finals for Message class

recognize BiglyBT
cleanups
This commit is contained in:
zzz
2017-08-22 12:34:38 +00:00
parent 3d385228f0
commit 51d4827657
5 changed files with 126 additions and 102 deletions

View File

@ -52,26 +52,26 @@ class Message
// Not all fields are used for every message.
// KEEP_ALIVE doesn't have a real wire representation
byte type;
final byte type;
// Used for HAVE, REQUEST, PIECE and CANCEL messages.
// Also SUGGEST, REJECT, ALLOWED_FAST
// low byte used for EXTENSION message
// low two bytes used for PORT message
int piece;
final int piece;
// Used for REQUEST, PIECE and CANCEL messages.
// Also REJECT
int begin;
int length;
final int begin;
final int length;
// Used for PIECE and BITFIELD and EXTENSION messages
byte[] data;
int off;
int len;
final int off;
final int len;
// Used to do deferred fetch of data
DataLoader dataLoader;
private final DataLoader dataLoader;
// now unused
//SimpleTimer.TimedEvent expireEvent;
@ -79,6 +79,79 @@ class Message
private static final int BUFSIZE = PeerState.PARTSIZE;
private static final ByteCache _cache = ByteCache.getInstance(16, BUFSIZE);
/**
* For types KEEP_ALIVE, CHOKE, UNCHOKE, INTERESTED, UNINTERESTED, HAVE_ALL, HAVE_NONE
* @since 0.9.32
*/
Message(byte type) {
this(type, 0, 0, 0, null, 0, 0, null);
}
/**
* For types HAVE, PORT, SUGGEST, ALLOWED_FAST
* @since 0.9.32
*/
Message(byte type, int piece) {
this(type, piece, 0, 0, null, 0, 0, null);
}
/**
* For types REQUEST, REJECT, CANCEL
* @since 0.9.32
*/
Message(byte type, int piece, int begin, int length) {
this(type, piece, begin, length, null, 0, 0, null);
}
/**
* For type BITFIELD
* @since 0.9.32
*/
Message(byte[] data) {
this(BITFIELD, 0, 0, 0, data, 0, data.length, null);
}
/**
* For type EXTENSION
* @since 0.9.32
*/
Message(int id, byte[] data) {
this(EXTENSION, id, 0, 0, data, 0, data.length, null);
}
/**
* For type PIECE with deferred data
* @since 0.9.32
*/
Message(int piece, int begin, int length, DataLoader loader) {
this(PIECE, piece, begin, length, null, 0, length, loader);
}
/**
* For type PIECE with data
* We don't use this anymore.
* @since 0.9.32
*/
/****
Message(int piece, int begin, int length, byte[] data) {
this(PIECE, piece, begin, length, data, 0, length, null);
}
****/
/**
* @since 0.9.32
*/
private Message(byte type, int piece, int begin, int length, byte[] data, int off, int len, DataLoader loader) {
this.type = type;
this.piece = piece;
this.begin = begin;
this.length = length;
this.data = data;
this.off = off;
this.len = len;
dataLoader = loader;
}
/** Utility method for sending a message through a DataStream. */
void sendMessage(DataOutputStream dos) throws IOException
{
@ -121,10 +194,10 @@ class Message
datalen += 4;
// msg type is 1 byte
if (type == EXTENSION)
else if (type == EXTENSION)
datalen += 1;
if (type == PORT)
else if (type == PORT)
datalen += 2;
// add length of data for piece or bitfield array.
@ -150,10 +223,10 @@ class Message
type == REJECT)
dos.writeInt(length);
if (type == EXTENSION)
else if (type == EXTENSION)
dos.writeByte((byte) piece & 0xff);
if (type == PORT)
else if (type == PORT)
dos.writeShort(piece & 0xffff);
// Send actual data

View File

@ -49,12 +49,11 @@ class PeerConnectionIn implements Runnable
this.peer = peer;
this.din = din;
lastRcvd = System.currentTimeMillis();
quit = false;
}
void disconnect()
{
if (quit == true)
if (quit)
return;
quit = true;

View File

@ -126,11 +126,7 @@ class PeerConnectionOut implements Runnable
it.remove();
//SimpleTimer.getInstance().removeEvent(nm.expireEvent);
if (peer.supportsFast()) {
Message r = new Message();
r.type = Message.REJECT;
r.piece = nm.piece;
r.begin = nm.begin;
r.length = nm.length;
Message r = new Message(Message.REJECT, nm.piece, nm.begin, nm.length);
if (_log.shouldLog(Log.DEBUG))
_log.debug("Send " + peer + ": " + r);
r.sendMessage(dout);
@ -294,11 +290,7 @@ class PeerConnectionOut implements Runnable
it.remove();
removed = true;
if (type == Message.PIECE && peer.supportsFast()) {
Message r = new Message();
r.type = Message.REJECT;
r.piece = m.piece;
r.begin = m.begin;
r.length = m.length;
Message r = new Message(Message.REJECT, m.piece, m.begin, m.length);
if (_log.shouldLog(Log.DEBUG))
_log.debug("Send " + peer + ": " + r);
try {
@ -314,13 +306,12 @@ class PeerConnectionOut implements Runnable
void sendAlive()
{
Message m = new Message();
m.type = Message.KEEP_ALIVE;
// addMessage(m);
synchronized(sendQueue)
{
if(sendQueue.isEmpty())
if(sendQueue.isEmpty()) {
Message m = new Message(Message.KEEP_ALIVE);
sendQueue.offer(m);
}
sendQueue.notifyAll();
}
}
@ -335,11 +326,7 @@ class PeerConnectionOut implements Runnable
: Message.CHOKE;
if (!removeMessage(inverseType))
{
Message m = new Message();
if (choke)
m.type = Message.CHOKE;
else
m.type = Message.UNCHOKE;
Message m = new Message(choke ? Message.CHOKE : Message.UNCHOKE);
addMessage(m);
}
}
@ -353,11 +340,7 @@ class PeerConnectionOut implements Runnable
: Message.INTERESTED;
if (!removeMessage(inverseType))
{
Message m = new Message();
if (interest)
m.type = Message.INTERESTED;
else
m.type = Message.UNINTERESTED;
Message m = new Message(interest ? Message.INTERESTED : Message.UNINTERESTED);
addMessage(m);
}
}
@ -365,9 +348,7 @@ class PeerConnectionOut implements Runnable
void sendHave(int piece)
{
Message m = new Message();
m.type = Message.HAVE;
m.piece = piece;
Message m = new Message(Message.HAVE, piece);
addMessage(m);
}
@ -379,11 +360,7 @@ class PeerConnectionOut implements Runnable
} else if (fast && bitfield.count() <= 0) {
sendHaveNone();
} else {
Message m = new Message();
m.type = Message.BITFIELD;
m.data = bitfield.getFieldBytes();
m.off = 0;
m.len = m.data.length;
Message m = new Message(bitfield.getFieldBytes());
addMessage(m);
}
}
@ -434,11 +411,7 @@ class PeerConnectionOut implements Runnable
}
}
}
Message m = new Message();
m.type = Message.REQUEST;
m.piece = req.getPiece();
m.begin = req.off;
m.length = req.len;
Message m = new Message(Message.REQUEST, req.getPiece(), req.off, req.len);
addMessage(m);
req.sendTime = System.currentTimeMillis();
}
@ -482,14 +455,7 @@ class PeerConnectionOut implements Runnable
// queue a fake message... set everything up,
// except save the PeerState instead of the bytes.
Message m = new Message();
m.type = Message.PIECE;
m.piece = piece;
m.begin = begin;
m.length = length;
m.dataLoader = loader;
m.off = 0;
m.len = length;
Message m = new Message(piece, begin, length, loader);
addMessage(m);
}
@ -498,21 +464,17 @@ class PeerConnectionOut implements Runnable
* Also add a timeout.
* We don't use this anymore.
*/
/****
void sendPiece(int piece, int begin, int length, byte[] bytes)
{
Message m = new Message();
m.type = Message.PIECE;
m.piece = piece;
m.begin = begin;
m.length = length;
m.data = bytes;
m.off = 0;
m.len = length;
Message m = new Message(piece, begin, length, bytes);
// since we have the data already loaded, queue a timeout to remove it
// no longer prefetched
addMessage(m);
}
****/
/** send cancel */
void sendCancel(Request req)
{
// See if it is still in our send queue
@ -531,11 +493,7 @@ class PeerConnectionOut implements Runnable
}
// Always send, just to be sure it it is really canceled.
Message m = new Message();
m.type = Message.CANCEL;
m.piece = req.getPiece();
m.begin = req.off;
m.length = req.len;
Message m = new Message(Message.CANCEL, req.getPiece(), req.off, req.len);
addMessage(m);
}
@ -578,20 +536,13 @@ class PeerConnectionOut implements Runnable
/** @since 0.8.2 */
void sendExtension(int id, byte[] bytes) {
Message m = new Message();
m.type = Message.EXTENSION;
m.piece = id;
m.data = bytes;
m.off = 0;
m.len = bytes.length;
Message m = new Message(id, bytes);
addMessage(m);
}
/** @since 0.8.4 */
void sendPort(int port) {
Message m = new Message();
m.type = Message.PORT;
m.piece = port;
Message m = new Message(Message.PORT, port);
addMessage(m);
}
@ -599,34 +550,28 @@ class PeerConnectionOut implements Runnable
* Unused
* @since 0.9.21
*/
/****
void sendSuggest(int piece) {
Message m = new Message();
m.type = Message.SUGGEST;
m.piece = piece;
Message m = new Message(Message.SUGGEST, piece);
addMessage(m);
}
****/
/** @since 0.9.21 */
private void sendHaveAll() {
Message m = new Message();
m.type = Message.HAVE_ALL;
Message m = new Message(Message.HAVE_ALL);
addMessage(m);
}
/** @since 0.9.21 */
private void sendHaveNone() {
Message m = new Message();
m.type = Message.HAVE_NONE;
Message m = new Message(Message.HAVE_NONE);
addMessage(m);
}
/** @since 0.9.21 */
void sendReject(int piece, int begin, int length) {
Message m = new Message();
m.type = Message.REJECT;
m.piece = piece;
m.begin = begin;
m.length = length;
Message m = new Message(Message.REJECT, piece, begin, length);
addMessage(m);
}
@ -634,10 +579,10 @@ class PeerConnectionOut implements Runnable
* Unused
* @since 0.9.21
*/
/****
void sendAllowedFast(int piece) {
Message m = new Message();
m.type = Message.ALLOWED_FAST;
m.piece = piece;
Message m = new Message(Message.ALLOWED_FAST, piece);
addMessage(m);
}
****/
}

View File

@ -1424,6 +1424,11 @@ public class KRPC implements I2PSessionMuxedListener, DHT {
List<Hash> rv = new ArrayList<Hash>(max);
for (BEValue bev : peers) {
byte[] b = bev.getBytes();
if (b.length != Hash.HASH_LENGTH) {
if (_log.shouldWarn())
_log.info("Bad peers entry from: " + nInfo);
continue;
}
//Hash h = new Hash(b);
Hash h = Hash.create(b);
rv.add(h);

View File

@ -1864,14 +1864,10 @@ public class I2PSnarkServlet extends BasicServlet {
String client;
if ("AwMD".equals(ch))
client = _t("I2PSnark");
else if ("BFJT".equals(ch))
client = "I2PRufus";
else if ("TTMt".equals(ch))
client = "I2P-BT";
else if ("LUFa".equals(ch))
client = "Vuze" + getAzVersion(pid.getID());
else if ("CwsL".equals(ch))
client = "I2PSnarkXL";
else if ("LUJJ".equals(ch))
client = "BiglyBT" + getAzVersion(pid.getID());
else if ("LVhE".equals(ch))
client = "XD" + getAzVersion(pid.getID());
else if ("ZV".equals(ch.substring(2,4)) || "VUZP".equals(ch))
@ -1880,6 +1876,12 @@ public class I2PSnarkServlet extends BasicServlet {
client = "Transmission" + getAzVersion(pid.getID());
else if ("LUtU".equals(ch))
client = "KTorrent" + getAzVersion(pid.getID());
else if ("CwsL".equals(ch))
client = "I2PSnarkXL";
else if ("BFJT".equals(ch))
client = "I2PRufus";
else if ("TTMt".equals(ch))
client = "I2P-BT";
else
client = _t("Unknown") + " (" + ch + ')';
out.write(client + "&nbsp;<tt title=\"");
@ -1981,7 +1983,7 @@ public class I2PSnarkServlet extends BasicServlet {
private static String getAzVersion(byte[] id) {
if (id[7] != '-')
return "";
StringBuilder buf = new StringBuilder(16);
StringBuilder buf = new StringBuilder(8);
buf.append(' ');
for (int i = 3; i <= 6; i++) {
int val = id[i] - '0';