handle repeated attempts to close() gracefully

logging, formatting
This commit is contained in:
jrandom
2004-04-17 00:04:38 +00:00
committed by zzz
parent 86759d2f9c
commit a859908e83

View File

@ -164,11 +164,10 @@ abstract class I2PSessionImpl implements I2PSession, I2CPMessageReader.I2CPMessa
if (_log.shouldLog(Log.DEBUG)) _log.debug("Skipping line.* property: " + key); if (_log.shouldLog(Log.DEBUG)) _log.debug("Skipping line.* property: " + key);
} else if ((key.length() > 255) || (val.length() > 255)) { } else if ((key.length() > 255) || (val.length() > 255)) {
if (_log.shouldLog(Log.WARN)) if (_log.shouldLog(Log.WARN))
_log _log.warn("Not passing on property ["
.warn("Not passing on property [" + key
+ key + "] in the session configuration as the value is too long (max = 255): "
+ "] in the session configuration as the value is too long (max = 255): " + val);
+ val);
} else { } else {
rv.setProperty(key, val); rv.setProperty(key, val);
} }
@ -293,15 +292,14 @@ abstract class I2PSessionImpl implements I2PSession, I2CPMessageReader.I2CPMessa
*/ */
public abstract boolean sendMessage(Destination dest, byte[] payload) throws I2PSessionException; public abstract boolean sendMessage(Destination dest, byte[] payload) throws I2PSessionException;
public abstract boolean sendMessage(Destination dest, byte[] payload, SessionKey keyUsed, Set tagsSent) public abstract boolean sendMessage(Destination dest, byte[] payload, SessionKey keyUsed,
throws I2PSessionException; Set tagsSent) throws I2PSessionException;
public abstract void receiveStatus(int msgId, long nonce, int status); public abstract void receiveStatus(int msgId, long nonce, int status);
protected boolean isGuaranteed() { protected boolean isGuaranteed() {
return I2PClient.PROP_RELIABILITY_GUARANTEED String reliability = _options.getProperty(I2PClient.PROP_RELIABILITY, I2PClient.PROP_RELIABILITY_GUARANTEED);
.equals(_options.getProperty(I2PClient.PROP_RELIABILITY, return I2PClient.PROP_RELIABILITY_GUARANTEED.equals(reliability);
I2PClient.PROP_RELIABILITY_GUARANTEED));
} }
protected static final Set createNewTags(int num) { protected static final Set createNewTags(int num) {
@ -320,13 +318,14 @@ abstract class I2PSessionImpl implements I2PSession, I2CPMessageReader.I2CPMessa
byte data[] = msg.getPayload().getUnencryptedData(); byte data[] = msg.getPayload().getUnencryptedData();
if ((data == null) || (data.length <= 0)) { if ((data == null) || (data.length <= 0)) {
if (_log.shouldLog(Log.ERROR)) if (_log.shouldLog(Log.ERROR))
_log.error("addNewMessage of a message with no unencrypted data", _log.error("addNewMessage of a message with no unencrypted data",
new Exception("Empty message")); new Exception("Empty message"));
} else { } else {
final long size = data.length; final long size = data.length;
Thread notifier = new I2PThread(new Runnable() { Thread notifier = new I2PThread(new Runnable() {
public void run() { public void run() {
if (_sessionListener != null) _sessionListener.messageAvailable(I2PSessionImpl.this, id, size); if (_sessionListener != null)
_sessionListener.messageAvailable(I2PSessionImpl.this, id, size);
} }
}); });
notifier.setName("Notifier [" + _sessionId + "/" + id + "]"); notifier.setName("Notifier [" + _sessionId + "/" + id + "]");
@ -343,12 +342,12 @@ abstract class I2PSessionImpl implements I2PSession, I2CPMessageReader.I2CPMessa
I2CPMessageHandler handler = I2PClientMessageHandlerMap.getHandler(message.getType()); I2CPMessageHandler handler = I2PClientMessageHandlerMap.getHandler(message.getType());
if (handler == null) { if (handler == null) {
if (_log.shouldLog(Log.WARN)) if (_log.shouldLog(Log.WARN))
_log.warn("Unknown message or unhandleable message received: type = " _log.warn("Unknown message or unhandleable message received: type = "
+ message.getType()); + message.getType());
} else { } else {
if (_log.shouldLog(Log.DEBUG)) if (_log.shouldLog(Log.DEBUG))
_log.debug("Message received of type " + message.getType() _log.debug("Message received of type " + message.getType()
+ " to be handled by " + handler); + " to be handled by " + handler);
handler.handleMessage(message, this); handler.handleMessage(message, this);
} }
} }
@ -365,61 +364,39 @@ abstract class I2PSessionImpl implements I2PSession, I2CPMessageReader.I2CPMessa
/** /**
* Retrieve the destination of the session * Retrieve the destination of the session
*/ */
public Destination getMyDestination() { public Destination getMyDestination() { return _myDestination; }
return _myDestination;
}
/** /**
* Retrieve the decryption PrivateKey * Retrieve the decryption PrivateKey
*/ */
public PrivateKey getDecryptionKey() { public PrivateKey getDecryptionKey() { return _privateKey; }
return _privateKey;
}
/** /**
* Retrieve the signing SigningPrivateKey * Retrieve the signing SigningPrivateKey
*/ */
public SigningPrivateKey getPrivateKey() { public SigningPrivateKey getPrivateKey() { return _signingPrivateKey; }
return _signingPrivateKey;
}
/** /**
* Retrieve the helper that generates I2CP messages * Retrieve the helper that generates I2CP messages
*/ */
I2CPMessageProducer getProducer() { I2CPMessageProducer getProducer() { return _producer; }
return _producer;
}
/** /**
* Retrieve the configuration options * Retrieve the configuration options
*/ */
Properties getOptions() { Properties getOptions() { return _options; }
return _options;
}
/** /**
* Retrieve the session's ID * Retrieve the session's ID
*/ */
SessionId getSessionId() { SessionId getSessionId() { return _sessionId; }
return _sessionId; void setSessionId(SessionId id) { _sessionId = id; }
}
/**
* Configure the session's ID
*/
void setSessionId(SessionId id) {
_sessionId = id;
}
/** configure the listener */ /** configure the listener */
public void setSessionListener(I2PSessionListener lsnr) { public void setSessionListener(I2PSessionListener lsnr) { _sessionListener = lsnr; }
_sessionListener = lsnr;
}
/** has the session been closed (or not yet connected)? */ /** has the session been closed (or not yet connected)? */
public boolean isClosed() { public boolean isClosed() { return _closed; }
return _closed;
}
/** /**
* Deliver an I2CP message to the router * Deliver an I2CP message to the router
@ -458,6 +435,8 @@ abstract class I2PSessionImpl implements I2PSession, I2CPMessageReader.I2CPMessa
} }
public void destroySession(boolean sendDisconnect) { public void destroySession(boolean sendDisconnect) {
if (_closed) return;
if (_log.shouldLog(Log.DEBUG)) _log.debug("Destroy the session", new Exception("DestroySession()")); if (_log.shouldLog(Log.DEBUG)) _log.debug("Destroy the session", new Exception("DestroySession()"));
_closed = true; _closed = true;
if (sendDisconnect) { if (sendDisconnect) {
@ -501,20 +480,22 @@ abstract class I2PSessionImpl implements I2PSession, I2CPMessageReader.I2CPMessa
} }
protected void disconnect() { protected void disconnect() {
if (_closed) return;
if (_log.shouldLog(Log.DEBUG)) _log.debug("Disconnect() called", new Exception("Disconnect")); if (_log.shouldLog(Log.DEBUG)) _log.debug("Disconnect() called", new Exception("Disconnect"));
if (shouldReconnect()) { if (shouldReconnect()) {
if (reconnect()) { if (reconnect()) {
if (_log.shouldLog(Log.INFO)) _log.info("I2CP reconnection successful"); if (_log.shouldLog(Log.INFO)) _log.info("I2CP reconnection successful");
return; return;
} else { } else {
_log.error("I2CP reconnection failed"); if (_log.shouldLog(Log.ERROR)) _log.error("I2CP reconnection failed");
} }
} }
_log if (_log.shouldLog(Log.ERROR))
.error("Disconned from the router, and not trying to reconnect further. I hope you're not hoping anything else will happen"); _log.error("Disconned from the router, and not trying to reconnect further. I hope you're not hoping anything else will happen");
if (_sessionListener != null) _sessionListener.disconnected(this); if (_sessionListener != null) _sessionListener.disconnected(this);
_closed = true;
closeSocket(); closeSocket();
} }
@ -531,8 +512,8 @@ abstract class I2PSessionImpl implements I2PSession, I2CPMessageReader.I2CPMessa
_totalReconnectAttempts++; _totalReconnectAttempts++;
} else { } else {
if (_log.shouldLog(Log.CRIT)) if (_log.shouldLog(Log.CRIT))
_log.log(Log.CRIT, "Max number of reconnects exceeded [" _log.log(Log.CRIT, "Max number of reconnects exceeded ["
+ _totalReconnectAttempts + "], we give up!"); + _totalReconnectAttempts + "], we give up!");
return false; return false;
} }
if (_log.shouldLog(Log.INFO)) _log.info("Reconnecting..."); if (_log.shouldLog(Log.INFO)) _log.info("Reconnecting...");