/* * Author: Wenbing Zhao * Last Modified: 9/18/2006 * For EEC484/584 Project 1 */ // This class describes the frame structure used by the par protocol public class Frame { public static final byte[] PREAMBLE = {'E','E','C','4','8','4','/','5','8','4'}; public static final int MAX_FRAME_PAYLOAD = 1024; public static final int MAX_FRAME_SIZE = MAX_FRAME_PAYLOAD + 6 + PREAMBLE.length; // payload length + header fields + preamble len public int seq; // sequence number for duplicate detection, actually 1 bit public int ack; // acked sequence number, actually 1 bit public int length; // payload length, really need 4 bytes public byte[] payload = new byte[MAX_FRAME_PAYLOAD]; private boolean m_isValid = true; public Frame() { seq = -1; ack = -1; length = 0; } public Frame(byte[] receivedData) { // Does the frame carries the right preamble? m_isValid = verifyFrame(receivedData); int index = PREAMBLE.length; if(m_isValid) { //System.out.println("Valid frame"); // set seq number seq = receivedData[index++]; // set ack sequence number ack = receivedData[index++]; //System.out.println("b1: "+receivedData[index]); //System.out.println("b2: "+receivedData[index+1]); //System.out.println("b3: "+receivedData[index+2]); //System.out.println("b4: "+receivedData[index+3]); // payload length byte[] intArray = {receivedData[index], receivedData[index+1], receivedData[index+2], receivedData[index+3]}; index += 4; length = ByteArrayUtils.readInt(intArray); // the rest is the application payload //System.out.println("payload len: "+length); for(int i=0; i