📄 rreppacket.java
字号:
package dsr;import java.net.*;import java.util.*;import java.lang.*;import java.io.*;//This implements the RREP packetclass RREPPacket { public byte type; //the type of the packet. not really required here since all //packets have seperate class public short flags; //represents the flags information public byte hopCount; //hop count public int dstSeqNum; //destination sequence number public InetAddress dstIPAddr; //destination ip address public InetAddress orgIPAddr; //originator's ip address// public int lifetime; //default constructor public RREPPacket() {} //constructor public RREPPacket (byte type, byte hopCount, int dstSeqNum, InetAddress dstIPAddr, InetAddress orgIPAddr) { this.type = type; this.flags = (byte)2; this.hopCount = hopCount; this.dstSeqNum = dstSeqNum; this.dstIPAddr = dstIPAddr; this.orgIPAddr = orgIPAddr; //this.lifetime = lifetime; } //copy constructor public RREPPacket (RREPPacket rhs) { this.type = rhs.type; this.flags = rhs.flags; this.hopCount = rhs.hopCount; this.dstSeqNum = rhs.dstSeqNum; this.dstIPAddr = rhs.dstIPAddr; this.orgIPAddr = rhs.orgIPAddr; //this.lifetime = rhs.lifetime; } //decodes the datagram packet to get the RREP packet information //the datagram packet transmitted over the net is in bytes which needs to be decoded public RREPPacket 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.dstSeqNum = src.readInt(); byte b[] = new byte[4]; src.read(b, 0, 4); this.dstIPAddr = InetAddress.getByAddress(b); src.read(b, 0, 4); this.orgIPAddr = InetAddress.getByAddress(b); //this.lifetime = src.readInt(); } catch (IOException ie) { System.out.println("IO ERROR in decode\n"); } return this; } //since the datagram packet has to be in bytes when being transmitted over the net //the RREP packet has to be converted into bytes first public byte[] encode(RREPPacket packet) { ByteArrayOutputStream buf = new ByteArrayOutputStream(); DataOutputStream out = new DataOutputStream(buf); try { out.writeByte(this.type); out.writeShort(this.flags); out.writeByte(this.hopCount); out.writeInt(this.dstSeqNum); try{ out.write((packet.dstIPAddr).getAddress(), 0, 4); out.write((packet.orgIPAddr).getAddress(), 0, 4); } catch (NullPointerException e) { System.out.println("the message is " + e.getMessage()); } //out.writeInt(this.lifetime); out.flush(); } catch (IOException ie) { System.out.println("IO ERROR\n"); } return buf.toByteArray(); } //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 + "Destination Sequence # = " + dstSeqNum + EOLN + "Destination Address = " + dstIPAddr + EOLN + "Originator IP Address = " + orgIPAddr + EOLN; // "Lifetime = " + lifetime + EOLN; return value; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -