-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnetwork.cpp
More file actions
104 lines (82 loc) · 2.7 KB
/
network.cpp
File metadata and controls
104 lines (82 loc) · 2.7 KB
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
#include "network.hpp"
#include "application.hpp"
#include "random.h"
#include <iostream>
#include <map>
#include <climits>
#include <functional>
#include <memory>
using namespace dhtsim;
template <typename A> CentralizedNetwork<A>::CentralizedNetwork(unsigned int linkLimit) {
this->linkLimit = linkLimit;
this->epoch = 0;
}
template <typename A> A CentralizedNetwork<A>::getNewAddress() {
A attempt;
const uint32_t max_tries = 1000;
uint32_t tries;
for (tries = 0; tries < max_tries; tries++) {
attempt = global_rng.Number<A>(std::pair(0, std::numeric_limits<A>::max()));
auto it = this->inhabitants.find(attempt);
if (it == this->inhabitants.end()) {
return attempt;
}
}
return 0;
}
template <typename A> A CentralizedNetwork<A>::add(std::shared_ptr<Application<A>> app) {
A address = this->getNewAddress();
if (address == 0) return address;
this->inhabitants[address] = app;
app->setAddress(address);
app->tick(this->epoch);
return address;
}
template <typename A> void CentralizedNetwork<A>::remove(std::shared_ptr<Application<A>> app) {
this->inhabitants.erase(app->getAddress());
}
template <typename A> void CentralizedNetwork<A>::tick() {
// Keeps of the total bytes transferred per link
unsigned long totalLinkTransfer;
// Keeps track of the total bytes transferred during this tick.
unsigned long totalTransferred = 0;
// In the following for loop, app is a shared_ptr to the
// application
for (const auto [address, app] : this->inhabitants) {
totalLinkTransfer = 0;
// handle inbound messages
app->tick(this->epoch);
// handle outbound messages
std::optional<Message<A>> outboundMessage = app->unqueueOut();
while (outboundMessage.has_value()) {
auto size = outboundMessage->data.size();
totalLinkTransfer += size;
if (size > this->linkLimit) {
std::cerr << "DROPPED message of length " << size << std::endl;
break;
}
if (totalLinkTransfer > this->linkLimit) {
std::cerr << "RETRYING message of length " << size << std::endl;
app->send(*outboundMessage);
break;
}
this->passAlongMessage(*outboundMessage);
outboundMessage = app->unqueueOut();
}
totalTransferred += totalLinkTransfer;
}
std::cout << "[E] T " << this->epoch << " " << totalTransferred << std::endl;
this->epoch++;
}
template <typename A> void CentralizedNetwork<A>::passAlongMessage(Message<A> message) {
message.hops++;
A dest = message.destination;
auto it = this->inhabitants.find(dest);
if (it != this->inhabitants.end()) {
auto [address, app] = *it;
app->recv(message);
}
// The destination doesn't exist on this network, so just drop
// the message.
}
template class dhtsim::CentralizedNetwork<uint32_t>;