⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rrep.java

📁 DSr project for the java applications
💻 JAVA
字号:
package dsr;import java.lang.*;import java.io.*;import java.util.*;import java.net.*;//this class implements the generation and reception of a RREP packetpublic class RREP {  //this function takes care of receiving an RREP packet. It first gets the RREP packet  //from the datagram packet and decodes it. It then updates the routing table (if needed)  //with the information in the RREP packet. And then if it is not the intended destination  //it forwards the packet towards the source  public static void recvRREP(EventQueueEntry workingEvent) {    RREPPacket rrep = new RREPPacket();    rrep = rrep.decode(workingEvent.packet);      //decodes the datagram packet    System.out.println("inside recvRREP");    //increase the hopCount.    rrep.hopCount++;    //check to see if it has a valid route to the destination that is mentioned in the    //RREP packet.    RouteTable.PrintRouteTable();    int pos = RouteTable.FindRouteTableEntry(rrep.dstIPAddr);    RouteTableEntry tmpRoute =          new RouteTableEntry(rrep.dstIPAddr, workingEvent.srcIP, rrep.hopCount, (byte)0);    if (pos == -1) { //does not have an entry to that destination      //should update its own routing table with that info.      RouteTable.AddRouteTableEntry(tmpRoute);    }    else {      RouteTableEntry entry = new RouteTableEntry(RouteTable.getThisEntry(pos));      //checks to see if the new information provides a better route to destination      if (entry.hopCount > rrep.hopCount) {        RouteTable.DeleteRouteTableEntry(pos);        RouteTable.AddRouteTableEntry(tmpRoute);      }    }    //update the route table for the node from where the RREP was received    pos = RouteTable.FindRouteTableEntry(workingEvent.srcIP);    if (pos == -1) {      RouteTable.AddRouteTableEntry(new RouteTableEntry(workingEvent.srcIP,          workingEvent.srcIP, (byte)1, (byte)0));    }    //need to check first if this packet was meant for me or not....    //if it is meant for me.. do nothing.. we have already taken care of that.    //InterfaceList.findInterface();    if (!InterfaceList.checkIfInterfacePresent(rrep.orgIPAddr)) {      //it then gets the next hop for the destination and forwards the packet to the source that      //needs the RREP packet.      pos = RouteTable.FindRouteTableEntry(rrep.orgIPAddr);      tmpRoute = RouteTable.getThisEntry(pos);      send(rrep, tmpRoute.nextHop);    }    RouteTable.PrintRouteTable();  }  //this function handles the generation of a RREP packet. So first if i was the one who is the  //destination required by the RREQ packet then i go ahead generate an RREP and send it to the  //next hop towards the source. if i am not destination but have a route to the destination then  //i send the RREP packet to the source and also send that to the destination so that the  //destination also has the reverse route to the source.  public static void genRREP(RREQPacket rreq, InetAddress recvAddr) {    //get the src and destination address from the RREQ    InetAddress src = rreq.srcIPAddr;    InetAddress dst = rreq.dstIPAddr;    //if i am the destination then i generate an RREP and send it to the next hop    RREPPacket outRREP = new RREPPacket(Const.RREP_TYPE, (byte)0, 0, dst, src);    int pos = RouteTable.FindRouteTableEntry(dst);    if (pos != -1){      RouteTableEntry tmpRoute = new RouteTableEntry(RouteTable.getThisEntry(pos));      if(tmpRoute.selfRoute == (byte)0) {        //i am the destination for that RREQ just send back the packet        System.out.println("it is the destination os it just sends the RREP to ");        send(outRREP, recvAddr);        System.out.println(recvAddr);      }      else {        //i am not the destination but there is a route through me        System.out.println("it is not the destination, so it sends the RREP both ways");        send(outRREP, recvAddr);        System.out.println(recvAddr);        send(outRREP, tmpRoute.nextHop);        System.out.println(tmpRoute.nextHop);      }    }  }  //sends the packet to the next hop  public static void send(RREPPacket r, InetAddress dst) {    byte[] bytesToSend = r.encode(r);    DatagramPacket sendPacket = new DatagramPacket(bytesToSend, bytesToSend.length,          dst, Const.DSRPORT);    //send it to that particular socket.    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 + -