From 2abd6593c8b047d3fd579aa6cc0058bbebe266f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Smr=C5=BE?= Date: Sun, 17 Jan 2021 21:59:46 +0100 Subject: Server watching local state head --- src/network.cpp | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) (limited to 'src/network.cpp') diff --git a/src/network.cpp b/src/network.cpp index 4b79dcb..bd64e07 100644 --- a/src/network.cpp +++ b/src/network.cpp @@ -21,8 +21,8 @@ using std::unique_lock; using namespace erebos; -Server::Server(const Identity & self, vector> && svcs): - p(new Priv(self, std::move(svcs))) +Server::Server(const Head & head, vector> && svcs): + p(new Priv(head, *head->identity(), std::move(svcs))) { } @@ -139,7 +139,9 @@ void PeerList::onUpdate(function w) } -Server::Priv::Priv(const Identity & self, vector> && svcs): +Server::Priv::Priv(const Head & local, const Identity & self, + vector> && svcs): + localHead(local), self(self), services(std::move(svcs)) { @@ -176,6 +178,8 @@ Server::Priv::Priv(const Identity & self, vector> && svcs): threadListen = thread([this] { doListen(); }); threadAnnounce = thread([this] { doAnnounce(); }); + + local.watch(std::bind(&Priv::handleLocalHeadChange, this, std::placeholders::_1)); } Server::Priv::~Priv() @@ -447,6 +451,23 @@ void Server::Priv::handlePacket(Server::Peer & peer, const TransportHeader & hea } } +void Server::Priv::handleLocalHeadChange(const Head & head) +{ + scoped_lock lock(dataMutex); + if (auto id = head->identity()) { + if (id->ref()->digest() != self.ref()->digest()) { + self = *id; + + TransportHeader header({ + { TransportHeader::Type::AnnounceSelf, *self.ref() } + }); + + for (const auto & peer : peers) + peer->send(header, { **self.ref() }); + } + } +} + void Server::Peer::send(const TransportHeader & header, const vector & objs) const { vector data, part, out; -- cgit v1.2.3