📄 eventqueue.java
字号:
package dsr;import java.util.*;import java.net.*;import java.io.*;//this is an implementation of the Event Queue. Whenever a packet is received by//the server it is stored in the Event Queue and then it spawns a thread to//handle that particular packet.public class EventQueue { public static List eventList; //list of EventQueueEntry. //constructor public EventQueue() { eventList = Collections.synchronizedList(new LinkedList()); } //print out the EventQueue public static void printEventQueue() { Iterator l = eventList.iterator(); int i = 0; while (l.hasNext()) { i++; System.out.println("the element in the event queue is " + ((EventQueueEntry)l.next()).toString() ); } System.out.println("the event queue had " + i + " elements "); } //This code is for inserting a packet that was received into the event queue. public static boolean insertEventQueueEntry(DatagramPacket packet, InetAddress srcIP, InetAddress dstIP) { //the srcIP is the IP address of the node from which this packet was //received. //first it checks if this packet was generated by me. if yes then i don't need //to handle this packet. if(checkIfPacketBelongsToMe(packet, srcIP, dstIP)) return false; EventQueueEntry newEntry = new EventQueueEntry(packet, srcIP, dstIP); System.out.println("the entry is " + newEntry.toString()); /* try { if (!eventList.isEmpty() && eventList.contains(newEntry)){ return true; } } catch (NullPointerException e) { System.out.println("message is " + e.getMessage()); } */ //since this is not a packet that was generated by me i need to handle it //so i add it to the event queue. try{ eventList.add(newEntry); } catch (NullPointerException e) { System.out.println("message is " + e.getMessage()); } System.out.println("an element added to the eventQueue"); /*//kickDSR(); //start handling the packets. StartUp init = new StartUp(); Thread thread = new Thread(init); thread.start(); */ //This is what handles the packet depending on the type of the packet received. startDSR(); //System.out.println("Created a thread " + thread.getName() + "for starting the DSR protocol"); return true; } /* public void run() { StartUp s = new StartUp(); s.startDSR(); } */ //when a packet in inserted in the event queue. it calls the following function to //handle that particular packet. public static void startDSR() { System.out.println("ok the aodv has been started"); System.out.println("the entries in the eventqueue are as follows:- " ); EventQueue.printEventQueue(); EventQueueEntry workingEvent = new EventQueueEntry((EventQueueEntry)EventQueue.eventList.remove(0)); //we decode the datagram packet to get the type of the packet. //the type is required to find out what kind of packet it is and how it needs to be handled ByteArrayInputStream payload = new ByteArrayInputStream(workingEvent.packet.getData()); DataInputStream src = new DataInputStream(payload); byte type = (byte)1; System.out.println("after printing out the event queue"); try { type = src.readByte(); } catch (IOException ie) {} //handling the packet on the basis of its type switch(type) { case Const.RREQ_TYPE: System.out.println("ok ........ inside startDSR... RREQ pacet"); try { RREQ.recvRREQ(workingEvent); } catch (UnknownHostException e) {} break; case Const.RREP_TYPE: System.out.println("in side startDSR..... RREP packet"); RREP.recvRREP(workingEvent); break; case Const.EVENT_RREP_ACK: //RREP_ACK.recv.RREP_ACK(); break; case Const.EVENT_RERR: //RERR.recvRERR(); break; case Const.EVENT_CLEANUP: //RouteTable.findInactiveRouteTableEntry(); //floodQueue.deleteOldFloodIDQueueEntry(); break; default: break; } } //the following functions checks if the packet has been generated by me. reurns true //if it wasgenereated by me otherwise false. public static boolean checkIfPacketBelongsToMe(DatagramPacket packet, InetAddress srcIP, InetAddress dstIP) { Iterator vItr = InterfaceList.interfaceList.iterator(); InetAddress localAddress; while (vItr.hasNext()){ localAddress = ((InterfaceListEntry)vItr.next()).ipAddress; if (localAddress.equals(srcIP)) { return true; //found a matching entry. so it was generated by me } } return false; } //get the first entry in the event queue table public static EventQueueEntry getFirstEventQueueEntry() { return (EventQueueEntry)eventList.get(0); } //delete a particular entry in the Event queue public static boolean deleteEventQueueEntryByID(int ID) { EventQueueEntry tmp_entry = new EventQueueEntry(); for (int i=0; i < eventList.size(); ++i) { tmp_entry = (EventQueueEntry)eventList.get(i); if(tmp_entry.ID == ID) { try { eventList.remove(i); return true; } catch (ArrayIndexOutOfBoundsException e) { System.out.println("ArrayIndexOutOfBoundsException = " + e.getMessage()); } } } return false; } //delete the first entry in the Event Queue public static boolean deleteFirstEventQueueEntry() { try { eventList.remove(0); return true; } catch (ArrayIndexOutOfBoundsException e) { System.out.println("ArrayIndexOutOfBoundsException = " + e.getMessage()); } return false; } //clean up the whole of the event Queue public static void cleanUPEventQueue() { eventList.clear(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -