CARLA
Toggle main menu visibility
Loading...
Searching...
No Matches
LibCarla
source
carla
multigpu
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"
10
#include "
carla/multigpu/commands.h
"
11
#include "
carla/multigpu/primary.h
"
12
#include "
carla/streaming/detail/tcp/Message.h
"
13
#include "
carla/streaming/detail/Token.h
"
14
#include "
carla/streaming/detail/Types.h
"
15
16
#include <cstddef>
17
#include <cstdint>
18
#include <mutex>
19
#include <optional>
20
21
namespace
carla
{
22
namespace
multigpu
{
23
24
// using session = std::shared_ptr<Primary>;
25
// using callback_response = std::function<void(std::shared_ptr<Primary>, carla::Buffer)>;
26
using
token_type
=
carla::streaming::detail::token_type
;
27
using
stream_id
=
carla::streaming::detail::stream_id_type
;
28
using
actor_id
= uint32_t;
29
30
class
Router
;
31
32
class
PrimaryCommands
{
33
public
:
34
35
36
PrimaryCommands
();
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
;
86
actor_id
actor
;
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
Message.h
carla::Buffer
A piece of raw data.
Definition
carla/Buffer.h:42
carla::multigpu::PrimaryCommands::_tokens
std::unordered_map< stream_id, token_type > _tokens
Definition
primaryCommands.h:121
carla::multigpu::PrimaryCommands::set_router
void set_router(std::shared_ptr< Router > router)
Definition
primaryCommands.cpp:32
carla::multigpu::PrimaryCommands::SendIsAlive
void SendIsAlive()
Definition
primaryCommands.cpp:98
carla::multigpu::PrimaryCommands::GetToken
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.
Definition
primaryCommands.cpp:175
carla::multigpu::PrimaryCommands::RerouteLostSensors
std::size_t RerouteLostSensors()
Routes every sensor whose secondary disconnected to the secondaries connected since the previous call...
Definition
primaryCommands.cpp:255
carla::multigpu::PrimaryCommands::TokenError
TokenError
Definition
primaryCommands.h:90
carla::multigpu::PrimaryCommands::DisableForROS
void DisableForROS(stream_id sensor_id)
Definition
primaryCommands.cpp:235
carla::multigpu::PrimaryCommands::SendIsEnabledForROS
bool SendIsEnabledForROS(stream_id sensor_id)
Definition
primaryCommands.cpp:155
carla::multigpu::PrimaryCommands::_mutex
std::mutex _mutex
Definition
primaryCommands.h:130
carla::multigpu::PrimaryCommands::SendFrameData
void SendFrameData(carla::Buffer buffer)
Definition
primaryCommands.cpp:37
carla::multigpu::PrimaryCommands::SendEnableForROS
bool SendEnableForROS(stream_id sensor_id)
Definition
primaryCommands.cpp:112
carla::multigpu::PrimaryCommands::SendGetToken
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)
Definition
primaryCommands.cpp:53
carla::multigpu::PrimaryCommands::IsRoutedToLiveSecondary
bool IsRoutedToLiveSecondary(stream_id sensor_id)
Whether sensor_id is routed to a secondary that is still connected.
Definition
primaryCommands.cpp:306
carla::multigpu::PrimaryCommands::PrimaryCommands
PrimaryCommands()
Definition
primaryCommands.cpp:25
carla::multigpu::PrimaryCommands::EnableForROS
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...
Definition
primaryCommands.cpp:213
carla::multigpu::PrimaryCommands::_servers
std::unordered_map< stream_id, Route > _servers
Definition
primaryCommands.h:124
carla::multigpu::PrimaryCommands::SendDisableForROS
void SendDisableForROS(stream_id sensor_id)
Definition
primaryCommands.cpp:136
carla::multigpu::PrimaryCommands::ForgetSensor
void ForgetSensor(stream_id sensor_id)
Drops the routing of a destroyed sensor so it is never routed again.
Definition
primaryCommands.cpp:300
carla::multigpu::PrimaryCommands::ReadBoolReply
static bool ReadBoolReply(const carla::Buffer &buffer)
Reads a one-bool reply; buffer must not be empty.
Definition
primaryCommands.cpp:321
carla::multigpu::PrimaryCommands::RestoreRosAfterReroute
void RestoreRosAfterReroute(stream_id sensor_id)
Enables ROS again on the new secondary of a re-routed sensor if it was enabled before.
Definition
primaryCommands.cpp:311
carla::multigpu::PrimaryCommands::IsEnabledForROS
bool IsEnabledForROS(stream_id sensor_id)
Definition
primaryCommands.cpp:247
carla::multigpu::PrimaryCommands::_router
std::shared_ptr< Router > _router
Definition
primaryCommands.h:120
carla::multigpu::PrimaryCommands::SendLoadMap
void SendLoadMap(std::string map, const LoadMapExtras &extras={})
Definition
primaryCommands.cpp:43
carla::multigpu::Router
Definition
router.h:42
carla::streaming::detail::token_type
Serializes a stream endpoint.
Definition
detail/Token.h:61
commands.h
Token.h
carla::multigpu
Definition
commands.h:17
carla::multigpu::token_type
carla::streaming::detail::token_type token_type
Definition
primaryCommands.h:26
carla::multigpu::stream_id
carla::streaming::detail::stream_id_type stream_id
Definition
primaryCommands.h:27
carla::multigpu::actor_id
uint32_t actor_id
Definition
primaryCommands.h:28
carla::streaming::detail::stream_id_type
uint32_t stream_id_type
Definition
streaming/detail/Types.h:18
carla
This file contains definitions of common data structures used in traffic manager.
Definition
SignComponent.h:21
primary.h
Types.h
carla::multigpu::LoadMapExtras
What a LOAD_MAP carries besides the map: for a world the primary generated from OpenDRIVE (generate_o...
Definition
commands.h:51
carla::multigpu::PrimaryCommands::Route
Definition
primaryCommands.h:84
carla::multigpu::PrimaryCommands::Route::session
std::weak_ptr< Primary > session
Definition
primaryCommands.h:85
carla::multigpu::PrimaryCommands::Route::ros_enabled
bool ros_enabled
Definition
primaryCommands.h:87
carla::multigpu::PrimaryCommands::Route::actor
actor_id actor
Definition
primaryCommands.h:86
Generated by
1.18.0