From 25a1ba71e3cf4250489291f688423963aa603498 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Smr=C5=BE?= Date: Sat, 6 May 2023 17:09:32 +0200 Subject: Server and services configuration using separate objects --- include/erebos/attach.h | 2 +- include/erebos/contact.h | 6 ++---- include/erebos/message.h | 19 ++++++++++++++----- include/erebos/network.h | 37 ++++++++++++++++++++++++++++++++++++- include/erebos/pairing.h | 41 ++++++++++++++++++++++++++--------------- include/erebos/service.h | 6 ++++-- include/erebos/sync.h | 6 ++---- 7 files changed, 85 insertions(+), 32 deletions(-) (limited to 'include') diff --git a/include/erebos/attach.h b/include/erebos/attach.h index dab0110..6ea3d64 100644 --- a/include/erebos/attach.h +++ b/include/erebos/attach.h @@ -21,7 +21,7 @@ struct AttachIdentity; class AttachService : public PairingService { public: - AttachService(); + AttachService(Config &&, const Server &); virtual ~AttachService(); UUID uuid() const override; diff --git a/include/erebos/contact.h b/include/erebos/contact.h index 24a4c89..963e22a 100644 --- a/include/erebos/contact.h +++ b/include/erebos/contact.h @@ -73,20 +73,18 @@ struct ContactAccepted; class ContactService : public PairingService { public: - ContactService(); + ContactService(Config &&, const Server &); virtual ~ContactService(); UUID uuid() const override; - void serverStarted(const class Server &) override; - void request(const Peer &); protected: virtual Stored handlePairingComplete(const Peer &) override; virtual void handlePairingResult(Context &, Stored) override; - const class Server * server; + const Server & server; }; template class Signed; diff --git a/include/erebos/message.h b/include/erebos/message.h index 70a11bc..436c0ed 100644 --- a/include/erebos/message.h +++ b/include/erebos/message.h @@ -74,21 +74,30 @@ private: class DirectMessageService : public Service { public: - DirectMessageService(); + using ThreadWatcher = std::function; + + class Config + { + public: + Config & onUpdate(ThreadWatcher); + + private: + friend class DirectMessageService; + vector watchers; + }; + + DirectMessageService(Config &&, const Server &); virtual ~DirectMessageService(); UUID uuid() const override; void handle(Context &) override; - typedef std::function ThreadWatcher; - void onUpdate(ThreadWatcher); DirectMessageThread thread(const Identity &); DirectMessage send(const Identity &, const Peer &, const std::string &); private: - struct Priv; - unique_ptr p; + const Config config; }; } diff --git a/include/erebos/network.h b/include/erebos/network.h index b47efee..c211fbf 100644 --- a/include/erebos/network.h +++ b/include/erebos/network.h @@ -10,11 +10,16 @@ struct sockaddr_in; namespace erebos { +using std::vector; +using std::unique_ptr; + +class ServerConfig; + class Server { struct Priv; public: - Server(const Head &, std::vector> &&); + Server(const Head &, ServerConfig &&); Server(const std::shared_ptr &); ~Server(); @@ -36,12 +41,42 @@ private: const std::shared_ptr p; }; +class ServerConfig +{ +public: + ServerConfig() = default; + ServerConfig(const ServerConfig &) = delete; + ServerConfig(ServerConfig &&) = default; + ServerConfig & operator=(const ServerConfig &) = delete; + ServerConfig & operator=(ServerConfig &&) = default; + + template + typename S::Config & service(); + +private: + friend class Server; + vector(const Server &)>> services; +}; + template S & Server::svc() { return dynamic_cast(svcHelper(typeid(S))); } +template +typename S::Config & ServerConfig::service() +{ + auto config = make_shared(); + auto & configRef = *config; + + services.push_back([config = move(config)](const Server & server) { + return make_unique(move(*config), server); + }); + + return configRef; +} + class Peer { public: diff --git a/include/erebos/pairing.h b/include/erebos/pairing.h index ea349a9..71c9288 100644 --- a/include/erebos/pairing.h +++ b/include/erebos/pairing.h @@ -29,11 +29,6 @@ using std::vector; class PairingServiceBase : public Service { public: - virtual ~PairingServiceBase(); - - typedef function RequestInitHook; - void onRequestInit(RequestInitHook); - enum class Outcome { Success, @@ -44,12 +39,28 @@ public: Stale, }; - typedef function(const Peer &, string, future &&)> ConfirmHook; - void onResponse(ConfirmHook); - void onRequest(ConfirmHook); + using RequestInitHook = function; + using ConfirmHook = function(const Peer &, string, future &&)>; + using RequestNonceFailedHook = function; + + class Config + { + public: + Config & onRequestInit(RequestInitHook); + Config & onResponse(PairingServiceBase::ConfirmHook); + Config & onRequest(PairingServiceBase::ConfirmHook); + Config & onRequestNonceFailed(RequestNonceFailedHook); + + private: + friend class PairingServiceBase; + RequestInitHook requestInitHook; + ConfirmHook responseHook; + ConfirmHook requestHook; + RequestNonceFailedHook requestNonceFailedHook; + }; - typedef function RequestNonceFailedHook; - void onRequestNonceFailed(RequestNonceFailedHook); + PairingServiceBase(Config &&); + virtual ~PairingServiceBase(); protected: void requestPairing(UUID serviceId, const Peer & peer); @@ -62,11 +73,7 @@ private: const vector & nonce1, const vector & nonce2); static string confirmationNumber(const vector &); - RequestInitHook requestInitHook; - ConfirmHook responseHook; - ConfirmHook requestHook; - RequestNonceFailedHook requestNonceFailedHook; - + const Config config; optional result; enum class StatePhase { @@ -99,6 +106,10 @@ private: template class PairingService : public PairingServiceBase { +public: + PairingService(Config && config): + PairingServiceBase(move(config)) {} + protected: virtual Stored handlePairingComplete(const Peer &) = 0; virtual void handlePairingResult(Context &, Stored) = 0; diff --git a/include/erebos/service.h b/include/erebos/service.h index 7a9a366..7e037f8 100644 --- a/include/erebos/service.h +++ b/include/erebos/service.h @@ -7,12 +7,16 @@ namespace erebos { +class Server; + class Service { public: Service(); virtual ~Service(); + using Config = monostate; + class Context { public: @@ -35,8 +39,6 @@ public: virtual UUID uuid() const = 0; virtual void handle(Context &) = 0; - - virtual void serverStarted(const class Server &); }; } diff --git a/include/erebos/sync.h b/include/erebos/sync.h index 1ab927d..662a558 100644 --- a/include/erebos/sync.h +++ b/include/erebos/sync.h @@ -15,19 +15,17 @@ using std::vector; class SyncService : public Service { public: - SyncService(); + SyncService(Config &&, const Server &); virtual ~SyncService(); UUID uuid() const override; void handle(Context &) override; - void serverStarted(const class Server &) override; - private: void peerWatcher(size_t, const class Peer *); void localStateWatcher(const vector &); - const class Server * server; + const Server & server; Watched> watchedLocal; }; -- cgit v1.2.3