delay.h 2.46 KB
Newer Older
Robert Ricci's avatar
Robert Ricci committed
1
2
/*
 * EMULAB-COPYRIGHT
3
 * Copyright (c) 2002-2006 University of Utah and the Flux Group.
Robert Ricci's avatar
Robert Ricci committed
4
5
6
 * All rights reserved.
 */

7
8
9
#ifndef __DELAY_H
#define __DELAY_H

10
11
// For DBL_MAX
#include <float.h>
12
13
// For fabs()
#include <math.h>
14

15
16
#include <iostream>
using namespace std;
17

18
inline double double_distance(double a,double b) {
19
20
21
22
23
24
25
26
27
  if (b == 0) {
    if (a == 0)
      return 0;
    else
      return DBL_MAX;
  }
  return fabs((double)a/(double)b - 1.0);
}
inline double delay_distance(int a, int b) {
28
  return double_distance((double)a,(double)b);
29
30
}
inline double bandwidth_distance(int a,int b) {
31
  return double_distance((double)a,(double)b);
32
33
}
inline double loss_distance(double a,double b) {
34
  return double_distance(a,b);
35
36
37
38
}

class tb_delay_info {
public:
39
40
41
42
43
  // TODO: Make a real constructor!
  tb_delay_info() : bandwidth(0), delay(0), loss(0.0),
    adjust_to_native_bandwidth(false) { ; } ;

  // Note the adjust_to_native_bandwidth flag below
44
45
46
  int bandwidth;
  int delay;
  double loss;
47
48
49
50
51
52
53

  // If true, we just use the native bandwidth of the interface that gets 
  // picked; the bandwidth member gets updated on every assignment. This only
  // makes sense on virtual links.
  bool adjust_to_native_bandwidth;

  // TODO: We don't actually use any of these; we should get 'em out of here.
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
  int bw_under,bw_over;
  int delay_under,delay_over;
  double loss_under,loss_over;
  double bw_weight,delay_weight,loss_weight;

  double distance(tb_delay_info &target) {
      // ricci - hack to try to remove this behavior
      return 0;
    if (((bw_under != -1) && (target.bandwidth < bandwidth-bw_under)) ||
	((bw_over != -1) && (target.bandwidth > bandwidth+bw_over)) ||
	((delay_under != -1) && (target.delay < delay-delay_under)) ||
	((delay_over != -1) && (target.delay > delay+delay_over)) ||
	((loss_under != -1) && (target.loss < loss-loss_under)) ||
	((loss_over != -1) && (target.loss > loss+loss_over))) {
      return -1;
    }
    return bandwidth_distance(target.bandwidth,bandwidth)*bw_weight+
      delay_distance(target.delay,delay)*delay_weight+
      loss_distance(target.loss,loss)*loss_weight;
  }
  
  friend ostream &operator<<(ostream &o, const tb_delay_info& delay)
  {
77
78
79
80
81
82
83
84
    // Only print out the stuff that matters, not all of the pieces we don't
    // use
    o << "tb_delay_info: bw=" << delay.bandwidth;
    if (delay.adjust_to_native_bandwidth) {
        o << "(adjusted)";
    }
    o << " delay=" << delay.delay;
    o << " loss=" << delay.loss;
85
86
87
88
89
    o << endl;
    return o;
  }
};

90
#endif
91