📄 rreq.java
字号:
package dsr;import java.net.*;import java.io.*;//this implements the code for the generation and the reception of the RREQ packets.public class RREQ { //this function handles the reception of the RREQ packets. It first decodes the datagrampacket //to get the RREQ packet. increases the hop count by 1. It first checks if this request //has been received before as well. if not then this packet has to be handled. so if i am the //destination for this RREQ packet then i generate a RREP packet and send it to the source //otherwise the RREQ packet is updated and locally broadcasted. public static void recvRREQ(EventQueueEntry workingEvent) throws UnknownHostException { //get the packet first System.out.println("inside the recvRREQ ::::::::::::::::::::::::::::::"); System.out.println("the packet was revceived from: " + workingEvent.packet.getAddress()); RREQPacket rreq = new RREQPacket(); rreq = rreq.decode(workingEvent.packet); //update the hopCount by 1 rreq.hopCount++; System.out.println("the packet in RREQ is " + rreq.toString()); System.out.println("the packet in RREQ is " + rreq.floodID); //find what interface the packet was received on InetAddress localAddress = workingEvent.dstIP; //look in the flood ID Queue to find out if the request was already received. FloodIDQueueEntry tmpFloodEntry = FloodIDQueue.findFloodIDQueueEntry(rreq.dstIPAddr, rreq.floodID); FloodIDQueue.PrintFloodIDQueue(); //Insert this packet to the floodIDQueue if it has to be dealt with if (tmpFloodEntry != null) { System.out.println("i am also looking for that address"); return; } long currTime = System.currentTimeMillis(); FloodIDQueue.insertFloodIDQueueEntry(rreq.srcIPAddr, rreq.dstIPAddr, rreq.floodID, currTime + Const.PATH_TRAVERSAL_TIME); //update reverse route entry RouteTable.updateReverseRouteTableEntry(workingEvent); //if route table has an valid entry to the destination int pos = RouteTable.FindRouteTableEntry(rreq.dstIPAddr); if (pos != -1) { //there is a valid entry or i, myself am the destination //generate a RREP and send it back RREP.genRREP(rreq, workingEvent.srcIP); System.out.println("generated an RREP message and is send back to the client"); //add code to send back the RREP to the node from where it was received. //the address can be obtained from the route table. } else { //else update the RREQ and do a local broadcast System.out.println("updated RREQ and done a locabroadcast"); RREQPacket outRREQ = new RREQPacket((byte)1, rreq.flags, rreq.hopCount, rreq.dstIPAddr, rreq.dstSeqNum, rreq.srcIPAddr, rreq.srcSeqNum); outRREQ.floodID = rreq.floodID; try { localBroadcast(outRREQ); } catch (UnknownHostException e) {} } } //this function takes care of the generation of the RREQ packets. It first checks in this //RREQ has already been generated. So if not, then create a new RREQ packet, insert that in //the floodID queue so that one can take care of duplicate RREQ packets. So enter that in the //timer queue so that retries can be made.. and then broad cast locally public static void genRREQ(InetAddress src, InetAddress dest) { //need to check if this RREQ has already been generated if (TimerQueue.checkForThisRREQInTimerQueue(src, dest, Const.EVENT_RREQ)) { return; } //create the RREQ packet RREQPacket outRREQ = new RREQPacket((byte)1, Const.EVENT_RREQ, (byte)0, dest, 0, src, 0); //insert it in the floodID queue so as to prevent duplicate packets FloodIDQueue.insertFloodIDQueueEntry(src, dest, outRREQ.floodID, Const.PATH_TRAVERSAL_TIME + System.currentTimeMillis()); FloodIDQueue.PrintFloodIDQueue(); //inseert it in the timer queue so that we can take care of the time till which time the //previously generated RREQ is valid TimerQueue.insertInTimerQueue(System.currentTimeMillis() + Const.PATH_TRAVERSAL_TIME, src, dest, (byte)2, Const.RREQ_TYPE); TimerQueue.PrintTimerQueue(); try { localBroadcast(outRREQ); } catch (UnknownHostException e) {} } //code for the local broad cast of the RREQ packet public static void localBroadcast(RREQPacket rreq) throws UnknownHostException { System.out.println("inside localbraodcast"); InetAddress broadcastAddress = InetAddress.getByName("255.255.255.255"); byte[] bytesToSend = rreq.encode(rreq); DatagramPacket sendPacket = new DatagramPacket(bytesToSend, bytesToSend.length, broadcastAddress, Const.DSRPORT); InetAddress localAddress = ((InterfaceListEntry)InterfaceList.interfaceList.lastElement()).ipAddress; try{ DatagramSocket sock = new DatagramSocket(0, localAddress); try { sock.send(sendPacket); }catch (IOException e) {} } catch (SocketException e) {} }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -