Skip to content

iceoryx_posh/runtime/posh_runtime.hpp🔗

Namespaces🔗

Name
iox
iox::roudi
iox::runtime

Classes🔗

Name
class iox::runtime::PoshRuntime
The runtime that is needed for each application to communicate with the RouDi daemon.

Source code🔗

// Copyright (c) 2019 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_RUNTIME_POSH_RUNTIME_HPP
#define IOX_POSH_RUNTIME_POSH_RUNTIME_HPP

#include "iceoryx_posh/capro/service_description.hpp"
#include "iceoryx_posh/iceoryx_posh_types.hpp"
#include "iceoryx_posh/internal/popo/building_blocks/condition_variable_data.hpp"
#include "iceoryx_posh/internal/popo/ports/application_port.hpp"
#include "iceoryx_posh/internal/popo/ports/interface_port.hpp"
#include "iceoryx_posh/internal/popo/ports/publisher_port_user.hpp"
#include "iceoryx_posh/internal/popo/ports/subscriber_port_user.hpp"
#include "iceoryx_posh/internal/runtime/ipc_runtime_interface.hpp"
#include "iceoryx_posh/internal/runtime/node_property.hpp"
#include "iceoryx_posh/internal/runtime/shared_memory_user.hpp"
#include "iceoryx_posh/popo/subscriber_options.hpp"
#include "iceoryx_posh/runtime/port_config_info.hpp"
#include "iceoryx_utils/cxx/method_callback.hpp"
#include "iceoryx_utils/cxx/string.hpp"
#include "iceoryx_utils/internal/concurrent/periodic_task.hpp"

#include <atomic>
#include <map>
#include <mutex>
#include <thread>
#include <vector>

namespace iox
{
namespace roudi
{
class RuntimeTestInterface;
} // namespace roudi

namespace runtime
{
class Node;
class NodeData;

enum class FindServiceError
{
    INVALID_STATE,
    UNABLE_TO_WRITE_TO_ROUDI_CHANNEL,
    INSTANCE_CONTAINER_OVERFLOW
};

class PoshRuntime
{
  public:
    static PoshRuntime& getInstance() noexcept;

    static PoshRuntime& initRuntime(const RuntimeName_t& name) noexcept;

    RuntimeName_t getInstanceName() const noexcept;

    void shutdown() noexcept;

    cxx::expected<InstanceContainer, FindServiceError>
    findService(const capro::ServiceDescription& serviceDescription) noexcept;

    bool offerService(const capro::ServiceDescription& serviceDescription) noexcept;

    void stopOfferService(const capro::ServiceDescription& serviceDescription) noexcept;

    PublisherPortUserType::MemberType_t*
    getMiddlewarePublisher(const capro::ServiceDescription& service,
                           const popo::PublisherOptions& publisherOptions = popo::PublisherOptions(),
                           const PortConfigInfo& portConfigInfo = PortConfigInfo()) noexcept;

    SubscriberPortUserType::MemberType_t*
    getMiddlewareSubscriber(const capro::ServiceDescription& service,
                            const popo::SubscriberOptions& subscriberOptions = popo::SubscriberOptions(),
                            const PortConfigInfo& portConfigInfo = PortConfigInfo()) noexcept;

    popo::InterfacePortData* getMiddlewareInterface(const capro::Interfaces interface,
                                                    const NodeName_t& nodeName = {""}) noexcept;

    popo::ApplicationPortData* getMiddlewareApplication() noexcept;

    popo::ConditionVariableData* getMiddlewareConditionVariable() noexcept;

    NodeData* createNode(const NodeProperty& nodeProperty) noexcept;

    const std::atomic<uint64_t>* getServiceRegistryChangeCounter() noexcept;

    bool sendRequestToRouDi(const IpcMessage& msg, IpcMessage& answer) noexcept;

  public:
    PoshRuntime(const PoshRuntime&) = delete;
    PoshRuntime& operator=(const PoshRuntime&) = delete;
    PoshRuntime(PoshRuntime&&) = delete;
    PoshRuntime& operator=(PoshRuntime&&) = delete;
    virtual ~PoshRuntime() noexcept;

    friend class roudi::RuntimeTestInterface;

  protected:
    using factory_t = PoshRuntime& (*)(cxx::optional<const RuntimeName_t*>);

    // Protected constructor for IPC setup
    PoshRuntime(cxx::optional<const RuntimeName_t*> name, const bool doMapSharedMemoryIntoThread = true) noexcept;

    static PoshRuntime& defaultRuntimeFactory(cxx::optional<const RuntimeName_t*> name) noexcept;

    static RuntimeName_t& defaultRuntimeInstanceName() noexcept;

    static factory_t& getRuntimeFactory() noexcept;

    static void setRuntimeFactory(const factory_t& factory) noexcept;

    static PoshRuntime& getInstance(cxx::optional<const RuntimeName_t*> name) noexcept;

  private:
    cxx::expected<PublisherPortUserType::MemberType_t*, IpcMessageErrorType>
    requestPublisherFromRoudi(const IpcMessage& sendBuffer) noexcept;

    cxx::expected<SubscriberPortUserType::MemberType_t*, IpcMessageErrorType>
    requestSubscriberFromRoudi(const IpcMessage& sendBuffer) noexcept;

    cxx::expected<popo::ConditionVariableData*, IpcMessageErrorType>
    requestConditionVariableFromRoudi(const IpcMessage& sendBuffer) noexcept;

    const RuntimeName_t& verifyInstanceName(cxx::optional<const RuntimeName_t*> name) noexcept;

    const RuntimeName_t m_appName;
    mutable std::mutex m_appIpcRequestMutex;

    // IPC channel interface for POSIX IPC from RouDi
    IpcRuntimeInterface m_ipcChannelInterface;
    // Shared memory interface for POSIX IPC from RouDi
    SharedMemoryUser m_ShmInterface;
    popo::ApplicationPort m_applicationPort;

    std::atomic<bool> m_shutdownRequested{false};
    void sendKeepAliveAndHandleShutdownPreparation() noexcept;
    static_assert(PROCESS_KEEP_ALIVE_INTERVAL > roudi::DISCOVERY_INTERVAL, "Keep alive interval too small");

    concurrent::PeriodicTask<cxx::MethodCallback<void>> m_keepAliveTask{
        concurrent::PeriodicTaskAutoStart,
        PROCESS_KEEP_ALIVE_INTERVAL,
        "KeepAlive",
        *this,
        &PoshRuntime::sendKeepAliveAndHandleShutdownPreparation};
};

} // namespace runtime
} // namespace iox

#endif // IOX_POSH_RUNTIME_POSH_RUNTIME_HPP

Updated on 17 June 2021 at 11:15:27 CEST