delay.h 2.06 KB
Newer Older
Robert Ricci's avatar
Robert Ricci committed
1
2
/*
 * EMULAB-COPYRIGHT
3
 * Copyright (c) 2002-2003 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
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
}

class tb_delay_info {
public:
  int bandwidth;
  int delay;
  double loss;
  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)
  {
    o << "tb_delay_info: bw=" << delay.bandwidth << "+" <<
      delay.bw_over << "-" << delay.bw_under << "/" << delay.bw_weight;
    o << " delay=" << delay.delay << "+" << delay.delay_over <<
      "-" << delay.delay_under << "/" << delay.delay_weight;
    o << " loss=" << delay.loss << "+" << delay.loss_over << "-" <<
      delay.loss_under << "/" << delay.loss_weight;
    o << endl;
    return o;
  }
};

76
#endif
77