From a16b33031c7bcf2eabf1e0c3571000234b7740df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Smr=C5=BE?= Date: Sun, 24 Jan 2021 22:46:48 +0100 Subject: Attach service --- src/network.cpp | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 64 insertions(+), 1 deletion(-) (limited to 'src/network.cpp') diff --git a/src/network.cpp b/src/network.cpp index c0bcc2a..48ba4b9 100644 --- a/src/network.cpp +++ b/src/network.cpp @@ -26,8 +26,23 @@ Server::Server(const Head & head, vector> && svc { } +Server:: Server(const std::shared_ptr & ptr): + p(ptr) +{ +} + Server::~Server() = default; +const Head & Server::localHead() const +{ + return p->localHead; +} + +const Identity & Server::identity() const +{ + return p->self; +} + Service & Server::svcHelper(const std::type_info & tinfo) { for (auto & s : p->services) @@ -45,6 +60,27 @@ PeerList & Server::peerList() const Peer::Peer(const shared_ptr & p): p(p) {} Peer::~Peer() = default; +Server Peer::server() const +{ + if (auto speer = p->speer.lock()) + return Server(speer->server.getptr()); + throw runtime_error("Server no longer running"); +} + +const Storage & Peer::tempStorage() const +{ + if (auto speer = p->speer.lock()) + return speer->tempStorage; + throw runtime_error("Server no longer running"); +} + +const PartialStorage & Peer::partialStorage() const +{ + if (auto speer = p->speer.lock()) + return speer->partStorage; + throw runtime_error("Server no longer running"); +} + string Peer::name() const { if (auto speer = p->speer.lock()) { @@ -87,6 +123,21 @@ bool Peer::hasChannel() const } bool Peer::send(UUID uuid, const Ref & ref) const +{ + return send(uuid, ref, *ref); +} + +bool Peer::send(UUID uuid, const Object & obj) const +{ + if (auto speer = p->speer.lock()) { + auto ref = speer->tempStorage.storeObject(obj); + return send(uuid, ref, obj); + } + + return false; +} + +bool Peer::send(UUID uuid, const Ref & ref, const Object & obj) const { if (hasChannel()) if (auto speer = p->speer.lock()) { @@ -94,13 +145,20 @@ bool Peer::send(UUID uuid, const Ref & ref) const { TransportHeader::Type::ServiceType, uuid }, { TransportHeader::Type::ServiceRef, ref }, }); - speer->send(header, { *ref }); + speer->send(header, { obj }); return true; } return false; } +bool Peer::operator==(const Peer & other) const { return p == other.p; } +bool Peer::operator!=(const Peer & other) const { return p != other.p; } +bool Peer::operator<(const Peer & other) const { return p < other.p; } +bool Peer::operator<=(const Peer & other) const { return p <= other.p; } +bool Peer::operator>(const Peer & other) const { return p > other.p; } +bool Peer::operator>=(const Peer & other) const { return p >= other.p; } + PeerList::PeerList(): p(new Priv) {} PeerList::PeerList(const shared_ptr & p): p(p) {} @@ -200,6 +258,11 @@ Server::Priv::~Priv() close(sock); } +shared_ptr Server::Priv::getptr() +{ + return shared_from_this(); +} + void Server::Priv::doListen() { vector buf, decrypted, *current; -- cgit v1.2.3