CARLA
streaming/low_level/Server.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 
11 #include "carla/streaming/Stream.h"
12 
13 #include <boost/asio/io_context.hpp>
14 
15 namespace carla {
16 namespace streaming {
17 namespace low_level {
18 
19  /// A low-level streaming server. Each new stream has a token associated, this
20  /// token can be used by a client to subscribe to the stream. This server
21  /// requires an external io_context running.
22  ///
23  /// @warning This server cannot be destructed before its @a io_context is
24  /// stopped.
25  template <typename T>
26  class Server {
27  public:
28 
29  using underlying_server = T;
30 
32 
34 
36 
37  template <typename InternalEPType, typename ExternalEPType>
38  explicit Server(
39  boost::asio::io_context &io_context,
42  : _server(io_context, std::move(internal_ep)),
43  _dispatcher(std::move(external_ep)) {
44  StartServer();
45  }
46 
47  template <typename InternalEPType>
48  explicit Server(
49  boost::asio::io_context &io_context,
51  : _server(io_context, std::move(internal_ep)),
53  StartServer();
54  }
55 
56  template <typename... EPArgs>
57  explicit Server(boost::asio::io_context &io_context, EPArgs &&... args)
58  : Server(io_context, make_endpoint<protocol_type>(std::forward<EPArgs>(args)...)) {}
59 
61  return _server.GetLocalEndpoint();
62  }
63 
64  void SetTimeout(time_duration timeout) {
65  _server.SetTimeout(timeout);
66  }
67 
69  return _dispatcher.MakeStream();
70  }
71 
73  return _dispatcher.CloseStream(id);
74  }
75 
76  void SetSynchronousMode(bool is_synchro) {
77  _server.SetSynchronousMode(is_synchro);
78  }
79 
81  return _dispatcher.GetToken(sensor_id);
82  }
83 
84  void EnableForROS(stream_id sensor_id) {
85  _dispatcher.EnableForROS(sensor_id);
86  }
87 
88  void DisableForROS(stream_id sensor_id) {
89  _dispatcher.DisableForROS(sensor_id);
90  }
91 
92  bool IsEnabledForROS(stream_id sensor_id) {
93  return _dispatcher.IsEnabledForROS(sensor_id);
94  }
95 
96  private:
97 
98  void StartServer() {
99  auto on_session_opened = [this](auto session) {
100  if (!_dispatcher.RegisterSession(session)) {
101  session->Close();
102  }
103  };
104  auto on_session_closed = [this](auto session) {
105  log_debug("on_session_closed called");
107  };
108  _server.Listen(on_session_opened, on_session_closed);
109  }
110 
112 
114  };
115 
116 } // namespace low_level
117 } // namespace streaming
118 } // namespace carla
Server(boost::asio::io_context &io_context, EPArgs &&... args)
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:133
static auto make_endpoint(boost::asio::ip::basic_endpoint< Protocol > ep)
Definition: EndPoint.h:90
uint32_t stream_id_type
Definition: Types.h:18
static void log_debug(Args &&...)
Definition: Logging.h:75
token_type GetToken(stream_id_type sensor_id)
Definition: Dispatcher.cpp:106
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
token_type GetToken(stream_id sensor_id)
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
Positive time duration up to milliseconds resolution.
Definition: Time.h:19
carla::streaming::Stream MakeStream()
Definition: Dispatcher.cpp:37
void CloseStream(carla::streaming::detail::stream_id_type id)
Server(boost::asio::io_context &io_context, detail::EndPoint< protocol_type, InternalEPType > internal_ep)
underlying_server::endpoint GetLocalEndpoint() const
carla::streaming::detail::token_type token_type
Server(boost::asio::io_context &io_context, detail::EndPoint< protocol_type, InternalEPType > internal_ep, detail::EndPoint< protocol_type, ExternalEPType > external_ep)