001: /*
002: * Copyright (c) 2000 by Matt Welsh and The Regents of the University of
003: * California. All rights reserved.
004: *
005: * Permission to use, copy, modify, and distribute this software and its
006: * documentation for any purpose, without fee, and without written agreement is
007: * hereby granted, provided that the above copyright notice and the following
008: * two paragraphs appear in all copies of this software.
009: *
010: * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
011: * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
012: * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
013: * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
014: *
015: * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
016: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
017: * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
018: * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
019: * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
020: *
021: * Author: Matt Welsh <mdw@cs.berkeley.edu>
022: *
023: */
024:
025: package seda.sandStorm.lib.aSocket;
026:
027: import seda.sandStorm.api.*;
028: import seda.sandStorm.core.*;
029:
030: /**
031: * An ATcpInPacket represents a packet which was received from an
032: * asynchronous socket. When a packet is received on a connection,
033: * an ATcpInPacket is pushed to the SinkIF associated with an
034: * ATcpConnection.
035: *
036: * @author Matt Welsh
037: * @see ATcpConnection
038: */
039: public class ATcpInPacket implements QueueElementIF {
040:
041: private ATcpConnection conn;
042: private BufferElement buf;
043: // package access
044: long seqNum;
045:
046: public ATcpInPacket(ATcpConnection conn, BufferElement buf) {
047: this .conn = conn;
048: this .buf = buf;
049: this .seqNum = 0;
050: }
051:
052: public ATcpInPacket(ATcpConnection conn, BufferElement buf,
053: long seqNum) {
054: this .conn = conn;
055: this .buf = buf;
056: this .seqNum = seqNum;
057: }
058:
059: public ATcpInPacket(ATcpConnection conn, byte data[], int len) {
060: this .conn = conn;
061: byte newdata[] = new byte[len];
062: System.arraycopy(data, 0, newdata, 0, len);
063: this .buf = new BufferElement(newdata);
064: this .seqNum = 0;
065: }
066:
067: public ATcpInPacket(ATcpConnection conn, byte data[], int len,
068: long seqNum) {
069: this .conn = conn;
070: byte newdata[] = new byte[len];
071: System.arraycopy(data, 0, newdata, 0, len);
072: this .buf = new BufferElement(newdata);
073: this .seqNum = seqNum;
074: }
075:
076: public ATcpInPacket(ATcpConnection conn, byte data[], int len,
077: boolean copy) {
078: this .conn = conn;
079: if (copy) {
080: byte newdata[] = new byte[len];
081: System.arraycopy(data, 0, newdata, 0, len);
082: this .buf = new BufferElement(newdata);
083: } else {
084: this .buf = new BufferElement(data, 0, len);
085: }
086: this .seqNum = 0;
087: }
088:
089: public ATcpInPacket(ATcpConnection conn, byte data[], int len,
090: boolean copy, long seqNum) {
091: this .conn = conn;
092: if (copy) {
093: byte newdata[] = new byte[len];
094: System.arraycopy(data, 0, newdata, 0, len);
095: this .buf = new BufferElement(newdata);
096: } else {
097: this .buf = new BufferElement(data, 0, len);
098: }
099: this .seqNum = seqNum;
100: }
101:
102: /**
103: * Return the connection from which this packet was received.
104: */
105: public ATcpConnection getConnection() {
106: return conn;
107: }
108:
109: /**
110: * Return the data from an incoming TCP packet.
111: */
112: public byte[] getBytes() {
113: return buf.data;
114: }
115:
116: /**
117: * Return the size of the packet data.
118: */
119: public int size() {
120: return buf.size;
121: }
122:
123: /**
124: * Return the BufferElement associated with the packet data.
125: */
126: public BufferElement getBufferElement() {
127: return buf;
128: }
129:
130: /**
131: * Return the sequence number associated with this packet.
132: * Sequence numbers range from 1 to Long.MAX_VALUE, then wrap
133: * around to Long.MIN_VALUE. A sequence number of 0 indicates that
134: * no sequence number was associated with this packet when it was
135: * created.
136: */
137: public long getSequenceNumber() {
138: return seqNum;
139: }
140:
141: public String toString() {
142: return "ATcpInPacket [conn=" + conn + ", size=" + buf.size
143: + "]";
144: }
145:
146: }
|