CARLA
primaryCommands.cpp
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 
8 
9 // #include "carla/Logging.h"
11 #include "carla/multigpu/primary.h"
12 #include "carla/multigpu/router.h"
16 
17 namespace carla {
18 namespace multigpu {
19 
21 }
22 
23 PrimaryCommands::PrimaryCommands(std::shared_ptr<Router> router) :
24  _router(router) {
25 }
26 
27 void PrimaryCommands::set_router(std::shared_ptr<Router> router) {
28  _router = router;
29 }
30 
31 // broadcast to all secondary servers the frame data
33  _router->Write(MultiGPUCommand::SEND_FRAME, std::move(buffer));
34  // log_info("sending frame command");
35 }
36 
37 // broadcast to all secondary servers the map to load
38 void PrimaryCommands::SendLoadMap(std::string map) {
39  carla::Buffer buf((unsigned char *) map.c_str(), (size_t) map.size() + 1);
40  _router->Write(MultiGPUCommand::LOAD_MAP, std::move(buf));
41 }
42 
43 // send to who the router wants the request for a token
45  log_info("asking for a token");
46  carla::Buffer buf((carla::Buffer::value_type *) &sensor_id,
47  (size_t) sizeof(stream_id));
48  auto fut = _router->WriteToNext(MultiGPUCommand::GET_TOKEN, std::move(buf));
49 
50  auto response = fut.get();
51  token_type new_token(*reinterpret_cast<carla::streaming::detail::token_data *>(response.buffer.data()));
52  log_info("got a token: ", new_token.get_stream_id(), ", ", new_token.get_port());
53  return new_token;
54 }
55 
56 // send to know if a connection is alive
58  std::string msg("Are you alive?");
59  carla::Buffer buf((unsigned char *) msg.c_str(), (size_t) msg.size());
60  log_info("sending is alive command");
61  auto fut = _router->WriteToNext(MultiGPUCommand::YOU_ALIVE, std::move(buf));
62  auto response = fut.get();
63  log_info("response from alive command: ", response.buffer.data());
64 }
65 
67  // search if the sensor has been activated in any secondary server
68  auto it = _servers.find(sensor_id);
69  if (it != _servers.end()) {
70  carla::Buffer buf((carla::Buffer::value_type *) &sensor_id,
71  (size_t) sizeof(stream_id));
72  auto fut = _router->WriteToOne(it->second, MultiGPUCommand::ENABLE_ROS, std::move(buf));
73 
74  auto response = fut.get();
75  bool res = (*reinterpret_cast<bool *>(response.buffer.data()));
76  } else {
77  log_error("enable_for_ros for sensor", sensor_id, " not found on any server");
78  }
79 }
80 
82  // search if the sensor has been activated in any secondary server
83  auto it = _servers.find(sensor_id);
84  if (it != _servers.end()) {
85  carla::Buffer buf((carla::Buffer::value_type *) &sensor_id,
86  (size_t) sizeof(stream_id));
87  auto fut = _router->WriteToOne(it->second, MultiGPUCommand::DISABLE_ROS, std::move(buf));
88 
89  auto response = fut.get();
90  bool res = (*reinterpret_cast<bool *>(response.buffer.data()));
91  } else {
92  log_error("disable_for_ros for sensor", sensor_id, " not found on any server");
93  }
94 }
95 
97  // search if the sensor has been activated in any secondary server
98  auto it = _servers.find(sensor_id);
99  if (it != _servers.end()) {
100  carla::Buffer buf((carla::Buffer::value_type *) &sensor_id,
101  (size_t) sizeof(stream_id));
102  auto fut = _router->WriteToOne(it->second, MultiGPUCommand::IS_ENABLED_ROS, std::move(buf));
103 
104  auto response = fut.get();
105  bool res = (*reinterpret_cast<bool *>(response.buffer.data()));
106  return res;
107  } else {
108  log_error("is_enabled_for_ros for sensor", sensor_id, " not found on any server");
109  return false;
110  }
111 }
112 
114  // search if the sensor has been activated in any secondary server
115  auto it = _tokens.find(sensor_id);
116  if (it != _tokens.end()) {
117  // return already activated sensor token
118  log_debug("Using token from already activated sensor: ", it->second.get_stream_id(), ", ", it->second.get_port());
119  return it->second;
120  }
121  else {
122  // enable the sensor on one secondary server
123  auto server = _router->GetNextServer();
124  auto token = SendGetToken(sensor_id);
125  // add to the maps
126  _tokens[sensor_id] = token;
127  _servers[sensor_id] = server;
128  log_debug("Using token from new activated sensor: ", token.get_stream_id(), ", ", token.get_port());
129  return token;
130  }
131 }
132 
134  auto it = _servers.find(sensor_id);
135  if (it != _servers.end()) {
136  SendEnableForROS(sensor_id);
137  } else {
138  // we need to activate the sensor in any server yet, and repeat
139  GetToken(sensor_id);
140  EnableForROS(sensor_id);
141  }
142 }
143 
145  auto it = _servers.find(sensor_id);
146  if (it != _servers.end()) {
147  SendDisableForROS(sensor_id);
148  }
149 }
150 
152  auto it = _servers.find(sensor_id);
153  if (it != _servers.end()) {
154  return SendIsEnabledForROS(sensor_id);
155  }
156  return false;
157 }
158 
159 } // namespace multigpu
160 } // namespace carla
void DisableForROS(stream_id sensor_id)
std::unordered_map< stream_id, std::weak_ptr< Primary > > _servers
token_type SendGetToken(carla::streaming::detail::stream_id_type sensor_id)
void SendLoadMap(std::string map)
static void log_error(Args &&... args)
Definition: Logging.h:110
This file contains definitions of common data structures used in traffic manager. ...
Definition: Carla.cpp:133
std::unordered_map< stream_id, token_type > _tokens
void SendEnableForROS(stream_id sensor_id)
static void log_debug(Args &&...)
Definition: Logging.h:75
Serializes a stream endpoint.
Definition: detail/Token.h:61
carla::streaming::detail::stream_id_type stream_id
unsigned char value_type
Definition: carla/Buffer.h:51
static void log_info(Args &&... args)
Definition: Logging.h:82
void EnableForROS(stream_id sensor_id)
token_type GetToken(stream_id sensor_id)
std::shared_ptr< Router > _router
const auto & get_stream_id() const
Definition: detail/Token.h:116
bool IsEnabledForROS(stream_id sensor_id)
void SendFrameData(carla::Buffer buffer)
A piece of raw data.
Definition: carla/Buffer.h:42
void SendDisableForROS(stream_id sensor_id)
bool SendIsEnabledForROS(stream_id sensor_id)
void set_router(std::shared_ptr< Router > router)