diff options
author | Roman Smrž <roman.smrz@seznam.cz> | 2023-05-06 17:09:32 +0200 |
---|---|---|
committer | Roman Smrž <roman.smrz@seznam.cz> | 2023-05-06 18:54:24 +0200 |
commit | 25a1ba71e3cf4250489291f688423963aa603498 (patch) | |
tree | fb1e23060b810237316f4cde6524c0b1a66d5d0e /include | |
parent | 07f61b1ad6363cd471b7dab3f94c3160636fd6a0 (diff) |
Server and services configuration using separate objects
Diffstat (limited to 'include')
-rw-r--r-- | include/erebos/attach.h | 2 | ||||
-rw-r--r-- | include/erebos/contact.h | 6 | ||||
-rw-r--r-- | include/erebos/message.h | 19 | ||||
-rw-r--r-- | include/erebos/network.h | 37 | ||||
-rw-r--r-- | include/erebos/pairing.h | 41 | ||||
-rw-r--r-- | include/erebos/service.h | 6 | ||||
-rw-r--r-- | include/erebos/sync.h | 6 |
7 files changed, 85 insertions, 32 deletions
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<AttachIdentity> { 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<ContactAccepted> { 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<ContactAccepted> handlePairingComplete(const Peer &) override; virtual void handlePairingResult(Context &, Stored<ContactAccepted>) override; - const class Server * server; + const Server & server; }; template<class T> 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<void(const DirectMessageThread &, ssize_t, ssize_t)>; + + class Config + { + public: + Config & onUpdate(ThreadWatcher); + + private: + friend class DirectMessageService; + vector<ThreadWatcher> watchers; + }; + + DirectMessageService(Config &&, const Server &); virtual ~DirectMessageService(); UUID uuid() const override; void handle(Context &) override; - typedef std::function<void(const DirectMessageThread &, ssize_t, ssize_t)> ThreadWatcher; - void onUpdate(ThreadWatcher); DirectMessageThread thread(const Identity &); DirectMessage send(const Identity &, const Peer &, const std::string &); private: - struct Priv; - unique_ptr<Priv> 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<LocalState> &, std::vector<std::unique_ptr<Service>> &&); + Server(const Head<LocalState> &, ServerConfig &&); Server(const std::shared_ptr<Priv> &); ~Server(); @@ -36,12 +41,42 @@ private: const std::shared_ptr<Priv> p; }; +class ServerConfig +{ +public: + ServerConfig() = default; + ServerConfig(const ServerConfig &) = delete; + ServerConfig(ServerConfig &&) = default; + ServerConfig & operator=(const ServerConfig &) = delete; + ServerConfig & operator=(ServerConfig &&) = default; + + template<class S> + typename S::Config & service(); + +private: + friend class Server; + vector<function<unique_ptr<Service>(const Server &)>> services; +}; + template<class S> S & Server::svc() { return dynamic_cast<S&>(svcHelper(typeid(S))); } +template<class S> +typename S::Config & ServerConfig::service() +{ + auto config = make_shared<typename S::Config>(); + auto & configRef = *config; + + services.push_back([config = move(config)](const Server & server) { + return make_unique<S>(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<void(const Peer &)> RequestInitHook; - void onRequestInit(RequestInitHook); - enum class Outcome { Success, @@ -44,12 +39,28 @@ public: Stale, }; - typedef function<future<bool>(const Peer &, string, future<Outcome> &&)> ConfirmHook; - void onResponse(ConfirmHook); - void onRequest(ConfirmHook); + using RequestInitHook = function<void(const Peer &)>; + using ConfirmHook = function<future<bool>(const Peer &, string, future<Outcome> &&)>; + using RequestNonceFailedHook = function<void(const Peer &)>; + + 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<void(const Peer &)> RequestNonceFailedHook; - void onRequestNonceFailed(RequestNonceFailedHook); + PairingServiceBase(Config &&); + virtual ~PairingServiceBase(); protected: void requestPairing(UUID serviceId, const Peer & peer); @@ -62,11 +73,7 @@ private: const vector<uint8_t> & nonce1, const vector<uint8_t> & nonce2); static string confirmationNumber(const vector<uint8_t> &); - RequestInitHook requestInitHook; - ConfirmHook responseHook; - ConfirmHook requestHook; - RequestNonceFailedHook requestNonceFailedHook; - + const Config config; optional<Ref> result; enum class StatePhase { @@ -99,6 +106,10 @@ private: template<class Result> class PairingService : public PairingServiceBase { +public: + PairingService(Config && config): + PairingServiceBase(move(config)) {} + protected: virtual Stored<Result> handlePairingComplete(const Peer &) = 0; virtual void handlePairingResult(Context &, Stored<Result>) = 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<Ref> &); - const class Server * server; + const Server & server; Watched<vector<Ref>> watchedLocal; }; |