/******************************************************************************** * Copyright (C) 2019 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH * * * * This software is distributed under the terms of the * * GNU Lesser General Public Licence (LGPL) version 3, * * copied verbatim in the file "LICENSE" * ********************************************************************************/ #ifndef FAIR_MQ_SDK_TOPOLOGY_H #define FAIR_MQ_SDK_TOPOLOGY_H #include #include #include #include #include #include #include #include #include #include #include #include namespace fair { namespace mq { enum class AsyncOpResultCode { Ok, Timeout, Error, Aborted }; auto operator<<(std::ostream& os, AsyncOpResultCode v) -> std::ostream&; using AsyncOpResultMessage = std::string; struct AsyncOpResult { AsyncOpResultCode code; AsyncOpResultMessage msg; operator AsyncOpResultCode() const { return code; } }; auto operator<<(std::ostream& os, AsyncOpResult v) -> std::ostream&; namespace sdk { using DeviceState = fair::mq::State; using DeviceTransition = fair::mq::Transition; struct DeviceStatus { bool initialized; DeviceState state; }; using TopologyState = std::unordered_map; using TopologyTransition = fair::mq::Transition; /** * @class Topology Topology.h * @brief Represents a FairMQ topology */ class Topology { public: /// @brief (Re)Construct a FairMQ topology from an existing DDS topology /// @param topo Initialized DDS CTopology explicit Topology(DDSTopology topo, DDSSession session = DDSSession()); explicit Topology(const Topology&) = delete; Topology& operator=(const Topology&) = delete; explicit Topology(Topology&&) = delete; Topology& operator=(Topology&&) = delete; ~Topology(); struct ChangeStateResult { AsyncOpResult rc; TopologyState state; friend auto operator<<(std::ostream& os, ChangeStateResult v) -> std::ostream&; }; using ChangeStateCallback = std::function; using Duration = std::chrono::milliseconds; /// @brief Initiate state transition on all FairMQ devices in this topology /// @param t FairMQ device state machine transition /// @param cb Completion callback /// @param timeout Timeout in milliseconds, 0 means no timeout auto ChangeState(TopologyTransition t, ChangeStateCallback cb, Duration timeout = std::chrono::milliseconds(0)) -> void; /// @brief Perform a state transition on all FairMQ devices in this topology /// @param t FairMQ device state machine transition /// @param timeout Timeout in milliseconds, 0 means no timeout /// @return The result of the state transition auto ChangeState(TopologyTransition t, Duration timeout = std::chrono::milliseconds(0)) -> ChangeStateResult; private: DDSSession fDDSSession; DDSTopology fDDSTopo; TopologyState fTopologyState; bool fStateChangeOngoing; DeviceState fTargetState; std::mutex fMtx; std::mutex fExecutionMtx; std::condition_variable fCV; std::condition_variable fExecutionCV; std::thread fExecutionThread; ChangeStateCallback fChangeStateCallback; std::chrono::milliseconds fStateChangeTimeout; bool fShutdown; void WaitForState(); void AddNewStateEntry(uint64_t senderId, const std::string& state); }; using Topo = Topology; } // namespace sdk } // namespace mq } // namespace fair #endif /* FAIR_MQ_SDK_TOPOLOGY_H */