- Add floodfillEnabled() to NetworkDatabaseFacade so callers don't
    have to cast to FNDF, and Dummy NDF will work
  - Remove static FNDF.floodfillEnabled()
* SSUDemo: cleanups
This commit is contained in:
zzz
2013-05-17 15:11:23 +00:00
parent a374f00613
commit be262c6a70
12 changed files with 48 additions and 28 deletions

View File

@ -77,4 +77,13 @@ public abstract class NetworkDatabaseFacade implements Service {
/** @since 0.9 */
public ReseedChecker reseedChecker() { return null; };
/**
* For convenience, so users don't have to cast to FNDF, and unit tests using
* Dummy NDF will work.
*
* @return false; FNDF overrides to return actual setting
* @since IPv6
*/
public boolean floodfillEnabled() { return false; };
}

View File

@ -601,7 +601,7 @@ public class Router implements RouterClock.ClockShiftListener {
}
// if prop set to true, don't tell people we are ff even if we are
if (FloodfillNetworkDatabaseFacade.floodfillEnabled(_context) &&
if (_context.netDb().floodfillEnabled() &&
!_context.getBooleanProperty("router.hideFloodfillParticipant"))
ri.addCapability(FloodfillNetworkDatabaseFacade.CAPABILITY_FLOODFILL);

View File

@ -38,7 +38,7 @@ class ExploreKeySelectorJob extends JobImpl {
public String getName() { return "Explore Key Selector Job"; }
public void runJob() {
if (((FloodfillNetworkDatabaseFacade)_facade).floodfillEnabled()) {
if (_facade.floodfillEnabled()) {
requeue(30*RERUN_DELAY_MS);
return;
}

View File

@ -263,10 +263,8 @@ public class FloodfillNetworkDatabaseFacade extends KademliaNetworkDatabaseFacad
}
}
@Override
public boolean floodfillEnabled() { return _floodfillEnabled; }
public static boolean floodfillEnabled(RouterContext ctx) {
return ((FloodfillNetworkDatabaseFacade)ctx.netDb()).floodfillEnabled();
}
/**
* @param peer may be null, returns false if null

View File

@ -38,7 +38,7 @@ public class HandleFloodfillDatabaseLookupMessageJob extends HandleDatabaseLooku
*/
@Override
protected boolean answerAllQueries() {
if (!FloodfillNetworkDatabaseFacade.floodfillEnabled(getContext())) return false;
if (!getContext().netDb().floodfillEnabled()) return false;
return FloodfillNetworkDatabaseFacade.isFloodfill(getContext().router().getRouterInfo());
}
@ -52,7 +52,7 @@ public class HandleFloodfillDatabaseLookupMessageJob extends HandleDatabaseLooku
super.sendClosest(key, routerInfoSet, toPeer, replyTunnel);
// go away, you got the wrong guy, send our RI back unsolicited
if (!FloodfillNetworkDatabaseFacade.floodfillEnabled(getContext())) {
if (!getContext().netDb().floodfillEnabled()) {
// We could just call sendData(myhash, myri, toPeer, replyTunnel) but
// that would increment the netDb.lookupsHandled and netDb.lookupsMatched stats
DatabaseStoreMessage msg = new DatabaseStoreMessage(getContext());

View File

@ -187,7 +187,7 @@ public class HandleFloodfillDatabaseStoreMessageJob extends JobImpl {
// flood it
if (invalidMessage == null &&
FloodfillNetworkDatabaseFacade.floodfillEnabled(getContext()) &&
getContext().netDb().floodfillEnabled() &&
_message.getReplyToken() > 0) {
if (wasNew) {
// DOS prevention

View File

@ -736,7 +736,7 @@ public class KademliaNetworkDatabaseFacade extends NetworkDatabaseFacade {
// As the net grows this won't be sufficient, and we'll have to implement
// flushing some from memory, while keeping all on disk.
long adjustedExpiration;
if (FloodfillNetworkDatabaseFacade.floodfillEnabled(_context))
if (floodfillEnabled())
adjustedExpiration = ROUTER_INFO_EXPIRATION_FLOODFILL;
else
// _kb.size() includes leasesets but that's ok

View File

@ -134,7 +134,7 @@ class SearchJob extends JobImpl {
// The other two places this was called (one below and one in FNDF)
// have been commented out.
// Returning false essentially enables kademlia as a backup to floodfill for search responses.
if (FloodfillNetworkDatabaseFacade.floodfillEnabled(ctx))
if (ctx.netDb().floodfillEnabled())
return false;
return ctx.getProperty("netDb.floodfillOnly", DEFAULT_FLOODFILL_ONLY);
}

View File

@ -51,7 +51,7 @@ class StartExplorersJob extends JobImpl {
public String getName() { return "Start Explorers Job"; }
public void runJob() {
if (! (((FloodfillNetworkDatabaseFacade)_facade).floodfillEnabled() ||
if (! (_facade.floodfillEnabled() ||
getContext().router().gracefulShutdownInProgress())) {
int num = MAX_PER_RUN;
if (_facade.getDataStore().size() < LOW_ROUTERS)
@ -93,7 +93,7 @@ class StartExplorersJob extends JobImpl {
*/
private long getNextRunDelay() {
// we don't explore if floodfill
if (((FloodfillNetworkDatabaseFacade)_facade).floodfillEnabled())
if (_facade.floodfillEnabled())
return MAX_RERUN_DELAY_MS;
// If we don't know too many peers, or just started, explore aggressively

View File

@ -39,7 +39,6 @@ import net.i2p.router.MessageSelector;
import net.i2p.router.OutNetMessage;
import net.i2p.router.Router;
import net.i2p.router.RouterContext;
import net.i2p.router.networkdb.kademlia.FloodfillNetworkDatabaseFacade;
import net.i2p.util.ConcurrentHashSet;
import net.i2p.util.LHMCache;
import net.i2p.util.Log;
@ -145,7 +144,7 @@ public abstract class TransportImpl implements Transport {
if (ri != null) {
char bw = ri.getBandwidthTier().charAt(0);
if (bw != 'U' &&
! ((FloodfillNetworkDatabaseFacade)_context.netDb()).floodfillEnabled())
!_context.netDb().floodfillEnabled())
def = MAX_CONNECTION_FACTOR * (1 + bw - Router.CAPABILITY_BW12);
}
// increase limit for SSU, for now

View File

@ -34,7 +34,6 @@ import net.i2p.router.CommSystemFacade;
import net.i2p.router.OutNetMessage;
import net.i2p.router.Router;
import net.i2p.router.RouterContext;
import net.i2p.router.networkdb.kademlia.FloodfillNetworkDatabaseFacade;
import net.i2p.router.transport.Transport;
import static net.i2p.router.transport.Transport.AddressSource.*;
import net.i2p.router.transport.TransportBid;
@ -1907,7 +1906,7 @@ public class UDPTransport extends TransportImpl implements TimedWeightedPriority
(!_context.router().isHidden()) &&
(!introducersRequired()) &&
haveCapacity() &&
(!((FloodfillNetworkDatabaseFacade)_context.netDb()).floodfillEnabled()) &&
(!_context.netDb().floodfillEnabled()) &&
_introManager.introducedCount() < IntroductionManager.MAX_OUTBOUND &&
_introManager.introducedCount() < getMaxConnections() / 4;
}

View File

@ -32,6 +32,7 @@ public class SSUDemo {
}
public SSUDemo() {}
public void run() {
String cfgFile = "router.config";
Properties envProps = getEnv();
@ -50,10 +51,10 @@ public class SSUDemo {
loadPeers();
}
private Properties getEnv() {
private static Properties getEnv() {
Properties envProps = System.getProperties();
// disable the TCP transport, as its deprecated
envProps.setProperty("i2np.tcp.disable", "true");
// disable the NTCP transport
envProps.setProperty("i2np.ntcp.enable", "false");
// we want SNTP synchronization for replay prevention
envProps.setProperty("time.disabled", "false");
// allow 127.0.0.1/10.0.0.1/etc (useful for testing). If this is false,
@ -73,11 +74,11 @@ public class SSUDemo {
envProps.setProperty("i2p.dummyTunnelManager", "true");
// set to false if you want to use HMAC-SHA256-128 instead of HMAC-MD5-128 as
// the SSU MAC
envProps.setProperty("i2p.HMACMD5", "true");
//envProps.setProperty("i2p.HMACMD5", "true");
// if you're using the HMAC MD5, by default it will use a 32 byte MAC field,
// which is a bug, as it doesn't generate the same values as a 16 byte MAC field.
// set this to false if you don't want the bug
envProps.setProperty("i2p.HMACBrokenSize", "false");
//envProps.setProperty("i2p.HMACBrokenSize", "false");
// no need to include any stats in the routerInfo we send to people on SSU
// session establishment
envProps.setProperty("router.publishPeerRankings", "false");
@ -98,9 +99,9 @@ public class SSUDemo {
}
/** random place for storing router info files - written as $dir/base64(SHA256(info.getIdentity)) */
private File getInfoDir() { return new File("/tmp/ssuDemoInfo/"); }
private static File getInfoDir() { return new File("/tmp/ssuDemoInfo/"); }
private void storeMyInfo(RouterInfo info) {
private static void storeMyInfo(RouterInfo info) {
File infoDir = getInfoDir();
if (!infoDir.exists())
infoDir.mkdirs();
@ -198,16 +199,20 @@ public class SSUDemo {
public FooJobBuilder() {
I2NPMessageImpl.registerBuilder(new FooBuilder(), FooMessage.MESSAGE_TYPE);
}
public Job createJob(I2NPMessage receivedMessage, RouterIdentity from, Hash fromHash) {
return new FooHandleJob(_us, receivedMessage, from, fromHash);
}
}
private class FooHandleJob extends JobImpl {
private I2NPMessage _msg;
private static class FooHandleJob extends JobImpl {
private final I2NPMessage _msg;
public FooHandleJob(RouterContext ctx, I2NPMessage receivedMessage, RouterIdentity from, Hash fromHash) {
super(ctx);
_msg = receivedMessage;
}
public void runJob() {
// we know its a FooMessage, since thats the type of message that the handler
// is registered as
@ -216,27 +221,33 @@ public class SSUDemo {
}
public String getName() { return "Handle Foo message"; }
}
private class FooBuilder implements I2NPMessageImpl.Builder {
private static class FooBuilder implements I2NPMessageImpl.Builder {
public I2NPMessage build(I2PAppContext ctx) { return new FooMessage(ctx, null); }
}
/**
* Just carry some data...
*/
class FooMessage extends I2NPMessageImpl {
private static class FooMessage extends I2NPMessageImpl {
private byte[] _data;
public static final int MESSAGE_TYPE = 17;
public FooMessage(I2PAppContext ctx, byte data[]) {
super(ctx);
_data = data;
}
/** pull the read data off */
public byte[] getData() { return _data; }
/** specify the payload to be sent */
public void setData(byte data[]) { _data = data; }
public int getType() { return MESSAGE_TYPE; }
protected int calculateWrittenLength() { return _data.length; }
public void readMessage(byte[] data, int offset, int dataSize, int type) throws I2NPMessageException {
_data = new byte[dataSize];
System.arraycopy(data, offset, _data, 0, dataSize);
@ -260,12 +271,15 @@ public class SSUDemo {
return new HandleJob(_us, receivedMessage, from, fromHash);
}
}
private class HandleJob extends JobImpl {
private I2NPMessage _msg;
private final I2NPMessage _msg;
public HandleJob(RouterContext ctx, I2NPMessage receivedMessage, RouterIdentity from, Hash fromHash) {
super(ctx);
_msg = receivedMessage;
}
public void runJob() {
// we know its a DatabaseStoreMessage, since thats the type of message that the handler
// is registered as
@ -276,6 +290,7 @@ public class SSUDemo {
iae.printStackTrace();
}
}
public String getName() { return "Handle netDb store"; }
}
}