CARLA
Loading...
Searching...
No Matches
primaryCommands.h
Go to the documentation of this file.
1// Copyright (c) 2026 Computer Vision Center (CVC) at the Universitat Autonoma
2// de Barcelona (UAB).
3//
4// This work is licensed under the terms of the MIT license.
5// For a copy, see <https://opensource.org/licenses/MIT>.
6
7#pragma once
8
9// #include "carla/Logging.h"
15
16#include <cstddef>
17#include <cstdint>
18#include <mutex>
19#include <optional>
20
21namespace carla {
22namespace multigpu {
23
24// using session = std::shared_ptr<Primary>;
25// using callback_response = std::function<void(std::shared_ptr<Primary>, carla::Buffer)>;
28using actor_id = uint32_t;
29
30class Router;
31
33 public:
34
35
37 PrimaryCommands(std::shared_ptr<Router> router);
38
39 void set_router(std::shared_ptr<Router> router);
40
41 // broadcast to all secondary servers the frame data
42 void SendFrameData(carla::Buffer buffer);
43
44 // broadcast to all secondary servers the map to load
45 void SendLoadMap(std::string map, const LoadMapExtras &extras = {});
46
47 // send to know if a connection is alive
48 void SendIsAlive();
49
50 /// Returns std::nullopt if no secondary could provide the token (e.g. the
51 /// selected secondary disconnected with the request in flight, or has no
52 /// episode loaded). Nothing is cached on failure, so a later call for the
53 /// same sensor retries. A sensor whose secondary disconnected is routed
54 /// again here. @a sensor_actor_id is the primary's id of the sensor actor,
55 /// which the secondary resolves to its own sensor stream.
56 [[nodiscard]]
57 std::optional<token_type> GetToken(stream_id sensor_id, actor_id sensor_actor_id);
58
59 /// Returns false if no secondary accepted this sensor or the secondary
60 /// holding it did not reply; ROS enablement has not happened in that case.
61 [[nodiscard]]
62 bool EnableForROS(stream_id sensor_id, actor_id sensor_actor_id);
63
64 void DisableForROS(stream_id sensor_id);
65
66 bool IsEnabledForROS(stream_id sensor_id);
67
68 /// Routes every sensor whose secondary disconnected to the secondaries
69 /// connected since the previous call, under the same primary stream id,
70 /// so clients already subscribed reconnect to it without a new listen().
71 /// The first new secondary that stays connected takes every lost sensor.
72 /// A sensor the new secondary refuses is forgotten and routed lazily
73 /// later; if it disconnects instead, the sensor stays lost for the next
74 /// one. ROS publishing enabled through EnableForROS() is enabled again.
75 /// Blocks on round trips; never call it from a Router callback. Returns
76 /// how many sensors were routed again.
77 std::size_t RerouteLostSensors();
78
79 /// Drops the routing of a destroyed sensor so it is never routed again.
80 void ForgetSensor(stream_id sensor_id);
81
82 private:
83
84 struct Route {
85 std::weak_ptr<Primary> session;
87 bool ros_enabled = false;
88 };
89
90 enum class TokenError { None, NoReply, Refused };
91
92 // send to one secondary (@a target, or round-robin if null) to get the
93 // token of a sensor; also reports which secondary session actually
94 // answered, so the caller never has to guess it from a separate (and
95 // racy) round-robin lookup. On failure @a error says whether a secondary
96 // answered (Refused) or not (NoReply).
97 std::optional<token_type> SendGetToken(
98 stream_id sensor_id,
99 actor_id sensor_actor_id,
100 const std::weak_ptr<Primary> *target,
101 std::weak_ptr<Primary> &out_session,
102 TokenError &error);
103
104 // manage ROS enable/disable of sensor
105 bool SendEnableForROS(stream_id sensor_id);
106 void SendDisableForROS(stream_id sensor_id);
107 bool SendIsEnabledForROS(stream_id sensor_id);
108
109 /// Whether @a sensor_id is routed to a secondary that is still connected.
110 /// Caller must hold _mutex.
111 bool IsRoutedToLiveSecondary(stream_id sensor_id);
112
113 /// Enables ROS again on the new secondary of a re-routed sensor if it was
114 /// enabled before. Caller must hold _mutex.
115 void RestoreRosAfterReroute(stream_id sensor_id);
116
117 /// Reads a one-bool reply; @a buffer must not be empty.
118 static bool ReadBoolReply(const carla::Buffer &buffer);
119
120 std::shared_ptr<Router> _router;
121 std::unordered_map<stream_id, token_type> _tokens;
122 // Keeps the routes of disconnected secondaries until they are routed
123 // again, forgotten, or a map load clears them.
124 std::unordered_map<stream_id, Route> _servers;
125
126 // Serializes every round trip through _router (GetToken/EnableForROS/...).
127 // The router keeps at most one in-flight promise per secondary session
128 // (see Router::_promises), so two overlapping requests to the same
129 // session would otherwise clobber each other's promise.
130 std::mutex _mutex;
131};
132
133} // namespace multigpu
134} // namespace carla
A piece of raw data.
std::unordered_map< stream_id, token_type > _tokens
void set_router(std::shared_ptr< Router > router)
std::optional< token_type > GetToken(stream_id sensor_id, actor_id sensor_actor_id)
Returns std::nullopt if no secondary could provide the token (e.g.
std::size_t RerouteLostSensors()
Routes every sensor whose secondary disconnected to the secondaries connected since the previous call...
void DisableForROS(stream_id sensor_id)
bool SendIsEnabledForROS(stream_id sensor_id)
void SendFrameData(carla::Buffer buffer)
bool SendEnableForROS(stream_id sensor_id)
std::optional< token_type > SendGetToken(stream_id sensor_id, actor_id sensor_actor_id, const std::weak_ptr< Primary > *target, std::weak_ptr< Primary > &out_session, TokenError &error)
bool IsRoutedToLiveSecondary(stream_id sensor_id)
Whether sensor_id is routed to a secondary that is still connected.
bool EnableForROS(stream_id sensor_id, actor_id sensor_actor_id)
Returns false if no secondary accepted this sensor or the secondary holding it did not reply; ROS ena...
std::unordered_map< stream_id, Route > _servers
void SendDisableForROS(stream_id sensor_id)
void ForgetSensor(stream_id sensor_id)
Drops the routing of a destroyed sensor so it is never routed again.
static bool ReadBoolReply(const carla::Buffer &buffer)
Reads a one-bool reply; buffer must not be empty.
void RestoreRosAfterReroute(stream_id sensor_id)
Enables ROS again on the new secondary of a re-routed sensor if it was enabled before.
bool IsEnabledForROS(stream_id sensor_id)
std::shared_ptr< Router > _router
void SendLoadMap(std::string map, const LoadMapExtras &extras={})
Serializes a stream endpoint.
carla::streaming::detail::stream_id_type stream_id
This file contains definitions of common data structures used in traffic manager.
What a LOAD_MAP carries besides the map: for a world the primary generated from OpenDRIVE (generate_o...
Definition commands.h:51