1#ifndef VEHICLE_RSU_MANAGER_H
2#define VEHICLE_RSU_MANAGER_H
32template <
typename Protocol_T>
54 std::vector<RSUInfo> _known_rsus;
55 mutable std::mutex _rsu_list_mutex;
56 RSUInfo* _current_leader;
57 std::chrono::seconds _rsu_timeout;
58 unsigned int _vehicle_id;
61 std::vector<MacKeyType> _neighbor_rsu_keys;
62 mutable std::mutex _neighbor_keys_mutex;
65 std::unique_ptr<Periodic_Thread<VehicleRSUManager>> _cleanup_thread;
66 std::atomic<bool> _running;
70 std::chrono::seconds
timeout = std::chrono::seconds(10))
73 <<
"] RSU Manager initialized with "
74 <<
timeout.count() <<
"s timeout\n";
78 <<
"] Starting periodic cleanup thread (5s interval)\n";
79 _cleanup_thread = std::make_unique<Periodic_Thread<VehicleRSUManager>>(
82 _cleanup_thread->start(1000000);
84 <<
"] Periodic cleanup thread started\n";
89 <<
"] RSU Manager shutting down\n";
90 _running.store(
false, std::memory_order_release);
91 if (_cleanup_thread) {
93 <<
"] Stopping periodic cleanup thread\n";
94 _cleanup_thread->join();
101 double x,
double y,
double radius,
103 if (!_running.load(std::memory_order_acquire))
return;
105 std::lock_guard<std::mutex>
lock(_rsu_list_mutex);
108 <<
"] Processing RSU STATUS from " <<
rsu_address.to_string()
109 <<
" at (" << x <<
", " << y <<
") radius=" << radius <<
"m\n";
113 for (
size_t i = 0;
i < 16; ++
i) {
126 if (
it != _known_rsus.end()) {
131 it->group_key = group_key;
132 it->last_seen = std::chrono::steady_clock::now();
134 <<
"] Updated RSU " <<
rsu_address.to_string() <<
"\n";
137 _known_rsus.emplace_back(
rsu_address, x, y, radius, group_key);
139 <<
"] Discovered new RSU " <<
rsu_address.to_string()
140 <<
" at (" << x <<
", " << y <<
")\n";
149 if (_known_rsus.empty()) {
150 if (_current_leader !=
nullptr) {
152 <<
"] Lost all RSUs - clearing leader\n";
154 _current_leader =
nullptr;
156 <<
"] No RSUs available for leader selection\n";
167 <<
"] Updating leader selection among " << _known_rsus.size() <<
" RSUs\n";
173 std::sort(_known_rsus.begin(), _known_rsus.end(),
175 return a.distance_to_vehicle < b.distance_to_vehicle;
179 for (
size_t i = 0;
i < _known_rsus.size(); ++
i) {
180 const auto&
rsu = _known_rsus[
i];
182 <<
"] RSU " <<
rsu.address.to_string()
183 <<
" distance=" <<
rsu.distance_to_vehicle <<
"m\n";
196 <<
"] Leader changed from " << _current_leader->
address.to_string()
197 <<
" to " <<
new_leader->address.to_string() <<
"\n";
200 <<
"] First leader selected: " <<
new_leader->address.to_string() <<
"\n";
206 if (_current_leader) {
208 <<
"] Current leader: " << _current_leader->address.to_string()
209 <<
" (distance: " << _current_leader->distance_to_vehicle <<
"m)\n";
215 <<
"] Updating global leader storage with ID " << (
int)
leader_id <<
"\n";
237 for (
auto&
rsu : _known_rsus) {
245 if (!_running.load(std::memory_order_acquire))
return;
247 std::lock_guard<std::mutex>
lock(_rsu_list_mutex);
248 auto now = std::chrono::steady_clock::now();
252 <<
"] Running periodic RSU cleanup, checking " << _known_rsus.size() <<
" RSUs\n";
254 auto it = _known_rsus.begin();
255 while (
it != _known_rsus.end()) {
260 <<
"] Removing stale RSU " <<
it->address.to_string()
264 if (_current_leader == &(*
it)) {
266 <<
"] Removing current leader due to timeout\n";
267 _current_leader =
nullptr;
271 it = _known_rsus.erase(
it);
275 <<
"] RSU " <<
it->address.to_string()
283 <<
"] RSU list changed after cleanup, updating leader selection\n";
287 <<
"] No changes after RSU cleanup\n";
293 std::lock_guard<std::mutex>
lock(_rsu_list_mutex);
294 return _current_leader;
299 std::lock_guard<std::mutex>
lock(_rsu_list_mutex);
305 std::lock_guard<std::mutex>
lock(_neighbor_keys_mutex);
310 <<
"] Neighbor RSU key already exists\n";
314 _neighbor_rsu_keys.push_back(key);
316 <<
"] Added neighbor RSU key (total: " << _neighbor_rsu_keys.size() <<
")\n";
321 std::lock_guard<std::mutex>
lock(_neighbor_keys_mutex);
322 return _neighbor_rsu_keys;
327 std::lock_guard<std::mutex>
lock(_neighbor_keys_mutex);
328 auto it = std::find(_neighbor_rsu_keys.begin(), _neighbor_rsu_keys.end(), key);
329 if (
it != _neighbor_rsu_keys.end()) {
330 _neighbor_rsu_keys.erase(
it);
332 <<
"] Removed neighbor RSU key (remaining: " << _neighbor_rsu_keys.size() <<
")\n";
340 typename std::vector<RSUInfo>::iterator find_rsu_by_address(
341 const typename Protocol_T::Address& address) {
342 return std::find_if(_known_rsus.begin(), _known_rsus.end(),
343 [&address](
const RSUInfo& info) {
344 return info.address == address;
void setSelfId(LeaderIdType id)
Set the self ID for this clock instance (node's own PTP-relevant ID)
Definition clock.h:156
void activate(const PtpRelevantData *new_msg_data)
Activate the state machine with new PTP data.
Definition clock.h:177
static Clock & getInstance()
Get the singleton instance.
Definition clock.h:145
static double euclideanDistance(double x1, double y1, double x2, double y2)
Definition geo_utils.h:16
void setLeaderId(const Ethernet::Address &leader_id)
Set the current leader ID.
Definition leaderKeyStorage.h:65
static LeaderKeyStorage & getInstance()
Get the singleton instance.
Definition leaderKeyStorage.h:54
void setGroupMacKey(const MacKeyType &key)
Set the current group MAC key.
Definition leaderKeyStorage.h:93
static void getCurrentCoordinates(double &x, double &y)
Definition location_service.h:91
Definition vehicleRSUManager.h:33
std::vector< RSUInfo > get_known_rsus()
Definition vehicleRSUManager.h:298
void add_neighbor_rsu_key(const MacKeyType &key)
Definition vehicleRSUManager.h:304
std::vector< MacKeyType > get_neighbor_rsu_keys()
Definition vehicleRSUManager.h:320
void cleanup_stale_rsus()
Definition vehicleRSUManager.h:244
void update_distances()
Definition vehicleRSUManager.h:228
bool remove_neighbor_rsu_key(const MacKeyType &key)
Definition vehicleRSUManager.h:326
RSUInfo * get_current_leader()
Definition vehicleRSUManager.h:292
~VehicleRSUManager()
Definition vehicleRSUManager.h:87
void update_leader_selection()
Definition vehicleRSUManager.h:147
void process_rsu_status(const typename Protocol_T::Address &rsu_address, double x, double y, double radius, const MacKeyType &group_key)
Definition vehicleRSUManager.h:100
VehicleRSUManager(unsigned int vehicle_id, std::chrono::seconds timeout=std::chrono::seconds(10))
Definition vehicleRSUManager.h:69
uint32_t LeaderIdType
Definition clock.h:26
@ INF
Definition debug.h:208
Select_Debug<(Traits< T >::debugged &&Traits< Debug >::error)> db(Debug_Error l)
Definition debug.h:166
@ TRC
Definition debug.h:231
@ WRN
Definition debug.h:185
std::array< uint8_t, 16 > MacKeyType
Definition leaderKeyStorage.h:15
Definition vehicleRSUManager.h:35
RSUInfo()
Definition vehicleRSUManager.h:44
MacKeyType group_key
Definition vehicleRSUManager.h:40
Protocol_T::Address address
Definition vehicleRSUManager.h:36
RSUInfo(const typename Protocol_T::Address &addr, double x_coord, double y_coord, double r, const MacKeyType &key)
Definition vehicleRSUManager.h:46
double x
Definition vehicleRSUManager.h:37
double distance_to_vehicle
Definition vehicleRSUManager.h:42
std::chrono::steady_clock::time_point last_seen
Definition vehicleRSUManager.h:41
double y
Definition vehicleRSUManager.h:38
double radius
Definition vehicleRSUManager.h:39