CARLA
Toggle main menu visibility
Loading...
Searching...
No Matches
LibCarla
source
carla
streaming
detail
Dispatcher.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/streaming/EndPoint.h
"
10
#include "
carla/streaming/Stream.h
"
11
#include "
carla/streaming/detail/Session.h
"
12
#include "
carla/streaming/detail/Stream.h
"
13
#include "
carla/streaming/detail/Token.h
"
14
15
#include <memory>
16
#include <mutex>
17
#include <optional>
18
#include <unordered_map>
19
20
namespace
carla
{
21
namespace
streaming
{
22
namespace
detail
{
23
24
class
MultiStreamState
;
25
using
StreamMap
= std::unordered_map<stream_id_type, std::shared_ptr<MultiStreamState>>;
26
27
/// Keeps the mapping between streams and sessions.
28
class
Dispatcher
{
29
public
:
30
31
template
<
typename
Protocol,
typename
EndPo
int
Type>
32
explicit
Dispatcher
(
const
EndPoint<Protocol, EndPointType>
&ep)
33
:
_cached_token
(0u, ep) {}
34
35
~Dispatcher
();
36
37
carla::streaming::Stream
MakeStream
();
38
39
void
CloseStream
(
carla::streaming::detail::stream_id_type
id
);
40
41
bool
RegisterSession
(std::shared_ptr<Session> session);
42
43
void
DeregisterSession
(std::shared_ptr<Session> session);
44
45
token_type
GetToken
(
stream_id_type
sensor_id);
46
47
/// Like GetToken(), but returns std::nullopt for an unknown @a stream_id
48
/// instead of creating an empty stream for it.
49
[[nodiscard]]
50
std::optional<token_type>
FindToken
(
stream_id_type
stream_id);
51
52
/// When enabled, a session subscribes through an alias (see
53
/// SetStreamAlias) and a stream id without an alias is refused, since
54
/// aliases and local stream ids are separate id spaces that may overlap.
55
void
SetSessionsUseStreamAliases
(
bool
enabled);
56
57
/// Routes sessions subscribing to @a alias to the local stream @a stream_id.
58
void
SetStreamAlias
(
stream_id_type
alias,
stream_id_type
stream_id);
59
60
void
RemoveStreamAlias
(
stream_id_type
alias);
61
62
[[nodiscard]]
63
std::optional<stream_id_type>
FindStreamAlias
(
stream_id_type
alias);
64
65
void
SetROS2TopicVisibilityDefaultEnabled
(
bool
enabled) {
66
std::scoped_lock<std::mutex> lock(
_mutex
);
67
_topic_visibility_default_enabled
= enabled;
68
}
69
70
void
EnableForROS
(
stream_id_type
sensor_id) {
71
std::scoped_lock<std::mutex> lock(
_mutex
);
72
auto
search =
_stream_map
.find(sensor_id);
73
if
(search !=
_stream_map
.end()) {
74
search->second->EnableForROS();
75
}
76
}
77
78
void
DisableForROS
(
stream_id_type
sensor_id) {
79
std::scoped_lock<std::mutex> lock(
_mutex
);
80
auto
search =
_stream_map
.find(sensor_id);
81
if
(search !=
_stream_map
.end()) {
82
search->second->DisableForROS();
83
}
84
}
85
86
bool
IsEnabledForROS
(
stream_id_type
sensor_id) {
87
std::scoped_lock<std::mutex> lock(
_mutex
);
88
auto
search =
_stream_map
.find(sensor_id);
89
if
(search !=
_stream_map
.end()) {
90
return
search->second->IsEnabledForROS();
91
}
92
return
false
;
93
}
94
95
private
:
96
97
/// Stream a session subscribing to @a session_stream_id belongs to.
98
std::optional<stream_id_type>
ResolveSessionStreamId
(
stream_id_type
session_stream_id)
const
;
99
100
// We use a mutex here, but we assume that sessions and streams won't be
101
// created too often.
102
std::mutex
_mutex
;
103
104
token_type
_cached_token
;
105
106
StreamMap
_stream_map
;
107
108
bool
_sessions_use_stream_aliases
{
false
};
109
110
std::unordered_map<stream_id_type, stream_id_type>
_stream_aliases
;
111
112
std::unordered_map<const Session *, std::weak_ptr<MultiStreamState>>
_session_streams
;
113
114
// When true, every newly created stream starts visible to ROS 2 without an
115
// explicit EnableForROS call. Driven by the ROS2TopicVisibility setting.
116
bool
_topic_visibility_default_enabled
{
false
};
117
};
118
119
}
// namespace detail
120
}
// namespace streaming
121
}
// namespace carla
EndPoint.h
Session.h
Stream.h
carla::streaming::detail::Dispatcher
Keeps the mapping between streams and sessions.
Definition
Dispatcher.h:28
carla::streaming::detail::Dispatcher::_topic_visibility_default_enabled
bool _topic_visibility_default_enabled
Definition
Dispatcher.h:116
carla::streaming::detail::Dispatcher::SetSessionsUseStreamAliases
void SetSessionsUseStreamAliases(bool enabled)
When enabled, a session subscribes through an alias (see SetStreamAlias) and a stream id without an a...
Definition
Dispatcher.cpp:169
carla::streaming::detail::Dispatcher::SetStreamAlias
void SetStreamAlias(stream_id_type alias, stream_id_type stream_id)
Routes sessions subscribing to alias to the local stream stream_id.
Definition
Dispatcher.cpp:174
carla::streaming::detail::Dispatcher::DeregisterSession
void DeregisterSession(std::shared_ptr< Session > session)
Definition
Dispatcher.cpp:108
carla::streaming::detail::Dispatcher::_session_streams
std::unordered_map< const Session *, std::weak_ptr< MultiStreamState > > _session_streams
Definition
Dispatcher.h:112
carla::streaming::detail::Dispatcher::ResolveSessionStreamId
std::optional< stream_id_type > ResolveSessionStreamId(stream_id_type session_stream_id) const
Stream a session subscribing to session_stream_id belongs to.
Definition
Dispatcher.cpp:77
carla::streaming::detail::Dispatcher::Dispatcher
Dispatcher(const EndPoint< Protocol, EndPointType > &ep)
Definition
Dispatcher.h:32
carla::streaming::detail::Dispatcher::_sessions_use_stream_aliases
bool _sessions_use_stream_aliases
Definition
Dispatcher.h:108
carla::streaming::detail::Dispatcher::_stream_map
StreamMap _stream_map
Definition
Dispatcher.h:106
carla::streaming::detail::Dispatcher::MakeStream
carla::streaming::Stream MakeStream()
Definition
Dispatcher.cpp:37
carla::streaming::detail::Dispatcher::FindToken
std::optional< token_type > FindToken(stream_id_type stream_id)
Like GetToken(), but returns std::nullopt for an unknown stream_id instead of creating an empty strea...
Definition
Dispatcher.cpp:158
carla::streaming::detail::Dispatcher::_cached_token
token_type _cached_token
Definition
Dispatcher.h:104
carla::streaming::detail::Dispatcher::RegisterSession
bool RegisterSession(std::shared_ptr< Session > session)
Definition
Dispatcher.cpp:88
carla::streaming::detail::Dispatcher::DisableForROS
void DisableForROS(stream_id_type sensor_id)
Definition
Dispatcher.h:78
carla::streaming::detail::Dispatcher::_mutex
std::mutex _mutex
Definition
Dispatcher.h:102
carla::streaming::detail::Dispatcher::SetROS2TopicVisibilityDefaultEnabled
void SetROS2TopicVisibilityDefaultEnabled(bool enabled)
Definition
Dispatcher.h:65
carla::streaming::detail::Dispatcher::IsEnabledForROS
bool IsEnabledForROS(stream_id_type sensor_id)
Definition
Dispatcher.h:86
carla::streaming::detail::Dispatcher::FindStreamAlias
std::optional< stream_id_type > FindStreamAlias(stream_id_type alias)
Definition
Dispatcher.cpp:184
carla::streaming::detail::Dispatcher::CloseStream
void CloseStream(carla::streaming::detail::stream_id_type id)
Definition
Dispatcher.cpp:63
carla::streaming::detail::Dispatcher::_stream_aliases
std::unordered_map< stream_id_type, stream_id_type > _stream_aliases
Definition
Dispatcher.h:110
carla::streaming::detail::Dispatcher::RemoveStreamAlias
void RemoveStreamAlias(stream_id_type alias)
Definition
Dispatcher.cpp:179
carla::streaming::detail::Dispatcher::EnableForROS
void EnableForROS(stream_id_type sensor_id)
Definition
Dispatcher.h:70
carla::streaming::detail::Dispatcher::GetToken
token_type GetToken(stream_id_type sensor_id)
Definition
Dispatcher.cpp:127
carla::streaming::detail::EndPoint
Definition
EndPoint.h:24
carla::streaming::detail::MultiStreamState
A stream state that can hold any number of sessions.
Definition
MultiStreamState.h:25
carla::streaming::detail::token_type
Serializes a stream endpoint.
Definition
detail/Token.h:61
Stream.h
Token.h
carla::streaming::detail
Definition
Dispatcher.cpp:17
carla::streaming::detail::StreamMap
std::unordered_map< stream_id_type, std::shared_ptr< MultiStreamState > > StreamMap
Definition
Dispatcher.h:25
carla::streaming::detail::stream_id_type
uint32_t stream_id_type
Definition
streaming/detail/Types.h:18
carla::streaming
Definition
client/detail/Client.h:61
carla::streaming::Stream
detail::Stream< detail::MultiStreamState > Stream
A stream represents an unidirectional channel for sending data from server to client.
Definition
Stream.h:19
carla
This file contains definitions of common data structures used in traffic manager.
Definition
SignComponent.h:21
Generated by
1.18.0