-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.hpp
261 lines (211 loc) · 6.67 KB
/
base.hpp
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
#ifndef DHTSIM_BASE_H
#define DHTSIM_BASE_H
#include "application.hpp"
#include "message.hpp"
#include "time.hpp"
#include "callback.hpp"
#include <map>
#include <iostream>
#include <optional>
#include <functional>
namespace dhtsim {
template <typename A> class BaseApplication : public Application<A> {
public:
using SendCallbackSet = CallbackSet<Message<A>, Message<A>>;
BaseApplication() : epoch(0),
inqueue(), outqueue(),
callbacks() {}
virtual void recv(Message<A> m) { this->queueIn(m); }
virtual std::optional<Message<A>> unqueueOut();
virtual void handleMessage(const Message<A>& m);
virtual void tick(Time time);
/**
* Send a message.
* @param {m} The message to send.
* @param {callback} The function to execute with the response.
* @param {timeout} The timeout before message is considered "lost"
* @param {maxRetries} how many times to retry? (exponential backoff, starting with timeout)
*/
virtual void send(Message<A> m,
SendCallbackSet callback = SendCallbackSet(),
unsigned int maxRetries = 16,
unsigned long timeout = 0);
virtual void die() { this->dead = true; }
virtual bool isDead() { return this->dead; }
protected:
/** The current network's time. */
Time epoch;
/** Is this node dead? */
bool dead = false;
std::queue<Message<A>> inqueue, outqueue;
void queueIn(Message<A> m);
void queueOut(Message<A> m);
private:
/* This section is for variables that configure this
* application's network behavior. */
const unsigned int inqueueLimit = 1 << 15;
const unsigned int outqueueLimit = 1 << 15;
/**
* The number of retry messages this application is willing to
* store.
*/
const int retryBufferLimit = 1024;
/**
* The number of ticks to wait before re-sending for the first
* time.
*/
const unsigned long defaultTimeout = 20;
/** The base factor of exponential backoff for retrying. */
const int backoffFactor = 2;
/**
* A type that represents a message that has been sent.
*/
struct SentMessage {
/** The message that was sent. */
Message<A> message;
/** The function to be called when the response arrives. */
SendCallbackSet callback;
/** The time at which this message was last sent. */
Time timeSent;
/** The time at which this message is scheduled to be sent next. */
Time nextSend = std::numeric_limits<Time>::max();
/** The number of times we have retried. */
unsigned long retries;
/** How many times to retry before giving up? */
unsigned long maxRetries;
SentMessage() = default;
SentMessage(Message<A> m, SendCallbackSet cbf, Time time,
unsigned long timeout, unsigned int maxRetries)
: message(m), callback(cbf), timeSent(time),
nextSend(time + timeout), retries(0), maxRetries(maxRetries) {}
bool needsRetry() {
return this->retries < this->maxRetries;
}
/**
* Record a retry.
* @param time The time of retry.
* @param backoffFactor How many times longer will the next retry interval be?
*/
void retry(Time time, int backoffFactor) {
Time diff = this->nextSend - this->timeSent;
this->timeSent = time;
this->nextSend = this->timeSent + diff * backoffFactor;
this->retries++;
}
void success(Message<A> m) {
this->callback.success(m);
}
void failure() {
this->callback.failure(this->message);
}
};
void attemptRetry(SentMessage& record);
/**
* A map of message tags to "sent message" records. When a
* message is received and its tag matches one of the keys,
* the callback function is called with the message as its
* parameter.
*/
std::map<unsigned long, SentMessage> callbacks;
};
template <typename A> std::optional<Message<A>> BaseApplication<A>::unqueueOut() {
if (this->outqueue.empty()) {
return {};
}
auto message = this->outqueue.front();
this->outqueue.pop();
return message;
}
template <typename A> void BaseApplication<A>::queueIn(Message<A> m) {
if (this->inqueue.size() < this->inqueueLimit) {
this->inqueue.push(m);
} else {
std::clog << "[" << this->getAddress() << "]"
<< " Input queue full, dropping packet."
<< std::endl;
}
}
template <typename A> void BaseApplication<A>::queueOut(Message<A> m) {
if (this->outqueue.size() < this->outqueueLimit) {
this->outqueue.push(m);
} else {
std::clog << "[" << this->getAddress() << "]"
<< " Output queue full, dropping packet."
<< std::endl;
}
}
template <typename A> void BaseApplication<A>::attemptRetry(SentMessage& record) {
// Send the message again, but this time
// without a callback!.
this->send(record.message);
// Let the record know about the retry.
record.retry(this->epoch, this->backoffFactor);
}
template <typename A> void BaseApplication<A>::tick(Time time) {
// Dead nodes send no messages.
if (this->dead) return;
// Update our record of the time.
this->epoch = time;
// handle inbound messages
while (!this->inqueue.empty()) {
auto message = this->inqueue.front();
// std::clog << this->getAddress()
// << " got a message from "
// << message.originator
// << " tagged " << message.tag << std::endl;
this->inqueue.pop();
this->handleMessage(message);
}
// Check for messages whose responses are overdue
auto it = this->callbacks.begin();
for ( ; it != this->callbacks.end(); ) {
auto& [idx, record] = *it;
// Is the message overdue for re-sending?
if (this->epoch >= record.nextSend) {
if (record.needsRetry()) {
this->attemptRetry(record);
} else {
record.failure();
it = this->callbacks.erase(it);
continue;
}
}
it++;
}
}
template <typename A> void BaseApplication<A>::send(
Message<A> m,
BaseApplication<A>::SendCallbackSet callback,
unsigned int maxRetries,
unsigned long timeout) {
// Dead nodes send no messages, but we'll be nice and call the
// failure function. It doesn't really make much sense, but
// this becomes a lot harder otherwise.
if (this->dead) {
callback.failure(m);
return;
}
if (m.tag == 0) {
m.tag = this->randomTag();
}
if (timeout == 0) {
// Use default value
timeout = this->defaultTimeout;
}
if (!callback.empty()) {
SentMessage sentmsg(m, callback, this->epoch, timeout, maxRetries);
this->callbacks[m.tag] = sentmsg;
}
this->queueOut(m);
}
template <typename A> void BaseApplication<A>::handleMessage(const Message<A>& m) {
auto tag = m.tag;
auto it = this->callbacks.find(tag);
if (it != this->callbacks.end()) {
auto [tag, sentrecord] = *it;
sentrecord.success(m);
this->callbacks.erase(it);
}
}
}
#endif