#pragma once /* * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ #include "RouterMessage.pb.h" #include "WhisperCom/Router.hpp" #include "zmq.hpp" #include #include #include #include #include /** * @brief main namespace of WhisperCom * */ namespace WhisperCom { class Service { private: /// the local url to connect to std::string localUrl_; /// the radio url to use within the router std::string radioUrl_; /// the dish url to use within the router std::string dishUrl_; /// a WhisperComm router for relaying messages off the computer std::unique_ptr router_; /// context for zeromq zmq::context_t zmqContext_; /// socket for communication with the router zmq::socket_t socket_; /// time of last keepalive std::atomic lastKeepAlive_; /// number of seconds without message to assume router dead std::uint32_t routerDeadTimeOut_; /// number of seconds to transmit and request q keepalive std::uint32_t keepaliveTime_; void keepalive_(); std::unique_ptr keepaliveThread_; std::atomic stopKeepAlive_; std::atomic isKeepAliveRunning_; void receive_(); std::unique_ptr receiveThread_; std::atomic stopReceive_; std::atomic isReceiveRunning_; std::queue> messageQueue_; std::mutex mutexMessageQueue_; std::condition_variable condWaitMessageQueue_; void processMessage_(const zmq::message_t &msg); void verifyRouter_(); public: Service(const std::string localUrl, const std::string radioUrl,const std::string dishUrl, std::uint32_t routerDeadTimeout=10,std::uint32_t keepaliveTime=2); Service() : Service("ipc:///tmp/whispercom", "udp://239.0.0.1:40000", "udp://239.0.0.1:40000") {} ~Service(); void subscribe(const std::string &topic); void unsubscribe(const std::string &topic); void unsubscribeAll(); bool hasMessages() {std::lock_guard guard{mutexMessageQueue_}; return messageQueue_.size();} bool waitForMessage(std::chrono::milliseconds timeout); bool waitForMessage(std::chrono::milliseconds timeout, std::shared_ptr message); std::shared_ptr getMessage(); bool sendMessage(const std::string &topic, WhisperCom::Protobuf::Message &data); void stop(); }; // class Service }; // namespace WhisperCom