iceoryx_doc  1.0.1
base_subscriber.hpp
1 // Copyright (c) 2020 by Robert Bosch GmbH. All rights reserved.
2 // Copyright (c) 2020 - 2021 by Apex.AI Inc. All rights reserved.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 // SPDX-License-Identifier: Apache-2.0
17 
18 #ifndef IOX_POSH_POPO_BASE_SUBSCRIBER_HPP
19 #define IOX_POSH_POPO_BASE_SUBSCRIBER_HPP
20 
21 #include "iceoryx_posh/internal/popo/ports/subscriber_port_user.hpp"
22 #include "iceoryx_posh/popo/enum_trigger_type.hpp"
23 #include "iceoryx_posh/popo/sample.hpp"
24 #include "iceoryx_posh/popo/subscriber_options.hpp"
25 #include "iceoryx_posh/popo/wait_set.hpp"
26 #include "iceoryx_posh/runtime/posh_runtime.hpp"
27 #include "iceoryx_utils/cxx/expected.hpp"
28 #include "iceoryx_utils/cxx/optional.hpp"
29 #include "iceoryx_utils/cxx/unique_ptr.hpp"
30 
31 namespace iox
32 {
33 namespace popo
34 {
35 using uid_t = UniquePortId;
36 
37 enum class SubscriberEvent : EventEnumIdentifier
38 {
39  DATA_RECEIVED
40 };
41 
42 enum class SubscriberState : StateEnumIdentifier
43 {
44  HAS_DATA
45 };
46 
49 template <typename port_t = iox::SubscriberPortUserType>
51 {
52  public:
53  virtual ~BaseSubscriber();
54 
59  uid_t getUid() const noexcept;
60 
66 
71  void subscribe() noexcept;
72 
77  SubscribeState getSubscriptionState() const noexcept;
78 
82  void unsubscribe() noexcept;
83 
88  bool hasData() const noexcept;
89 
95  bool hasMissedData() noexcept;
96 
98  void releaseQueuedData() noexcept;
99 
100  friend class NotificationAttorney;
101 
102  protected:
107  using PortType = port_t;
108 
109  BaseSubscriber() noexcept; // Required for testing.
110  BaseSubscriber(const capro::ServiceDescription& service, const SubscriberOptions& subscriberOptions) noexcept;
111 
112  BaseSubscriber(const BaseSubscriber& other) = delete;
113  BaseSubscriber& operator=(const BaseSubscriber&) = delete;
114  BaseSubscriber(BaseSubscriber&& rhs) = delete;
115  BaseSubscriber& operator=(BaseSubscriber&& rhs) = delete;
116 
119  cxx::expected<const mepoo::ChunkHeader*, ChunkReceiveResult> takeChunk() noexcept;
120 
121  void invalidateTrigger(const uint64_t trigger) noexcept;
122 
126  void enableState(iox::popo::TriggerHandle&& triggerHandle, const SubscriberState subscriberState) noexcept;
127 
131  WaitSetIsConditionSatisfiedCallback
132  getCallbackForIsStateConditionSatisfied(const SubscriberState subscriberState) const noexcept;
133 
136  void disableState(const SubscriberState subscriberState) noexcept;
137 
141  void enableEvent(iox::popo::TriggerHandle&& triggerHandle, const SubscriberEvent subscriberState) noexcept;
142 
145  void disableEvent(const SubscriberEvent subscriberEvent) noexcept;
146 
150  const port_t& port() const noexcept;
151 
155  port_t& port() noexcept;
156 
157  protected:
158  port_t m_port{nullptr};
159  TriggerHandle m_trigger;
160 };
161 
162 } // namespace popo
163 } // namespace iox
164 
165 #include "iceoryx_posh/internal/popo/base_subscriber.inl"
166 
167 #endif // IOX_POSH_POPO_BASE_SUBSCRIBER_HPP
class for the identification of a communication event including information on the service,...
Definition: service_description.hpp:86
base class for all types of subscriber
Definition: base_subscriber.hpp:51
WaitSetIsConditionSatisfiedCallback getCallbackForIsStateConditionSatisfied(const SubscriberState subscriberState) const noexcept
Only usable by the WaitSet, not for public use. Returns method pointer to the event corresponding has...
Definition: base_subscriber.inl:139
void enableState(iox::popo::TriggerHandle &&triggerHandle, const SubscriberState subscriberState) noexcept
Only usable by the WaitSet, not for public use. Attaches the triggerHandle to the internal trigger.
Definition: base_subscriber.inl:111
capro::ServiceDescription getServiceDescription() const noexcept
getServiceDescription Get the service description of the subscriber.
Definition: base_subscriber.inl:53
void enableEvent(iox::popo::TriggerHandle &&triggerHandle, const SubscriberEvent subscriberState) noexcept
Only usable by the WaitSet, not for public use. Attaches the triggerHandle to the internal trigger.
Definition: base_subscriber.inl:162
void subscribe() noexcept
subscribe Initiate subscription.
Definition: base_subscriber.inl:59
uid_t getUid() const noexcept
uid Get the unique ID of the subscriber.
Definition: base_subscriber.inl:46
const port_t & port() const noexcept
const accessor of the underlying port
Definition: base_subscriber.inl:199
void disableState(const SubscriberState subscriberState) noexcept
Only usable by the WaitSet, not for public use. Resets the internal triggerHandle.
Definition: base_subscriber.inl:150
bool hasData() const noexcept
Check if data is available.
Definition: base_subscriber.inl:77
void unsubscribe() noexcept
unsubscribe Unsubscribes if currently subscribed, otherwise do nothing.
Definition: base_subscriber.inl:71
cxx::expected< const mepoo::ChunkHeader *, ChunkReceiveResult > takeChunk() noexcept
small helper method to unwrap the expected<optional<ChunkHeader*>> from the tryGetChunk method of the...
Definition: base_subscriber.inl:89
void releaseQueuedData() noexcept
Releases any unread queued data.
Definition: base_subscriber.inl:95
void disableEvent(const SubscriberEvent subscriberEvent) noexcept
Only usable by the WaitSet, not for public use. Resets the internal triggerHandle.
Definition: base_subscriber.inl:187
bool hasMissedData() noexcept
Check if data has been missed since the last call of this method.
Definition: base_subscriber.inl:83
SubscribeState getSubscriptionState() const noexcept
getSubscriptionState Get current subscription state.
Definition: base_subscriber.inl:65
Class which allows accessing private methods to friends of NotificationAttorney. Used for example by ...
Definition: notification_attorney.hpp:33
TriggerHandle is threadsafe without restrictions in a single process. Not qualified for inter process...
Definition: trigger_handle.hpp:38
Definition: service_description.hpp:29
This struct is used to configure the subscriber.
Definition: subscriber_options.hpp:31