summaryrefslogtreecommitdiff
path: root/src/sync.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/sync.cpp')
-rw-r--r--src/sync.cpp66
1 files changed, 66 insertions, 0 deletions
diff --git a/src/sync.cpp b/src/sync.cpp
new file mode 100644
index 0000000..5680da6
--- /dev/null
+++ b/src/sync.cpp
@@ -0,0 +1,66 @@
+#include <erebos/sync.h>
+
+#include <erebos/identity.h>
+#include <erebos/network.h>
+
+using namespace erebos;
+
+using std::scoped_lock;
+
+static const UUID myUUID("a4f538d0-4e50-4082-8e10-7e3ec2af175d");
+
+SyncService::SyncService(Config &&, const Server & s):
+ server(s)
+{
+ server.peerList().onUpdate(std::bind(&SyncService::peerWatcher, this,
+ std::placeholders::_1, std::placeholders::_2));
+ watchedLocal = server.localState().lens<vector<Ref>>().watch(std::bind(&SyncService::localStateWatcher, this,
+ std::placeholders::_1));
+}
+
+SyncService::~SyncService() = default;
+
+UUID SyncService::uuid() const
+{
+ return myUUID;
+}
+
+void SyncService::handle(Context & ctx)
+{
+ auto pid = ctx.peer().identity();
+ if (!pid)
+ return;
+
+ const auto & powner = pid->finalOwner();
+ const Identity owner = ctx.peer().server().identity().finalOwner();
+
+ if (!powner.sameAs(owner))
+ return;
+
+ ctx.local(
+ ctx.local()->sharedRefAdd(ctx.ref())
+ );
+}
+
+void SyncService::peerWatcher(size_t, const Peer * peer)
+{
+ if (peer) {
+ if (auto id = peer->identity()) {
+ if (id->finalOwner().sameAs(server.identity().finalOwner()))
+ for (const auto & r : server.localState().get().sharedRefs())
+ peer->send(myUUID, r);
+ }
+ }
+}
+
+void SyncService::localStateWatcher(const vector<Ref> & refs)
+{
+ const auto & plist = server.peerList();
+ const Identity owner = server.identity().finalOwner();
+
+ for (size_t i = 0; i < plist.size(); i++)
+ if (auto id = plist.at(i).identity())
+ if (id->finalOwner().sameAs(owner))
+ for (const auto & r : refs)
+ plist.at(i).send(myUUID, r);
+}