-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNode.cpp
150 lines (120 loc) · 4.57 KB
/
Node.cpp
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
/*
* 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/>.
*/
#include <algorithm>
#include <cassert>
#include "Node.h"
using namespace std;
Node::nodeid_t Node::unique_id_counter_ = 0;
Node::Node(const Location& loc) :
loc_(loc), unique_id_(++unique_id_counter_), receiving_data_(0), n_rtx_(0), colliding_(false), pending_beacon_(false) { // 0 is not a valid PacketId number
}
Node& Node::getClosestNeighbour(const Location& dest) {
Node *best = nullptr;
auto best_sq_distance = dest.getSquaredDistanceTo(getLocation());
for (auto ne : neighbours_) {
auto& neigh = ne.get();
auto sq_distance = dest.getSquaredDistanceTo(neigh.getLocation());
if (sq_distance < best_sq_distance) {
best_sq_distance = sq_distance;
best = &ne.get();
}
}
/* Unreachable destination */
if (best == nullptr) {
throw RoutingException("Cannot find route", shared_from_this(), dest);
}
return *best;
}
#ifndef NDEBUG
ostream& NodeEvent::dump(ostream& os) const {
Event::dump(os);
os << "Node: " << node_.getId() << ' ';
return os;
}
#endif
void BeaconEvent::process() {
switch (bkind_) {
case beacon_kind_t::BEACON_START:
/* Avoid obvious collissions
* a) We are not receiving data, even if not directed to us
* b) We are not transmitting data
* c) We are not transmitting a beacon (in this case the new ending beacon can come two early and end the original period
*/
if (node_.txingBeacon() == false && node_.receiving_data_ == 0 && node_.getDriver().getStatus() != DutyDriver::status_t::RECEIVING && node_.getDriver().getStatus() != DutyDriver::status_t::TRANSMITTING) {
auto endBEaconEvenet = node_.sendBeacon(getDispatchTime());
if (endBEaconEvenet != nullptr)
newEvent(move(endBEaconEvenet));
}
// Generate the next beacon
newEvent(make_unique<BeaconEvent>(node_.getDriver().scheduleRx(getDispatchTime()), node_));
break;
case beacon_kind_t::BEACON_END:
node_.endListening(getDispatchTime());
break;
}
}
#ifndef NDEBUG
ostream& operator<<(ostream& os, const DataEvent::data_kind_t& kind) {
switch (kind) {
case DataEvent::data_kind_t::DATA_START:
os << "DATA_START";
break;
case DataEvent::data_kind_t::DATA_END:
os << "DATA_END";
break;
};
return os;
}
ostream& DataEvent::dump(ostream& os) const {
NodeEvent::dump(os);
os << "Kind: DATA(" << dkind_ << ") Dst: " << dst_.getId() << ' ';
return os;
}
#endif
void DataEvent::process() {
switch (dkind_) {
case data_kind_t::DATA_START:
node_.startTransmission(dst_, getDispatchTime());
// New DataEnd Event
newEvent(std::make_unique<DataEvent>(getDispatchTime() + getTxTime(packet_), node_, dst_, packet_, data_kind_t::DATA_END));
break;
case data_kind_t::DATA_END:
node_.endTransmission(dst_, packet_, getDispatchTime());
break;
}
}
std::ostream& NodeException::debug(ostream& os) const {
SimulationException::debug(os);
os << *node_ << endl;
return os;
}
std::ostream& RoutingException::debug(ostream& os) const {
NodeException::debug(os);
os << "Destination: " << dst_ << endl;
for (auto ne : node_->neighbours_)
os << ne.get().getId() << " " << ne.get().getLocation() << " Distance: " << ne.get().getLocation().getDistanceTo(dst_) << endl;
cerr << "Node distance: " << node_->getLocation().getDistanceTo(dst_) << endl;
return os;
}
ostream& operator<<(ostream& os, const Node& n) {
os << "Node id: " << n.getId() << " Location: " << n.getLocation() << endl;
os << " Neighbours: ";
for (auto ne : n.neighbours_) {
os << ne.get().getId() << " ";
}
return os;
}