various cleanups, javadocs, logging

This commit is contained in:
zzz
2009-12-26 20:00:47 +00:00
parent 1fc32c5e6f
commit 0b0e3fffe4
8 changed files with 61 additions and 22 deletions

View File

@ -99,9 +99,9 @@ public class MessageInputStream extends InputStream {
} }
} }
private long[] locked_getNacks() { private long[] locked_getNacks() {
List ids = null; List<Long> ids = null;
for (long i = _highestReadyBlockId + 1; i < _highestBlockId; i++) { for (long i = _highestReadyBlockId + 1; i < _highestBlockId; i++) {
Long l = new Long(i); Long l = Long.valueOf(i);
if (_notYetReadyBlocks.containsKey(l)) { if (_notYetReadyBlocks.containsKey(l)) {
// ACK // ACK
} else { } else {
@ -113,7 +113,7 @@ public class MessageInputStream extends InputStream {
if (ids != null) { if (ids != null) {
long rv[] = new long[ids.size()]; long rv[] = new long[ids.size()];
for (int i = 0; i < rv.length; i++) for (int i = 0; i < rv.length; i++)
rv[i] = ((Long)ids.get(i)).longValue(); rv[i] = ids.get(i).longValue();
return rv; return rv;
} else { } else {
return null; return null;

View File

@ -11,6 +11,7 @@ import net.i2p.data.ByteArray;
* needs to be done (e.g. write(foo); toByteArray();), call releaseBuffer * needs to be done (e.g. write(foo); toByteArray();), call releaseBuffer
* to put the buffer back into the cache. * to put the buffer back into the cache.
* *
* @deprecated unused
*/ */
public class CachingByteArrayOutputStream extends ByteArrayOutputStream { public class CachingByteArrayOutputStream extends ByteArrayOutputStream {
private ByteCache _cache; private ByteCache _cache;

View File

@ -66,6 +66,22 @@ public abstract class I2NPMessageImpl extends DataStructureImpl implements I2NPM
throw new DataFormatException("Bad bytes", ime); throw new DataFormatException("Bad bytes", ime);
} }
} }
/**
* Read the header, then read the rest into buffer, then call
* readMessage in the implemented message type
*
* Specifically:
* 1 byte type (if caller didn't read already, as specified by the type param
* 4 byte ID
* 8 byte expiration
* 2 byte size
* 1 byte checksum
* size bytes of payload (read by readMessage() in implementation)
*
* @param type the message type or -1 if we should read it here
* @param buffer temp buffer to use
*/
public int readBytes(InputStream in, int type, byte buffer[]) throws I2NPMessageException, IOException { public int readBytes(InputStream in, int type, byte buffer[]) throws I2NPMessageException, IOException {
try { try {
if (type < 0) if (type < 0)
@ -268,6 +284,7 @@ public abstract class I2NPMessageImpl extends DataStructureImpl implements I2NPM
*/ */
/** used by SSU only */
public int toRawByteArray(byte buffer[]) { public int toRawByteArray(byte buffer[]) {
verifyUnwritten(); verifyUnwritten();
if (RAW_FULL_SIZE) if (RAW_FULL_SIZE)
@ -298,9 +315,13 @@ public abstract class I2NPMessageImpl extends DataStructureImpl implements I2NPM
} }
/*****
public static I2NPMessage fromRawByteArray(I2PAppContext ctx, byte buffer[], int offset, int len) throws I2NPMessageException { public static I2NPMessage fromRawByteArray(I2PAppContext ctx, byte buffer[], int offset, int len) throws I2NPMessageException {
return fromRawByteArray(ctx, buffer, offset, len, new I2NPMessageHandler(ctx)); return fromRawByteArray(ctx, buffer, offset, len, new I2NPMessageHandler(ctx));
} }
*****/
/** used by SSU only */
public static I2NPMessage fromRawByteArray(I2PAppContext ctx, byte buffer[], int offset, int len, I2NPMessageHandler handler) throws I2NPMessageException { public static I2NPMessage fromRawByteArray(I2PAppContext ctx, byte buffer[], int offset, int len, I2NPMessageHandler handler) throws I2NPMessageException {
int type = (int)DataHelper.fromLong(buffer, offset, 1); int type = (int)DataHelper.fromLong(buffer, offset, 1);
offset++; offset++;

View File

@ -126,7 +126,7 @@ public class OutNetMessage {
} }
return ZERO; return ZERO;
} }
private static final Long ZERO = new Long(0); private static final Long ZERO = Long.valueOf(0);
private void locked_initTimestamps() { private void locked_initTimestamps() {
if (_timestamps == null) { if (_timestamps == null) {
_timestamps = new HashMap(8); _timestamps = new HashMap(8);

View File

@ -772,8 +772,8 @@ public class NTCPConnection implements FIFOBandwidthLimiter.CompleteListener {
private long acquiredOn; private long acquiredOn;
PrepBuffer() { PrepBuffer() {
unencrypted = new byte[16*1024]; unencrypted = new byte[BUFFER_SIZE];
base = new byte[16*1024]; base = new byte[BUFFER_SIZE];
pad = new byte[16]; pad = new byte[16];
crc = new Adler32(); crc = new Adler32();
} }
@ -1033,7 +1033,7 @@ public class NTCPConnection implements FIFOBandwidthLimiter.CompleteListener {
/** _decryptBlockBuf contains another cleartext block of I2NP to parse */ /** _decryptBlockBuf contains another cleartext block of I2NP to parse */
private boolean recvUnencryptedI2NP() { private boolean recvUnencryptedI2NP() {
_curReadState.receiveBlock(_decryptBlockBuf); _curReadState.receiveBlock(_decryptBlockBuf);
if (_curReadState.getSize() > 16*1024) { if (_curReadState.getSize() > BUFFER_SIZE) {
if (_log.shouldLog(Log.ERROR)) if (_log.shouldLog(Log.ERROR))
_log.error("I2NP message too big - size: " + _curReadState.getSize() + " Dropping " + toString()); _log.error("I2NP message too big - size: " + _curReadState.getSize() + " Dropping " + toString());
_context.statManager().addRateData("ntcp.corruptTooLargeI2NP", _curReadState.getSize(), getUptime()); _context.statManager().addRateData("ntcp.corruptTooLargeI2NP", _curReadState.getSize(), getUptime());
@ -1112,7 +1112,8 @@ public class NTCPConnection implements FIFOBandwidthLimiter.CompleteListener {
return obj == this; return obj == this;
} }
private final static List _i2npHandlers = new ArrayList(4); private static final int MAX_HANDLERS = 4;
private final static List _i2npHandlers = new ArrayList(MAX_HANDLERS);
private final static I2NPMessageHandler acquireHandler(RouterContext ctx) { private final static I2NPMessageHandler acquireHandler(RouterContext ctx) {
I2NPMessageHandler rv = null; I2NPMessageHandler rv = null;
synchronized (_i2npHandlers) { synchronized (_i2npHandlers) {
@ -1125,7 +1126,7 @@ public class NTCPConnection implements FIFOBandwidthLimiter.CompleteListener {
} }
private static void releaseHandler(I2NPMessageHandler handler) { private static void releaseHandler(I2NPMessageHandler handler) {
synchronized (_i2npHandlers) { synchronized (_i2npHandlers) {
if (_i2npHandlers.size() < 4) if (_i2npHandlers.size() < MAX_HANDLERS)
_i2npHandlers.add(handler); _i2npHandlers.add(handler);
} }
} }
@ -1137,13 +1138,13 @@ public class NTCPConnection implements FIFOBandwidthLimiter.CompleteListener {
byte data[]; byte data[];
ByteArrayInputStream bais; ByteArrayInputStream bais;
public DataBuf() { public DataBuf() {
data = new byte[16*1024]; data = new byte[BUFFER_SIZE];
bais = new ByteArrayInputStream(data); bais = new ByteArrayInputStream(data);
} }
} }
private static int MAX_DATA_READ_BUFS = 16; private static final int MAX_DATA_READ_BUFS = 16;
private final static List _dataReadBufs = new ArrayList(16); private final static List _dataReadBufs = new ArrayList(MAX_DATA_READ_BUFS);
private static DataBuf acquireReadBuf() { private static DataBuf acquireReadBuf() {
synchronized (_dataReadBufs) { synchronized (_dataReadBufs) {
if (_dataReadBufs.size() > 0) if (_dataReadBufs.size() > 0)
@ -1178,7 +1179,7 @@ public class NTCPConnection implements FIFOBandwidthLimiter.CompleteListener {
_crc = new Adler32(); _crc = new Adler32();
init(); init();
} }
public void init() { private void init() {
_size = -1; _size = -1;
_nextWrite = 0; _nextWrite = 0;
_expectedCrc = -1; _expectedCrc = -1;
@ -1268,11 +1269,15 @@ public class NTCPConnection implements FIFOBandwidthLimiter.CompleteListener {
I2NPMessage read = h.readMessage(_dataBuf.bais); I2NPMessage read = h.readMessage(_dataBuf.bais);
long timeToRecv = System.currentTimeMillis() - _stateBegin; long timeToRecv = System.currentTimeMillis() - _stateBegin;
releaseHandler(h); releaseHandler(h);
if (_log.shouldLog(Log.DEBUG)) if (_log.shouldLog(Log.INFO))
_log.debug("I2NP message " + _messagesRead + "/" + (read != null ? read.getUniqueId() : 0) _log.info("I2NP message " + _messagesRead + "/" + (read != null ? read.getUniqueId() : 0)
+ " received after " + timeToRecv + " with " + _size +"/"+ (_blocks*16) + " bytes on " + toString()); + " received after " + timeToRecv + " with " + _size +"/"+ (_blocks*16) + " bytes on " + NTCPConnection.this.toString());
_context.statManager().addRateData("ntcp.receiveTime", timeToRecv, timeToRecv); _context.statManager().addRateData("ntcp.receiveTime", timeToRecv, timeToRecv);
_context.statManager().addRateData("ntcp.receiveSize", _size, timeToRecv); _context.statManager().addRateData("ntcp.receiveSize", _size, timeToRecv);
// FIXME move end of try block here.
// On the input side, move releaseHandler() and init() to a finally block.
if (read != null) { if (read != null) {
_transport.messageReceived(read, _remotePeer, null, timeToRecv, _size); _transport.messageReceived(read, _remotePeer, null, timeToRecv, _size);
if (_messagesRead <= 0) if (_messagesRead <= 0)
@ -1283,23 +1288,27 @@ public class NTCPConnection implements FIFOBandwidthLimiter.CompleteListener {
// get it ready for the next I2NP message // get it ready for the next I2NP message
init(); init();
} catch (IOException ioe) { } catch (IOException ioe) {
if (_log.shouldLog(Log.DEBUG)) if (_log.shouldLog(Log.WARN))
_log.debug("Error parsing I2NP message", ioe); _log.warn("Error parsing I2NP message", ioe);
_context.statManager().addRateData("ntcp.corruptI2NPIOE", 1, getUptime()); _context.statManager().addRateData("ntcp.corruptI2NPIOE", 1, getUptime());
close(); close();
// handler and databuf are lost
return; return;
} catch (I2NPMessageException ime) { } catch (I2NPMessageException ime) {
if (_log.shouldLog(Log.DEBUG)) if (_log.shouldLog(Log.WARN))
_log.debug("Error parsing I2NP message", ime); _log.warn("Error parsing I2NP message", ime);
_context.statManager().addRateData("ntcp.corruptI2NPIME", 1, getUptime()); _context.statManager().addRateData("ntcp.corruptI2NPIME", 1, getUptime());
close(); close();
// handler and databuf are lost
return; return;
} }
} else { } else {
if (_log.shouldLog(Log.ERROR)) if (_log.shouldLog(Log.ERROR))
_log.error("CRC incorrect for message " + _messagesRead + " (calc=" + val + " expected=" + _expectedCrc + ") size=" + _size + " blocks " + _blocks); _log.error("CRC incorrect for message " + _messagesRead + " (calc=" + val + " expected=" + _expectedCrc + ") size=" + _size + " blocks " + _blocks);
_context.statManager().addRateData("ntcp.corruptI2NPCRC", 1, getUptime()); _context.statManager().addRateData("ntcp.corruptI2NPCRC", 1, getUptime());
// should we try to read in the message and keep going?
close(); close();
// databuf is lost
return; return;
} }
} }

View File

@ -8,6 +8,8 @@ import net.i2p.util.ConcurrentHashSet;
/** /**
* waste lots of RAM * waste lots of RAM
*
* @deprecated unused
*/ */
class HashSetIVValidator implements IVValidator { class HashSetIVValidator implements IVValidator {
private final Set<ByteArray> _received; private final Set<ByteArray> _received;

View File

@ -32,9 +32,11 @@ public class HopProcessor {
static final int IV_LENGTH = 16; static final int IV_LENGTH = 16;
private static final ByteCache _cache = ByteCache.getInstance(128, IV_LENGTH); private static final ByteCache _cache = ByteCache.getInstance(128, IV_LENGTH);
/** @deprecated unused */
public HopProcessor(I2PAppContext ctx, HopConfig config) { public HopProcessor(I2PAppContext ctx, HopConfig config) {
this(ctx, config, createValidator()); this(ctx, config, createValidator());
} }
public HopProcessor(I2PAppContext ctx, HopConfig config, IVValidator validator) { public HopProcessor(I2PAppContext ctx, HopConfig config, IVValidator validator) {
_context = ctx; _context = ctx;
_log = ctx.logManager().getLog(HopProcessor.class); _log = ctx.logManager().getLog(HopProcessor.class);
@ -42,6 +44,7 @@ public class HopProcessor {
_validator = validator; _validator = validator;
} }
/** @deprecated unused */
protected static IVValidator createValidator() { protected static IVValidator createValidator() {
// yeah, we'll use an O(1) validator later (e.g. bloom filter) // yeah, we'll use an O(1) validator later (e.g. bloom filter)
return new HashSetIVValidator(); return new HashSetIVValidator();
@ -88,10 +91,10 @@ public class HopProcessor {
encrypt(orig, offset, length); encrypt(orig, offset, length);
updateIV(orig, offset); updateIV(orig, offset);
} }
if (_log.shouldLog(Log.DEBUG)) { //if (_log.shouldLog(Log.DEBUG)) {
//_log.debug("Data after processing: " + Base64.encode(orig, IV_LENGTH, orig.length - IV_LENGTH)); //_log.debug("Data after processing: " + Base64.encode(orig, IV_LENGTH, orig.length - IV_LENGTH));
//_log.debug("IV sent: " + Base64.encode(orig, 0, IV_LENGTH)); //_log.debug("IV sent: " + Base64.encode(orig, 0, IV_LENGTH));
} //}
return true; return true;
} }

View File

@ -79,6 +79,9 @@ public class PumpedTunnelGateway extends TunnelGateway {
* scheduling a later delayed flush as necessary. this allows the gw.add call to * scheduling a later delayed flush as necessary. this allows the gw.add call to
* go quickly, rather than blocking its callers on potentially substantial * go quickly, rather than blocking its callers on potentially substantial
* processing. * processing.
*
* @param queueBuf Empty list for convenience, to use as a temporary buffer.
* Must be empty when called; will always be emptied before return.
*/ */
void pump(List<Pending> queueBuf) { void pump(List<Pending> queueBuf) {
synchronized (_prequeue) { synchronized (_prequeue) {