2020#include " rclcpp/executors/events_executor.hpp"
2121#include " rclcpp/executors/events_executor_entities_collector.hpp"
2222
23- using rclcpp::executors::EventsExecutorCallbackData ;
23+ using rclcpp::executors::ExecutorEvent ;
2424using rclcpp::executors::EventsExecutorEntitiesCollector;
2525
2626EventsExecutorEntitiesCollector::EventsExecutorEntitiesCollector (
@@ -76,7 +76,6 @@ EventsExecutorEntitiesCollector::~EventsExecutorEntitiesCollector()
7676 weak_nodes_.clear ();
7777 weak_clients_map_.clear ();
7878 weak_services_map_.clear ();
79- callback_data_map_.clear ();
8079 weak_waitables_map_.clear ();
8180 weak_subscriptions_map_.clear ();
8281 weak_nodes_to_guard_conditions_.clear ();
@@ -238,9 +237,8 @@ EventsExecutorEntitiesCollector::set_callback_group_entities_callbacks(
238237 if (subscription) {
239238 weak_subscriptions_map_.emplace (subscription.get (), subscription);
240239
241- subscription->set_listener_callback (
242- &EventsExecutor::push_event,
243- get_callback_data (subscription.get (), SUBSCRIPTION_EVENT));
240+ subscription->set_on_new_message_callback (
241+ create_entity_callback (subscription.get (), SUBSCRIPTION_EVENT));
244242 }
245243 return false ;
246244 });
@@ -249,9 +247,8 @@ EventsExecutorEntitiesCollector::set_callback_group_entities_callbacks(
249247 if (service) {
250248 weak_services_map_.emplace (service.get (), service);
251249
252- service->set_listener_callback (
253- &EventsExecutor::push_event,
254- get_callback_data (service.get (), SERVICE_EVENT));
250+ service->set_on_new_request_callback (
251+ create_entity_callback (service.get (), SERVICE_EVENT));
255252 }
256253 return false ;
257254 });
@@ -260,9 +257,8 @@ EventsExecutorEntitiesCollector::set_callback_group_entities_callbacks(
260257 if (client) {
261258 weak_clients_map_.emplace (client.get (), client);
262259
263- client->set_listener_callback (
264- &EventsExecutor::push_event,
265- get_callback_data (client.get (), CLIENT_EVENT));
260+ client->set_on_new_response_callback (
261+ create_entity_callback (client.get (), CLIENT_EVENT));
266262 }
267263 return false ;
268264 });
@@ -271,9 +267,8 @@ EventsExecutorEntitiesCollector::set_callback_group_entities_callbacks(
271267 if (waitable) {
272268 weak_waitables_map_.emplace (waitable.get (), waitable);
273269
274- waitable->set_listener_callback (
275- &EventsExecutor::push_event,
276- get_callback_data (waitable.get (), WAITABLE_EVENT));
270+ // waitable->set_listener_callback(
271+ // create_entity_callback(waitable.get(), WAITABLE_EVENT));
277272 }
278273 return false ;
279274 });
@@ -296,36 +291,32 @@ EventsExecutorEntitiesCollector::unset_callback_group_entities_callbacks(
296291 group->find_subscription_ptrs_if (
297292 [this ](const rclcpp::SubscriptionBase::SharedPtr & subscription) {
298293 if (subscription) {
299- subscription->set_listener_callback ( nullptr , nullptr );
294+ subscription->set_on_new_message_callback ( nullptr );
300295 weak_subscriptions_map_.erase (subscription.get ());
301- remove_callback_data (subscription.get (), SUBSCRIPTION_EVENT);
302296 }
303297 return false ;
304298 });
305299 group->find_service_ptrs_if (
306300 [this ](const rclcpp::ServiceBase::SharedPtr & service) {
307301 if (service) {
308- service->set_listener_callback ( nullptr , nullptr );
302+ service->set_on_new_request_callback ( nullptr );
309303 weak_services_map_.erase (service.get ());
310- remove_callback_data (service.get (), SERVICE_EVENT);
311304 }
312305 return false ;
313306 });
314307 group->find_client_ptrs_if (
315308 [this ](const rclcpp::ClientBase::SharedPtr & client) {
316309 if (client) {
317- client->set_listener_callback ( nullptr , nullptr );
310+ client->set_on_new_response_callback ( nullptr );
318311 weak_clients_map_.erase (client.get ());
319- remove_callback_data (client.get (), CLIENT_EVENT);
320312 }
321313 return false ;
322314 });
323315 group->find_waitable_ptrs_if (
324316 [this ](const rclcpp::Waitable::SharedPtr & waitable) {
325317 if (waitable) {
326- waitable->set_listener_callback (nullptr , nullptr );
318+ waitable->set_listener_callback (nullptr );
327319 weak_waitables_map_.erase (waitable.get ());
328- remove_callback_data (waitable.get (), WAITABLE_EVENT);
329320 }
330321 return false ;
331322 });
@@ -487,28 +478,13 @@ void
487478EventsExecutorEntitiesCollector::set_guard_condition_callback (
488479 const rclcpp::GuardCondition * guard_condition)
489480{
490- rcl_ret_t ret = rcl_guard_condition_set_listener_callback (
491- guard_condition,
492- &EventsExecutor::push_event,
493- get_callback_data (this , WAITABLE_EVENT));
494-
495- if (ret != RCL_RET_OK) {
496- throw std::runtime_error (" Couldn't set guard condition event callback" );
497- }
481+ // create_entity_callback(this, WAITABLE_EVENT);
498482}
499483
500484void
501485EventsExecutorEntitiesCollector::unset_guard_condition_callback (
502486 const rclcpp::GuardCondition * guard_condition)
503487{
504- rcl_ret_t ret = rcl_guard_condition_set_listener_callback (
505- guard_condition, nullptr , nullptr );
506-
507- if (ret != RCL_RET_OK) {
508- throw std::runtime_error (" Couldn't unset guard condition event callback" );
509- }
510-
511- remove_callback_data (this , WAITABLE_EVENT);
512488}
513489
514490rclcpp::SubscriptionBase::SharedPtr
@@ -592,56 +568,21 @@ EventsExecutorEntitiesCollector::add_waitable(rclcpp::Waitable::SharedPtr waitab
592568{
593569 weak_waitables_map_.emplace (waitable.get (), waitable);
594570
595- waitable->set_listener_callback (
596- &EventsExecutor::push_event,
597- get_callback_data (waitable.get (), WAITABLE_EVENT));
571+ waitable->set_listener_callback ([] (size_t , int ){});
598572}
599573
600- const EventsExecutorCallbackData *
601- EventsExecutorEntitiesCollector::get_callback_data (
574+ std::function< void ( size_t )>
575+ EventsExecutorEntitiesCollector::create_entity_callback (
602576 void * entity_id, ExecutorEventType event_type)
603577{
604- // Create an entity callback data object and check if
605- // we already have stored one like it
606- ExecutorEvent event = {entity_id, event_type, 0 };
607- EventsExecutorCallbackData data (associated_executor_, event);
608-
609- auto it = callback_data_map_.find (data);
610-
611- if (it != callback_data_map_.end ()) {
612- // We found a callback data matching entity ID and type.
613- // Increment callback data counter and return pointer to data
614- it->second ++;
615- return &it->first ;
616- }
617-
618- // There was no callback data object matching ID and type,
619- // create one and set counter to 1.
620- callback_data_map_.emplace (data, 1 );
621-
622- // Return a pointer to the just added entity callback data.
623- it = callback_data_map_.find (data);
624- return &it->first ;
625- }
626-
627- void
628- EventsExecutorEntitiesCollector::remove_callback_data (
629- void * entity_id, ExecutorEventType event_type)
630- {
631- // Create an entity callback data object and check if
632- // we already have stored one like it
633- ExecutorEvent event = {entity_id, event_type, 0 };
634- EventsExecutorCallbackData data (associated_executor_, event);
635-
636- auto it = callback_data_map_.find (data);
637-
638- if (it != callback_data_map_.end ()) {
639- // We found a callback data matching entity ID and type.
640- // If we have more than 1 decrement counter, otherwise remove it.
641- if (it->second > 1 ) {
642- it->second --;
643- } else {
644- callback_data_map_.erase (it);
578+ return [this , entity_id, event_type](size_t num_events) {
579+ ExecutorEvent event = {entity_id, event_type, num_events};
580+ // Event queue mutex scope
581+ {
582+ std::unique_lock<std::mutex> lock (associated_executor_->push_mutex_ );
583+ associated_executor_->events_queue_ ->push (event);
645584 }
646- }
585+ // Notify that the event queue has some events in it.
586+ associated_executor_->events_queue_cv_ .notify_one ();
587+ };
647588}
0 commit comments