File: lan.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 (382 lines) | stat: -rw-r--r-- 10,341 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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/*
 * Copyright (c) 1997 University of Southern 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 Information Sciences
 *      Institute of the University of Southern California.
 * 4. Neither the name of the University nor of the Institute 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.
 *
 */
#include "sincos.h"
#include "config.h"
#include "lan.h"
#include "edge.h"
#include "packet.h"
#include "node.h"
#include "view.h"
#include "psview.h"
#include "paint.h"
#include "netmodel.h"
#include "trace.h"

LanLink::LanLink(Edge *e): edge_(e)
{
  /*need to search through the destination node's links to find the link
    that's the twin of this one (i.e, same nodes, other direction) */
  int lan=e->src();
  Node *n=e->neighbor();
  Edge *te=n->links();
  while (te!=NULL) {
    if (te->dst()==lan) {
      pair_=te;
      break;
    }
    te=te->next_;
  }
}

int LanLink::placed()
{
  return edge_->neighbor()->marked();
}

//----------------------------------------------------------------------
// Lan::Lan(const char *name, NetModel *nm, double ps, double bw,
//          double delay, double angle)
//----------------------------------------------------------------------
Lan::Lan(const char *name, NetModel *nm, double ps, double bw,
         double delay, double angle) :
  Animation(0,0),
  nm_(nm),
  links_(NULL),
  ps_(ps),
  bw_(bw), 
  delay_(delay),
  angle_(angle),
  marked_(0),
  max_(0) {

  virtual_node_ = new VirtualNode(name, this);
  name_ = new char[strlen(name) + 1];
  strcpy(name_, name);
  ln_ = atoi(name); /*XXX*/
  paint_ = Paint::instance()->thick();

  dropHash_ = new Tcl_HashTable;
  // XXX: unique packet id == (src, dst, id). Its size may be changed later.
  Tcl_InitHashTable(dropHash_, 3);
}

//----------------------------------------------------------------------
//----------------------------------------------------------------------
Lan::~Lan() {
  if (dropHash_ != NULL) {
    Tcl_DeleteHashTable(dropHash_);
    delete dropHash_;
  }

  delete virtual_node_;
}

float Lan::distance(float /*x*/, float /*y*/) const 
{
	// TODO: to be added
	return HUGE_VAL;
}

//----------------------------------------------------------------------
//----------------------------------------------------------------------
static int to_the_left(Edge *e, double angle, int incoming) {
  double edge_angle = e->angle();
  if (incoming)
    edge_angle = 1.0 + edge_angle;
  if (edge_angle > 2.0)
    edge_angle -= 2.0;

  double a = angle - edge_angle;

  if (a < 0) {
    a += 2.0;
  }

  if (a > 1.0) {
    return 1;
  } else {
    return 0;
  }
}


//----------------------------------------------------------------------
// void Lan::add_link(Edge *e)
//   - add a link to the shared bus line
//----------------------------------------------------------------------
void Lan::add_link(Edge *e) {
  int left = 0; 
  int right = 0;
  double sine, cosine, left_length, right_length;
  Node * source, * destination;
  LanLink * ll;

  ll = new LanLink(e);
  source = e->getSourceNode();
  destination = e->getDestinationNode();
  SINCOSPI(angle_, &sine, &cosine);

  ll->next(links_);
  links_ = ll;
  virtual_node_->add_link(e);

  // Keep track of how long the "bus" is
  ll = links_;
  while (ll != NULL) {
    if (to_the_left(ll->edge(), angle_, 0)) {
      left++;
    } else {
      right++;
    }
    ll = ll->next();
  }

  left_length = left * (destination->size() + source->size() + size_);
  right_length = right * (destination->size() + source->size() + size_);

  if (max_ < left_length) {
    max_ = (int) ceil(left_length);
   }

  if (max_ < right_length) {
    max_ = (int) ceil(right_length);
  }
}

//----------------------------------------------------------------------
//----------------------------------------------------------------------
void Lan::update_bb() {
	double s,c;
	SINCOSPI(angle_,&s,&c);
	bb_.xmin = x_ - size_*c;
        bb_.xmax = x_+size_*c*(2*max_+1);
	bb_.ymin = y_ - size_*s;
        bb_.ymax = y_+size_*s*(2*max_+1);
}

//----------------------------------------------------------------------
//----------------------------------------------------------------------
void
Lan::size(double size) {
  size_ = size;
  update_bb();
}

//----------------------------------------------------------------------
//----------------------------------------------------------------------
void
Lan::place(double x, double y) {
  x_ = x;
  y_ = y;
  update_bb();
}

//----------------------------------------------------------------------
//----------------------------------------------------------------------
void Lan::draw(class View* nv, double time) {
  double s, c;
  SINCOSPI(angle_, &s, &c);
  //nv->line(x_ - size_ * c, y_ - size_ * s,
  //         x_ + size_ * c * (2 * max_ + 1), y_ + size_ * s * (2 * max_ + 1),
  //         paint_);
  nv->line(x_ - size_ * c, y_ - size_ * s,
           x_ + size_ * c + c * max_, y_ + size_ * s + s * max_,
           paint_);
}

//void Lan::draw(class PSView* nv, double /*time*/) const {
/*
  double s,c;
  SINCOSPI(angle_,&s,&c);
  nv->line(x_-size_*c, y_-size_*s, x_+size_*c*(2*max_+1), 
 	   y_+size_*s*(2*max_+1), paint_);
}
*/
void Lan::remove_drop(const TraceEvent &e)
{
  int id[3];
  id[0] = e.pe.src;
  id[1] = e.pe.dst;
  id[2] = e.pe.pkt.id;
  Tcl_HashEntry *he = Tcl_FindHashEntry(dropHash_, (const char *)id);
  if (he != NULL) {
    TraceEvent *pe = (TraceEvent *)Tcl_GetHashValue(he);
    delete pe;
    Tcl_DeleteHashEntry(he);
  }
}

void Lan::register_drop(const TraceEvent &e)
{
	int newEntry = 1;
	int id[3];
	id[0] = e.pe.src;
	id[1] = e.pe.dst;
	id[2] = e.pe.pkt.id;
	Tcl_HashEntry *he = Tcl_CreateHashEntry(dropHash_, (const char *)id, 
						&newEntry);
	if (he == NULL)
		return;
	if (newEntry) {
	  TraceEvent *pe = new TraceEvent;
	  *pe = e;
	  Tcl_SetHashValue(he, (ClientData)pe);
	}
}

void Lan::arrive_packet(Packet *p, Edge *e, double atime) {
  /*need to duplicate the packet on all other links 
    except the arrival link*/
  LanLink *l=links_;
  PacketAttr pkt;
  int id[3];

  pkt.size=p->size();
  pkt.id=p->id();
  pkt.attr=p->attr();
  strcpy(pkt.type,p->type());
  strcpy(pkt.convid,p->convid());
  while (l!=NULL) {
    Edge *ne=l->edge();
    if (l->pair()!=e) {
      //       Packet *np = nm_->newPacket(pkt, ne, atime);
      nm_->newPacket(pkt, ne, atime);

      id[0] = ne->src();
      id[1] = ne->dst();
      id[2] = p->id();
      Tcl_HashEntry *he = Tcl_FindHashEntry(dropHash_, (const char *)id);
      if (he != NULL) {
	// This is a drop packet, fake a trace event and add a drop
	TraceEvent *pe = (TraceEvent *)Tcl_GetHashValue(he);
	pe->time = atime;
	// The fact that this trace event is still there implies that 
	// we are going forwards.
	nm_->add_drop(*pe, atime, FORWARDS);
	delete pe;
	Tcl_DeleteHashEntry(he);
      }
    }
    l=l->next();
  }
  rgb *color = Paint::instance()->paint_to_rgb(p->paint());
  paint_ = Paint::instance()->lookup(color->colorname, 5);
}

void Lan::delete_packet(Packet *) {
  paint_ = Paint::instance()->thick();
}

//----------------------------------------------------------------------
//----------------------------------------------------------------------
double Lan::x(Edge *e) const {
  double sine, cosine;
  SINCOSPI(angle_, &sine, &cosine);
  LanLink * l = links_;
  int incoming =- 1;
  int left = -1;
  int right = -1;
  Node * source, * destination;

  source = e->getSourceNode();
  destination = e->getDestinationNode();

  while (l != NULL) {
    if (to_the_left(l->edge(), angle_, 0)) {
      left++;
    } else {
      right++;
    }
    if (l->pair() == e) {
      incoming = 1;
      break;
    } else if (l->edge()==e) {
      incoming = 0;
      break;
    }
    l = l->next();
  }

  if (to_the_left(e, angle_, incoming)) {
    return x_ + cosine * left * (destination->size() + source->size() + size_);
    //return x_ + cosine * left * 2 * size_;
  } else {
    return x_ + cosine * right * (destination->size() + source->size() + size_);
    //return x_ + cosine * right * 2 * size_; 
  }
}

double Lan::y(Edge *e) const {
  double s,c;
  SINCOSPI(angle_,&s,&c);
  LanLink *l=links_;
  int incoming=-1;
  int left=-1;
  int right=-1;
  while (l!=NULL) {
    if (to_the_left(l->edge(), angle_,0)) {
      left++;
    } else {
      right++;
    }
    if (l->pair()==e) {
      incoming=1;
      break;
    } else if (l->edge()==e) {
      incoming=0;
      break;
    }
    l=l->next();
  }

  Node * destination = e->getDestinationNode();
  if (to_the_left(e, angle_, incoming)) {
    return y_ + s * left * destination->size();
  } else {
    return y_ + s * right * destination->size();
  }
}

#ifdef NOTDEF
Edge *Lan::lookupEdge(Node *n) {
  LanNode *ln=nodes_;
  while(ln!=NULL) {
    if (ln->node()==n)
    {
      return ln->e1_;
    }
  }
  return NULL;
}
#endif