/** * Jeff Rupp * Master's Thesis * 2005 * * Class to represent a packet traveling through the network * will contain a byte array to represent the actual data, as well * as member varibles to keep various statistics */ package jdr.mobisim; import org.apache.log4j.*; public class MobilePacket extends PacketIF { private static Logger m_logger = Logger.getLogger(MobilePacket.class); // message types public static final byte INSIDE_CLUSTER = (byte)0; public static final byte BETWEEN_CLUSTER = (byte)1; public static final byte HELLO_MESSAGE = (byte)2; public static final byte CHOOSE_HEADS = (byte)3; public static final byte TELL_HEAD = (byte)4; public static final byte REJECT_HEAD = (byte)5; public static final byte RTS = (byte)6; public static final byte CTS = (byte)7; public static final byte ACK = (byte)8; public static final byte OK_TO_DO_NORMAL_COMM = (byte)9; public static final byte OVERRIDE_HEAD = (byte)10; // used by sink node to establish clusters public static final byte JOIN_CLUSTER = (byte)11; // used by lonely heads to join a cluster public static final byte LEACH_ADVERTISEMENT = (byte)12; public static final byte HEAD_TIRED = (byte)13; public static final byte CHILD_POWER = (byte)14; // Message format is for CHILD_POWER is 4 bytes, which represent the first 4 digits after the decimal of a double public static final byte ASSIGN_CHILD_NODES = (byte)15; // variable length message, int nodes // standard offsets for all messages public static final int SEQUENCE_NUMBER_BYTE_OFFSET = 0; public static final int ACK_SEQUENCE_NUMBER_BYTE_OFFSET = 4; public static final int SOURCE_NODE_BYTE_OFFSET = 8; public static final int DESTINATION_NODE_BYTE_OFFSET = 12; public static final int LAST_NODE_BYTE_OFFSET = 16; public static final int NEXT_NODE_BYTE_OFFSET = 20; public static final int MESSAGE_NUMBER_BYTE_OFFSET = 24; // fixed identifying number public static final int MESSAGE_TYPE_BYTE_OFFSET = 28; // the message length is limited to 128 bytes, by the data type ( using a byte ) public static final int MESSAGE_LENGTH_BYTE_OFFSET = 29; // offsets for specific message data (always starts here) public static final int BEGIN_GENERIC_DATA_BYTE_OFFSET = 30; // there will also always be a 4 byte CRC at the end of the message // the CRC value will be gotten (and set, to be able to break it) via a function call private long m_createTime = -1; private long m_arrivalTime = -1; private byte [] m_data; public MobilePacket() { m_createTime = Scheduler.getInstance().GetSimulationTime(); } public MobilePacket(long createTime, long arrivalTime, byte[] data, double hopCount, double pwrCons) { m_createTime = createTime; m_arrivalTime = arrivalTime; m_data = new byte[data.length]; System.arraycopy(data, 0, m_data, 0, data.length); m_hopCount = hopCount; m_powerConsumed = pwrCons; } public PacketIF duplicate() { MobilePacket packet = new MobilePacket(m_createTime, m_arrivalTime, m_data, m_hopCount, m_powerConsumed); return packet; } /** * The actual data contained in the packet, i.e. what would have been * transmited in the network * contents: * int sourceNode * int destinationNode * int lastNode * int nextNode * byte messageTypeFlags * 0 == inside Cluster message * 1 == between Clusters message * 2 == Cluster establishing message * byte messageLength * byte[messageLength] messageData */ public byte[] getData() { return m_data; } /** * sets the data that would traverse an actual network */ public void setData(byte[] data) { m_data = data; m_bitsTransmitted = m_data.length; synchronized(m_packetsLock) { // bits transmited doesn't change, so do the increment once, here. m_totalBitsTransmitted += m_bitsTransmitted; } } public double getTransmitedBitCount() { return (m_data.length * 8); } /** * The time at which the packet was transmitted */ public long getTransmitTime() { return m_createTime; } /** * The time at which the packet was transmitted, * settable to allow reuse of the same packet structure (for RTS) */ public void setTransmitTime(long createTime) { m_createTime = createTime; } /** * Sets the arrival time of the packet */ public void setPacketArrived() { setPacketArrived(Scheduler.getInstance().GetSimulationTime()); } public void setPacketArrived(long arrivalTime) { m_arrivalTime = arrivalTime; } /** * The time at which the packet arrived at its final destination */ public long getArrivalTime() { return m_arrivalTime; } public static String GetMessageTypeString(int msgType) { String stateStr = "unknown"; switch(msgType) { case INSIDE_CLUSTER: stateStr = "INSIDE_CLUSTER"; break; case BETWEEN_CLUSTER: stateStr = "BETWEEN_CLUSTER"; break; case HELLO_MESSAGE: stateStr = "HELLO_MESSAGE"; break; case CHOOSE_HEADS: stateStr = "CHOOSE_HEADS"; break; case TELL_HEAD: stateStr = "TELL_HEAD"; break; case REJECT_HEAD: stateStr = "REJECT_HEAD"; break; case RTS: stateStr = "RTS"; break; case CTS: stateStr = "CTS"; break; case ACK: stateStr = "ACK"; break; case OK_TO_DO_NORMAL_COMM: stateStr = "OK_TO_DO_NORMAL_COMM"; break; case OVERRIDE_HEAD: stateStr = "OVERRIDE_HEAD"; break; case JOIN_CLUSTER: stateStr = "JOIN_CLUSTER"; break; case LEACH_ADVERTISEMENT: stateStr = "LEACH_ADVERTISEMENT"; break; case HEAD_TIRED: stateStr = "HEAD_TIRED"; break; case CHILD_POWER: stateStr = "CHILD_POWER"; break; case ASSIGN_CHILD_NODES: stateStr = "ASSIGN_CHILD_NODES"; break; default: break; } return stateStr; } public String toString() { int sourceNode = MobilePacket.extractInt(m_data, MobilePacket.SOURCE_NODE_BYTE_OFFSET); int destinationNode = MobilePacket.extractInt(m_data, MobilePacket.DESTINATION_NODE_BYTE_OFFSET); int lastNode = MobilePacket.extractInt(m_data, MobilePacket.LAST_NODE_BYTE_OFFSET); int nextNode = MobilePacket.extractInt(m_data, MobilePacket.NEXT_NODE_BYTE_OFFSET); byte messageType = m_data[MobilePacket.MESSAGE_TYPE_BYTE_OFFSET]; byte messageLen = m_data[MobilePacket.MESSAGE_LENGTH_BYTE_OFFSET]; int seqNumber = MobilePacket.extractInt(m_data, MobilePacket.SEQUENCE_NUMBER_BYTE_OFFSET); String retVal = "transmit time: "+ m_createTime+ " \t arrival Time: " + m_arrivalTime+ " \t source node: "+ sourceNode + " \t destination node: "+ destinationNode+ " \t message type: "+ MobilePacket.GetMessageTypeString(messageType)+ " \t message length: "+ messageLen + " \t hop count: " + m_hopCount+ " \t power consumed: "+ m_powerConsumed+ " \t sequenceNumber: "+ seqNumber+ ""; return retVal; } } // end class definition