-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathNetworkController.hpp
More file actions
185 lines (151 loc) · 7.88 KB
/
Copy pathNetworkController.hpp
File metadata and controls
185 lines (151 loc) · 7.88 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
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
/*
* MIT License
*
* Copyright (c) 2015 NUClear Contributors
*
* This file is part of the NUClear codebase.
* See https://github.com/Fastcode/NUClear for further info.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef NUCLEAR_EXTENSION_NETWORKCONTROLLER_HPP
#define NUCLEAR_EXTENSION_NETWORKCONTROLLER_HPP
#include <algorithm>
#include <cerrno>
#include <csignal>
#include "../PowerPlant.hpp"
#include "../Reactor.hpp"
#include "../util/get_hostname.hpp"
#include "../util/network/network_hash_t.hpp"
#include "network/NUClearNetwork.hpp"
namespace NUClear {
namespace extension {
class NetworkController : public Reactor {
using NetworkListen = dsl::word::NetworkListen;
using NetworkEmit = dsl::word::emit::NetworkEmit;
using NetworkConfiguration = message::NetworkConfiguration;
using Unbind = dsl::operation::Unbind<NetworkListen>;
struct ProcessNetwork {};
public:
explicit NetworkController(std::unique_ptr<NUClear::Environment> environment)
: Reactor(std::move(environment)) {
// Set our function callback
network.set_packet_callback([this](const network::NUClearNetwork::NetworkTarget& remote,
const util::network::network_hash_t& hash,
const bool& reliable,
std::vector<uint8_t>&& payload) {
// Construct our NetworkSource information
dsl::word::NetworkSource src{remote.name, remote.target, reliable};
// Move the payload in as we are stealing it
std::vector<uint8_t> p(std::move(payload));
// Store in our thread local cache
dsl::store::ThreadStore<std::vector<uint8_t>>::value = &p;
dsl::store::ThreadStore<dsl::word::NetworkSource>::value = &src;
/* Mutex Scope */ {
// Lock our reaction mutex
const std::lock_guard<std::mutex> lock(reaction_mutex);
// Find interested reactions
auto rs = reactions.equal_range(hash);
// Execute on our interested reactions
for (auto it = rs.first; it != rs.second; ++it) {
powerplant.submit(it->second->get_task());
}
}
// Clear our cache
dsl::store::ThreadStore<std::vector<uint8_t>>::value = nullptr;
dsl::store::ThreadStore<dsl::word::NetworkSource>::value = nullptr;
});
// Set our join callback
network.set_join_callback([this](const network::NUClearNetwork::NetworkTarget& remote) {
auto l = std::make_unique<message::NetworkJoin>();
l->name = remote.name;
l->address = remote.target;
emit(l);
});
// Set our leave callback
network.set_leave_callback([this](const network::NUClearNetwork::NetworkTarget& remote) {
auto l = std::make_unique<message::NetworkLeave>();
l->name = remote.name;
l->address = remote.target;
emit(l);
});
// Set our event timer callback
network.set_next_event_callback([this](std::chrono::steady_clock::time_point t) {
const std::chrono::steady_clock::duration emit_offset = t - std::chrono::steady_clock::now();
emit<Scope::DELAY>(std::make_unique<ProcessNetwork>(),
std::chrono::duration_cast<NUClear::clock::duration>(emit_offset));
});
// Start listening for a new network type
on<Trigger<NetworkListen>>().then("Network Bind", [this](const NetworkListen& l) {
// Lock our reaction mutex
const std::lock_guard<std::mutex> lock(reaction_mutex);
// Insert our new reaction
reactions.insert(std::make_pair(l.hash, l.reaction));
});
// Stop listening for a network type
on<Trigger<Unbind>>().then("Network Unbind", [this](const Unbind& unbind) {
// Lock our reaction mutex
const std::lock_guard<std::mutex> lock(reaction_mutex);
// Find and delete this reaction
for (auto it = reactions.begin(); it != reactions.end(); ++it) {
if (it->second->id == unbind.id) {
reactions.erase(it);
break;
}
}
});
on<Trigger<NetworkEmit>>().then("Network Emit", [this](const NetworkEmit& emit) {
network.send(emit.hash, emit.payload, emit.target, emit.reliable);
});
on<Shutdown>().then("Shutdown Network", [this] { network.shutdown(); });
// Configure the NUClearNetwork options
on<Trigger<NetworkConfiguration>>().then([this](const NetworkConfiguration& config) {
// Unbind our announce handle
if (process_handle) {
process_handle.unbind();
}
// Unbind all our listen handles
if (!listen_handles.empty()) {
for (auto& h : listen_handles) {
h.unbind();
}
listen_handles.clear();
}
// Name becomes hostname by default if not set
const std::string name = config.name.empty() ? util::get_hostname() : config.name;
// Reset our network using this configuration
network.reset(name, config.announce_address, config.announce_port, config.bind_address, config.mtu);
// Execution handle
process_handle =
on<Trigger<ProcessNetwork>>().then("Network processing", [this] { network.process(); });
for (auto& fd : network.listen_fds()) {
listen_handles.push_back(on<IO>(fd, IO::READ).then("Packet", [this] { network.process(); }));
}
});
}
private:
/// Our NUClearNetwork object that handles the networking
network::NUClearNetwork network{};
/// The reaction that handles timed events from the network
ReactionHandle process_handle{};
/// The reactions that listen for io
std::vector<ReactionHandle> listen_handles{};
/// Mutex to guard the list of reactions
std::mutex reaction_mutex;
/// Map of type hashes to reactions that are interested in them
std::multimap<util::network::network_hash_t, std::shared_ptr<threading::Reaction>> reactions{};
};
} // namespace extension
} // namespace NUClear
#endif // NUCLEAR_EXTENSION_NETWORKCONTROLLER_HPP