CARLA
Dispatcher.h
Go to the documentation of this file.
1 // Copyright (c) 2017 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 
10 #include "carla/streaming/Stream.h"
14 
15 #include <memory>
16 #include <mutex>
17 #include <unordered_map>
18 
19 namespace carla {
20 namespace streaming {
21 namespace detail {
22 
23  class MultiStreamState;
24  using StreamMap = std::unordered_map<stream_id_type, std::shared_ptr<MultiStreamState>>;
25 
26  /// Keeps the mapping between streams and sessions.
27  class Dispatcher {
28  public:
29 
30  template <typename Protocol, typename EndPointType>
32  : _cached_token(0u, ep) {}
33 
34  ~Dispatcher();
35 
37 
39 
40  bool RegisterSession(std::shared_ptr<Session> session);
41 
42  void DeregisterSession(std::shared_ptr<Session> session);
43 
45 
46  void EnableForROS(stream_id_type sensor_id) {
47  auto search = _stream_map.find(sensor_id);
48  if (search != _stream_map.end()) {
49  search->second->EnableForROS();
50  }
51  }
52 
53  void DisableForROS(stream_id_type sensor_id) {
54  auto search = _stream_map.find(sensor_id);
55  if (search != _stream_map.end()) {
56  search->second->DisableForROS();
57  }
58  }
59 
60  bool IsEnabledForROS(stream_id_type sensor_id) {
61  auto search = _stream_map.find(sensor_id);
62  if (search != _stream_map.end()) {
63  return search->second->IsEnabledForROS();
64  }
65  return false;
66  }
67 
68  private:
69 
70  // We use a mutex here, but we assume that sessions and streams won't be
71  // created too often.
72  std::mutex _mutex;
73 
75 
77  };
78 
79 } // namespace detail
80 } // namespace streaming
81 } // namespace carla
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:133
uint32_t stream_id_type
Definition: Types.h:18
token_type GetToken(stream_id_type sensor_id)
Definition: Dispatcher.cpp:106
std::unordered_map< stream_id_type, std::shared_ptr< MultiStreamState > > StreamMap
Definition: Dispatcher.h:24
void CloseStream(carla::streaming::detail::stream_id_type id)
Definition: Dispatcher.cpp:60
Serializes a stream endpoint.
Definition: detail/Token.h:61
void DeregisterSession(std::shared_ptr< Session > session)
Definition: Dispatcher.cpp:91
bool RegisterSession(std::shared_ptr< Session > session)
Definition: Dispatcher.cpp:74
void EnableForROS(stream_id_type sensor_id)
Definition: Dispatcher.h:46
Dispatcher(const EndPoint< Protocol, EndPointType > &ep)
Definition: Dispatcher.h:31
void DisableForROS(stream_id_type sensor_id)
Definition: Dispatcher.h:53
Keeps the mapping between streams and sessions.
Definition: Dispatcher.h:27
bool IsEnabledForROS(stream_id_type sensor_id)
Definition: Dispatcher.h:60
carla::streaming::Stream MakeStream()
Definition: Dispatcher.cpp:37