logging & formatting to reduce gc churn

This commit is contained in:
jrandom
2004-04-27 08:41:38 +00:00
committed by zzz
parent 70faecb8b5
commit 34e8db0fe3
5 changed files with 213 additions and 182 deletions

View File

@ -88,7 +88,8 @@ public class DatabaseStoreMessage extends I2NPMessageImpl {
try { try {
_key = new Hash(); _key = new Hash();
_key.readBytes(in); _key.readBytes(in);
_log.debug("Hash read: " + _key.toBase64()); if (_log.shouldLog(Log.DEBUG))
_log.debug("Hash read: " + _key.toBase64());
_type = (int)DataHelper.readLong(in, 1); _type = (int)DataHelper.readLong(in, 1);
if (_type == KEY_TYPE_LEASESET) { if (_type == KEY_TYPE_LEASESET) {
_leaseSet = new LeaseSet(); _leaseSet = new LeaseSet();

View File

@ -1,9 +1,9 @@
package net.i2p.data.i2np; package net.i2p.data.i2np;
/* /*
* free (adj.): unencumbered; not under the control of others * free (adj.): unencumbered; not under the control of others
* Written by jrandom in 2003 and released into the public domain * Written by jrandom in 2003 and released into the public domain
* with no warranty of any kind, either expressed or implied. * with no warranty of any kind, either expressed or implied.
* It probably won't make your computer catch on fire, or eat * It probably won't make your computer catch on fire, or eat
* your children, but it might. Use at your own risk. * your children, but it might. Use at your own risk.
* *
*/ */
@ -23,7 +23,7 @@ import net.i2p.util.Log;
/** /**
* Contains the delivery instructions * Contains the delivery instructions
* *
* @author jrandom * @author jrandom
*/ */
@ -51,15 +51,15 @@ public class DeliveryInstructions extends DataStructureImpl {
private final static long FLAG_MODE = 96; private final static long FLAG_MODE = 96;
private final static long FLAG_DELAY = 16; private final static long FLAG_DELAY = 16;
public DeliveryInstructions() { public DeliveryInstructions() {
setEncrypted(false); setEncrypted(false);
setEncryptionKey(null); setEncryptionKey(null);
setDeliveryMode(-1); setDeliveryMode(-1);
setDestination(null); setDestination(null);
setRouter(null); setRouter(null);
setTunnelId(null); setTunnelId(null);
setDelayRequested(false); setDelayRequested(false);
setDelaySeconds(0); setDelaySeconds(0);
} }
public boolean getEncrypted() { return _encrypted; } public boolean getEncrypted() { return _encrypted; }
@ -80,186 +80,198 @@ public class DeliveryInstructions extends DataStructureImpl {
public void setDelaySeconds(long seconds) { _delaySeconds = seconds; } public void setDelaySeconds(long seconds) { _delaySeconds = seconds; }
public void readBytes(InputStream in) throws DataFormatException, IOException { public void readBytes(InputStream in) throws DataFormatException, IOException {
long flags = DataHelper.readLong(in, 1); long flags = DataHelper.readLong(in, 1);
_log.debug("Read flags: " + flags + " mode: " + flagMode(flags)); if (_log.shouldLog(Log.DEBUG))
_log.debug("Read flags: " + flags + " mode: " + flagMode(flags));
if (flagEncrypted(flags)) {
SessionKey k = new SessionKey(); if (flagEncrypted(flags)) {
k.readBytes(in); SessionKey k = new SessionKey();
setEncryptionKey(k); k.readBytes(in);
setEncrypted(true); setEncryptionKey(k);
} else { setEncrypted(true);
setEncrypted(false); } else {
} setEncrypted(false);
}
setDeliveryMode(flagMode(flags));
switch (flagMode(flags)) { setDeliveryMode(flagMode(flags));
case FLAG_MODE_LOCAL: switch (flagMode(flags)) {
break; case FLAG_MODE_LOCAL:
case FLAG_MODE_DESTINATION: break;
Hash destHash = new Hash(); case FLAG_MODE_DESTINATION:
destHash.readBytes(in); Hash destHash = new Hash();
setDestination(destHash); destHash.readBytes(in);
break; setDestination(destHash);
case FLAG_MODE_ROUTER: break;
Hash routerHash = new Hash(); case FLAG_MODE_ROUTER:
routerHash.readBytes(in); Hash routerHash = new Hash();
setRouter(routerHash); routerHash.readBytes(in);
break; setRouter(routerHash);
case FLAG_MODE_TUNNEL: break;
Hash tunnelRouterHash = new Hash(); case FLAG_MODE_TUNNEL:
tunnelRouterHash.readBytes(in); Hash tunnelRouterHash = new Hash();
setRouter(tunnelRouterHash); tunnelRouterHash.readBytes(in);
TunnelId id = new TunnelId(); setRouter(tunnelRouterHash);
id.readBytes(in); TunnelId id = new TunnelId();
setTunnelId(id); id.readBytes(in);
break; setTunnelId(id);
} break;
}
if (flagDelay(flags)) {
long delay = DataHelper.readLong(in, 4); if (flagDelay(flags)) {
setDelayRequested(true); long delay = DataHelper.readLong(in, 4);
setDelaySeconds(delay); setDelayRequested(true);
} else { setDelaySeconds(delay);
setDelayRequested(false); } else {
} setDelayRequested(false);
}
} }
private boolean flagEncrypted(long flags) { private boolean flagEncrypted(long flags) {
return (0 != (flags & FLAG_ENCRYPTED)); return (0 != (flags & FLAG_ENCRYPTED));
} }
private int flagMode(long flags) { private int flagMode(long flags) {
long v = flags & FLAG_MODE; long v = flags & FLAG_MODE;
v >>>= 5; v >>>= 5;
return (int)v; return (int)v;
} }
private boolean flagDelay(long flags) { private boolean flagDelay(long flags) {
return (0 != (flags & FLAG_DELAY)); return (0 != (flags & FLAG_DELAY));
} }
private long getFlags() { private long getFlags() {
long val = 0L; long val = 0L;
if (getEncrypted()) if (getEncrypted())
val = val | FLAG_ENCRYPTED; val = val | FLAG_ENCRYPTED;
long fmode = 0; long fmode = 0;
switch (getDeliveryMode()) { switch (getDeliveryMode()) {
case FLAG_MODE_LOCAL: case FLAG_MODE_LOCAL:
break; break;
case FLAG_MODE_DESTINATION: case FLAG_MODE_DESTINATION:
fmode = FLAG_MODE_DESTINATION << 5; fmode = FLAG_MODE_DESTINATION << 5;
break; break;
case FLAG_MODE_ROUTER: case FLAG_MODE_ROUTER:
fmode = FLAG_MODE_ROUTER << 5; fmode = FLAG_MODE_ROUTER << 5;
break; break;
case FLAG_MODE_TUNNEL: case FLAG_MODE_TUNNEL:
fmode = FLAG_MODE_TUNNEL << 5; fmode = FLAG_MODE_TUNNEL << 5;
break; break;
} }
val = val | fmode; val = val | fmode;
if (getDelayRequested()) if (getDelayRequested())
val = val | FLAG_DELAY; val = val | FLAG_DELAY;
_log.debug("getFlags() = " + val); _log.debug("getFlags() = " + val);
return val; return val;
} }
private byte[] getAdditionalInfo() throws DataFormatException { private byte[] getAdditionalInfo() throws DataFormatException {
ByteArrayOutputStream baos = new ByteArrayOutputStream(64); ByteArrayOutputStream baos = new ByteArrayOutputStream(64);
try { try {
if (getEncrypted()) { if (getEncrypted()) {
if (_encryptionKey == null) throw new DataFormatException("Encryption key is not set"); if (_encryptionKey == null) throw new DataFormatException("Encryption key is not set");
_encryptionKey.writeBytes(baos); _encryptionKey.writeBytes(baos);
_log.debug("IsEncrypted"); if (_log.shouldLog(Log.DEBUG))
} else { _log.debug("IsEncrypted");
_log.debug("Is NOT Encrypted"); } else {
} if (_log.shouldLog(Log.DEBUG))
switch (getDeliveryMode()) { _log.debug("Is NOT Encrypted");
case FLAG_MODE_LOCAL: }
_log.debug("mode = local"); switch (getDeliveryMode()) {
break; case FLAG_MODE_LOCAL:
case FLAG_MODE_DESTINATION: if (_log.shouldLog(Log.DEBUG))
if (_destinationHash == null) throw new DataFormatException("Destination hash is not set"); _log.debug("mode = local");
_destinationHash.writeBytes(baos); break;
_log.debug("mode = destination, hash = " + _destinationHash); case FLAG_MODE_DESTINATION:
break; if (_destinationHash == null) throw new DataFormatException("Destination hash is not set");
case FLAG_MODE_ROUTER: _destinationHash.writeBytes(baos);
if (_routerHash == null) throw new DataFormatException("Router hash is not set"); if (_log.shouldLog(Log.DEBUG))
_routerHash.writeBytes(baos); _log.debug("mode = destination, hash = " + _destinationHash);
_log.debug("mode = router, routerHash = " + _routerHash); break;
break; case FLAG_MODE_ROUTER:
case FLAG_MODE_TUNNEL: if (_routerHash == null) throw new DataFormatException("Router hash is not set");
if ( (_routerHash == null) || (_tunnelId == null) ) throw new DataFormatException("Router hash or tunnel ID is not set"); _routerHash.writeBytes(baos);
_routerHash.writeBytes(baos); if (_log.shouldLog(Log.DEBUG))
_tunnelId.writeBytes(baos); _log.debug("mode = router, routerHash = " + _routerHash);
_log.debug("mode = tunnel, tunnelId = " + _tunnelId.getTunnelId() + ", routerHash = " + _routerHash); break;
break; case FLAG_MODE_TUNNEL:
} if ( (_routerHash == null) || (_tunnelId == null) ) throw new DataFormatException("Router hash or tunnel ID is not set");
if (getDelayRequested()) { _routerHash.writeBytes(baos);
_log.debug("delay requested: " + getDelaySeconds()); _tunnelId.writeBytes(baos);
DataHelper.writeLong(baos, 4, getDelaySeconds()); if (_log.shouldLog(Log.DEBUG))
} else { _log.debug("mode = tunnel, tunnelId = " + _tunnelId.getTunnelId()
_log.debug("delay NOT requested"); + ", routerHash = " + _routerHash);
} break;
} catch (IOException ioe) { }
throw new DataFormatException("Unable to write out additional info", ioe); if (getDelayRequested()) {
} if (_log.shouldLog(Log.DEBUG))
return baos.toByteArray(); _log.debug("delay requested: " + getDelaySeconds());
DataHelper.writeLong(baos, 4, getDelaySeconds());
} else {
if (_log.shouldLog(Log.DEBUG))
_log.debug("delay NOT requested");
}
} catch (IOException ioe) {
throw new DataFormatException("Unable to write out additional info", ioe);
}
return baos.toByteArray();
} }
public void writeBytes(OutputStream out) throws DataFormatException, IOException { public void writeBytes(OutputStream out) throws DataFormatException, IOException {
if ( (_deliveryMode < 0) || (_deliveryMode > FLAG_MODE_TUNNEL) ) throw new DataFormatException("Invalid data: mode = " + _deliveryMode); if ( (_deliveryMode < 0) || (_deliveryMode > FLAG_MODE_TUNNEL) ) throw new DataFormatException("Invalid data: mode = " + _deliveryMode);
long flags = getFlags(); long flags = getFlags();
_log.debug("Write flags: " + flags + " mode: " + getDeliveryMode() + " =?= " + flagMode(flags)); if (_log.shouldLog(Log.DEBUG))
byte additionalInfo[] = getAdditionalInfo(); _log.debug("Write flags: " + flags + " mode: " + getDeliveryMode()
DataHelper.writeLong(out, 1, flags); + " =?= " + flagMode(flags));
if (additionalInfo != null) { byte additionalInfo[] = getAdditionalInfo();
out.write(additionalInfo); DataHelper.writeLong(out, 1, flags);
out.flush(); if (additionalInfo != null) {
} out.write(additionalInfo);
out.flush();
}
} }
public boolean equals(Object obj) { public boolean equals(Object obj) {
if ( (obj == null) || !(obj instanceof DeliveryInstructions)) if ( (obj == null) || !(obj instanceof DeliveryInstructions))
return false; return false;
DeliveryInstructions instr = (DeliveryInstructions)obj; DeliveryInstructions instr = (DeliveryInstructions)obj;
return (getDelayRequested() == instr.getDelayRequested()) && return (getDelayRequested() == instr.getDelayRequested()) &&
(getDelaySeconds() == instr.getDelaySeconds()) && (getDelaySeconds() == instr.getDelaySeconds()) &&
(getDeliveryMode() == instr.getDeliveryMode()) && (getDeliveryMode() == instr.getDeliveryMode()) &&
(getEncrypted() == instr.getEncrypted()) && (getEncrypted() == instr.getEncrypted()) &&
DataHelper.eq(getDestination(), instr.getDestination()) && DataHelper.eq(getDestination(), instr.getDestination()) &&
DataHelper.eq(getEncryptionKey(), instr.getEncryptionKey()) && DataHelper.eq(getEncryptionKey(), instr.getEncryptionKey()) &&
DataHelper.eq(getRouter(), instr.getRouter()) && DataHelper.eq(getRouter(), instr.getRouter()) &&
DataHelper.eq(getTunnelId(), instr.getTunnelId()); DataHelper.eq(getTunnelId(), instr.getTunnelId());
} }
public int hashCode() { public int hashCode() {
return (int)getDelaySeconds() + return (int)getDelaySeconds() +
getDeliveryMode() + getDeliveryMode() +
DataHelper.hashCode(getDestination()) + DataHelper.hashCode(getDestination()) +
DataHelper.hashCode(getEncryptionKey()) + DataHelper.hashCode(getEncryptionKey()) +
DataHelper.hashCode(getRouter()) + DataHelper.hashCode(getRouter()) +
DataHelper.hashCode(getTunnelId()); DataHelper.hashCode(getTunnelId());
} }
public String toString() { public String toString() {
StringBuffer buf = new StringBuffer(128); StringBuffer buf = new StringBuffer(128);
buf.append("[DeliveryInstructions: "); buf.append("[DeliveryInstructions: ");
buf.append("\n\tDelivery mode: "); buf.append("\n\tDelivery mode: ");
switch (getDeliveryMode()) { switch (getDeliveryMode()) {
case DELIVERY_MODE_LOCAL: case DELIVERY_MODE_LOCAL:
buf.append("local"); buf.append("local");
break; break;
case DELIVERY_MODE_DESTINATION: case DELIVERY_MODE_DESTINATION:
buf.append("destination"); buf.append("destination");
break; break;
case DELIVERY_MODE_ROUTER: case DELIVERY_MODE_ROUTER:
buf.append("router"); buf.append("router");
break; break;
case DELIVERY_MODE_TUNNEL: case DELIVERY_MODE_TUNNEL:
buf.append("tunnel"); buf.append("tunnel");
break; break;
} }
buf.append("\n\tDelay requested: ").append(getDelayRequested()); buf.append("\n\tDelay requested: ").append(getDelayRequested());
buf.append("\n\tDelay seconds: ").append(getDelaySeconds()); buf.append("\n\tDelay seconds: ").append(getDelaySeconds());
buf.append("\n\tDestination: ").append(getDestination()); buf.append("\n\tDestination: ").append(getDestination());
@ -267,7 +279,7 @@ public class DeliveryInstructions extends DataStructureImpl {
buf.append("\n\tEncryption key: ").append(getEncryptionKey()); buf.append("\n\tEncryption key: ").append(getEncryptionKey());
buf.append("\n\tRouter: ").append(getRouter()); buf.append("\n\tRouter: ").append(getRouter());
buf.append("\n\tTunnelId: ").append(getTunnelId()); buf.append("\n\tTunnelId: ").append(getTunnelId());
return buf.toString(); return buf.toString();
} }
} }

View File

@ -84,7 +84,8 @@ public class GarlicClove extends DataStructureImpl {
public void readBytes(InputStream in) throws DataFormatException, IOException { public void readBytes(InputStream in) throws DataFormatException, IOException {
_instructions = new DeliveryInstructions(); _instructions = new DeliveryInstructions();
_instructions.readBytes(in); _instructions.readBytes(in);
_log.debug("Read instructions: " + _instructions); if (_log.shouldLog(Log.DEBUG))
_log.debug("Read instructions: " + _instructions);
try { try {
_msg = _handler.readMessage(in); _msg = _handler.readMessage(in);
} catch (I2NPMessageException ime) { } catch (I2NPMessageException ime) {
@ -92,10 +93,12 @@ public class GarlicClove extends DataStructureImpl {
} }
_cloveId = DataHelper.readLong(in, 4); _cloveId = DataHelper.readLong(in, 4);
_expiration = DataHelper.readDate(in); _expiration = DataHelper.readDate(in);
_log.debug("CloveID read: " + _cloveId + " expiration read: " + _expiration); if (_log.shouldLog(Log.DEBUG))
_log.debug("CloveID read: " + _cloveId + " expiration read: " + _expiration);
_certificate = new Certificate(); _certificate = new Certificate();
_certificate.readBytes(in); _certificate.readBytes(in);
_log.debug("Read cert: " + _certificate); if (_log.shouldLog(Log.DEBUG))
_log.debug("Read cert: " + _certificate);
int replyStyle = (int)DataHelper.readLong(in, 1); int replyStyle = (int)DataHelper.readLong(in, 1);
setSourceRouteBlockAction(replyStyle); setSourceRouteBlockAction(replyStyle);
if (replyStyle != ACTION_NONE) { if (replyStyle != ACTION_NONE) {
@ -124,13 +127,17 @@ public class GarlicClove extends DataStructureImpl {
throw new DataFormatException("Source route block must be specified for non-null action"); throw new DataFormatException("Source route block must be specified for non-null action");
_instructions.writeBytes(out); _instructions.writeBytes(out);
_log.debug("Wrote instructions: " + _instructions); if (_log.shouldLog(Log.DEBUG))
_log.debug("Wrote instructions: " + _instructions);
_msg.writeBytes(out); _msg.writeBytes(out);
DataHelper.writeLong(out, 4, _cloveId); DataHelper.writeLong(out, 4, _cloveId);
DataHelper.writeDate(out, _expiration); DataHelper.writeDate(out, _expiration);
_log.debug("CloveID written: " + _cloveId + " expiration written: " + _expiration); if (_log.shouldLog(Log.DEBUG))
_log.debug("CloveID written: " + _cloveId + " expiration written: "
+ _expiration);
_certificate.writeBytes(out); _certificate.writeBytes(out);
_log.debug("Written cert: " + _certificate); if (_log.shouldLog(Log.DEBUG))
_log.debug("Written cert: " + _certificate);
DataHelper.writeLong(out, 1, _replyAction); DataHelper.writeLong(out, 1, _replyAction);
if ( (_replyAction != 0) && (_sourceRouteBlock != null) ) if ( (_replyAction != 0) && (_sourceRouteBlock != null) )
_sourceRouteBlock.writeBytes(out); _sourceRouteBlock.writeBytes(out);

View File

@ -77,7 +77,8 @@ public abstract class I2NPMessageImpl extends DataStructureImpl implements I2NPM
} catch (DataFormatException dfe) { } catch (DataFormatException dfe) {
throw new I2NPMessageException("Error reading the message header", dfe); throw new I2NPMessageException("Error reading the message header", dfe);
} }
_log.debug("Reading bytes: type = " + type + " / uniqueId : " + _uniqueId + " / expiration : " + _expiration); if (_log.shouldLog(Log.DEBUG))
_log.debug("Reading bytes: type = " + type + " / uniqueId : " + _uniqueId + " / expiration : " + _expiration);
readMessage(in, type); readMessage(in, type);
} }
public void writeBytes(OutputStream out) throws DataFormatException, IOException { public void writeBytes(OutputStream out) throws DataFormatException, IOException {
@ -85,7 +86,8 @@ public abstract class I2NPMessageImpl extends DataStructureImpl implements I2NPM
DataHelper.writeLong(out, 1, getType()); DataHelper.writeLong(out, 1, getType());
DataHelper.writeLong(out, 4, _uniqueId); DataHelper.writeLong(out, 4, _uniqueId);
DataHelper.writeDate(out, _expiration); DataHelper.writeDate(out, _expiration);
_log.debug("Writing bytes: type = " + getType() + " / uniqueId : " + _uniqueId + " / expiration : " + _expiration); if (_log.shouldLog(Log.DEBUG))
_log.debug("Writing bytes: type = " + getType() + " / uniqueId : " + _uniqueId + " / expiration : " + _expiration);
byte[] data = writeMessage(); byte[] data = writeMessage();
out.write(data); out.write(data);
} catch (I2NPMessageException ime) { } catch (I2NPMessageException ime) {

View File

@ -60,9 +60,11 @@ public class TunnelMessage extends I2NPMessageImpl {
try { try {
_tunnelId = new TunnelId(); _tunnelId = new TunnelId();
_tunnelId.readBytes(in); _tunnelId.readBytes(in);
_log.debug("Read tunnel message for tunnel " + _tunnelId); if (_log.shouldLog(Log.DEBUG))
_log.debug("Read tunnel message for tunnel " + _tunnelId);
_size = DataHelper.readLong(in, 4); _size = DataHelper.readLong(in, 4);
_log.debug("Read tunnel message size: " + _size); if (_log.shouldLog(Log.DEBUG))
_log.debug("Read tunnel message size: " + _size);
if (_size < 0) throw new I2NPMessageException("Invalid size in the structure: " + _size); if (_size < 0) throw new I2NPMessageException("Invalid size in the structure: " + _size);
_data = new byte[(int)_size]; _data = new byte[(int)_size];
int read = read(in, _data); int read = read(in, _data);
@ -90,17 +92,23 @@ public class TunnelMessage extends I2NPMessageImpl {
ByteArrayOutputStream os = new ByteArrayOutputStream(32); ByteArrayOutputStream os = new ByteArrayOutputStream(32);
try { try {
_tunnelId.writeBytes(os); _tunnelId.writeBytes(os);
_log.debug("Writing tunnel message for tunnel " + _tunnelId); if (_log.shouldLog(Log.DEBUG))
_log.debug("Writing tunnel message for tunnel " + _tunnelId);
DataHelper.writeLong(os, 4, _data.length); DataHelper.writeLong(os, 4, _data.length);
_log.debug("Writing tunnel message length: " + _data.length); if (_log.shouldLog(Log.DEBUG))
_log.debug("Writing tunnel message length: " + _data.length);
os.write(_data); os.write(_data);
_log.debug("Writing tunnel message data"); if (_log.shouldLog(Log.DEBUG))
_log.debug("Writing tunnel message data");
if ( (_verification == null) || (_encryptedInstructions == null) ) { if ( (_verification == null) || (_encryptedInstructions == null) ) {
DataHelper.writeLong(os, 1, FLAG_DONT_INCLUDESTRUCTURE); DataHelper.writeLong(os, 1, FLAG_DONT_INCLUDESTRUCTURE);
_log.debug("Writing DontIncludeStructure flag"); if (_log.shouldLog(Log.DEBUG))
_log.debug("Writing DontIncludeStructure flag");
} else { } else {
DataHelper.writeLong(os, 1, FLAG_INCLUDESTRUCTURE); DataHelper.writeLong(os, 1, FLAG_INCLUDESTRUCTURE);
_log.debug("Writing IncludeStructure flag, then the verification structure, then the E(instr).length [" + _encryptedInstructions.length + "], then the E(instr)"); if (_log.shouldLog(Log.DEBUG))
_log.debug("Writing IncludeStructure flag, then the verification structure, then the " +
"E(instr).length [" + _encryptedInstructions.length + "], then the E(instr)");
_verification.writeBytes(os); _verification.writeBytes(os);
DataHelper.writeLong(os, 2, _encryptedInstructions.length); DataHelper.writeLong(os, 2, _encryptedInstructions.length);
os.write(_encryptedInstructions); os.write(_encryptedInstructions);
@ -109,7 +117,8 @@ public class TunnelMessage extends I2NPMessageImpl {
throw new I2NPMessageException("Error writing out the message data", dfe); throw new I2NPMessageException("Error writing out the message data", dfe);
} }
byte rv[] = os.toByteArray(); byte rv[] = os.toByteArray();
_log.debug("Overall data being written: " + rv.length); if (_log.shouldLog(Log.DEBUG))
_log.debug("Overall data being written: " + rv.length);
return rv; return rv;
} }