Skip to content

iceoryx_posh/gateway/gateway_generic.hpp🔗

Namespaces🔗

Name
iox
iox::gw
iox::units::duration_literals

Classes🔗

Name
class iox:🇬🇼:GatewayGeneric
A reference generic gateway implementation.

Source code🔗

// Copyright (c) 2020 by Robert Bosch GmbH. All rights reserved.
// Copyright (c) 2021 by Apex.AI Inc. All rights reserved.
//
// 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.
//
// SPDX-License-Identifier: Apache-2.0

#ifndef IOX_POSH_GW_GATEWAY_GENERIC_HPP
#define IOX_POSH_GW_GATEWAY_GENERIC_HPP

#include "iceoryx_hoofs/cxx/expected.hpp"
#include "iceoryx_hoofs/cxx/function_ref.hpp"
#include "iceoryx_hoofs/cxx/optional.hpp"
#include "iceoryx_hoofs/cxx/string.hpp"
#include "iceoryx_hoofs/cxx/vector.hpp"
#include "iceoryx_hoofs/internal/concurrent/smart_lock.hpp"
#include "iceoryx_hoofs/internal/units/duration.hpp"
#include "iceoryx_posh/capro/service_description.hpp"
#include "iceoryx_posh/gateway/gateway_base.hpp"
#include "iceoryx_posh/gateway/gateway_config.hpp"
#include "iceoryx_posh/iceoryx_posh_config.hpp"
#include "iceoryx_posh/iceoryx_posh_types.hpp"

#include <atomic>
#include <thread>

namespace iox
{
namespace gw
{
using namespace iox::units::duration_literals;

enum class GatewayError : uint8_t
{
    UNSUPPORTED_SERVICE_TYPE,
    UNSUCCESSFUL_CHANNEL_CREATION,
    NONEXISTANT_CHANNEL
};

template <typename channel_t, typename gateway_t = GatewayBase>
class GatewayGeneric : public gateway_t
{
    using ChannelVector = cxx::vector<channel_t, MAX_CHANNEL_NUMBER>;
    using ConcurrentChannelVector = concurrent::smart_lock<ChannelVector>;

  public:
    virtual ~GatewayGeneric() noexcept;

    GatewayGeneric(const GatewayGeneric&) = delete;
    GatewayGeneric& operator=(const GatewayGeneric&) = delete;
    GatewayGeneric(GatewayGeneric&&) = delete;
    GatewayGeneric& operator=(GatewayGeneric&&) = delete;

    void runMultithreaded() noexcept;
    void shutdown() noexcept;

    virtual void loadConfiguration(const config::GatewayConfig& config) noexcept = 0;
    virtual void discover(const capro::CaproMessage& msg) noexcept = 0;
    virtual void forward(const channel_t& channel) noexcept = 0;

    uint64_t getNumberOfChannels() const noexcept;

  protected:
    GatewayGeneric(capro::Interfaces interface,
                   units::Duration discoveryPeriod = 1000_ms,
                   units::Duration forwardingPeriod = 50_ms) noexcept;

    template <typename IceoryxPubSubOptions>
    cxx::expected<channel_t, GatewayError> addChannel(const capro::ServiceDescription& service,
                                                      const IceoryxPubSubOptions& options) noexcept;

    cxx::optional<channel_t> findChannel(const capro::ServiceDescription& service) const noexcept;

    void forEachChannel(const cxx::function_ref<void(channel_t&)> f) const noexcept;

    cxx::expected<GatewayError> discardChannel(const capro::ServiceDescription& service) noexcept;

  private:
    ConcurrentChannelVector m_channels;

    std::atomic_bool m_isRunning{false};

    units::Duration m_discoveryPeriod;
    units::Duration m_forwardingPeriod;

    std::thread m_discoveryThread;
    std::thread m_forwardingThread;

    void forwardingLoop() noexcept;
    void discoveryLoop() noexcept;
};

} // namespace gw
} // namespace iox

#include "iceoryx_posh/internal/gateway/gateway_generic.inl"

#endif // IOX_POSH_GW_GATEWAY_GENERIC_HPP

Updated on 18 December 2023 at 13:11:43 CET