CARLA
router.h
Go to the documentation of this file.
1 // Copyright (c) 2022 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/Logging.h"
11 #include "carla/ThreadPool.h"
12 #include "carla/multigpu/primary.h"
15 
16 #include <boost/asio/io_context.hpp>
17 #include <boost/asio/ip/tcp.hpp>
18 
19 #include <mutex>
20 #include <vector>
21 #include <sstream>
22 #include <unordered_map>
23 
24 namespace carla {
25 namespace multigpu {
26 
27  // class Primary;
28  class Listener;
29 
30  struct SessionInfo {
31  std::shared_ptr<Primary> session;
33  };
34 
35  class Router : public std::enable_shared_from_this<Router> {
36  public:
37 
38  Router(void);
39  explicit Router(uint16_t port);
40  ~Router();
41 
42  void Write(MultiGPUCommand id, Buffer &&buffer);
43  std::future<SessionInfo> WriteToNext(MultiGPUCommand id, Buffer &&buffer);
44  std::future<SessionInfo> WriteToOne(std::weak_ptr<Primary> server, MultiGPUCommand id, Buffer &&buffer);
45  void Stop();
46 
47  void SetCallbacks();
48  void SetNewConnectionCallback(std::function<void(void)>);
49 
50  void AsyncRun(size_t worker_threads);
51 
52  boost::asio::ip::tcp::endpoint GetLocalEndpoint() const;
53 
55  return (!_sessions.empty());
56  }
57 
59  return _commander;
60  }
61 
62  std::weak_ptr<Primary> GetNextServer();
63 
64  private:
65  void ConnectSession(std::shared_ptr<Primary> session);
66  void DisconnectSession(std::shared_ptr<Primary> session);
67  void ClearSessions();
68 
69  // mutex and thread pool must be at the beginning to be destroyed last
70  std::mutex _mutex;
72  boost::asio::ip::tcp::endpoint _endpoint;
73  std::vector<std::shared_ptr<Primary>> _sessions;
74  std::shared_ptr<Listener> _listener;
75  uint32_t _next;
76  std::unordered_map<Primary *, std::shared_ptr<std::promise<SessionInfo>>> _promises;
78  std::function<void(void)> _callback;
79  };
80 
81 } // namespace multigpu
82 } // namespace carla
ThreadPool _pool
Definition: router.h:71
boost::asio::ip::tcp::endpoint _endpoint
Definition: router.h:72
std::shared_ptr< Primary > session
Definition: router.h:31
bool HasClientsConnected()
Definition: router.h:54
std::function< void(void)> _callback
Definition: router.h:78
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:133
std::unordered_map< Primary *, std::shared_ptr< std::promise< SessionInfo > > > _promises
Definition: router.h:76
PrimaryCommands & GetCommander()
Definition: router.h:58
std::shared_ptr< Listener > _listener
Definition: router.h:74
std::vector< std::shared_ptr< Primary > > _sessions
Definition: router.h:73
A thread pool based on Boost.Asio&#39;s io context.
Definition: ThreadPool.h:24
PrimaryCommands _commander
Definition: router.h:77
A piece of raw data.
Definition: carla/Buffer.h:42
std::mutex _mutex
Definition: router.h:70
carla::Buffer buffer
Definition: router.h:32