WhisperCom/include/WhisperCom/Router.hpp

90 lines
2.5 KiB
C++

#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 "zmq.hpp"
#include <condition_variable>
#include <cstdint>
#include <memory>
#include <mutex>
#include <thread>
#include <map>
#include <vector>
/**
* @brief main namespace of WhisperCom
*
*/
namespace WhisperCom
{
class Router
{
private:
/// is the router ready
bool isReady_;
/// local zmq url
std::string localUrl_;
/// url for the radio socket
std::string radiolUrl_;
/// url for the dish socket
std::string dishUrl_;
/// zeromq context
zmq::context_t zmqContext_;
/// local, system internal socket for connecting services
zmq::socket_t localSocket_;
/// external socket for listening
zmq::socket_t listening_;
/// external socket for transmitting
zmq::socket_t transmitting_;
zmq::socket_t dummySocket_;
/// thread for running local processing in
std::unique_ptr<std::thread> localProcesing_;
/// is the local processing thread running?
std::atomic<bool> isLocalProcessingRunning_;
/// stop the local processing thread
std::atomic<bool> stopLocalProcessing_;
/// map of connected clients and the last seen time
std::map<std::uint32_t, std::uint32_t> localClients_;
///mutex to protect localClients_
std::mutex mutexLocalClients_;
/// map of clients subscribed to a specific topic
std::map<std::string,std::vector<std::uint32_t>> subscription_;
/// mutex to protect subscription_
std::mutex mutexSubscription_;
void processLocalMessages_();
void processLocalMessage_(zmq::message_t &msg);
void processExternalMessage_(const zmq::message_t &msg);
public:
Router(const std::string localUrl, const std::string radioUrl, const std::string dishUrl);
Router() : Router("ipc:///tmp/whispercom", "udp://239.0.0.1:40000", "udp://*:40000"){}
~Router();
void start();
void stop();
bool isReady() const {return isReady_;}
}; // class Router
}; //namespace WhisperCom