Debian Codegraph @

protolib/src/sim/ns/ns233/priqueue.cc

raw
1 /* -*- Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*- */
2 /*
3 * Copyright (c) 1997 Regents of the University of California.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by the Computer Systems
17 * Engineering Group at Lawrence Berkeley Laboratory.
18 * 4. Neither the name of the University nor of the Laboratory may be used
19 * to endorse or promote products derived from this software without
20 * specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34 /* Ported from CMU/Monarch's code, nov'98 -Padma.*/
35
36 /* -*- c++ -*-
37 priqueue.cc
38
39 A simple priority queue with a remove packet function
40 $Id: priqueue.cc,v 1.1 2008-07-07 17:38:46 dean Exp $
41 */
42
43 #include <object.h>
44 #include <queue.h>
45 #include <drop-tail.h>
46 #include <packet.h>
47 #include <cmu-trace.h>
48
49 #include "priqueue.h"
50
51 typedef int (*PacketFilter)(Packet *, void *);
52
53 PriQueue_List PriQueue::prhead = { 0 };
54
55 static class PriQueueClass : public TclClass {
56 public:
57 PriQueueClass() : TclClass("Queue/DropTail/PriQueue") {}
58 TclObject* create(int, const char*const*) {
59 return (new PriQueue);
60 }
61 } class_PriQueue;
62
63
64 PriQueue::PriQueue() : DropTail()
65 {
66 bind("Prefer_Routing_Protocols", &Prefer_Routing_Protocols);
67 LIST_INSERT_HEAD(&prhead, this, link);
68 }
69
70 int
71 PriQueue::command(int argc, const char*const* argv)
72 {
73 if (argc == 2 && strcasecmp(argv[1], "reset") == 0)
74 {
75 Terminate();
76 //FALL-THROUGH to give parents a chance to reset
77 }
78 return DropTail::command(argc, argv);
79 }
80
81 void
82 PriQueue::recv(Packet *p, Handler *h)
83 {
84 struct hdr_cmn *ch = HDR_CMN(p);
85
86 if(Prefer_Routing_Protocols) {
87
88 switch(ch->ptype()) {
89 case PT_DSR:
90 case PT_MESSAGE:
91 case PT_TORA:
92 case PT_AODV:
93 case PT_PROTOLIBMK:
94 recvHighPriority(p, h);
95 break;
96
97 default:
98 Queue::recv(p, h);
99 }
100 }
101 else {
102 Queue::recv(p, h);
103 }
104 }
105
106
107 void
108 PriQueue::recvHighPriority(Packet *p, Handler *)
109 // insert packet at front of queue
110 {
111 q_->enqueHead(p);
112 if (q_->length() >= qlim_)
113 {
114 Packet *to_drop = q_->lookup(q_->length()-1);
115 q_->remove(to_drop);
116 drop(to_drop);
117 }
118
119 if (!blocked_) {
120 /*
121 * We're not blocked. Get a packet and send it on.
122 * We perform an extra check because the queue
123 * might drop the packet even if it was
124 * previously empty! (e.g., RED can do this.)
125 */
126 p = deque();
127 if (p != 0) {
128 blocked_ = 1;
129 target_->recv(p, &qh_);
130 }
131 }
132 }
133
134 void
135 PriQueue::filter(PacketFilter filter, void * data)
136 // apply filter to each packet in queue,
137 // - if filter returns 0 leave packet in queue
138 // - if filter returns 1 remove packet from queue
139 {
140 int i = 0;
141 while (i < q_->length())
142 {
143 Packet *p = q_->lookup(i);
144 if (filter(p,data))
145 {
146 q_->remove(p); // decrements q len
147 }
148 else i++;
149 }
150 }
151
152 Packet*
153 PriQueue::filter(nsaddr_t id)
154 {
155 Packet *p = 0;
156 Packet *pp = 0;
157 struct hdr_cmn *ch;
158
159 for(p = q_->head(); p; p = p->next_) {
160 ch = HDR_CMN(p);
161 if(ch->next_hop() == id)
162 break;
163 pp = p;
164 }
165
166 /*
167 * Deque Packet
168 */
169 if(p) {
170 if(pp == 0)
171 q_->remove(p);
172 else
173 q_->remove(p, pp);
174 }
175 return p;
176 }
177
178 /*
179 * Called at the end of the simulation to purge the IFQ.
180 */
181 void
182 PriQueue::Terminate()
183 {
184 Packet *p;
185 while((p = deque())) {
186 drop(p, DROP_END_OF_SIMULATION);
187 //drop(p);
188
189 }
190 }
191
192
193
194