iceoryx_posh 2.0.3
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_hoofs/cxx/expected.hpp"
22#include "iceoryx_hoofs/cxx/function_ref.hpp"
23#include "iceoryx_hoofs/cxx/optional.hpp"
24#include "iceoryx_hoofs/cxx/string.hpp"
25#include "iceoryx_hoofs/cxx/vector.hpp"
26#include "iceoryx_hoofs/internal/concurrent/smart_lock.hpp"
27#include "iceoryx_hoofs/internal/units/duration.hpp"
28#include "iceoryx_posh/capro/service_description.hpp"
29#include "iceoryx_posh/gateway/gateway_base.hpp"
30#include "iceoryx_posh/gateway/gateway_config.hpp"
31#include "iceoryx_posh/iceoryx_posh_config.hpp"
32#include "iceoryx_posh/iceoryx_posh_types.hpp"
33
34#include <atomic>
35#include <thread>
36
37namespace iox
38{
39namespace gw
40{
41using namespace iox::units::duration_literals;
42
43enum class GatewayError : uint8_t
44{
45 UNSUPPORTED_SERVICE_TYPE,
46 UNSUCCESSFUL_CHANNEL_CREATION,
47 NONEXISTANT_CHANNEL
48};
49
57template <typename channel_t, typename gateway_t = GatewayBase>
58class GatewayGeneric : public gateway_t
59{
60 using ChannelVector = cxx::vector<channel_t, MAX_CHANNEL_NUMBER>;
61 using ConcurrentChannelVector = concurrent::smart_lock<ChannelVector>;
62
63 public:
64 virtual ~GatewayGeneric() noexcept;
65
66 GatewayGeneric(const GatewayGeneric&) = delete;
67 GatewayGeneric& operator=(const GatewayGeneric&) = delete;
69 GatewayGeneric& operator=(GatewayGeneric&&) = delete;
70
71 void runMultithreaded() noexcept;
72 void shutdown() noexcept;
73
78 virtual void loadConfiguration(const config::GatewayConfig& config) noexcept = 0;
83 virtual void discover(const capro::CaproMessage& msg) noexcept = 0;
88 virtual void forward(const channel_t& channel) noexcept = 0;
89
90 uint64_t getNumberOfChannels() const noexcept;
91
92 protected:
93 GatewayGeneric(capro::Interfaces interface,
94 units::Duration discoveryPeriod = 1000_ms,
95 units::Duration forwardingPeriod = 50_ms) noexcept;
96
114 template <typename IceoryxPubSubOptions>
115 cxx::expected<channel_t, GatewayError> addChannel(const capro::ServiceDescription& service,
116 const IceoryxPubSubOptions& options) noexcept;
117
124 cxx::optional<channel_t> findChannel(const capro::ServiceDescription& service) const noexcept;
125
131 void forEachChannel(const cxx::function_ref<void(channel_t&)> f) const noexcept;
132
138 cxx::expected<GatewayError> discardChannel(const capro::ServiceDescription& service) noexcept;
139
140 private:
141 ConcurrentChannelVector m_channels;
142
143 std::atomic_bool m_isRunning{false};
144
145 units::Duration m_discoveryPeriod;
146 units::Duration m_forwardingPeriod;
147
148 std::thread m_discoveryThread;
149 std::thread m_forwardingThread;
150
151 void forwardingLoop() noexcept;
152 void discoveryLoop() noexcept;
153};
154
155} // namespace gw
156} // namespace iox
157
158#include "iceoryx_posh/internal/gateway/gateway_generic.inl"
159
160#endif // IOX_POSH_GW_GATEWAY_GENERIC_HPP
A reference generic gateway implementation.
Definition: gateway_generic.hpp:59
cxx::expected< GatewayError > discardChannel(const capro::ServiceDescription &service) noexcept
discardChannel Discard the channel for the given service in the internal collection if one exists.
virtual void discover(const capro::CaproMessage &msg) noexcept=0
discover Process discovery messages coming from iceoryx.
cxx::optional< channel_t > findChannel(const capro::ServiceDescription &service) const noexcept
findChannel Searches for a channel for the given service in the internally stored collection and retu...
virtual void loadConfiguration(const config::GatewayConfig &config) noexcept=0
loadConfiguration Load the provided configuration.
void forEachChannel(const cxx::function_ref< void(channel_t &)> f) const noexcept
forEachChannel Executs the given function for each channel in the internally stored collection.
virtual void forward(const channel_t &channel) noexcept=0
forward Forward data between the two terminals of the channel used by the implementation.
cxx::expected< channel_t, GatewayError > addChannel(const capro::ServiceDescription &service, const IceoryxPubSubOptions &options) noexcept
addChannel Creates a channel for the given service and stores a copy of it in an internal collection ...
Generic configuration for gateways.
Definition: gateway_config.hpp:32