I2P Address: [http://git.idk.i2p]

Skip to content
Snippets Groups Projects
TunnelCreateStatusMessage.java 4.24 KiB
Newer Older
package net.i2p.data.i2np;
/*
 * free (adj.): unencumbered; not under the control of others
 * Written by jrandom in 2003 and released into the public domain
 * with no warranty of any kind, either expressed or implied.
 * It probably won't make your computer catch on fire, or eat
 * your children, but it might.  Use at your own risk.
 *
 */

import java.io.IOException;
import net.i2p.I2PAppContext;
import net.i2p.data.DataHelper;
import net.i2p.data.TunnelId;
import net.i2p.util.Log;

/**
 * Defines the message a router sends to another router in reply to a
 * TunnelCreateMessage
 *
 * @author jrandom
 */
public class TunnelCreateStatusMessage extends I2NPMessageImpl {
    private final static Log _log = new Log(TunnelCreateStatusMessage.class);
    public final static int MESSAGE_TYPE = 7;
jrandom's avatar
jrandom committed
    private TunnelId _receiveTunnelId;
    private int _status;
jrandom's avatar
jrandom committed
    private long _nonce;
    
    public final static int STATUS_SUCCESS = 0;
    
    public TunnelCreateStatusMessage(I2PAppContext context) {
        super(context);
jrandom's avatar
jrandom committed
        setReceiveTunnelId(null);
jrandom's avatar
jrandom committed
        setNonce(-1);
jrandom's avatar
jrandom committed
    public TunnelId getReceiveTunnelId() { return _receiveTunnelId; }
    public void setReceiveTunnelId(TunnelId id) { 
        _receiveTunnelId = id; 
jrandom's avatar
jrandom committed
        if ( (id != null) && (id.getTunnelId() <= 0) )
            throw new IllegalArgumentException("wtf, tunnelId " + id);
    }
    
    public int getStatus() { return _status; }
    public void setStatus(int status) { _status = status; }
    
jrandom's avatar
jrandom committed
    public long getNonce() { return _nonce; }
    public void setNonce(long nonce) { _nonce = nonce; }
jrandom's avatar
jrandom committed
    public void readMessage(byte data[], int offset, int dataSize, int type) throws I2NPMessageException, IOException {
        if (type != MESSAGE_TYPE) throw new I2NPMessageException("Message type is incorrect for this message");
jrandom's avatar
jrandom committed
        int curIndex = offset;
        
jrandom's avatar
jrandom committed
        _receiveTunnelId = new TunnelId(DataHelper.fromLong(data, curIndex, 4));
jrandom's avatar
jrandom committed
        curIndex += 4;
        
jrandom's avatar
jrandom committed
        if (_receiveTunnelId.getTunnelId() <= 0)
            throw new I2NPMessageException("wtf, negative tunnelId? " + _receiveTunnelId);
jrandom's avatar
jrandom committed
        
        _status = (int)DataHelper.fromLong(data, curIndex, 1);
        curIndex++;
jrandom's avatar
jrandom committed
        
        _nonce = DataHelper.fromLong(data, curIndex, 4);
jrandom's avatar
jrandom committed
        
jrandom's avatar
jrandom committed
    /** calculate the message body's length (not including the header and footer */
    protected int calculateWrittenLength() { 
jrandom's avatar
jrandom committed
        return 4 + 1 + 4; // id + status + nonce
jrandom's avatar
jrandom committed
    }
    /** write the message body to the output array, starting at the given index */
    protected int writeMessageBody(byte out[], int curIndex) throws I2NPMessageException {
jrandom's avatar
jrandom committed
        if ( (_receiveTunnelId == null) || (_nonce <= 0) ) throw new I2NPMessageException("Not enough data to write out");
        if (_receiveTunnelId.getTunnelId() <= 0) throw new I2NPMessageException("Invalid tunnelId!? " + _receiveTunnelId);
jrandom's avatar
jrandom committed
        DataHelper.toLong(out, curIndex, 4, _receiveTunnelId.getTunnelId());
        curIndex += 4;
        DataHelper.toLong(out, curIndex, 1, _status);
        curIndex++;
        DataHelper.toLong(out, curIndex, 4, _nonce);
jrandom's avatar
jrandom committed
        curIndex += 4;
        return curIndex;
    }
    
    public int getType() { return MESSAGE_TYPE; }
    
sponge's avatar
sponge committed
    @Override
    public int hashCode() {
jrandom's avatar
jrandom committed
        return DataHelper.hashCode(getReceiveTunnelId()) +
jrandom's avatar
jrandom committed
               (int)getNonce();
sponge's avatar
sponge committed
    @Override
    public boolean equals(Object object) {
        if ( (object != null) && (object instanceof TunnelCreateStatusMessage) ) {
            TunnelCreateStatusMessage msg = (TunnelCreateStatusMessage)object;
jrandom's avatar
jrandom committed
            return DataHelper.eq(getReceiveTunnelId(),msg.getReceiveTunnelId()) &&
                   DataHelper.eq(getNonce(),msg.getNonce()) &&
                   (getStatus() == msg.getStatus());
        } else {
            return false;
        }
    }
    
sponge's avatar
sponge committed
    @Override
    public String toString() {
        StringBuilder buf = new StringBuilder();
        buf.append("[TunnelCreateStatusMessage: ");
jrandom's avatar
jrandom committed
        buf.append("\n\tTunnel ID: ").append(getReceiveTunnelId());
        buf.append("\n\tStatus: ").append(getStatus());
jrandom's avatar
jrandom committed
        buf.append("\n\tNonce: ").append(getNonce());
        buf.append("]");
        return buf.toString();
    }
}