CARLA
Loading...
Searching...
No Matches
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
14
15#include <memory>
16#include <mutex>
17#include <optional>
18#include <unordered_map>
19
20namespace carla {
21namespace streaming {
22namespace 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 EndPointType>
33 : _cached_token(0u, ep) {}
34
36
38
40
41 bool RegisterSession(std::shared_ptr<Session> session);
42
43 void DeregisterSession(std::shared_ptr<Session> session);
44
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
61
62 [[nodiscard]]
63 std::optional<stream_id_type> FindStreamAlias(stream_id_type alias);
64
66 std::scoped_lock<std::mutex> lock(_mutex);
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
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
105
107
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.
117 };
118
119} // namespace detail
120} // namespace streaming
121} // namespace carla
Keeps the mapping between streams and sessions.
Definition Dispatcher.h:28
void SetSessionsUseStreamAliases(bool enabled)
When enabled, a session subscribes through an alias (see SetStreamAlias) and a stream id without an a...
void SetStreamAlias(stream_id_type alias, stream_id_type stream_id)
Routes sessions subscribing to alias to the local stream stream_id.
void DeregisterSession(std::shared_ptr< Session > session)
std::unordered_map< const Session *, std::weak_ptr< MultiStreamState > > _session_streams
Definition Dispatcher.h:112
std::optional< stream_id_type > ResolveSessionStreamId(stream_id_type session_stream_id) const
Stream a session subscribing to session_stream_id belongs to.
Dispatcher(const EndPoint< Protocol, EndPointType > &ep)
Definition Dispatcher.h:32
carla::streaming::Stream MakeStream()
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...
bool RegisterSession(std::shared_ptr< Session > session)
void DisableForROS(stream_id_type sensor_id)
Definition Dispatcher.h:78
void SetROS2TopicVisibilityDefaultEnabled(bool enabled)
Definition Dispatcher.h:65
bool IsEnabledForROS(stream_id_type sensor_id)
Definition Dispatcher.h:86
std::optional< stream_id_type > FindStreamAlias(stream_id_type alias)
void CloseStream(carla::streaming::detail::stream_id_type id)
std::unordered_map< stream_id_type, stream_id_type > _stream_aliases
Definition Dispatcher.h:110
void RemoveStreamAlias(stream_id_type alias)
void EnableForROS(stream_id_type sensor_id)
Definition Dispatcher.h:70
token_type GetToken(stream_id_type sensor_id)
Serializes a stream endpoint.
std::unordered_map< stream_id_type, std::shared_ptr< MultiStreamState > > StreamMap
Definition Dispatcher.h:25
This file contains definitions of common data structures used in traffic manager.