- B32 lookup if required for non-announce queries only

- Token timeout tweaks
- Most classes package private
This commit is contained in:
zzz
2012-06-03 16:05:38 +00:00
parent 152b2152cb
commit 121491a3be
11 changed files with 52 additions and 16 deletions

View File

@ -28,7 +28,7 @@ import net.i2p.util.SimpleTimer;
* @since 0.8.4
* @author zzz
*/
public class DHTNodes extends ConcurrentHashMap<NID, NodeInfo> {
class DHTNodes extends ConcurrentHashMap<NID, NodeInfo> {
private final I2PAppContext _context;
private long _expireTime;

View File

@ -11,7 +11,7 @@ import net.i2p.crypto.SHA1Hash;
* @since 0.8.4
* @author zzz
*/
public class InfoHash extends SHA1Hash {
class InfoHash extends SHA1Hash {
public InfoHash(byte[] data) {
super(data);

View File

@ -127,6 +127,7 @@ public class KRPC implements I2PSessionMuxedListener, DHT {
private static final long MAX_NODEINFO_AGE = 60*60*1000;
/** how long since generated do we delete - BEP 5 says 10 minutes */
private static final long MAX_TOKEN_AGE = 60*60*1000;
private static final long MAX_INBOUND_TOKEN_AGE = MAX_TOKEN_AGE - 5*60*1000;
/** how long since sent do we wait for a reply */
private static final long MAX_MSGID_AGE = 2*60*1000;
/** how long since sent do we wait for a reply */
@ -679,6 +680,7 @@ public class KRPC implements I2PSessionMuxedListener, DHT {
// TODO sendQuery with onReply / onTimeout args
/**
* Blocking if repliable and we must lookup b32
* @param repliable true for all but announce
* @return null on error
*/
@ -691,11 +693,19 @@ public class KRPC implements I2PSessionMuxedListener, DHT {
NodeInfo newInfo = _knownNodes.get(nInfo.getNID());
if (newInfo != null && newInfo.getDestination() != null) {
nInfo = newInfo;
} else {
// lookup b32?
} else if (!repliable) {
// Don't lookup for announce query, we should already have it
if (_log.shouldLog(Log.WARN))
_log.warn("No destination for: " + nInfo);
_log.warn("Dropping non-repliable query, no dest for " + nInfo);
return null;
} else {
// Lookup the dest for the hash
// TODO spin off into thread or queue? We really don't want to block here
if (!lookupDest(nInfo)) {
if (_log.shouldLog(Log.WARN))
_log.warn("Dropping repliable query, no dest for " + nInfo);
return null;
}
}
}
map.put("y", "q");
@ -734,7 +744,7 @@ public class KRPC implements I2PSessionMuxedListener, DHT {
} else {
// lookup b32?
if (_log.shouldLog(Log.WARN))
_log.warn("No destination for: " + nInfo);
_log.warn("Dropping response, no dest for " + nInfo);
return false;
}
}
@ -763,7 +773,7 @@ public class KRPC implements I2PSessionMuxedListener, DHT {
} else {
// lookup b32?
if (_log.shouldLog(Log.WARN))
_log.warn("No destination for: " + nInfo);
_log.warn("Dropping sendError, no dest for " + nInfo);
return false;
}
}
@ -772,6 +782,32 @@ public class KRPC implements I2PSessionMuxedListener, DHT {
return sendMessage(nInfo.getDestination(), nInfo.getPort() + 1, map, false);
}
/**
* Get the dest for a NodeInfo lacking it, and store it there.
* Blocking.
* @return success
*/
private boolean lookupDest(NodeInfo nInfo) {
if (_log.shouldLog(Log.INFO))
_log.info("looking up dest for " + nInfo);
try {
// use a short timeout for now
Destination dest = _session.lookupDest(nInfo.getHash(), 5*1000);
if (dest != null) {
nInfo.setDestination(dest);
if (_log.shouldLog(Log.INFO))
_log.info("lookup success for " + nInfo);
return true;
}
} catch (I2PSessionException ise) {
if (_log.shouldLog(Log.WARN))
_log.warn("lookup fail", ise);
}
if (_log.shouldLog(Log.INFO))
_log.info("lookup fail for " + nInfo);
return false;
}
/**
* Lowest-level send message call.
* @param repliable true for all but announce
@ -1345,7 +1381,7 @@ public class KRPC implements I2PSessionMuxedListener, DHT {
}
for (Iterator<Token> iter = _incomingTokens.values().iterator(); iter.hasNext(); ) {
Token tok = iter.next();
if (tok.lastSeen() < now - MAX_TOKEN_AGE)
if (tok.lastSeen() < now - MAX_INBOUND_TOKEN_AGE)
iter.remove();
}
// TODO sent queries?

View File

@ -12,7 +12,7 @@ import net.i2p.data.ByteArray;
* @since 0.8.4
* @author zzz
*/
public class MsgID extends ByteArray {
class MsgID extends ByteArray {
private static final int MY_TOK_LEN = 8;

View File

@ -11,7 +11,7 @@ import net.i2p.crypto.SHA1Hash;
* @since 0.8.4
* @author zzz
*/
public class NID extends SHA1Hash {
class NID extends SHA1Hash {
public NID(byte[] data) {
super(data);

View File

@ -21,7 +21,7 @@ import net.i2p.data.SimpleDataStructure;
* @author zzz
*/
public class NodeInfo extends SimpleDataStructure {
class NodeInfo extends SimpleDataStructure {
private long lastSeen;
private NID nID;

View File

@ -12,7 +12,7 @@ import net.i2p.data.Hash;
* @since 0.8.4
* @author zzz
*/
public class Peer extends Hash {
class Peer extends Hash {
private long lastSeen;

View File

@ -13,7 +13,7 @@ import net.i2p.data.Hash;
* @since 0.8.4
* @author zzz
*/
public class Peers extends ConcurrentHashMap<Hash, Peer> {
class Peers extends ConcurrentHashMap<Hash, Peer> {
public Peers() {
super();

View File

@ -15,7 +15,7 @@ import net.i2p.data.DataHelper;
* @since 0.8.4
* @author zzz
*/
public class Token extends ByteArray {
class Token extends ByteArray {
private static final int MY_TOK_LEN = 8;
private final long lastSeen;

View File

@ -12,7 +12,7 @@ import net.i2p.data.DataHelper;
* @since 0.8.4
* @author zzz
*/
public class TokenKey extends SHA1Hash {
class TokenKey extends SHA1Hash {
public TokenKey(NID nID, InfoHash ih) {
super(DataHelper.xor(nID.getData(), ih.getData()));

View File

@ -11,7 +11,7 @@ import java.util.concurrent.ConcurrentHashMap;
* @since 0.8.4
* @author zzz
*/
public class Torrents extends ConcurrentHashMap<InfoHash, Peers> {
class Torrents extends ConcurrentHashMap<InfoHash, Peers> {
public Torrents() {
super();