Changeset ef40282


Ignore:
Timestamp:
Jul 8, 2011 5:03:19 PM (9 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
f173e88
Parents:
32d9204
Message:

TunnelBuildReplyMessage? into a common base class

Files:
1 added
5 edited

Legend:

Unmodified
Added
Removed
  • history.txt

    r32d9204 ref40282  
    112011-07-08 zzz
     2  * I2NP: Consolidate common code from TunnelBuildMessage and
     3          TunnelBuildReplyMessage into a common base class
    24  * NetDB: Fix NPE at startup (ticket #493)
    35  * Sha256Standalone:
  • router/java/src/net/i2p/data/i2np/TunnelBuildMessage.java

    r32d9204 ref40282  
    44
    55import net.i2p.I2PAppContext;
    6 import net.i2p.data.ByteArray;
    76
    87/**
    9  *
     8 *  The basic build message with 8 records.
    109 */
    11 public class TunnelBuildMessage extends I2NPMessageImpl {
    12     protected ByteArray _records[];
    13     protected int RECORD_COUNT;
    14     public static final int MAX_RECORD_COUNT = 8;
    15    
     10public class TunnelBuildMessage extends TunnelBuildMessageBase {
     11
    1612    public static final int MESSAGE_TYPE = 21;
    1713
    1814    public TunnelBuildMessage(I2PAppContext context) {
    19         this(context, MAX_RECORD_COUNT);
     15        super(context, MAX_RECORD_COUNT);
    2016    }
    2117
    2218    /** @since 0.7.12 */
    2319    protected TunnelBuildMessage(I2PAppContext context, int records) {
    24         super(context);
    25         if (records > 0) {
    26             RECORD_COUNT = records;
    27             _records = new ByteArray[records];
    28         }
    29         // else will be initialized by readMessage() in VTBM
     20        super(context, records);
    3021    }
    3122
    32     public void setRecord(int index, ByteArray record) { _records[index] = record; }
    33     public ByteArray getRecord(int index) { return _records[index]; }
    34     /** @since 0.7.12 */
    35     public int getRecordCount() { return RECORD_COUNT; }
    36    
    37     public static final int RECORD_SIZE = 512+16;
    38    
    39     protected int calculateWrittenLength() { return RECORD_SIZE * RECORD_COUNT; }
    4023    public int getType() { return MESSAGE_TYPE; }
    41     public void readMessage(byte[] data, int offset, int dataSize, int type) throws I2NPMessageException, IOException {
    42         if (type != MESSAGE_TYPE)
    43             throw new I2NPMessageException("Message type is incorrect for this message");
    44         if (dataSize != calculateWrittenLength())
    45             throw new I2NPMessageException("Wrong length (expects " + calculateWrittenLength() + ", recv " + dataSize + ")");
    46        
    47         for (int i = 0; i < RECORD_COUNT; i++) {
    48             int off = offset + (i * RECORD_SIZE);
    49             byte rec[] = new byte[RECORD_SIZE];
    50             System.arraycopy(data, off, rec, 0, RECORD_SIZE);
    51             setRecord(i, new ByteArray(rec)); //new ByteArray(data, off, len));
    52         }
    53     }
    54    
    55     protected int writeMessageBody(byte[] out, int curIndex) throws I2NPMessageException {
    56         int remaining = out.length - (curIndex + calculateWrittenLength());
    57         if (remaining < 0)
    58             throw new I2NPMessageException("Not large enough (too short by " + remaining + ")");
    59         for (int i = 0; i < RECORD_COUNT; i++) {
    60             System.arraycopy(_records[i].getData(), _records[i].getOffset(), out, curIndex, RECORD_SIZE);
    61             curIndex += RECORD_SIZE;
    62         }
    63         return curIndex;
    64     }
    6524
    6625    @Override
  • router/java/src/net/i2p/data/i2np/TunnelBuildReplyMessage.java

    r32d9204 ref40282  
    44
    55import net.i2p.I2PAppContext;
    6 import net.i2p.data.ByteArray;
    76
    87/**
     8 *  The basic build reply message with 8 records.
    99 * Transmitted from the new outbound endpoint to the creator through a
    1010 * reply tunnel
    1111 */
    12 public class TunnelBuildReplyMessage extends I2NPMessageImpl {
    13     protected ByteArray _records[];
    14     protected int RECORD_COUNT;
    15     public static final int MAX_RECORD_COUNT = TunnelBuildMessage.MAX_RECORD_COUNT;
    16    
     12public class TunnelBuildReplyMessage extends TunnelBuildMessageBase {
     13
    1714    public static final int MESSAGE_TYPE = 22;
    1815
    1916    public TunnelBuildReplyMessage(I2PAppContext context) {
    20         this(context, MAX_RECORD_COUNT);
     17        super(context, MAX_RECORD_COUNT);
    2118    }
    2219
    2320    /** @since 0.7.12 */
    2421    protected TunnelBuildReplyMessage(I2PAppContext context, int records) {
    25         super(context);
    26         if (records > 0) {
    27             RECORD_COUNT = records;
    28             _records = new ByteArray[records];
    29         }
    30         // else will be initialized by readMessage() in VTBRM
     22        super(context, records);
    3123    }
    3224
    33     public void setRecord(int index, ByteArray record) { _records[index] = record; }
    34     public ByteArray getRecord(int index) { return _records[index]; }
    35     /** @since 0.7.12 */
    36     public int getRecordCount() { return RECORD_COUNT; }
    37    
    38     public static final int RECORD_SIZE = TunnelBuildMessage.RECORD_SIZE;
    39    
    40     protected int calculateWrittenLength() { return RECORD_SIZE * RECORD_COUNT; }
    4125    public int getType() { return MESSAGE_TYPE; }
    42     public void readMessage(byte[] data, int offset, int dataSize, int type) throws I2NPMessageException, IOException {
    43         if (type != MESSAGE_TYPE)
    44             throw new I2NPMessageException("Message type is incorrect for this message");
    45         if (dataSize != calculateWrittenLength())
    46             throw new I2NPMessageException("Wrong length (expects " + calculateWrittenLength() + ", recv " + dataSize + ")");
    47        
    48         for (int i = 0; i < RECORD_COUNT; i++) {
    49             int off = offset + (i * RECORD_SIZE);
    50             int len = RECORD_SIZE;
    51             byte rec[] = new byte[RECORD_SIZE];
    52             System.arraycopy(data, off, rec, 0, RECORD_SIZE);
    53             setRecord(i, new ByteArray(rec));
    54             //setRecord(i, new ByteArray(data, off, len));
    55         }
    56     }
    57    
    58     protected int writeMessageBody(byte[] out, int curIndex) throws I2NPMessageException {
    59         int remaining = out.length - (curIndex + calculateWrittenLength());
    60         if (remaining < 0)
    61             throw new I2NPMessageException("Not large enough (too short by " + remaining + ")");
    62         for (int i = 0; i < RECORD_COUNT; i++) {
    63             System.arraycopy(_records[i].getData(), _records[i].getOffset(), out, curIndex, RECORD_SIZE);
    64             curIndex += RECORD_SIZE;
    65         }
    66         return curIndex;
    67     }
    6826
    6927    @Override
  • router/java/src/net/i2p/data/i2np/VariableTunnelBuildMessage.java

    r32d9204 ref40282  
    88
    99/**
     10 * Variable number of records.
     11 *
    1012 * @since 0.7.12
    1113 */
     
    3032    @Override
    3133    public void readMessage(byte[] data, int offset, int dataSize, int type) throws I2NPMessageException, IOException {
    32         if (type != MESSAGE_TYPE)
    33             throw new I2NPMessageException("Message type is incorrect for this message");
     34        // message type will be checked in super()
    3435        int r = (int)DataHelper.fromLong(data, offset, 1);
    3536        if (r <= 0 || r > MAX_RECORD_COUNT)
     
    3940            throw new I2NPMessageException("Wrong length (expects " + calculateWrittenLength() + ", recv " + dataSize + ")");
    4041        _records = new ByteArray[RECORD_COUNT];
    41         super.readMessage(data, offset + 1, dataSize, TunnelBuildMessage.MESSAGE_TYPE);
     42        super.readMessage(data, offset + 1, dataSize, type);
    4243    }
    4344   
  • router/java/src/net/i2p/data/i2np/VariableTunnelBuildReplyMessage.java

    r32d9204 ref40282  
    99/**
    1010 * Transmitted from the new outbound endpoint to the creator through a
    11  * reply tunnel
     11 * reply tunnel.
     12 * Variable number of records.
    1213 *
    1314 * @since 0.7.12
     
    3132    public int getType() { return MESSAGE_TYPE; }
    3233
     34    @Override
    3335    public void readMessage(byte[] data, int offset, int dataSize, int type) throws I2NPMessageException, IOException {
    34         if (type != MESSAGE_TYPE)
    35             throw new I2NPMessageException("Message type is incorrect for this message");
     36        // message type will be checked in super()
    3637        int r = (int)DataHelper.fromLong(data, offset, 1);
    3738        if (r <= 0 || r > MAX_RECORD_COUNT)
     
    4142            throw new I2NPMessageException("Wrong length (expects " + calculateWrittenLength() + ", recv " + dataSize + ")");
    4243        _records = new ByteArray[RECORD_COUNT];
    43         super.readMessage(data, offset + 1, dataSize, TunnelBuildReplyMessage.MESSAGE_TYPE);
     44        super.readMessage(data, offset + 1, dataSize, type);
    4445    }
    4546   
     47    @Override
    4648    protected int writeMessageBody(byte[] out, int curIndex) throws I2NPMessageException {
    4749        int remaining = out.length - (curIndex + calculateWrittenLength());
Note: See TracChangeset for help on using the changeset viewer.