summaryrefslogtreecommitdiff
path: root/src/network.cpp
diff options
context:
space:
mode:
authorRoman Smrž <roman.smrz@seznam.cz>2021-01-17 21:59:46 +0100
committerRoman Smrž <roman.smrz@seznam.cz>2021-01-19 21:44:22 +0100
commit2abd6593c8b047d3fd579aa6cc0058bbebe266f8 (patch)
tree44af7148f648538846ef64bed0f8560f77de7e5d /src/network.cpp
parent1466751256580d8b0e6eea46a8028dcab9742f6b (diff)
Server watching local state head
Diffstat (limited to 'src/network.cpp')
-rw-r--r--src/network.cpp27
1 files changed, 24 insertions, 3 deletions
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<unique_ptr<Service>> && svcs):
- p(new Priv(self, std::move(svcs)))
+Server::Server(const Head<LocalState> & head, vector<unique_ptr<Service>> && svcs):
+ p(new Priv(head, *head->identity(), std::move(svcs)))
{
}
@@ -139,7 +139,9 @@ void PeerList::onUpdate(function<void(size_t, const Peer *)> w)
}
-Server::Priv::Priv(const Identity & self, vector<unique_ptr<Service>> && svcs):
+Server::Priv::Priv(const Head<LocalState> & local, const Identity & self,
+ vector<unique_ptr<Service>> && svcs):
+ localHead(local),
self(self),
services(std::move(svcs))
{
@@ -176,6 +178,8 @@ Server::Priv::Priv(const Identity & self, vector<unique_ptr<Service>> && 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<LocalState> & 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<Object> & objs) const
{
vector<uint8_t> data, part, out;