summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoman Smrž <roman.smrz@seznam.cz>2021-01-06 21:52:53 +0100
committerRoman Smrž <roman.smrz@seznam.cz>2021-01-06 21:52:53 +0100
commit381b6505b5afaacc9194168697f6bc93c43d2775 (patch)
tree1190917b6ee40d6f4a74bf724466b3062ab86f4f
parent96eb8471d11667a56466a6f701d1d23fa64c2225 (diff)
Access services via Server object
-rw-r--r--include/erebos/message.h10
-rw-r--r--include/erebos/network.h11
-rw-r--r--src/message.cpp12
-rw-r--r--src/message.h4
-rw-r--r--src/network.cpp10
5 files changed, 36 insertions, 11 deletions
diff --git a/include/erebos/message.h b/include/erebos/message.h
index c94e8c3..66d221c 100644
--- a/include/erebos/message.h
+++ b/include/erebos/message.h
@@ -4,11 +4,14 @@
#include <chrono>
#include <functional>
+#include <memory>
#include <optional>
#include <string>
namespace erebos {
+using std::unique_ptr;
+
class Identity;
class DirectMessage
@@ -78,13 +81,14 @@ public:
void handle(Context &) const override;
typedef std::function<void(const DirectMessageThread &, ssize_t, ssize_t)> ThreadWatcher;
- static void onUpdate(ThreadWatcher);
- static DirectMessageThread thread(const Identity &);
+ void onUpdate(ThreadWatcher);
+ DirectMessageThread thread(const Identity &);
- static DirectMessage send(const Identity &, const Peer &, const std::string &);
+ DirectMessage send(const Identity &, const Peer &, const std::string &);
private:
struct Priv;
+ unique_ptr<Priv> p;
};
}
diff --git a/include/erebos/network.h b/include/erebos/network.h
index d730fb5..a2f989e 100644
--- a/include/erebos/network.h
+++ b/include/erebos/network.h
@@ -4,6 +4,7 @@
#include <erebos/service.h>
#include <functional>
+#include <typeinfo>
namespace erebos {
@@ -13,14 +14,24 @@ public:
Server(const Identity &, std::vector<std::unique_ptr<Service>> &&);
~Server();
+ template<class S> S & svc();
+
class PeerList & peerList() const;
struct Peer;
private:
+ Service & svcHelper(const std::type_info &);
+
struct Priv;
const std::shared_ptr<Priv> p;
};
+template<class S>
+S & Server::svc()
+{
+ return dynamic_cast<S&>(svcHelper(typeid(S)));
+}
+
class Peer
{
public:
diff --git a/src/message.cpp b/src/message.cpp
index 354703e..601bc39 100644
--- a/src/message.cpp
+++ b/src/message.cpp
@@ -202,10 +202,10 @@ const Identity & DirectMessageThread::peer() const
}
-vector<DirectMessageService::ThreadWatcher> DirectMessageService::Priv::watchers;
-mutex DirectMessageService::Priv::watcherLock;
+DirectMessageService::DirectMessageService():
+ p(new Priv)
+{}
-DirectMessageService::DirectMessageService() = default;
DirectMessageService::~DirectMessageService() = default;
UUID DirectMessageService::uuid() const
@@ -229,14 +229,14 @@ void DirectMessageService::handle(Context & ctx) const
lock.unlock();
- for (const auto & w : Priv::watchers)
+ for (const auto & w : p->watchers)
w(dmt, -1, -1);
}
void DirectMessageService::onUpdate(ThreadWatcher w)
{
- scoped_lock l(Priv::watcherLock);
- Priv::watchers.push_back(w);
+ scoped_lock l(p->watcherLock);
+ p->watchers.push_back(w);
}
DirectMessageThread DirectMessageService::thread(const Identity & peer)
diff --git a/src/message.h b/src/message.h
index c4a3549..efdec8f 100644
--- a/src/message.h
+++ b/src/message.h
@@ -51,8 +51,8 @@ struct DirectMessageThread::Iterator::Priv
struct DirectMessageService::Priv
{
- static vector<ThreadWatcher> watchers;
- static mutex watcherLock;
+ vector<ThreadWatcher> watchers;
+ mutex watcherLock;
};
}
diff --git a/src/network.cpp b/src/network.cpp
index 91f1923..4b79dcb 100644
--- a/src/network.cpp
+++ b/src/network.cpp
@@ -6,6 +6,7 @@
#include <algorithm>
#include <cstring>
#include <iostream>
+#include <stdexcept>
#include <arpa/inet.h>
#include <ifaddrs.h>
@@ -13,6 +14,7 @@
#include <unistd.h>
using std::holds_alternative;
+using std::runtime_error;
using std::scoped_lock;
using std::to_string;
using std::unique_lock;
@@ -26,6 +28,14 @@ Server::Server(const Identity & self, vector<unique_ptr<Service>> && svcs):
Server::~Server() = default;
+Service & Server::svcHelper(const std::type_info & tinfo)
+{
+ for (auto & s : p->services)
+ if (typeid(*s) == tinfo)
+ return *s;
+ throw runtime_error("service not found");
+}
+
PeerList & Server::peerList() const
{
return p->plist;