File: queue.cc

package info (click to toggle)
nam 1.15-5
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 29,240 kB
  • sloc: cpp: 17,338; tcl: 10,655; sh: 2,997; ansic: 1,252; makefile: 139; perl: 66
file content (289 lines) | stat: -rw-r--r-- 7,948 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
 * Copyright (c) 1991,1993 Regents of the University of California.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *  This product includes software developed by the Computer Systems
 *  Engineering Group at Lawrence Berkeley Laboratory.
 * 4. Neither the name of the University nor of the Laboratory may be used
 *    to endorse or promote products derived from this software without
 *    specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * @(#) $Header: /cvsroot/nsnam/nam-1/queue.cc,v 1.18 2001/04/18 00:14:16 mehringe Exp $ (LBL)
 */

#ifdef WIN32
#include <windows.h>
#endif
#include "netview.h"
#include "psview.h"
#include "queue.h"
#include "drop.h"
#include "monitor.h"
#include "sincos.h"

QueueItem::QueueItem(const PacketAttr& p, double tim, long offset) :
  Animation(tim, offset),
  qnext_(0) {
  pkt_ = p;
}

QueueItem::~QueueItem()
{
  if (monitor_!=NULL) {
    monitor_->delete_monitor_object(this);
  }
}

//----------------------------------------------------------------------
// void 
// QueueItem::draw(View* nv, double time )
//   - Draws a packet queue on a link next to the node  
//   - only draw things that are within the bounding
//     box... otherwise, we might hang the display :( -kfall
//
//   - note: this is an incomplete fix; there remains
//           a bug where the packets in the queue aren't erased
//           properly when this case triggers 8/8/99
//
//           I believe I fixed this bug. It was due to th bounding 
//           box for the view was not being calculated properly so
//           queues would be cut off before they reached their proper
//           height.   -John Mehringer
//----------------------------------------------------------------------
void 
QueueItem::draw(View* nv, double time ) {
  BBox bb;
  nv->BoundingBox(bb);

  if (bb.inside(bb_)) {
    nv->fill(px_, py_, 4, paint_);
    if (monitor_ != NULL)
        monitor_->draw(nv, px_[0],py_[0]);
  }
}

float QueueItem::distance(float x, float y) const 
{
  float cx, cy;
  cx = (bb_.xmin + bb_.xmax) / 2;
  cy = (bb_.ymin + bb_.ymax) / 2;
  return ((cx-x) * (cx-x) + (cy-y)*(cy-y));
}

//---------------------------------------------------------------------
// void
// QueueItem::position(float& x, float& y)
//   - Gets the position (left corner) of the queue item and puts
//     it into x and y. 
//---------------------------------------------------------------------
void
QueueItem::position(float& x, float& y) {
  // Need to find out where px_ and py_ are being set.
  // Look at locate and Queue relocate.
  x = px_[0];
  y = py_[0];
}

void QueueItem::update_bb()
{
  bb_.xmin = bb_.xmax = px_[0];
  bb_.ymin = bb_.ymax = py_[0];
  for (int i = 0; i < 4; i++) {
    if (px_[i] < bb_.xmin)
      bb_.xmin = px_[i];
    if (px_[i] > bb_.xmax)
      bb_.xmax = px_[i];
    if (py_[i] < bb_.ymin)
      bb_.ymin = py_[i];
    if (py_[i] < bb_.ymax)
      bb_.ymax = py_[i];
  }
}

//----------------------------------------------------------------------
// void 
// QueueItem::locate(float x, float y, float dx, float dy)
//   - Place a queue item with it's bottom left corner at x,y.
//   - dx, dy are the width and height of the diagonal of the diamond
//     drawn to represent the queue item.
//   - In a vertical queue dx is 0 due to the way the Queue::place()
//     calculates packet height and width.
//----------------------------------------------------------------------
void
QueueItem::locate(float x, float y, float dx, float dy) {
  // Bottom Left Corner
  px_[0] = x;
  py_[0] = y;

  // Bottom Right Corner 
  x += dy;
  y -= dx;
  px_[1] = x;
  py_[1] = y;

  // Top Right Corner
  x += dx;
  y += dy;
  px_[2] = x;
  py_[2] = y;

  // Top  Left Corner
  x -= dy;
  y += dx;
  px_[3] = x;
  py_[3] = y;

  update_bb();
}

const char* QueueItem::info() const
{
  static char text[128];
  sprintf(text, "%s %d: %s\n   %d bytes\n",
    pkt_.type, pkt_.id, pkt_.convid, pkt_.size);
  return (text);
}

const char* QueueItem::getname() const
{
  static char text[128];
  sprintf(text, "p");
  return (text);
}

void QueueItem::monitor(Monitor *m, double /*now*/, char *result, int /*len*/)
{
  monitor_=m;
  sprintf(result, "%s %d: %s\n   %d bytes\n",
                pkt_.type, pkt_.id, pkt_.convid, pkt_.size);
}

MonState *QueueItem::monitor_state()
{
  // return any state we wish the monitor to remember after we're gone
  MonState *ms=new MonState;
  ms->type=MON_PACKET;
  ms->pkt.id=pkt_.id;
  return ms;
}

Queue::Queue(float angle)
  : cnt_(0), psize_(0.), nb_(0), angle_(angle)
{
  head_ = 0;
  tail_ = &head_;
}

void Queue::place(double psize, double x, double y) {
  psize_ = psize;
  double qc, qs;
  SINCOSPI(angle_, &qs, &qc);

  dx_ = qc * psize_;
  dy_ = qs * psize_;

  px_ = 3 * dx_ / 4;
  py_ = 3 * dy_ / 4;

  qx_ = x + 2 * dx_;
  qy_ = y + 2 * dy_;
}

void Queue::relocate()
{
  float x = qx_, y = qy_;

  for (QueueItem *q = head_; q != 0; q = q->qnext_) {
    q->locate(x, y, px_, py_);
    x += dx_;
    y += dy_;
  }
}

QueueItem *Queue::remove(const PacketAttr& p, int& pos)
{
  QueueItem* q;
  pos = 0;
  for (QueueItem **pp = &head_; (q = *pp) != 0; pp = &q->qnext_) {
    ++pos;
    if (q->pkt_.id == p.id
        /* kfall && q->pkt_.attr == p.attr, see below */
        && q->pkt_.size == p.size
        && strcmp(q->pkt_.convid, p.convid) == 0
        && strcmp(q->pkt_.type, p.type)== 0) {
      --cnt_;
      nb_ -= q->pkt_.size;
      *pp = q->qnext_;
      if (*pp == 0)
        tail_ = pp;
      relocate();
      return (q);
    }
  }
  return (0);
  /* 
   * the removal of the attr comparison is needed
   * because when RED marks packets, the "-" records
   * have a different attrib than the corresponding "r" or
   * "+" records.  If one looks for matching attribs,
   * then all of the packets drained from the queue
   * that have ecn marks on them are never actually
   * erased, leaving curious packets laying around
   * in the queue even when the outgoing link is idle
   * -kfall 8/8/99
   */
}

void Queue::enque(QueueItem *q, int mode)
{
  /*add to the tail if time is running forwards, else add to the head*/
  if (mode==QUEUE_TAIL) {
    *tail_ = q;
    tail_ = &q->qnext_;
    q->qnext_ = 0;
    ++cnt_;
    nb_ += q->pkt_.size;
    relocate();
  } else if (mode==QUEUE_HEAD) {
    q->qnext_=head_;
    head_=q;
    ++cnt_;
    nb_ += q->pkt_.size;
    relocate();
  }
}

void Queue::reset(double /*now*/)
{
  while (head_ != 0) {
    QueueItem *n = head_->qnext_;
    delete head_;
    head_ = n;
  }
  head_ = 0;
  tail_ = &head_;
  cnt_ = 0;
  nb_ = 0;
}