-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathservice_server_async_await.cpp
More file actions
54 lines (46 loc) · 2.49 KB
/
Copy pathservice_server_async_await.cpp
File metadata and controls
54 lines (46 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/// Copyright © 2025 Technische Hochschule Augsburg
/// All rights reserved.
/// Author: Ivo Ivanov
/// This software is licensed under the Apache License, Version 2.0.
/// This example shows how to create an asynchronously responding service server.
/// After it receives a request, it calls asynchronously another upstream service
/// that is actually capable of answering the request. Once it receives the result, it responds.
#include <icey/icey.hpp>
#include "std_srvs/srv/set_bool.hpp"
using namespace std::chrono_literals;
using ExampleService = std_srvs::srv::SetBool;
using Request = ExampleService::Request::SharedPtr;
using Response = ExampleService::Response::SharedPtr;
int main(int argc, char **argv) {
rclcpp::init(argc, argv);
auto node = std::make_shared<rclcpp::Node>("icey_service_service_async_await_example");
auto ctx = std::make_shared<icey::Context>(node.get());
/// Create a service client for an upstream service that is actually capable of answering the
/// request.
auto upstream_service_client = ctx->create_client<ExampleService>("set_bool_service_upstream");
/// Create the service server and give it a asynchronous callback (containing keyword co_await)
ctx->create_service<ExampleService>(
"set_bool_service", [&](auto request) -> icey::Promise<Response> {
RCLCPP_INFO_STREAM(
node->get_logger(),
"Received request: " << request->data << ", calling upstream service ... ");
/// Call the upstream service with 1s timeout asynchronously:
icey::Result<Response, std::string> upstream_result =
co_await upstream_service_client.call(request, 1s);
if (upstream_result.has_error()) {
RCLCPP_INFO_STREAM(node->get_logger(),
"Upstream service returned error: " << upstream_result.error());
/// Return nothing: This will simply not respond to the client, leading to a timeout
co_return nullptr;
} else {
Response upstream_response = upstream_result.value();
RCLCPP_INFO_STREAM(node->get_logger(), "Got response from upstream service: "
<< upstream_result.value()->success
<< ", responding ...");
/// Respond to the client with the upstream response:
co_return upstream_response;
}
});
RCLCPP_INFO_STREAM(node->get_logger(), "Created service server, waiting for requests ... ");
rclcpp::spin(node);
}