* Transport:

- Update addressses before publishing
      - Increment address cost if near capacity
      - Synchronize notifyReplaceAddress()
This commit is contained in:
zzz
2010-02-15 04:08:02 +00:00
parent 49a6cdbda6
commit 1d3f0fe96c
6 changed files with 62 additions and 5 deletions

View File

@ -256,7 +256,7 @@ public class CommSystemFacadeImpl extends CommSystemFacade {
* This should really be moved to ntcp/NTCPTransport.java, why is it here?
*/
@Override
public void notifyReplaceAddress(RouterAddress UDPAddr) {
public synchronized void notifyReplaceAddress(RouterAddress UDPAddr) {
if (UDPAddr == null)
return;
NTCPTransport t = (NTCPTransport) _manager.getTransport(NTCPTransport.STYLE);
@ -348,7 +348,21 @@ public class CommSystemFacadeImpl extends CommSystemFacade {
}
if (!changed) {
_log.warn("No change to NTCP Address");
if (oldAddr != null) {
int oldCost = oldAddr.getCost();
int newCost = NTCPAddress.DEFAULT_COST;
if (TransportImpl.ADJUST_COST && !t.haveCapacity())
newCost++;
if (newCost != oldCost) {
oldAddr.setCost(newCost);
if (_log.shouldLog(Log.WARN))
_log.warn("Changing NTCP cost from " + oldCost + " to " + newCost);
} else {
_log.warn("No change to NTCP Address");
}
} else {
_log.warn("No change to NTCP Address");
}
return;
}

View File

@ -35,6 +35,7 @@ public interface Transport {
public RouterAddress startListening();
public void stopListening();
public RouterAddress getCurrentAddress();
public RouterAddress updateAddress();
public static final String SOURCE_UPNP = "upnp";
public static final String SOURCE_INTERFACE = "local";
public static final String SOURCE_CONFIG = "config"; // unused

View File

@ -32,6 +32,7 @@ import net.i2p.router.MessageSelector;
import net.i2p.router.OutNetMessage;
import net.i2p.router.Router;
import net.i2p.router.RouterContext;
import net.i2p.router.RouterVersion;
import net.i2p.router.networkdb.kademlia.FloodfillNetworkDatabaseFacade;
import net.i2p.util.ConcurrentHashSet;
import net.i2p.util.Log;
@ -420,10 +421,23 @@ public abstract class TransportImpl implements Transport {
}
}
/** To protect dev anonymity. Set to true after 0.7.12 is out */
public static final boolean ADJUST_COST = !RouterVersion.VERSION.equals("0.7.11");
/** What addresses are we currently listening to? */
public RouterAddress getCurrentAddress() {
return _currentAddress;
}
/**
* Ask the transport to update its address based on current information and return it
* Transports should override.
* @since 0.7.12
*/
public RouterAddress updateAddress() {
return _currentAddress;
}
/**
* Replace any existing addresses for the current transport with the given
* one.
@ -433,8 +447,6 @@ public abstract class TransportImpl implements Transport {
_currentAddress = address;
if (_listener != null)
_listener.transportAddressChanged();
if ("SSU".equals(getStyle()))
_context.commSystem().notifyReplaceAddress(address);
}
/**

View File

@ -323,8 +323,14 @@ public class TransportManager implements TransportEventListener {
return TransportImpl.getIP(dest);
}
/**
* This forces a rebuild
*/
public Map<String, RouterAddress> getAddresses() {
Map<String, RouterAddress> rv = new HashMap(_transports.size());
// do this first since SSU may force a NTCP change
for (Transport t : _transports)
t.updateAddress();
for (Transport t : _transports) {
if (t.getCurrentAddress() != null)
rv.put(t.getStyle(), t.getCurrentAddress());

View File

@ -547,6 +547,7 @@ public class NTCPTransport extends TransportImpl {
if (ctx == null) {
System.err.println("NIO transport has no context?");
} else {
// this generally returns null -- see javadoc
RouterAddress ra = CommSystemFacadeImpl.createNTCPAddress(ctx);
if (ra != null) {
NTCPAddress addr = new NTCPAddress(ra);

View File

@ -1164,6 +1164,17 @@ public class UDPTransport extends TransportImpl implements TimedWeightedPriority
return (_context.getProperty(PROP_EXTERNAL_HOST) != null);
}
/**
* Rebuild to get updated cost and introducers.
* Do not tell the router (he is the one calling this)
* @since 0.7.12
*/
@Override
public RouterAddress updateAddress() {
rebuildExternalAddress(false);
return getCurrentAddress();
}
void rebuildExternalAddress() { rebuildExternalAddress(true); }
void rebuildExternalAddress(boolean allowRebuildRouterInfo) {
// if the external port is specified, we want to use that to bind to even
@ -1216,7 +1227,10 @@ public class UDPTransport extends TransportImpl implements TimedWeightedPriority
options.setProperty(UDPAddress.PROP_INTRO_KEY, _introKey.toBase64());
RouterAddress addr = new RouterAddress();
addr.setCost(DEFAULT_COST);
if (ADJUST_COST && !haveCapacity())
addr.setCost(DEFAULT_COST + 1);
else
addr.setCost(DEFAULT_COST);
addr.setExpiration(null);
addr.setTransportStyle(STYLE);
addr.setOptions(options);
@ -1241,6 +1255,15 @@ public class UDPTransport extends TransportImpl implements TimedWeightedPriority
}
}
/**
* Replace then tell NTCP that we changed.
*/
@Override
protected void replaceAddress(RouterAddress address) {
super.replaceAddress(address);
_context.commSystem().notifyReplaceAddress(address);
}
protected void replaceAddress(RouterAddress address, RouterAddress oldAddress) {
replaceAddress(address);
if (oldAddress != null) {