001: // @(#)HeaderMsg.java 1.5 "@(#)HeaderMsg.java 1.5 99/09/23 Sun Microsystems"
002:
003: package com.sun.portal.netlet.econnection;
004:
005: import java.io.ByteArrayOutputStream;
006: import java.io.DataInputStream;
007: import java.io.DataOutputStream;
008: import java.io.EOFException;
009: import java.io.IOException;
010: import java.net.SocketException;
011:
012: public abstract class HeaderMsg implements MessageConstants {
013:
014: protected byte version;
015:
016: protected short opCode;
017:
018: protected int msgLen;
019:
020: protected int seq = 0;
021:
022: // until I can find a better way to do size stuff...
023: protected final int HEADER_LEN = 11;
024:
025: protected final int MAX_MSG_LEN = 100000; // somewhat arbitrary...
026:
027: public HeaderMsg() {
028: }
029:
030: public HeaderMsg(byte vers, short op) {
031: version = vers;
032: opCode = op;
033: }
034:
035: public int readHeader(DataInputStream in) {
036: msgLen = 0;
037: try {
038: int tmp_seq;
039: do {
040: version = in.readByte();
041: opCode = in.readShort();
042: tmp_seq = in.readInt();
043: msgLen = in.readInt();
044: } while (opCode == DUMMY_MSG);
045:
046: if (tmp_seq != seq) {
047: System.out
048: .println("Netlet HeaderMsg: Unexpected sequence number");
049: return (-1);
050: }
051: seq++;
052: } catch (EOFException e) {
053: // conn closed, do nothing
054: return (-1);
055: } catch (SocketException e) {
056: return (-1);
057: // user closed session, do nothing
058: } catch (IOException e) {
059: System.out
060: .println("HeaderMsg: readHeader IOException:" + e);
061: return (-1);
062: }
063: return (0);
064: }
065:
066: public void writeHeaderToByteArray(ByteArrayOutputStream b) {
067: DataOutputStream b_out = new DataOutputStream(b);
068:
069: try {
070: b_out.writeByte(version);
071: b_out.writeShort(opCode);
072: b_out.writeInt(seq);
073: b_out.writeInt(msgLen);
074: seq++;
075: } catch (IOException e) {
076: }
077: }
078:
079: public abstract int readMsg(DataInputStream in);
080:
081: public abstract int writeMsg(DataOutputStream out);
082:
083: public byte getVersion() {
084: return (version);
085: }
086:
087: public void setVersion(int v) {
088: version = (byte) v; // if version get's big, this will overflow...
089: }
090:
091: public short getOpCode() {
092: return (opCode);
093: }
094:
095: public void setOpCode(short oc) {
096: opCode = oc;
097: }
098:
099: public int getMsgLen() {
100: return (msgLen);
101: }
102:
103: public void setMsgLen(int ml) {
104: msgLen = ml;
105: }
106: }
|