net.i2p.data.i2np
Class I2NPMessageImpl

java.lang.Object
  extended by net.i2p.data.DataStructureImpl
      extended by net.i2p.data.i2np.I2NPMessageImpl
All Implemented Interfaces:
Serializable, DataStructure, I2NPMessage
Direct Known Subclasses:
FastI2NPMessageImpl, TunnelBuildMessageBase

public abstract class I2NPMessageImpl
extends DataStructureImpl
implements I2NPMessage

Defines the base message implementation.

Author:
jrandom
See Also:
Serialized Form

Nested Class Summary
static interface I2NPMessageImpl.Builder
          interface for extending the types of messages handled - unused
 
Field Summary
protected  I2PAppContext _context
           
protected  long _expiration
           
protected  Log _log
           
protected  long _uniqueId
          Warning, lazily initialized by readBytes(), writeBytes(), toByteArray(), getUniqueId(), and setUniqueId(); otherwise will be -1.
static int CHECKSUM_LENGTH
           
static long DEFAULT_EXPIRATION_MS
           
static int HEADER_LENGTH
          16
 
Fields inherited from interface net.i2p.data.i2np.I2NPMessage
MAX_ID_VALUE, MAX_SIZE
 
Constructor Summary
I2NPMessageImpl(I2PAppContext context)
           
 
Method Summary
protected abstract  int calculateWrittenLength()
          calculate the message body's length (not including the header and footer
static I2NPMessage createMessage(I2PAppContext context, int type)
          Yes, this is fairly ugly, but its the only place it ever happens.
static I2NPMessage fromRawByteArray(I2PAppContext ctx, byte[] buffer, int offset, int len, I2NPMessageHandler handler)
          Read the message with a short 5-byte header.
 long getMessageExpiration()
          Date after which the message should be dropped (and the associated uniqueId forgotten)
 int getMessageSize()
          How large the message is, including any checksums, i.e.
 int getRawMessageSize()
          The raw header consists of a one-byte type and a 4-byte expiration in seconds only.
 long getUniqueId()
          Replay resistant message Id
 int readBytes(byte[] data, int type, int offset)
          Read the header, then read the rest into buffer, then call readMessage in the implemented message type
 int readBytes(byte[] data, int type, int offset, int maxLen)
          Set a limit on the max to read from the data buffer, so that we can use a large buffer but prevent the reader from reading off the end.
 void readBytes(InputStream in)
          Deprecated. unused
 int readBytes(InputStream in, int type, byte[] buffer)
          Deprecated. unused
 void readMessage(byte[] data, int offset, int dataSize, int type, I2NPMessageHandler handler)
           
static void registerBuilder(I2NPMessageImpl.Builder builder, int type)
          Deprecated. unused
 void setMessageExpiration(long exp)
          The expiration is set to one minute from now in the constructor but it can be overridden here.
 void setUniqueId(long id)
          The ID is set to a random value when written but it can be overridden here.
 byte[] toByteArray()
           
 int toByteArray(byte[] buffer)
          write the message to the buffer, returning the number of bytes written.
 int toRawByteArray(byte[] buffer)
          Write the message with a short 5-byte header.
 void writeBytes(OutputStream out)
          Deprecated. unused
protected abstract  int writeMessageBody(byte[] out, int curIndex)
          write the message body to the output array, starting at the given index.
 
Methods inherited from class net.i2p.data.DataStructureImpl
calculateHash, fromBase64, fromByteArray, read, toBase64
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface net.i2p.data.i2np.I2NPMessage
getType, readMessage
 
Methods inherited from interface net.i2p.data.DataStructure
calculateHash, fromBase64, fromByteArray, toBase64
 

Field Detail

_log

protected final Log _log

_context

protected final I2PAppContext _context

_expiration

protected long _expiration

_uniqueId

protected long _uniqueId
Warning, lazily initialized by readBytes(), writeBytes(), toByteArray(), getUniqueId(), and setUniqueId(); otherwise will be -1. Extending classes should take care when accessing this field; to ensure initialization, use getUniqueId() instead.


DEFAULT_EXPIRATION_MS

public static final long DEFAULT_EXPIRATION_MS
See Also:
Constant Field Values

CHECKSUM_LENGTH

public static final int CHECKSUM_LENGTH
See Also:
Constant Field Values

HEADER_LENGTH

public static final int HEADER_LENGTH
16

See Also:
Constant Field Values
Constructor Detail

I2NPMessageImpl

public I2NPMessageImpl(I2PAppContext context)
Method Detail

registerBuilder

public static final void registerBuilder(I2NPMessageImpl.Builder builder,
                                         int type)
Deprecated. unused


readBytes

public void readBytes(InputStream in)
               throws DataFormatException,
                      IOException
Deprecated. unused

Read the whole message but only if it's exactly 1024 bytes. Unused - All transports provide encapsulation and so we have byte arrays available.

Specified by:
readBytes in interface DataStructure
Parameters:
in - stream to read from
Throws:
DataFormatException - if the data is improperly formatted
IOException - if there was a problem reading the stream

readBytes

public int readBytes(InputStream in,
                     int type,
                     byte[] buffer)
              throws I2NPMessageException,
                     IOException
Deprecated. unused

Read the header, then read the rest into buffer, then call readMessage in the implemented message type This does a copy from the stream to the buffer, so if you already have a byte array, use the other readBytes() instead.
  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)
Unused - All transports provide encapsulation and so we have byte arrays available.

Specified by:
readBytes in interface I2NPMessage
Parameters:
type - the message type or -1 if we should read it here
buffer - temp buffer to use
in - stream to read from starting at type if type is < 0 (16 byte header) starting at ID if type is >= 0 (15 byte header)
Returns:
total length of the message
Throws:
I2NPMessageException - if the stream doesn't contain a valid message that this class can read.
IOException - if there is a problem reading from the stream

readBytes

public int readBytes(byte[] data,
                     int type,
                     int offset)
              throws I2NPMessageException
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)

Specified by:
readBytes in interface I2NPMessage
Parameters:
type - the message type or -1 if we should read it here
data - the data
offset - where to start starting at type if type is < 0 (16 byte header) starting at ID if type is >= 0 (15 byte header)
Returns:
total length of the message
Throws:
I2NPMessageException - if there is no valid message

readBytes

public int readBytes(byte[] data,
                     int type,
                     int offset,
                     int maxLen)
              throws I2NPMessageException
Set a limit on the max to read from the data buffer, so that we can use a large buffer but prevent the reader from reading off the end.

Specified by:
readBytes in interface I2NPMessage
Parameters:
maxLen - read no more than this many bytes from data starting at offset, even if it is longer This includes the type byte only if type < 0
data - the data, may or may not include the type
type - I2NP message type. If less than zero, read the type from data
offset - where to start starting at type if type is < 0 (16 byte header) starting at ID if type is >= 0 (15 byte header)
Returns:
size of the message read (including headers)
Throws:
I2NPMessageException - if there is no valid message
Since:
0.8.12

writeBytes

public void writeBytes(OutputStream out)
                throws DataFormatException,
                       IOException
Deprecated. unused

Don't do this if you need a byte array - use toByteArray()

Specified by:
writeBytes in interface DataStructure
Parameters:
out - stream to write to
Throws:
DataFormatException - if the data was incomplete or not yet ready to be written
IOException - if there was a problem writing to the stream

getUniqueId

public long getUniqueId()
Replay resistant message Id

Specified by:
getUniqueId in interface I2NPMessage

setUniqueId

public void setUniqueId(long id)
The ID is set to a random value when written but it can be overridden here.

Specified by:
setUniqueId in interface I2NPMessage

getMessageExpiration

public long getMessageExpiration()
Date after which the message should be dropped (and the associated uniqueId forgotten)

Specified by:
getMessageExpiration in interface I2NPMessage

setMessageExpiration

public void setMessageExpiration(long exp)
The expiration is set to one minute from now in the constructor but it can be overridden here.

Specified by:
setMessageExpiration in interface I2NPMessage

getMessageSize

public int getMessageSize()
Description copied from interface: I2NPMessage
How large the message is, including any checksums, i.e. full 16 byte header

Specified by:
getMessageSize in interface I2NPMessage

getRawMessageSize

public int getRawMessageSize()
The raw header consists of a one-byte type and a 4-byte expiration in seconds only. Used by SSU only!

Specified by:
getRawMessageSize in interface I2NPMessage

toByteArray

public byte[] toByteArray()
Specified by:
toByteArray in interface DataStructure
Overrides:
toByteArray in class DataStructureImpl
Returns:
may be null if data is not set

toByteArray

public int toByteArray(byte[] buffer)
Description copied from interface: I2NPMessage
write the message to the buffer, returning the number of bytes written. the data is formatted so as to be self contained, with the type, size, expiration, unique id, as well as a checksum bundled along. Full 16 byte header.

Specified by:
toByteArray in interface I2NPMessage

calculateWrittenLength

protected abstract int calculateWrittenLength()
calculate the message body's length (not including the header and footer


writeMessageBody

protected abstract int writeMessageBody(byte[] out,
                                        int curIndex)
                                 throws I2NPMessageException
write the message body to the output array, starting at the given index.

Returns:
the index into the array after the last byte written
Throws:
I2NPMessageException

toRawByteArray

public int toRawByteArray(byte[] buffer)
Write the message with a short 5-byte header. THe header consists of a one-byte type and a 4-byte expiration in seconds only. Used by SSU only!

Specified by:
toRawByteArray in interface I2NPMessage

readMessage

public void readMessage(byte[] data,
                        int offset,
                        int dataSize,
                        int type,
                        I2NPMessageHandler handler)
                 throws I2NPMessageException
Specified by:
readMessage in interface I2NPMessage
Throws:
I2NPMessageException

fromRawByteArray

public static I2NPMessage fromRawByteArray(I2PAppContext ctx,
                                           byte[] buffer,
                                           int offset,
                                           int len,
                                           I2NPMessageHandler handler)
                                    throws I2NPMessageException
Read the message with a short 5-byte header. THe header consists of a one-byte type and a 4-byte expiration in seconds only. Used by SSU only!

Throws:
I2NPMessageException

createMessage

public static I2NPMessage createMessage(I2PAppContext context,
                                        int type)
                                 throws I2NPMessageException
Yes, this is fairly ugly, but its the only place it ever happens.

Returns:
non-null, returns an UnknownI2NPMessage if unknown type
Throws:
I2NPMessageException