 1Content-type: application/x-Unknown; charset=UTF8package nmc.common;

import java.io.DataOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.IOException;

public abstract class Message extends Object {
    public static final int SET_CURRENT_LIMIT_CMD       = 10;  // obsolete
    public static final int ACK_REPLY                   = 11;
    public static final int START_STREAMING_CMD         = 12;  // obsolete
    public static final int STOP_STREAMING_CMD          = 13;  // obsolete
    public static final int STREAMING_DATA_REPLY        = 14;  // obsolete
    public static final int POLL_CMD                    = 15;
    public static final int POLLED_DATA_REPLY           = 16;
    public static final int ERROR_REPLY                 = 18;  // obsolete
    public static final int ECHO_CMD                    = 19;  // ?
    public static final int ECHO_REPLY                  = 20;  // ?
    public static final int INITIALIZE_INSTRUMENT_CMD   = 21;  // ??
    public static final int INITIALIZE_INSTRUMENT_REPLY = 22;  // ??
    public static final int CONFIGURATION_CMD           = 23;
    public static final int CONFIGURATION_REPLY         = 24;
    public static final int WAKEUP_CMD                  = 25;
    public static final int GET_ABSOLUTE_TIME_CMD       = 26;
    public static final int ABSOLUTE_TIME_REPLY         = 27;

    protected int seqNo;    // the sequence number
    protected int len;      // length (in bytes) beyond this

    public abstract int getMessageType(); // one of the above types

    // uses the cerealize(DataOutputStream s), which must be
    // implemented by a subclass.
    public byte[] cerealize() throws IOException {
    	ByteArrayOutputStream bos= new ByteArrayOutputStream();
    	DataOutputStream dos = new DataOutputStream(bos);
    	this.cerealize(dos);
    	return bos.toByteArray();
    }

    protected void cerealize(DataOutputStream s) throws IOException {
    	s.writeInt(getMessageType());
    	s.writeInt(seqNo);
    	s.writeInt(len);
    }
    public void decerealize(DataInputStream s) throws IOException {
    	seqNo = s.readInt();
    	len = s.readInt();
    }
    public abstract String toString();
}
