📄 rreqpacket.java
字号:
package dsr;import java.io.*;import java.util.*;import java.net.*;//this implements the RREQ packetclass RREQPacket { public byte type; //this is for the type of packet. again not required. public short flags; //implements the various flags.. public byte hopCount; //hop count public int floodID; //unique flood ID to identify the packet public InetAddress dstIPAddr; //destination ip adddress to which the route is sought public int dstSeqNum; //destination sequence number public InetAddress srcIPAddr; //source address from where the route is sought public int srcSeqNum; //source sequence number static int tmpID = 0; //static int to generate the unique flood ID private byte RREQ_ = (byte)1; //default constructor public RREQPacket() { type = RREQ_; flags = (short)1; hopCount = (byte)hopCount; dstSeqNum = srcSeqNum = floodID = 0; } //constructor public RREQPacket (byte type, short flags, byte hopCount, InetAddress dstIPAddr, int dstSeqNum, InetAddress srcIPAddr, int srcSeqNum) { this.type = type; this.flags = flags; this.hopCount = hopCount; this.floodID = ++tmpID; this.dstIPAddr = dstIPAddr; this.dstSeqNum = dstSeqNum; this.srcIPAddr = srcIPAddr; this.srcSeqNum = srcSeqNum; } //copy constructor public RREQPacket(RREQPacket rhs ) { this.type = rhs.type; this.flags = rhs.flags; this.hopCount = rhs.hopCount; this.floodID = rhs.floodID; this.dstIPAddr = rhs.dstIPAddr; this.dstSeqNum = rhs.dstSeqNum; this.srcIPAddr = rhs.srcIPAddr; this.srcSeqNum = rhs.srcSeqNum; } //this function is to encode the RREQ packet into a datagram packet. public static byte[] encode(RREQPacket packet) { ByteArrayOutputStream buf = new ByteArrayOutputStream(); DataOutputStream out = new DataOutputStream(buf); try { out.writeByte(packet.type); out.writeShort(packet.flags); out.writeByte(packet.hopCount); out.writeInt(packet.floodID); try{ out.write((packet.dstIPAddr).getAddress(), 0, 4); } catch (NullPointerException e) { System.out.println("the message is " + e.getMessage()); } out.writeInt(packet.dstSeqNum); try { out.write(packet.srcIPAddr.getAddress(), 0, 4); } catch (NullPointerException e) {} out.writeInt(packet.srcSeqNum); out.writeInt(packet.srcSeqNum); out.flush(); } catch (IOException ie) { System.out.println("IO ERROR\n"); } return buf.toByteArray(); } //this function decodes the datagram packet and gets the RREQ packet info public RREQPacket decode(DatagramPacket packet) { ByteArrayInputStream payload = new ByteArrayInputStream(packet.getData()); DataInputStream src = new DataInputStream(payload); try { this.type = src.readByte(); this.flags = src.readShort(); this.hopCount = src.readByte(); this.floodID = src.readInt(); byte b[] = new byte[4]; src.read(b, 0, 4); this.dstIPAddr = InetAddress.getByAddress(b); this.dstSeqNum = src.readInt(); src.read(b, 0 ,4); this.srcIPAddr = InetAddress.getByAddress(b); this.srcSeqNum = src.readInt(); } catch (IOException ie) { System.out.println("IO ERROR in decode\n"); } return this; } //put info in displayable form public String toString() { final String EOLN = java.lang.System.getProperty("line.separator"); String value = "type = " + type + EOLN + "flags = " + flags + EOLN + "hop Count = " + hopCount + EOLN + "Flood ID = " + floodID + EOLN + "Destination IP Address = " + dstIPAddr + EOLN + "Destinatoin Sequence # = " + dstSeqNum + EOLN + "Source IP Address = " + srcIPAddr + EOLN + "Source Sequence # = " + srcSeqNum + EOLN; return value; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -