Program Listing for File event.hpp

Return to documentation for file (src/detail/event.hpp)

// Copyright 2024 Open Source Robotics Foundation, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef DETAIL__EVENT_HPP_
#define DETAIL__EVENT_HPP_

#include <deque>
#include <memory>
#include <mutex>
#include <string>

#include "rmw/event.h"
#include "rmw/event_callback_type.h"

#include "rmw_wait_set_data.hpp"

namespace rmw_zenoh_cpp
{
// A struct that represents an event status in rmw_zenoh.
enum rmw_zenoh_event_type_t
{
  // sentinel value
  ZENOH_EVENT_INVALID,

  // subscription events
  ZENOH_EVENT_REQUESTED_QOS_INCOMPATIBLE,
  ZENOH_EVENT_MESSAGE_LOST,
  ZENOH_EVENT_SUBSCRIPTION_INCOMPATIBLE_TYPE,
  ZENOH_EVENT_SUBSCRIPTION_MATCHED,

  // publisher events
  // RMW_EVENT_LIVELINESS_LOST,
  // RMW_EVENT_OFFERED_DEADLINE_MISSED,
  ZENOH_EVENT_OFFERED_QOS_INCOMPATIBLE,
  ZENOH_EVENT_PUBLISHER_INCOMPATIBLE_TYPE,
  ZENOH_EVENT_PUBLICATION_MATCHED,
};

#define ZENOH_EVENT_ID_MAX rmw_zenoh_event_type_t::ZENOH_EVENT_PUBLICATION_MATCHED

rmw_zenoh_event_type_t zenoh_event_from_rmw_event(rmw_event_type_t rmw_event_type);

struct rmw_zenoh_event_status_t
{
  size_t total_count;
  size_t total_count_change;
  size_t current_count;
  int32_t current_count_change;
  // The data field can be used to store serialized information for more complex statuses.
  std::string data;
  // A boolean field to indicate if the status changed since the last take.
  bool changed;

  // Constructor.
  rmw_zenoh_event_status_t();
};

class DataCallbackManager
{
public:
  void set_callback(const void * user_data, rmw_event_callback_t callback);

  void trigger_callback();

private:
  std::mutex event_mutex_;
  rmw_event_callback_t callback_ {nullptr};
  const void * user_data_ {nullptr};
  size_t unread_count_ {0};
};

class EventsManager
{
public:
  void event_set_callback(
    rmw_zenoh_event_type_t event_id,
    rmw_event_callback_t callback,
    const void * user_data);

  rmw_zenoh_event_status_t take_event_status(rmw_zenoh_event_type_t event_id);

  void update_event_status(
    rmw_zenoh_event_type_t event_id,
    int32_t current_count_change);

  bool queue_has_data_and_attach_condition_if_not(
    rmw_zenoh_event_type_t event_id,
    rmw_wait_set_data_t * wait_set_data);

  bool detach_condition_and_event_queue_is_empty(rmw_zenoh_event_type_t event_id);

private:
  void trigger_event_callback(rmw_zenoh_event_type_t event_id);

  void notify_event(rmw_zenoh_event_type_t event_id);

  mutable std::mutex event_mutex_;
  mutable std::mutex event_condition_mutex_;
  rmw_wait_set_data_t * wait_set_data_[ZENOH_EVENT_ID_MAX + 1]{nullptr};

  rmw_event_callback_t event_callback_[ZENOH_EVENT_ID_MAX + 1] {nullptr};
  const void * event_data_[ZENOH_EVENT_ID_MAX + 1] {nullptr};
  size_t event_unread_count_[ZENOH_EVENT_ID_MAX + 1] {0};
  // Statuses for events supported.
  rmw_zenoh_event_status_t event_statuses_[ZENOH_EVENT_ID_MAX + 1];
};
}  // namespace rmw_zenoh_cpp

#endif  // DETAIL__EVENT_HPP_