iceoryx_doc  1.0.1
gateway_generic.hpp
1 // Copyright (c) 2020 by Robert Bosch GmbH. All rights reserved.
2 // Copyright (c) 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_GW_GATEWAY_GENERIC_HPP
19 #define IOX_POSH_GW_GATEWAY_GENERIC_HPP
20 
21 #include "iceoryx_posh/capro/service_description.hpp"
22 #include "iceoryx_posh/gateway/gateway_base.hpp"
23 #include "iceoryx_posh/gateway/gateway_config.hpp"
24 #include "iceoryx_posh/iceoryx_posh_config.hpp"
25 #include "iceoryx_posh/iceoryx_posh_types.hpp"
26 #include "iceoryx_utils/cxx/expected.hpp"
27 #include "iceoryx_utils/cxx/function_ref.hpp"
28 #include "iceoryx_utils/cxx/optional.hpp"
29 #include "iceoryx_utils/cxx/string.hpp"
30 #include "iceoryx_utils/cxx/vector.hpp"
31 #include "iceoryx_utils/internal/concurrent/smart_lock.hpp"
32 #include "iceoryx_utils/internal/units/duration.hpp"
33 
34 #include <atomic>
35 #include <thread>
36 
37 namespace iox
38 {
39 namespace gw
40 {
41 using namespace iox::units::duration_literals;
42 
43 enum class GatewayError : uint8_t
44 {
45  INVALID_STATE,
46  UNSUPPORTED_SERVICE_TYPE,
47  UNSUCCESSFUL_CHANNEL_CREATION,
48  NONEXISTANT_CHANNEL
49 };
50 
58 template <typename channel_t, typename gateway_t = GatewayBase>
59 class GatewayGeneric : public gateway_t
60 {
61  using ChannelVector = cxx::vector<channel_t, MAX_CHANNEL_NUMBER>;
62  using ConcurrentChannelVector = concurrent::smart_lock<ChannelVector>;
63 
64  public:
65  virtual ~GatewayGeneric() noexcept;
66 
67  GatewayGeneric(const GatewayGeneric&) = delete;
68  GatewayGeneric& operator=(const GatewayGeneric&) = delete;
69  GatewayGeneric(GatewayGeneric&&) = delete;
70  GatewayGeneric& operator=(GatewayGeneric&&) = delete;
71 
72  void runMultithreaded() noexcept;
73  void shutdown() noexcept;
74 
79  virtual void loadConfiguration(const config::GatewayConfig& config) noexcept = 0;
84  virtual void discover(const capro::CaproMessage& msg) noexcept = 0;
89  virtual void forward(const channel_t& channel) noexcept = 0;
90 
91  uint64_t getNumberOfChannels() const noexcept;
92 
93  protected:
94  GatewayGeneric(capro::Interfaces interface,
95  units::Duration discoveryPeriod = 1000_ms,
96  units::Duration forwardingPeriod = 50_ms) noexcept;
97 
115  template <typename IceoryxPubSubOptions>
116  cxx::expected<channel_t, GatewayError> addChannel(const capro::ServiceDescription& service,
117  const IceoryxPubSubOptions& options) noexcept;
118 
125  cxx::optional<channel_t> findChannel(const capro::ServiceDescription& service) const noexcept;
126 
132  void forEachChannel(const cxx::function_ref<void(channel_t&)> f) const noexcept;
133 
139  cxx::expected<GatewayError> discardChannel(const capro::ServiceDescription& service) noexcept;
140 
141  private:
142  ConcurrentChannelVector m_channels;
143 
144  std::atomic_bool m_isRunning{false};
145 
146  units::Duration m_discoveryPeriod;
147  units::Duration m_forwardingPeriod;
148 
149  std::thread m_discoveryThread;
150  std::thread m_forwardingThread;
151 
152  void forwardingLoop() noexcept;
153  void discoveryLoop() noexcept;
154 };
155 
156 } // namespace gw
157 } // namespace iox
158 
159 #include "iceoryx_posh/internal/gateway/gateway_generic.inl"
160 
161 #endif // IOX_POSH_GW_GATEWAY_GENERIC_HPP
C'tors for CaPro messages.
Definition: capro_message.hpp:60
A reference generic gateway implementation.
Definition: gateway_generic.hpp:60
virtual void discover(const capro::CaproMessage &msg) noexcept=0
discover Process discovery messages coming from iceoryx.
virtual void loadConfiguration(const config::GatewayConfig &config) noexcept=0
loadConfiguration Load the provided configuration.
virtual void forward(const channel_t &channel) noexcept=0
forward Forward data between the two terminals of the channel used by the implementation.
Definition: service_description.hpp:29
Generic configuration for gateways.
Definition: gateway_config.hpp:32