#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. // Return false if you want to stop listening on the key using BoostrapConnectionListenCallbackFunc = std::function; struct ListenHandle { PubsubKey key; }; 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 ListenHandle listen(const PubsubKey &pubsubKey, BoostrapConnectionListenCallbackFunc callbackFunc); // Returns false if data is larger than 800kb. // Note: @data is copied in this function. // Note: You can't put data on a pubsubkey that you are not listening on. Call @listen first. bool put(const PubsubKey &pubsubKey, const void *data, const usize size); bool cancelListen(const ListenHandle &listener); std::vector> getPeers(); private: void receiveDataFromServer(std::shared_ptr peer, MessageType messageType, const void *data, const usize size); void receiveDataFromPeer(std::shared_ptr peer, MessageType messageType, const void *data, const usize size); private: DirectConnections connections; std::shared_ptr serverPeer; PubsubKeyMap listenCallbackFuncs; PubsubKeyMap>> subscribedPeers; std::recursive_mutex listenerCallbackFuncMutex; std::recursive_mutex subscribedPeersMutex; }; }