#pragma once #include "../utils.hpp" #include "DirectConnection.hpp" #include "PubsubKey.hpp" #include namespace sibs { class BootstrapConnectionException : public std::runtime_error { public: BootstrapConnectionException(const std::string &errMsg) : std::runtime_error(errMsg) {} }; class PubsubKeyAlreadyListeningException : public std::runtime_error { public: PubsubKeyAlreadyListeningException(const std::string &errMsg) : std::runtime_error(errMsg) {} }; // @peer is nullptr is data was sent by local user using BoostrapConnectionListenCallbackFunc = std::function; class BootstrapConnection { DISABLE_COPY(BootstrapConnection) public: // Throws BootstrapConnectionException on error BootstrapConnection(const Ipv4 &bootstrapAddress); // Throws PubsubKeyAlreadyListeningException if we are already listening on the key @pubsubKey void listen(const PubsubKey &pubsubKey, BoostrapConnectionListenCallbackFunc callbackFunc); void put(const PubsubKey &pubsubKey, std::shared_ptr> data); private: void receiveDataFromServer(std::shared_ptr peer, const void *data, const usize size); void receiveDataFromPeer(BoostrapConnectionListenCallbackFunc listenCallbackFunc, std::shared_ptr peer, const void *data, const usize size); private: DirectConnections connections; std::shared_ptr serverPeer; PubsubKeyMap listenCallbackFuncs; PubsubKeyMap>> subscribedPeers; std::mutex listenerCallbackFuncMutex; std::mutex subscribedPeersMutex; }; }