Skip to content

Commit 0b966cd

Browse files
Mauro Passerinoapojomovsky
authored andcommitted
Support intra-process communication: Clients & Services (ros2#1847)
Signed-off-by: Mauro Passerino <[email protected]>
1 parent 3e9ea82 commit 0b966cd

33 files changed

+1652
-79
lines changed

rclcpp/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ set(${PROJECT_NAME}_SRCS
3939
src/rclcpp/any_executable.cpp
4040
src/rclcpp/callback_group.cpp
4141
src/rclcpp/client.cpp
42+
src/rclcpp/client_intra_process_base.cpp
4243
src/rclcpp/clock.cpp
4344
src/rclcpp/context.cpp
4445
src/rclcpp/contexts/default_context.cpp
@@ -108,6 +109,7 @@ set(${PROJECT_NAME}_SRCS
108109
src/rclcpp/serialization.cpp
109110
src/rclcpp/serialized_message.cpp
110111
src/rclcpp/service.cpp
112+
src/rclcpp/service_intra_process_base.cpp
111113
src/rclcpp/signal_handler.cpp
112114
src/rclcpp/subscription_base.cpp
113115
src/rclcpp/subscription_intra_process_base.cpp

rclcpp/include/rclcpp/client.hpp

Lines changed: 114 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,13 @@
3737

3838
#include "rclcpp/clock.hpp"
3939
#include "rclcpp/detail/cpp_callback_trampoline.hpp"
40+
#include "rclcpp/detail/resolve_use_intra_process.hpp"
4041
#include "rclcpp/exceptions.hpp"
4142
#include "rclcpp/expand_topic_or_service_name.hpp"
43+
#include "rclcpp/experimental/client_intra_process.hpp"
44+
#include "rclcpp/experimental/intra_process_manager.hpp"
4245
#include "rclcpp/function_traits.hpp"
46+
#include "rclcpp/intra_process_setting.hpp"
4347
#include "rclcpp/logging.hpp"
4448
#include "rclcpp/macros.hpp"
4549
#include "rclcpp/node_interfaces/node_graph_interface.hpp"
@@ -48,6 +52,8 @@
4852
#include "rclcpp/utilities.hpp"
4953
#include "rclcpp/visibility_control.hpp"
5054

55+
#include "rcutils/logging_macros.h"
56+
5157
#include "rmw/error_handling.h"
5258
#include "rmw/impl/cpp/demangle.hpp"
5359
#include "rmw/rmw.h"
@@ -254,6 +260,15 @@ class ClientBase
254260
rclcpp::QoS
255261
get_response_subscription_actual_qos() const;
256262

263+
/// Return the waitable for intra-process
264+
/**
265+
* \return the waitable sharedpointer for intra-process, or nullptr if intra-process is not setup.
266+
* \throws std::runtime_error if the intra process manager is destroyed
267+
*/
268+
RCLCPP_PUBLIC
269+
rclcpp::Waitable::SharedPtr
270+
get_intra_process_waitable();
271+
257272
/// Set a callback to be called when each new response is received.
258273
/**
259274
* The callback receives a size_t which is the number of responses received
@@ -358,6 +373,19 @@ class ClientBase
358373
void
359374
set_on_new_response_callback(rcl_event_callback_t callback, const void * user_data);
360375

376+
using IntraProcessManagerWeakPtr =
377+
std::weak_ptr<rclcpp::experimental::IntraProcessManager>;
378+
379+
/// Implementation detail.
380+
RCLCPP_PUBLIC
381+
void
382+
setup_intra_process(
383+
uint64_t intra_process_client_id,
384+
IntraProcessManagerWeakPtr weak_ipm);
385+
386+
std::shared_ptr<rclcpp::experimental::ClientIntraProcessBase> client_intra_process_;
387+
std::atomic_uint ipc_sequence_number_{1};
388+
361389
rclcpp::node_interfaces::NodeGraphInterface::WeakPtr node_graph_;
362390
std::shared_ptr<rcl_node_t> node_handle_;
363391
std::shared_ptr<rclcpp::Context> context_;
@@ -373,6 +401,11 @@ class ClientBase
373401
std::shared_ptr<rcl_client_t> client_handle_;
374402

375403
std::atomic<bool> in_use_by_wait_set_{false};
404+
405+
std::recursive_mutex ipc_mutex_;
406+
bool use_intra_process_{false};
407+
IntraProcessManagerWeakPtr weak_ipm_;
408+
uint64_t intra_process_client_id_;
376409
};
377410

378411
template<typename ServiceT>
@@ -468,12 +501,14 @@ class Client : public ClientBase
468501
* \param[in] node_graph The node graph interface of the corresponding node.
469502
* \param[in] service_name Name of the topic to publish to.
470503
* \param[in] client_options options for the subscription.
504+
* \param[in] ipc_setting Intra-process communication setting for the client.
471505
*/
472506
Client(
473507
rclcpp::node_interfaces::NodeBaseInterface * node_base,
474508
rclcpp::node_interfaces::NodeGraphInterface::SharedPtr node_graph,
475509
const std::string & service_name,
476-
rcl_client_options_t & client_options)
510+
rcl_client_options_t & client_options,
511+
rclcpp::IntraProcessSetting ipc_setting = rclcpp::IntraProcessSetting::NodeDefault)
477512
: ClientBase(node_base, node_graph),
478513
srv_type_support_handle_(rosidl_typesupport_cpp::get_service_type_support_handle<ServiceT>())
479514
{
@@ -496,10 +531,27 @@ class Client : public ClientBase
496531
}
497532
rclcpp::exceptions::throw_from_rcl_error(ret, "could not create client");
498533
}
534+
535+
// Setup intra process if requested.
536+
if (rclcpp::detail::resolve_use_intra_process(ipc_setting, *node_base)) {
537+
create_intra_process_client();
538+
}
499539
}
500540

501541
virtual ~Client()
502542
{
543+
if (!use_intra_process_) {
544+
return;
545+
}
546+
auto ipm = weak_ipm_.lock();
547+
if (!ipm) {
548+
// TODO(ivanpauno): should this raise an error?
549+
RCLCPP_WARN(
550+
rclcpp::get_logger("rclcpp"),
551+
"Intra process manager died before than a client.");
552+
return;
553+
}
554+
ipm->remove_client(intra_process_client_id_);
503555
}
504556

505557
/// Take the next response for this client.
@@ -616,7 +668,7 @@ class Client : public ClientBase
616668
Promise promise;
617669
auto future = promise.get_future();
618670
auto req_id = async_send_request_impl(
619-
*request,
671+
std::move(request),
620672
std::move(promise));
621673
return FutureAndRequestId(std::move(future), req_id);
622674
}
@@ -651,7 +703,7 @@ class Client : public ClientBase
651703
Promise promise;
652704
auto shared_future = promise.get_future().share();
653705
auto req_id = async_send_request_impl(
654-
*request,
706+
std::move(request),
655707
std::make_tuple(
656708
CallbackType{std::forward<CallbackT>(cb)},
657709
shared_future,
@@ -682,7 +734,7 @@ class Client : public ClientBase
682734
PromiseWithRequest promise;
683735
auto shared_future = promise.get_future().share();
684736
auto req_id = async_send_request_impl(
685-
*request,
737+
request,
686738
std::make_tuple(
687739
CallbackWithRequestType{std::forward<CallbackT>(cb)},
688740
request,
@@ -824,11 +876,33 @@ class Client : public ClientBase
824876
CallbackWithRequestTypeValueVariant>;
825877

826878
int64_t
827-
async_send_request_impl(const Request & request, CallbackInfoVariant value)
879+
async_send_request_impl(SharedRequest request, CallbackInfoVariant value)
828880
{
881+
std::lock_guard<std::recursive_mutex> ipc_lock(ipc_mutex_);
882+
if (use_intra_process_) {
883+
auto ipm = weak_ipm_.lock();
884+
if (!ipm) {
885+
throw std::runtime_error(
886+
"intra process send called after destruction of intra process manager");
887+
}
888+
bool intra_process_server_available = ipm->service_is_available(intra_process_client_id_);
889+
890+
// Check if there's an intra-process server available matching this client.
891+
// If there's not, we fall back into inter-process communication, since
892+
// the server might be available in another process or was configured to not use IPC.
893+
if (intra_process_server_available) {
894+
// Send intra-process request
895+
ipm->send_intra_process_client_request<ServiceT>(
896+
intra_process_client_id_,
897+
std::make_pair(std::move(request), std::move(value)));
898+
return ipc_sequence_number_++;
899+
}
900+
}
901+
902+
// Send inter-process request
829903
int64_t sequence_number;
830904
std::lock_guard<std::mutex> lock(pending_requests_mutex_);
831-
rcl_ret_t ret = rcl_send_request(get_client_handle().get(), &request, &sequence_number);
905+
rcl_ret_t ret = rcl_send_request(get_client_handle().get(), request.get(), &sequence_number);
832906
if (RCL_RET_OK != ret) {
833907
rclcpp::exceptions::throw_from_rcl_error(ret, "failed to send request");
834908
}
@@ -854,6 +928,40 @@ class Client : public ClientBase
854928
return value;
855929
}
856930

931+
void
932+
create_intra_process_client()
933+
{
934+
// Check if the QoS is compatible with intra-process.
935+
auto qos_profile = get_response_subscription_actual_qos();
936+
937+
if (qos_profile.history() != rclcpp::HistoryPolicy::KeepLast) {
938+
throw std::invalid_argument(
939+
"intraprocess communication allowed only with keep last history qos policy");
940+
}
941+
if (qos_profile.depth() == 0) {
942+
throw std::invalid_argument(
943+
"intraprocess communication is not allowed with 0 depth qos policy");
944+
}
945+
if (qos_profile.durability() != rclcpp::DurabilityPolicy::Volatile) {
946+
throw std::invalid_argument(
947+
"intraprocess communication allowed only with volatile durability");
948+
}
949+
950+
// Create a ClientIntraProcess which will be given to the intra-process manager.
951+
using ClientIntraProcessT = rclcpp::experimental::ClientIntraProcess<ServiceT>;
952+
953+
client_intra_process_ = std::make_shared<ClientIntraProcessT>(
954+
context_,
955+
this->get_service_name(),
956+
qos_profile);
957+
958+
// Add it to the intra process manager.
959+
using rclcpp::experimental::IntraProcessManager;
960+
auto ipm = context_->get_sub_context<IntraProcessManager>();
961+
uint64_t intra_process_client_id = ipm->add_intra_process_client(client_intra_process_);
962+
this->setup_intra_process(intra_process_client_id, ipm);
963+
}
964+
857965
RCLCPP_DISABLE_COPY(Client)
858966

859967
std::unordered_map<

rclcpp/include/rclcpp/create_client.hpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,15 @@ create_client(
4646
std::shared_ptr<node_interfaces::NodeServicesInterface> node_services,
4747
const std::string & service_name,
4848
const rclcpp::QoS & qos = rclcpp::ServicesQoS(),
49-
rclcpp::CallbackGroup::SharedPtr group = nullptr)
49+
rclcpp::CallbackGroup::SharedPtr group = nullptr,
50+
rclcpp::IntraProcessSetting ipc_setting = rclcpp::IntraProcessSetting::NodeDefault)
5051
{
5152
return create_client<ServiceT>(
5253
node_base, node_graph, node_services,
5354
service_name,
5455
qos.get_rmw_qos_profile(),
55-
group);
56+
group,
57+
ipc_setting);
5658
}
5759

5860
/// Create a service client with a given type.
@@ -65,7 +67,8 @@ create_client(
6567
std::shared_ptr<node_interfaces::NodeServicesInterface> node_services,
6668
const std::string & service_name,
6769
const rmw_qos_profile_t & qos_profile,
68-
rclcpp::CallbackGroup::SharedPtr group)
70+
rclcpp::CallbackGroup::SharedPtr group,
71+
rclcpp::IntraProcessSetting ipc_setting = rclcpp::IntraProcessSetting::NodeDefault)
6972
{
7073
rcl_client_options_t options = rcl_client_get_default_options();
7174
options.qos = qos_profile;
@@ -74,7 +77,8 @@ create_client(
7477
node_base.get(),
7578
node_graph,
7679
service_name,
77-
options);
80+
options,
81+
ipc_setting);
7882

7983
auto cli_base_ptr = std::dynamic_pointer_cast<rclcpp::ClientBase>(cli);
8084
node_services->add_client(cli_base_ptr, group);

rclcpp/include/rclcpp/create_service.hpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,12 @@ create_service(
4646
const std::string & service_name,
4747
CallbackT && callback,
4848
const rclcpp::QoS & qos,
49-
rclcpp::CallbackGroup::SharedPtr group)
49+
rclcpp::CallbackGroup::SharedPtr group,
50+
rclcpp::IntraProcessSetting ipc_setting = rclcpp::IntraProcessSetting::NodeDefault)
5051
{
5152
return create_service<ServiceT, CallbackT>(
5253
node_base, node_services, service_name,
53-
std::forward<CallbackT>(callback), qos.get_rmw_qos_profile(), group);
54+
std::forward<CallbackT>(callback), qos.get_rmw_qos_profile(), group, ipc_setting);
5455
}
5556

5657
/// Create a service with a given type.
@@ -63,7 +64,8 @@ create_service(
6364
const std::string & service_name,
6465
CallbackT && callback,
6566
const rmw_qos_profile_t & qos_profile,
66-
rclcpp::CallbackGroup::SharedPtr group)
67+
rclcpp::CallbackGroup::SharedPtr group,
68+
rclcpp::IntraProcessSetting ipc_setting = rclcpp::IntraProcessSetting::NodeDefault)
6769
{
6870
rclcpp::AnyServiceCallback<ServiceT> any_service_callback;
6971
any_service_callback.set(std::forward<CallbackT>(callback));
@@ -72,8 +74,8 @@ create_service(
7274
service_options.qos = qos_profile;
7375

7476
auto serv = Service<ServiceT>::make_shared(
75-
node_base->get_shared_rcl_node_handle(),
76-
service_name, any_service_callback, service_options);
77+
node_base,
78+
service_name, any_service_callback, service_options, ipc_setting);
7779
auto serv_base_ptr = std::dynamic_pointer_cast<ServiceBase>(serv);
7880
node_services->add_service(serv_base_ptr, group);
7981
return serv;

rclcpp/include/rclcpp/detail/resolve_use_intra_process.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ namespace detail
2626
{
2727

2828
/// Return whether or not intra process is enabled, resolving "NodeDefault" if needed.
29-
template<typename OptionsT, typename NodeBaseT>
29+
template<typename NodeBaseT>
3030
bool
31-
resolve_use_intra_process(const OptionsT & options, const NodeBaseT & node_base)
31+
resolve_use_intra_process(IntraProcessSetting ipc_setting, const NodeBaseT & node_base)
3232
{
3333
bool use_intra_process;
34-
switch (options.use_intra_process_comm) {
34+
switch (ipc_setting) {
3535
case IntraProcessSetting::Enable:
3636
use_intra_process = true;
3737
break;

rclcpp/include/rclcpp/experimental/buffers/intra_process_buffer.hpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,51 @@ class TypedIntraProcessBuffer : public IntraProcessBuffer<MessageT, Alloc, Messa
239239
}
240240
};
241241

242+
template<typename BufferT>
243+
class ServiceIntraProcessBuffer : public IntraProcessBufferBase
244+
{
245+
public:
246+
RCLCPP_SMART_PTR_ALIASES_ONLY(ServiceIntraProcessBuffer)
247+
248+
virtual ~ServiceIntraProcessBuffer() {}
249+
250+
explicit
251+
ServiceIntraProcessBuffer(
252+
std::unique_ptr<BufferImplementationBase<BufferT>> buffer_impl)
253+
{
254+
buffer_ = std::move(buffer_impl);
255+
}
256+
257+
bool use_take_shared_method() const override
258+
{
259+
return false;
260+
}
261+
262+
bool has_data() const override
263+
{
264+
return buffer_->has_data();
265+
}
266+
267+
void clear() override
268+
{
269+
buffer_->clear();
270+
}
271+
272+
void add(BufferT && msg)
273+
{
274+
buffer_->enqueue(std::move(msg));
275+
}
276+
277+
BufferT
278+
consume()
279+
{
280+
return buffer_->dequeue();
281+
}
282+
283+
private:
284+
std::unique_ptr<BufferImplementationBase<BufferT>> buffer_;
285+
};
286+
242287
} // namespace buffers
243288
} // namespace experimental
244289
} // namespace rclcpp

0 commit comments

Comments
 (0)