-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPWDriver.cpp
113 lines (84 loc) · 3.39 KB
/
PWDriver.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
/*
* 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 "PWDriver.h"
#include "Event.h"
#include "PWNode.h"
#include <random>
#ifndef NDEBUG
#include <ostream>
#endif
namespace {
class PWDriverEvent : public Event {
PWDriver& driver_;
public:
PWDriverEvent(Event::evtime_t listen, PWDriver& driver) : Event(listen), driver_(driver) {
}
virtual void process() {
driver_.setStatus(DutyDriver::status_t::LISTENING, getDispatchTime());
}
#ifndef NDEBUG
virtual std::ostream& dump(std::ostream& os) const {
Event::dump(os);
os << "PWDriver(Listen): " << driver_.getNode().getId() << ' ';
return os;
}
#endif
};
}
PWDriver::PWDriver(const Node& node, Event::evtime_t bitlen) : DutyDriver(node, bitlen), rng_(node.getId()) {
};
/* In PW-MAC we sleep between ½ sleepLength and 1.5 sleepLength */
Event::evtime_t PWDriver::getTimeUntilListen() {
return sleep_length_ / 2. + sleep_length_ * (rng_() / (1. * rng_.randMax()));
}
void PWDriver::scheduleListen(Node::nodeid_t dst, Event::evtime_t now) {
if (getExpectedBeaconTime(dst, 0) > now) // Hack: there is a current prediction for the future
return;
const auto fire_time = getExpectedBeaconTime(dst, now);
Calendar::newEvent(std::make_unique<PWDriverEvent>(fire_time, *this));
#ifndef NDEBUG
std::cerr << "Node " << getNode().getId() << " schedules wake up at " << fire_time << " with destination " << dst << std::endl;
#endif
}
Event::evtime_t PWDriver::scheduleTxSlowPath(Event::evtime_t now, int backoff) const {
Calendar::randomGen_t& rng = Calendar::getCalendar().getRandomGenerator();
std::uniform_int_distribution<int> distr(0, backoff);
return now + getBitLen() * distr(rng);
}
Event::evtime_t PWDriver::getExpectedExactBeaconTime(Node::nodeid_t dst, Event::evtime_t now) {
Event::evtime_t last_beacon = 0;
int last_seed = dst;
if (predictions.find(dst) != predictions.end()) {
std::tie(last_beacon, last_seed) = predictions[dst];
}
auto rng = PseudoRNG(last_seed);
while (last_beacon < now) {
last_beacon += sleep_length_ / 2. + sleep_length_ * (rng() / (1. * rng_.randMax()));
predictions[dst] = std::make_tuple(last_beacon, rng.getCurrentValue());
}
return last_beacon;
}
Event::evtime_t PWDriver::getPrevBeaconTime(Node::nodeid_t dst, Event::evtime_t next_beacon) {
if(predictions.find(dst) == predictions.end()) {
return 0;
}
Event::evtime_t last_beacon;
int last_seed;
std::tie(last_beacon, last_seed) = predictions[dst];
auto prev_beacon_delta = sleep_length_ * last_seed / (1. * rng_.randMax()) + sleep_length_ / 2.;
return next_beacon - prev_beacon_delta;
}