RouterInfo, RouterAddress: Optimizations and integrity checks

- Remove synchronization
    - Do not allow contents to change after being set, throw IllegalStateException
    - Do not copy contents out in getters
    - Make options final
    - Add getOption() and getOptionsMap() methods
This commit is contained in:
zzz
2011-12-23 21:41:58 +00:00
parent 36cb07b0cc
commit 8fa720539a
13 changed files with 215 additions and 153 deletions

View File

@@ -488,9 +488,7 @@ public class Blocklist {
for (int j = 0; j < paddr.size(); j++) {
RouterAddress pa = (RouterAddress) pladdr.get(j);
if (pa == null) continue;
Properties pprops = pa.getOptions();
if (pprops == null) continue;
String phost = pprops.getProperty("host");
String phost = pa.getOption("host");
if (phost == null) continue;
if (oldphost != null && oldphost.equals(phost)) continue;
oldphost = phost;

View File

@@ -41,7 +41,7 @@ public class PublishLocalRouterInfoJob extends JobImpl {
RouterInfo ri = new RouterInfo(getContext().router().getRouterInfo());
if (_log.shouldLog(Log.DEBUG))
_log.debug("Old routerInfo contains " + ri.getAddresses().size()
+ " addresses and " + ri.getOptions().size() + " options");
+ " addresses and " + ri.getOptionsMap().size() + " options");
Properties stats = getContext().statPublisher().publishStatistics();
stats.setProperty(RouterInfo.PROP_NETWORK_ID, ""+Router.NETWORK_ID);
try {
@@ -60,7 +60,7 @@ public class PublishLocalRouterInfoJob extends JobImpl {
getContext().router().setRouterInfo(ri);
if (_log.shouldLog(Log.INFO))
_log.info("Newly updated routerInfo is published with " + stats.size()
+ "/" + ri.getOptions().size() + " options on "
+ "/" + ri.getOptionsMap().size() + " options on "
+ new Date(ri.getPublished()));
try {
getContext().netDb().publish(ri);

View File

@@ -133,8 +133,7 @@ class FloodfillMonitorJob extends JobImpl {
if (ra == null)
happy = false;
else {
Properties props = ra.getOptions();
if (props == null || props.getProperty("ihost0") != null)
if (ra.getOption("ihost0") != null)
happy = false;
}
}

View File

@@ -786,8 +786,7 @@ public class KademliaNetworkDatabaseFacade extends NetworkDatabaseFacade {
RouterAddress ra = routerInfo.getTargetAddress("SSU");
if (ra != null) {
// Introducers change often, introducee will ping introducer for 2 hours
Properties props = ra.getOptions();
if (props != null && props.getProperty("ihost0") != null)
if (ra.getOption("ihost0") != null)
return "Peer " + key.toBase64() + " published > 75m ago with SSU Introducers";
if (routerInfo.getTargetAddress("NTCP") == null)
return "Peer " + key.toBase64() + " published > 75m ago, SSU only without introducers";
@@ -824,7 +823,7 @@ public class KademliaNetworkDatabaseFacade extends NetworkDatabaseFacade {
if (_log.shouldLog(Log.DEBUG))
_log.debug("RouterInfo " + key.toBase64() + " is stored with "
+ routerInfo.getOptions().size() + " options on "
+ routerInfo.getOptionsMap().size() + " options on "
+ new Date(routerInfo.getPublished()));
_context.peerManager().setCapabilities(key, routerInfo.getCapabilities());

View File

@@ -701,8 +701,7 @@ public class ProfileOrganizer {
continue;
}
// This is the quick way of doing UDPAddress.getIntroducerCount() > 0
Properties props = ra.getOptions();
if (props != null && props.getProperty("ihost0") != null)
if (ra.getOption("ihost0") != null)
l.add(peer);
}
}
@@ -1263,9 +1262,7 @@ public class ProfileOrganizer {
if (paddr == null)
return rv;
for (RouterAddress pa : paddr) {
Properties pprops = pa.getOptions();
if (pprops == null) continue;
String phost = pprops.getProperty("host");
String phost = pa.getOption("host");
if (phost == null) continue;
InetAddress pi;
try {

View File

@@ -343,15 +343,12 @@ public class TransportManager implements TransportEventListener {
for (Transport t : _transports.values()) {
int port = t.getRequestedPort();
if (t.getCurrentAddress() != null) {
Properties opts = t.getCurrentAddress().getOptions();
if (opts != null) {
String s = opts.getProperty("port");
String s = t.getCurrentAddress().getOption("port");
if (s != null) {
try {
port = Integer.parseInt(s);
} catch (NumberFormatException nfe) {}
}
}
}
// Use UDP port for NTCP too - see comment in NTCPTransport.getRequestedPort() for why this is here
if (t.getStyle().equals(NTCPTransport.STYLE) && port <= 0 &&

View File

@@ -57,13 +57,13 @@ public class NTCPAddress {
_port = -1;
return;
}
String host = addr.getOptions().getProperty(PROP_HOST);
String host = addr.getOption(PROP_HOST);
if (host == null) {
_host = null;
_port = -1;
} else {
_host = host.trim();
String port = addr.getOptions().getProperty(PROP_PORT);
String port = addr.getOption(PROP_PORT);
if ( (port != null) && (port.trim().length() > 0) && !("null".equals(port)) ) {
try {
_port = Integer.parseInt(port.trim());
@@ -156,9 +156,7 @@ public class NTCPAddress {
public boolean equals(RouterAddress addr) {
if (addr == null) return false;
Properties opts = addr.getOptions();
if (opts == null) return false;
return ( (_host.equals(opts.getProperty(PROP_HOST))) &&
(Integer.toString(_port).equals(opts.getProperty(PROP_PORT))) );
return ( (_host.equals(addr.getOption(PROP_HOST))) &&
(Integer.toString(_port).equals(addr.getOption(PROP_PORT))) );
}
}

View File

@@ -64,31 +64,30 @@ public class UDPAddress {
private void parse(RouterAddress addr) {
if (addr == null) return;
Properties opts = addr.getOptions();
_host = opts.getProperty(PROP_HOST);
_host = addr.getOption(PROP_HOST);
if (_host != null) _host = _host.trim();
try {
String port = opts.getProperty(PROP_PORT);
String port = addr.getOption(PROP_PORT);
if (port != null)
_port = Integer.parseInt(port);
} catch (NumberFormatException nfe) {
_port = -1;
}
String key = opts.getProperty(PROP_INTRO_KEY);
String key = addr.getOption(PROP_INTRO_KEY);
if (key != null)
_introKey = Base64.decode(key.trim());
for (int i = MAX_INTRODUCERS; i >= 0; i--) {
String host = opts.getProperty(PROP_INTRO_HOST_PREFIX + i);
String host = addr.getOption(PROP_INTRO_HOST_PREFIX + i);
if (host == null) continue;
String port = opts.getProperty(PROP_INTRO_PORT_PREFIX + i);
String port = addr.getOption(PROP_INTRO_PORT_PREFIX + i);
if (port == null) continue;
String k = opts.getProperty(PROP_INTRO_KEY_PREFIX + i);
String k = addr.getOption(PROP_INTRO_KEY_PREFIX + i);
if (k == null) continue;
byte ikey[] = Base64.decode(k);
if ( (ikey == null) || (ikey.length != SessionKey.KEYSIZE_BYTES) )
continue;
String t = opts.getProperty(PROP_INTRO_TAG_PREFIX + i);
String t = addr.getOption(PROP_INTRO_TAG_PREFIX + i);
if (t == null) continue;
int p = -1;
try {