Skip to content

Commit f41fae5

Browse files
authored
Added RCL logging for BroadcastServer (open-rmf#458)
* Added new API to pass in a logger Signed-off-by: Saurabh Kamat <kamatsaurabh01@gmail.com> * Changed test to use new BraodcastServer API Signed-off-by: Saurabh Kamat <kamatsaurabh01@gmail.com> * Removed default value of node from logging related ws api Signed-off-by: Saurabh Kamat <kamatsaurabh01@gmail.com> * Lint fixes Signed-off-by: Saurabh Kamat <kamatsaurabh01@gmail.com> --------- Signed-off-by: Saurabh Kamat <kamatsaurabh01@gmail.com>
1 parent 52a01c0 commit f41fae5

File tree

3 files changed

+91
-7
lines changed

3 files changed

+91
-7
lines changed

rmf_fleet_adapter/test/tasks/test_Delivery.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ SCENARIO("Test Delivery")
372372
/// from the websocket connection
373373
/* *INDENT-OFF* */
374374
using WebsocketServer = rmf_websocket::BroadcastServer;
375-
const auto ws_server = WebsocketServer::make(
375+
const auto ws_server = WebsocketServer::make_with_logger(
376376
37878,
377377
[ &cb_mutex, delivery_id, &completed_promise,
378378
&at_least_one_incomplete, &fulfilled_promise](
@@ -397,6 +397,7 @@ SCENARIO("Test Delivery")
397397
else
398398
at_least_one_incomplete = true;
399399
},
400+
adapter.node()->get_node_logging_interface(),
400401
WebsocketServer::ApiMsgType::TaskStateUpdate
401402
);
402403
/* *INDENT-ON* */

rmf_websocket/include/rmf_websocket/BroadcastServer.hpp

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
#include <optional>
2424
#include <rmf_utils/impl_ptr.hpp>
2525

26+
#include <rclcpp/rclcpp.hpp>
27+
2628
namespace rmf_websocket {
2729

2830
//==============================================================================
@@ -43,7 +45,8 @@ class BroadcastServer
4345

4446
using ApiMessageCallback = std::function<void(const nlohmann::json&)>;
4547

46-
/// Add a callback to convert from a Description into an active Task.
48+
/// Create a wrapper around a websocket server for receiving states and logs for
49+
/// fleets, robots and tasks
4750
///
4851
/// \param[in] port
4952
/// server url port number
@@ -59,6 +62,29 @@ class BroadcastServer
5962
ApiMessageCallback callback,
6063
std::optional<ApiMsgType> msg_selection = std::nullopt);
6164

65+
/// Create a wrapper around a websocket server for receiving states and logs for
66+
/// fleets, robots and tasks
67+
///
68+
/// \param[in] port
69+
/// server url port number
70+
///
71+
/// \param[in] callback
72+
/// callback function when the message is received
73+
///
74+
/// \param[in] node_logging_interface
75+
/// node interface for logging. Default as nullptr which will result
76+
/// in errors and additional debug information not being logged
77+
///
78+
/// \param[in] msg_selection
79+
/// selected msg type to listen. Will listen to all msg if nullopt
80+
///
81+
static std::shared_ptr<BroadcastServer> make_with_logger(
82+
const int port,
83+
ApiMessageCallback callback,
84+
const rclcpp::node_interfaces::NodeLoggingInterface::SharedPtr
85+
node_logging_interface,
86+
std::optional<ApiMsgType> msg_selection = std::nullopt);
87+
6288
/// Start Server
6389
void start();
6490

rmf_websocket/src/rmf_websocket/BroadcastServer.cpp

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,20 @@ class BroadcastServer::Implementation
3939
Implementation(
4040
const int port,
4141
ApiMessageCallback callback,
42-
std::optional<ApiMsgType> msg_selection)
42+
std::optional<ApiMsgType> msg_selection,
43+
const rclcpp::node_interfaces::NodeLoggingInterface::SharedPtr
44+
node_logging_interface)
4345
: _data(std::make_shared<Data>(std::move(callback),
44-
std::move(msg_selection)))
46+
std::move(msg_selection))),
47+
_logger_interface(node_logging_interface)
4548
{
49+
if (_logger_interface)
50+
{
51+
RCLCPP_INFO_STREAM(
52+
_logger_interface->get_logger(),
53+
"Running websocket server on port " << port);
54+
}
55+
4656
try
4757
{
4858
// Hide all logs from websocketpp
@@ -66,17 +76,42 @@ class BroadcastServer::Implementation
6676
}
6777
catch (const websocketpp::exception& e)
6878
{
69-
std::cout << e.what() << std::endl;
79+
if (_logger_interface)
80+
{
81+
RCLCPP_ERROR_STREAM(
82+
_logger_interface->get_logger(),
83+
"WebSocket exception occured: " << e.what());
84+
}
7085
}
7186
catch (...)
7287
{
73-
std::cout << "other exception" << std::endl;
88+
if (_logger_interface)
89+
{
90+
RCLCPP_ERROR(
91+
_logger_interface->get_logger(), "Unknown exception occured");
92+
}
7493
}
7594
}
7695

7796
/// Start Server
7897
void start()
7998
{
99+
if (_logger_interface)
100+
{
101+
websocketpp::lib::asio::error_code ec;
102+
auto endpoint = _data->echo_server.get_local_endpoint(ec);
103+
if (ec)
104+
{
105+
RCLCPP_ERROR_STREAM(
106+
_logger_interface->get_logger(),
107+
"Failed to create connection: " << ec.message());
108+
return;
109+
}
110+
RCLCPP_INFO_STREAM(
111+
_logger_interface->get_logger(),
112+
"Starting BroadcastServer on port " << endpoint.port());
113+
}
114+
80115
// Start the ASIO io_service run loop
81116
_server_thread = std::thread(
82117
[data = _data]() { data->echo_server.run(); });
@@ -87,6 +122,12 @@ class BroadcastServer::Implementation
87122
{
88123
if (_server_thread.joinable())
89124
{
125+
if (_logger_interface)
126+
{
127+
RCLCPP_INFO(
128+
_logger_interface->get_logger(), "Stopping BroadcastServer");
129+
}
130+
90131
_data->echo_server.get_io_service().post(
91132
[data = _data]()
92133
{
@@ -154,6 +195,7 @@ class BroadcastServer::Implementation
154195
};
155196
std::shared_ptr<Data> _data;
156197
std::thread _server_thread;
198+
rclcpp::node_interfaces::NodeLoggingInterface::SharedPtr _logger_interface;
157199
};
158200

159201
//==============================================================================
@@ -165,7 +207,22 @@ std::shared_ptr<BroadcastServer> BroadcastServer::make(
165207
auto server = std::shared_ptr<BroadcastServer>(new BroadcastServer());
166208
server->_pimpl =
167209
rmf_utils::make_unique_impl<Implementation>(
168-
port, callback, msg_selection);
210+
port, callback, msg_selection, nullptr);
211+
return server;
212+
}
213+
214+
//==============================================================================
215+
std::shared_ptr<BroadcastServer> BroadcastServer::make_with_logger(
216+
const int port,
217+
ApiMessageCallback callback,
218+
const rclcpp::node_interfaces::NodeLoggingInterface::SharedPtr
219+
node_logging_interface,
220+
std::optional<ApiMsgType> msg_selection)
221+
{
222+
auto server = std::shared_ptr<BroadcastServer>(new BroadcastServer());
223+
server->_pimpl =
224+
rmf_utils::make_unique_impl<Implementation>(
225+
port, callback, msg_selection, node_logging_interface);
169226
return server;
170227
}
171228

0 commit comments

Comments
 (0)