00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 #include <object.h>
00044 #include <queue.h>
00045 #include <drop-tail.h>
00046 #include <packet.h>
00047 #include <cmu-trace.h>
00048
00049 #include "priqueue.h"
00050
00051 typedef int (*PacketFilter)(Packet *, void *);
00052
00053 PriQueue_List PriQueue::prhead = { 0 };
00054
00055 static class PriQueueClass : public TclClass {
00056 public:
00057 PriQueueClass() : TclClass("Queue/DropTail/PriQueue") {}
00058 TclObject* create(int, const char*const*) {
00059 return (new PriQueue);
00060 }
00061 } class_PriQueue;
00062
00063
00064 PriQueue::PriQueue() : DropTail()
00065 {
00066 bind("Prefer_Routing_Protocols", &Prefer_Routing_Protocols);
00067 LIST_INSERT_HEAD(&prhead, this, link);
00068 }
00069
00070 int
00071 PriQueue::command(int argc, const char*const* argv)
00072 {
00073 if (argc == 2 && strcasecmp(argv[1], "reset") == 0)
00074 {
00075 Terminate();
00076
00077 }
00078 return DropTail::command(argc, argv);
00079 }
00080
00081 void
00082 PriQueue::recv(Packet *p, Handler *h)
00083 {
00084 struct hdr_cmn *ch = HDR_CMN(p);
00085
00086 if(Prefer_Routing_Protocols) {
00087
00088 switch(ch->ptype()) {
00089 case PT_DSR:
00090 case PT_MESSAGE:
00091 case PT_TORA:
00092 case PT_AODV:
00093 recvHighPriority(p, h);
00094 break;
00095
00096 default:
00097 Queue::recv(p, h);
00098 }
00099 }
00100 else {
00101 Queue::recv(p, h);
00102 }
00103 }
00104
00105
00106 void
00107 PriQueue::recvHighPriority(Packet *p, Handler *)
00108
00109 {
00110 q_->enqueHead(p);
00111 if (q_->length() >= qlim_)
00112 {
00113 Packet *to_drop = q_->lookup(q_->length()-1);
00114 q_->remove(to_drop);
00115 drop(to_drop);
00116 }
00117
00118 if (!blocked_) {
00119
00120
00121
00122
00123
00124
00125 p = deque();
00126 if (p != 0) {
00127 blocked_ = 1;
00128 target_->recv(p, &qh_);
00129 }
00130 }
00131 }
00132
00133 void
00134 PriQueue::filter(PacketFilter filter, void * data)
00135
00136
00137
00138 {
00139 int i = 0;
00140 while (i < q_->length())
00141 {
00142 Packet *p = q_->lookup(i);
00143 if (filter(p,data))
00144 {
00145 q_->remove(p);
00146 }
00147 else i++;
00148 }
00149 }
00150
00151 Packet*
00152 PriQueue::filter(nsaddr_t id)
00153 {
00154 Packet *p = 0;
00155 Packet *pp = 0;
00156 struct hdr_cmn *ch;
00157
00158 for(p = q_->head(); p; p = p->next_) {
00159 ch = HDR_CMN(p);
00160 if(ch->next_hop() == id)
00161 break;
00162 pp = p;
00163 }
00164
00165
00166
00167
00168 if(p) {
00169 if(pp == 0)
00170 q_->remove(p);
00171 else
00172 q_->remove(p, pp);
00173 }
00174 return p;
00175 }
00176
00177
00178
00179
00180 void
00181 PriQueue::Terminate()
00182 {
00183 Packet *p;
00184 while((p = deque())) {
00185 drop(p, DROP_END_OF_SIMULATION);
00186
00187
00188 }
00189 }
00190
00191
00192
00193