File: lossmodel.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 (403 lines) | stat: -rw-r--r-- 12,873 bytes parent folder | download | duplicates (4)
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*
 * Copyright (c) 2001 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 <stdlib.h>
#include <math.h>
#ifdef WIN32
#include <windows.h>
#endif
#include "netview.h"
#include "edge.h"
#include "paint.h"
#include "sincos.h"
#include "lossmodel.h"
#include "queuehandle.h"

#define PROPERTY_STRING_LENGTH 256

static int g_lossmodel_number = 1;

//----------------------------------------------------------------------
//  Wrapper for creating LossModels in OTcl
//----------------------------------------------------------------------
static class LossModelClass : public TclClass {
public:
  LossModelClass() : TclClass("LossModel") {}

  TclObject* create(int argc, const char * const * argv) {
    const char * type;
    int id;
    double size;

    type = argv[4];
    id = strtol(argv[5], NULL, 10);
    size = strtod(argv[6], NULL);
    
    return (new LossModel(type, id, size));
  }
} class_lossmodel;

//----------------------------------------------------------------------
//----------------------------------------------------------------------
LossModel::LossModel(const char* type, int id, double _size) :
  Animation(0, 0) {

  setDefaults();
  number_ = id;
  if (g_lossmodel_number++ <= id) {
    g_lossmodel_number = id + 1;
  }

  label_ = new char[strlen(type) + 1];
  strcpy(label_, type);

  width_ = 50.0 * strlen(type);
  height_ = size_/2.0;

  size_ = _size;
  size(_size);
}

//----------------------------------------------------------------------
//----------------------------------------------------------------------
LossModel::LossModel(const char* name, double _size) :
  Animation(0, 0) {

  setDefaults();
  
  number_ = g_lossmodel_number;
  g_lossmodel_number++;

  width_ = 50.0 * strlen(name);
  height_ = size_/2.0;
  size_ = _size;
  label_ = new char[strlen(name) + 1];
  strcpy(label_, name);
  size(_size);
}

//----------------------------------------------------------------------
//----------------------------------------------------------------------
void
LossModel::setDefaults() {
  next_lossmodel_ = NULL;
  edge_ = NULL;

  x_ = 0.0;
  y_ = 0.0;
  angle_ = NO_ANGLE;
  color_ = "black";
  paint_ = Paint::instance()->thin();

  loss_unit_ = NULL;
  setLossUnit("pkt");

  // Periodic Parameters
  period_ = 1.0;
  offset_ = 0.0;
  burstlen_ = 0.0;

  // Uniform Parameters
  rate_ = 0.1;
}

//----------------------------------------------------------------------
// double 
// LossModel::distance(double x, double y) const {
//----------------------------------------------------------------------
double
LossModel::distance(double x, double y) const {
  return sqrt((x_-x)*(x_-x) + (y_-y)*(y_-y));
}

//----------------------------------------------------------------------
//----------------------------------------------------------------------
void
LossModel::color(const char * name) {
  if (color_) {
    delete []color_;
  }
  color_ = new char[strlen(name) + 1];
  strcpy(color_, name);
}

//----------------------------------------------------------------------
//----------------------------------------------------------------------
void LossModel::size(double s) {
  size_ = s;
  width_ = 10.0 + 25.0*strlen(label_);
  height_ = s/2.0;
  update_bb();
}

//----------------------------------------------------------------------
//----------------------------------------------------------------------
void LossModel::update_bb() {
  bb_.xmin = x_;
  bb_.ymin = y_;
  bb_.xmax = x_ + width_;
  bb_.ymax = y_ + height_;
}

//----------------------------------------------------------------------
//----------------------------------------------------------------------
void LossModel::label(const char* name) {
  if (label_) {
    delete label_;
  }
  label_ = new char[strlen(name) + 1];
  strcpy(label_, name);
}

//----------------------------------------------------------------------
//----------------------------------------------------------------------
void LossModel::reset(double) {
  paint_ = Paint::instance()->thick();
}

//----------------------------------------------------------------------
// void
// LossModel::attach(Agent * agent)
//   - Attach this loss model to an edge
//----------------------------------------------------------------------
void
LossModel::attachTo(Edge * edge) {
  edge_ = edge; 
  place();
}

//----------------------------------------------------------------------
//----------------------------------------------------------------------
void LossModel::clearEdge() {
  edge_->clearLossModel();
  edge_ = NULL;
}

//----------------------------------------------------------------------
// void
// LossModel::place()  
//   - place the loss model on the edge to which it is attached.
//----------------------------------------------------------------------
void
LossModel::place() {
  QueueHandle * queue_handle;
  if (edge_) {
    // Place loss model on top of the queue handle
    queue_handle = edge_->getQueueHandle();
    
    // Place loss model on top of queue handle
    x_ = queue_handle->x();
    y_ = queue_handle->y() + queue_handle->height(); 

    // Place loss model to the right of queue handle
    //x_ = queue_handle->x() + queue_handle->width();
    //y_ = queue_handle->y(); 
  }
}


//----------------------------------------------------------------------
//----------------------------------------------------------------------
const char* LossModel::info() const {
  static char text[128];

  sprintf(text, "Loss Model: %s %d->%d", label_, edge_->src(), edge_->dst());
  return (text);
}

//----------------------------------------------------------------------
//----------------------------------------------------------------------
const char* LossModel::getname() const
{
  static char text[128];
  sprintf(text, "a %s", label_);
  return (text);
}


//----------------------------------------------------------------------
// int 
// LossModel::inside(double, float px, float py) const 
//   - Check to see if point (px, py) is within the Loss Model box
//----------------------------------------------------------------------
int 
LossModel::inside(double, float px, float py) const {
  return (px >= x_ && 
          px <= (x_ + width_) &&
          py >= y_ && 
          py <= (y_ + height_));
}

//----------------------------------------------------------------------
// void
// LossModel::draw(View * view, double ) const
//   - Draw the Loss Model on its edge
//----------------------------------------------------------------------
void
LossModel::draw(View * view, double time) {
	double label_width, label_height, label_x, label_y;
	static char full_label[128];

	// Draw label centered inside of border
	if (label_) {
		//sprintf(full_label, "%s: %d->%d", label_, edge_->src(), edge_->dst());
		sprintf(full_label, "%s", label_);
		
		// We have to keep calculting the label width and height because
		// we have no way of knowing if the user has zoomed in or out 
		// on the current network view.
		label_height = 0.9 * height_;
		label_width = view->getStringWidth(full_label, label_height);

		// Add 10% of padding to width for the box
		setWidth(1.1 * label_width);
		
		// Center label in box
		label_x = x_ + (width_ - label_width);
		label_y = y_ + (height_ - label_height);

		view->string(full_label, label_x , label_y, label_height, NULL);
		
		update_bb();
	}

	
	// Draw Rectangle Border
	view->rect(x_, y_, x_ + width_, y_ + height_ , paint_);
}

//----------------------------------------------------------------------
// int
// LossModel::writeNsDefinitionScript(FILE * file)
//  - outputs ns script format for creating loss models
//----------------------------------------------------------------------
int
LossModel::writeNsDefinitionScript(FILE * file) {
  if (!strcmp(name(), "Periodic")) {
    fprintf(file, "set loss_model(%d) [new ErrorModel/%s]\n",
                   number_, label_);
    // Connect loss model to edge
    fprintf(file, "$ns lossmodel $loss_model(%d) $node(%d) $node(%d)\n", number_,
            edge_->src(), edge_->dst());

    // Set Properties
    fprintf(file, "$loss_model(%d) unit %s\n", number_, loss_unit_);
    fprintf(file, "$loss_model(%d) set period_ %f\n", number_, period_);
    fprintf(file, "$loss_model(%d) set offset_ %f\n", number_, offset_);
    fprintf(file, "$loss_model(%d) set burstlen_ %f\n", number_, burstlen_);
    // Set Drop Target
    fprintf(file, "$loss_model(%d) drop-target [new Agent/Null]\n", number_);

  } else if (!strcmp(name(), "Expo")) {

  } else if (!strcmp(name(), "Uniform")) {
    fprintf(file, "set loss_model(%d) [new ErrorModel/%s %f %s]\n",
                   number_, label_, rate_, loss_unit_);
    
    // Connect loss model to edge
    fprintf(file, "$ns lossmodel $loss_model(%d) $node(%d) $node(%d)\n", number_,
            edge_->src(), edge_->dst());

    // Set Drop Target
    fprintf(file, "$loss_model(%d) drop-target [new Agent/Null]\n", number_);

  } else {
    fprintf(stderr, "Unknown loss model %s needs to be implemented.\n", label_);
  }
                          
  return 0;
}

//----------------------------------------------------------------------
// LossModel::property()
//   - return the list of loss model configuration parameters
//     (properties) to be shown in the property edit window
//----------------------------------------------------------------------
const char*
LossModel::property() {
  static char text[PROPERTY_STRING_LENGTH];
  char * property_list;

  property_list = text;
  sprintf(text, "{\"Loss Model: %s %d->%d\" title title \"LossModel %d\"} ",
          name(), edge_->src(), edge_->dst(), number_);

  if (strcmp(name(), "Periodic") == 0) {
      property_list = &text[strlen(text)];
      sprintf(property_list, "{\"Period\" period_ text %f} ", period_);
      property_list = &text[strlen(text)];
      sprintf(property_list, "{\"Offset\" offset_ text %f} ", offset_); 
      property_list = &text[strlen(text)];
      sprintf(property_list, "{\"Burst Length\" burstlen_ text %f} ", burstlen_);

  } else if (strcmp(name(), "Expo") == 0) {

  } else if (strcmp(name(), "Uniform") == 0) {
      property_list = &text[strlen(text)];
      sprintf(property_list, "{\"Loss Rate\" rate_ text %f} ", rate_);
  }

  return(text);
}


//----------------------------------------------------------------------
//----------------------------------------------------------------------
int LossModel::command(int argc, const char * const * argv) {
  int length = strlen(argv[1]);

  if (strncmp(argv[1], "setRate", length) == 0) {
    setRate(strtod(argv[2],NULL));

  } else if (strncmp(argv[1], "setLossUnit", length) == 0) {
    setLossUnit(argv[2]);
  }

  return TCL_OK;
}

//----------------------------------------------------------------------
// void
// LossModel::setLossUnit(const char * unit) 
//   - sets the loss model's loss unit type (byte, pkt, etc...)
//----------------------------------------------------------------------
void
LossModel::setLossUnit(const char * unit) {
  if (loss_unit_) {
    delete []loss_unit_;
  }

  loss_unit_ = new char[strlen(unit) + 1];
  strcpy(loss_unit_, unit);
}