forked from I2P_Developers/i2p.i2p
Router: Store router ident and hash at startup
so ctx.routerHash() can be lockless
This commit is contained in:
@ -43,6 +43,7 @@ import net.i2p.data.SigningPrivateKey;
|
||||
import net.i2p.data.SigningPublicKey;
|
||||
import net.i2p.data.i2np.GarlicMessage;
|
||||
import net.i2p.data.router.RouterAddress;
|
||||
import net.i2p.data.router.RouterIdentity;
|
||||
import net.i2p.data.router.RouterInfo;
|
||||
import net.i2p.router.CommSystemFacade.Status;
|
||||
import net.i2p.router.crypto.FamilyKeyCrypto;
|
||||
@ -94,6 +95,8 @@ public class Router implements RouterClock.ClockShiftListener {
|
||||
private String _configFilename;
|
||||
private RouterInfo _routerInfo;
|
||||
private final ReentrantReadWriteLock _routerInfoLock = new ReentrantReadWriteLock(false);
|
||||
private RouterIdentity _routerIdent;
|
||||
private Hash _routerHash;
|
||||
/** not for external use */
|
||||
public final Object routerInfoFileLock = new Object();
|
||||
private final Object _configFileLock = new Object();
|
||||
@ -583,6 +586,26 @@ public class Router implements RouterClock.ClockShiftListener {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Our current router identity.
|
||||
* Warning, may be null if called very early.
|
||||
* Lockless.
|
||||
* @since 0.9.67
|
||||
*/
|
||||
public RouterIdentity getRouterIdentity() {
|
||||
return _routerIdent;
|
||||
}
|
||||
|
||||
/**
|
||||
* Our current router hash.
|
||||
* Warning, may be null if called very early.
|
||||
* Lockless.
|
||||
* @since 0.9.67
|
||||
*/
|
||||
public Hash getRouterHash() {
|
||||
return _routerHash;
|
||||
}
|
||||
|
||||
/**
|
||||
* Caller must ensure info is valid - no validation done here.
|
||||
* Not for external use.
|
||||
@ -592,6 +615,12 @@ public class Router implements RouterClock.ClockShiftListener {
|
||||
*/
|
||||
public void setRouterInfo(RouterInfo info) {
|
||||
_routerInfoLock.writeLock().lock();
|
||||
if (!info.getIdentity().equals(_routerIdent)) {
|
||||
if (_routerIdent != null) // shouldn't happen
|
||||
_log.log(Log.CRIT, "Changing router ident while running");
|
||||
_routerIdent = info.getIdentity();
|
||||
_routerHash = _routerIdent.calculateHash();
|
||||
}
|
||||
try {
|
||||
_routerInfo = info;
|
||||
} finally {
|
||||
|
@ -316,17 +316,12 @@ public class RouterContext extends I2PAppContext {
|
||||
* Convenience method for getting the router hash.
|
||||
* Equivalent to context.router().getRouterInfo().getIdentity().getHash()
|
||||
*
|
||||
* Warning - risk of deadlock - do not call while holding locks
|
||||
*
|
||||
* @return may be null if called very early
|
||||
*/
|
||||
public Hash routerHash() {
|
||||
if (_router == null)
|
||||
return null;
|
||||
RouterInfo ri = _router.getRouterInfo();
|
||||
if (ri == null)
|
||||
return null;
|
||||
return ri.getIdentity().getHash();
|
||||
return _router.getRouterHash();
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user