/
traffic.cc
94 lines (78 loc) · 1.41 KB
/
traffic.cc
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
#include "traffic.hpp"
#include "custom_dijkstra_call.hpp"
#include <boost/optional.hpp>
#include <list>
using namespace std;
traffic::traffic(double mcat, double mht, double mnsc):
mcat(mcat), catd(1 / mcat), catg(m_rng, catd),
mht(mht), mnsc(mnsc), idc()
{
schedule(0);
}
traffic::~traffic()
{
for(auto c: cs)
delete c;
delete_clients();
}
int
traffic::nr_clients() const
{
return cs.size();
}
void
traffic::operator()(double t)
{
// Before we create new clients, we delete those clients that
// requested deletion.
delete_clients();
// We are creating a client, but we ain't doing anything with the
// pointer we get! It's so, because it's up to the client to
// register itself with the traffic.
new client(mht, mnsc, *this);
schedule_next(t);
}
void
traffic::schedule_next(double t)
{
double dt = catg();
schedule(t + dt);
}
void
traffic::insert(client *c)
{
cs.insert(c);
}
void
traffic::erase(client *c)
{
cs.erase(c);
}
void
traffic::delete_me_later(client *c)
{
dl.push(c);
}
int
traffic::capacity_served() const
{
int capacity = 0;
// Iterate over all clients.
for(const client *cli: cs)
{
const connection &c = cli->get_connection();
int nsc = c.get_nsc();
int len = c.get_len();
capacity += len * nsc;
}
return capacity;
}
void
traffic::delete_clients()
{
while(!dl.empty())
{
delete dl.front();
dl.pop();
}
}