-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNode.h
334 lines (263 loc) · 10.3 KB
/
Node.h
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*
* Copyright (C) 2015 Miguel Rodríguez Pérez <miguel@det.uvigo.es>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef NODE_H
#define NODE_H
#include "Location.h"
#include "RoutingTable.h"
#include "Packet.h"
#include "Link.h"
#include "Event.h"
#include "DutyDriver.h"
#include <vector>
#include <memory>
#include <iostream>
class NodeException : public SimulationException {
public:
NodeException(std::string message, std::shared_ptr<Node> node) : SimulationException(message), node_(node) {
}
virtual std::ostream& debug(std::ostream& os) const;
protected:
std::shared_ptr<Node> node_;
};
class RoutingException : public NodeException {
public:
RoutingException(std::string message, std::shared_ptr<Node> node, Location dst) : NodeException(message, node), dst_(dst) {
}
virtual std::ostream& debug(std::ostream& os) const;
protected:
Location dst_;
};
class NodeEvent : public Event {
public:
NodeEvent(evtime_t d_time, Node& node) : Event(d_time), node_(node) {
}
#ifndef NDEBUG
virtual std::ostream& dump(std::ostream& os) const;
#endif
protected:
Node& node_;
};
class BeaconEvent : public NodeEvent {
public:
enum class beacon_kind_t {
BEACON_START, BEACON_END
};
BeaconEvent(evtime_t d_time, Node& node, beacon_kind_t bkind = beacon_kind_t::BEACON_START) : NodeEvent(d_time, node), bkind_(bkind) {
}
void process();
#ifndef NDEBUG
virtual std::ostream& dump(std::ostream& os) const {
NodeEvent::dump(os);
os << "Kind: Beacon_";
switch (bkind_) {
case beacon_kind_t::BEACON_START:
os << "START ";
break;
case beacon_kind_t::BEACON_END:
os << "END ";
break;
}
return os;
}
#endif
private:
beacon_kind_t bkind_;
};
class DataEvent : public NodeEvent {
public:
enum class data_kind_t {
DATA_START, DATA_END
};
DataEvent(evtime_t d_time, Node& src_node, Node& dst_node, const Packet& p, data_kind_t dkind = data_kind_t::DATA_START) : NodeEvent(d_time, src_node), dst_(dst_node), packet_(p), dkind_(dkind) {
}
void process();
#ifndef NDEBUG
virtual std::ostream& dump(std::ostream& os) const;
#endif
private:
Node& dst_;
const Packet packet_;
data_kind_t dkind_;
auto getTxTime(const Packet& p) const {
return 8 * p.getPSize() / static_cast<double> (Link::getCapacity());
}
};
#ifndef NDEBUG
std::ostream& operator<<(std::ostream& os, const DataEvent::data_kind_t& kind);
#endif
class Node : public std::enable_shared_from_this<Node> {
public:
using nodeid_t = unsigned int;
private:
friend class RoutingException;
static nodeid_t unique_id_counter_;
RoutingTable routing_table_;
const Location loc_;
const nodeid_t unique_id_;
unsigned int receiving_data_;
int n_rtx_;
bool colliding_;
bool pending_beacon_;
std::vector<std::reference_wrapper<Node>> neighbours_;
std::map<nodeid_t, std::shared_ptr<Link> > links_;
Node& getClosestNeighbour(const Location& dest);
// In pkts
auto getBackOff() const {
return (1 << n_rtx_) - 1; // 2**n_rtx - 1
}
auto txingBeacon() const {
return pending_beacon_;
}
public:
friend class BeaconEvent;
friend class DataEvent;
auto getId() const {
return unique_id_;
}
void addNeightbour(Node& neigh) {
neighbours_.push_back(neigh);
links_[neigh.getId()] = std::make_shared<Link>(neigh);
}
auto getLocation() const {
return loc_;
}
void Route(const Packet& p, Event::evtime_t now) {
if (p.getDestination() == getId())
return Process(p, now);
auto next_hop = routing_table_.getNext(p.getDestination());
if (next_hop == nullptr) {
next_hop = &getClosestNeighbour(p.getFinalLocation());
routing_table_.addEntry(p.getDestination(), *next_hop);
}
links_[next_hop->getId()]->queuePacket(p);
getDriver().newData(next_hop->getId(), now);
}
virtual DutyDriver& getDriver() = 0;
virtual const DutyDriver& getDriver() const = 0;
friend std::ostream& operator<<(std::ostream&, const Node&);
private:
void endListening(Event::evtime_t now) {
if (receiving_data_ == 0)
getDriver().setIdlestMode(now);
colliding_ = false;
pending_beacon_ = false;
}
void startTransmission(Node& dst, Event::evtime_t now) {
dst.startReception(getId(), now);
getDriver().setStatus(DutyDriver::status_t::TRANSMITTING, now);
}
void endTransmission(Node& dst, const Packet& p, Event::evtime_t now) {
dst.endReception(*this, p, now);
}
void startReception(nodeid_t, Event::evtime_t now) {
if (getDriver().getStatus() != DutyDriver::status_t::LISTENING) {
assert(getDriver().getStatus() != DutyDriver::status_t::SLEEPING);
}
receiving_data_ += 1;
getDriver().setStatus(DutyDriver::status_t::RECEIVING, now); // Useful for Collision Avoidance
}
auto ackReceived(Node& neigh, Event::evtime_t now, bool success = true) {
if (success == false) {
std::cout << "Collission when transferring from node " << getId() << " to node " << neigh.getId() << " @" << now << std::endl;
// We have to wait until next beacon
getDriver().setIdlestMode(now);
// If it is not RIC-MAC, we have to announce that there is pending data
getDriver().newData(neigh.getId(), now);
return DutyDriver::status_t::SLEEPING;
}
auto queue_size = links_[neigh.getId()]->removePacket();
// If we have more packets to send, ask receiver to go to listening state (Simulate a unicast beacon to it)
if (queue_size > 0) {
// Add new event to transmit the next packet
Calendar::newEvent(std::make_unique<DataEvent> (getDriver().scheduleTx(now, 0), *this, neigh, links_[neigh.getId()]->getNextPacket())); // No backoff as this is not a real beacon
getDriver().setStatus(DutyDriver::status_t::LISTENING, now);
return DutyDriver::status_t::RECEIVING;
} else {
getDriver().emptyQueue(neigh.getId());
getDriver().setIdlestMode(now);
return DutyDriver::status_t::SLEEPING;
}
}
void Process(const Packet& p, Event::evtime_t now) {
std::cout << "Packet " << p << " got delivered @" << now << " transit time: " << now - p.getCreationTime() << std::endl;
}
protected:
Node(const Location&);
virtual std::unique_ptr<DataEvent> getBeacon(Node& orig, Event::evtime_t now, int backoff) {
switch (getDriver().getStatus()) {
case DutyDriver::status_t::RECEIVING:
// Collision
if (colliding_ == false)
n_rtx_++;
colliding_ = true;
break;
case DutyDriver::status_t::TRANSMITTING:
// This may a collision, but on a receiver
break;
case DutyDriver::status_t::SLEEPING:
break; // Nothing to do
case DutyDriver::status_t::LISTENING:
if (pending_beacon_)
break; // No not transmit while waiting for reception
if (links_[orig.getId()]->getQueueSize() == 0)
break; // No data to send
auto packet = links_[orig.getId()]->getNextPacket();
getDriver().setStatus(DutyDriver::status_t::SLEEPING, now); // In case of backoff, we can rest a little until wakeup
return std::make_unique<DataEvent>(getDriver().scheduleTx(now, backoff * 8 * packet.getPSize()), *this, orig, packet);
}
return std::unique_ptr<DataEvent>(nullptr);
}
virtual std::unique_ptr<BeaconEvent> sendBeacon(Event::evtime_t now) {
if (getDriver().getStatus() != DutyDriver::status_t::SLEEPING && getDriver().getStatus() != DutyDriver::status_t::LISTENING)
return nullptr; // We do not send beacons when we are txing or rxing traffic
colliding_ = false;
pending_beacon_ = true;
getDriver().setStatus(DutyDriver::status_t::LISTENING, now);
int backoff = getBackOff();
for (auto ne : neighbours_) {
auto nev = ne.get().getBeacon(*this, now, backoff);
if (nev != nullptr)
Calendar::newEvent(std::move(nev));
}
// Wait for 5 bytes after the end of the backoff period.
#ifndef NDEBUG
std::cerr << "Node: " << getId() << " Backoff: " << backoff << std::endl;
#endif
return std::make_unique<BeaconEvent>((8 * 5 + backoff * 8 * Packet::getMaxPacketSize()) / Link::getCapacity() + now, *this, BeaconEvent::beacon_kind_t::BEACON_END);
}
virtual bool endReception(Node& orig, const Packet& p, Event::evtime_t now) {
assert(receiving_data_ > 0);
if (receiving_data_-- > 1 || colliding_) { // Collision
if (colliding_ == false) {
n_rtx_ += 1;
colliding_ = true;
}
if (receiving_data_ == 0)
colliding_ = false;
getDriver().setStatus(pending_beacon_ ? DutyDriver::status_t::LISTENING : DutyDriver::status_t::SLEEPING, now);
orig.ackReceived(*this, now, false);
return false;
} else {
n_rtx_ = 0;
auto newStatus = orig.ackReceived(*this, now); // Simulate a /small/ unicast ACK
getDriver().setStatus(pending_beacon_ ? DutyDriver::status_t::LISTENING : newStatus, now);
Route(p, now);
return true;
}
}
};
#endif /* NODE_H */