CARLA
streaming/low_level/Client.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 
12 #include <boost/asio/io_context.hpp>
13 
14 #include <memory>
15 #include <unordered_map>
16 
17 namespace carla {
18 namespace streaming {
19 namespace low_level {
20 
21  /// A client able to subscribe to multiple streams. Accepts an external
22  /// io_context.
23  ///
24  /// @warning The client should not be destroyed before the @a io_context is
25  /// stopped.
26  template <typename T>
27  class Client {
28  public:
29 
30  using underlying_client = T;
33 
34  explicit Client(boost::asio::ip::address fallback_address)
35  : _fallback_address(std::move(fallback_address)) {}
36 
37  explicit Client(const std::string &fallback_address)
38  : Client(carla::streaming::make_address(fallback_address)) {}
39 
40  explicit Client()
41  : Client(carla::streaming::make_localhost_address()) {}
42 
43  ~Client() {
44  for (auto &pair : _clients) {
45  pair.second->Stop();
46  }
47  }
48 
49  /// @warning cannot subscribe twice to the same stream (even if it's a
50  /// MultiStream).
51  template <typename Functor>
52  void Subscribe(
53  boost::asio::io_context &io_context,
54  token_type token,
55  Functor &&callback) {
56  DEBUG_ASSERT_EQ(_clients.find(token.get_stream_id()), _clients.end());
57  if (!token.has_address()) {
59  }
60  auto client = std::make_shared<underlying_client>(
61  io_context,
62  token,
63  std::forward<Functor>(callback));
64  client->Connect();
65  _clients.emplace(token.get_stream_id(), std::move(client));
66  }
67 
68  void UnSubscribe(token_type token) {
69  auto it = _clients.find(token.get_stream_id());
70  if (it != _clients.end()) {
71  it->second->Stop();
72  _clients.erase(it);
73  }
74  }
75 
76  private:
77 
78  boost::asio::ip::address _fallback_address;
79 
80  std::unordered_map<
82  std::shared_ptr<underlying_client>> _clients;
83  };
84 
85 } // namespace low_level
86 } // namespace streaming
87 } // namespace carla
static auto make_localhost_address()
Definition: EndPoint.h:71
std::unordered_map< detail::stream_id_type, std::shared_ptr< underlying_client > > _clients
A client able to subscribe to multiple streams.
static auto make_address(const std::string &address)
Definition: EndPoint.h:75
void set_address(const boost::asio::ip::address &addr)
Definition: Token.cpp:18
Client(boost::asio::ip::address fallback_address)
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:99
uint32_t stream_id_type
Definition: Types.h:18
#define DEBUG_ASSERT_EQ(lhs, rhs)
Definition: Debug.h:76
A client that connects to a single stream.
Client(const std::string &fallback_address)
Serializes a stream endpoint.
Definition: detail/Token.h:61
const auto & get_stream_id() const
Definition: detail/Token.h:111
void Subscribe(boost::asio::io_context &io_context, token_type token, Functor &&callback)