From 9aaba1211c95dc7e08437a7cca73452181e296d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Smr=C5=BE?= Date: Sun, 18 Apr 2021 22:25:17 +0200 Subject: Initial support for FRP behaviors --- src/CMakeLists.txt | 1 + src/frp.cpp | 133 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/identity.cpp | 10 ++++ src/network.cpp | 6 +++ src/network.h | 1 + 5 files changed, 151 insertions(+) create mode 100644 src/frp.cpp (limited to 'src') diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 61f491f..8f65555 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -5,6 +5,7 @@ include_directories( add_library(erebos attach channel + frp identity message network diff --git a/src/frp.cpp b/src/frp.cpp new file mode 100644 index 0000000..142fcd4 --- /dev/null +++ b/src/frp.cpp @@ -0,0 +1,133 @@ +#include + +#include +#include + +using namespace erebos; + +using std::condition_variable; +using std::move; +using std::mutex; +using std::nullopt; +using std::unique_lock; +using std::weak_ptr; + +mutex bhvTimeMutex; +condition_variable bhvTimeCond; +bool bhvTimeRunning = false; +uint64_t bhvTimeLast = 0; + +BhvCurTime::BhvCurTime() +{ + unique_lock lock(bhvTimeMutex); + bhvTimeCond.wait(lock, []{ return !bhvTimeRunning; }); + + bhvTimeRunning = true; + t = BhvTime(++bhvTimeLast); +} + +BhvCurTime::~BhvCurTime() +{ + if (t) { + unique_lock lock(bhvTimeMutex); + bhvTimeRunning = false; + lock.unlock(); + bhvTimeCond.notify_one(); + } +} + +BhvCurTime::BhvCurTime(BhvCurTime && other) +{ + t = other.t; + other.t = nullopt; +} + +BhvCurTime & BhvCurTime::operator=(BhvCurTime && other) +{ + t = other.t; + other.t = nullopt; + return *this; +} + + +BhvImplBase::~BhvImplBase() = default; + +void BhvImplBase::dependsOn(shared_ptr other) +{ + depends.push_back(other); + other->rdepends.push_back(shared_from_this()); +} + +void BhvImplBase::updated(const BhvCurTime & ctime) +{ + vector> toUpdate; + markDirty(ctime, toUpdate); + + for (auto & bhv : toUpdate) + bhv->updateDirty(ctime); +} + +void BhvImplBase::markDirty(const BhvCurTime & ctime, vector> & toUpdate) +{ + if (dirty) + return; + + if (!needsUpdate(ctime)) + return; + + dirty = true; + toUpdate.push_back(shared_from_this()); + + bool prune = false; + for (const auto & w : rdepends) { + if (auto b = w.lock()) + b->markDirty(ctime, toUpdate); + else + prune = true; + } + + if (prune) { + decltype(rdepends) pruned; + for (const auto & w : rdepends) + if (!w.expired()) + pruned.push_back(move(w)); + rdepends = move(pruned); + } +} + +void BhvImplBase::updateDirty(const BhvCurTime & ctime) +{ + if (!dirty) + return; + + for (auto & d : depends) + d->updateDirty(ctime); + + doUpdate(ctime); + dirty = false; + + bool prune = false; + for (const auto & wcb : watchers) { + if (auto cb = wcb.lock()) + (*cb)(ctime); + else + prune = true; + } + + if (prune) { + decltype(watchers) pruned; + for (const auto & w : watchers) + if (!w.expired()) + pruned.push_back(move(w)); + watchers = move(pruned); + } +} + +bool BhvImplBase::needsUpdate(const BhvCurTime &) const +{ + return true; +} + +void BhvImplBase::doUpdate(const BhvCurTime &) +{ +} diff --git a/src/identity.cpp b/src/identity.cpp index 374a872..a4c12f2 100644 --- a/src/identity.cpp +++ b/src/identity.cpp @@ -77,6 +77,16 @@ bool Identity::sameAs(const Identity & other) const other.p->data[0]->data->keyIdentity; } +bool Identity::operator==(const Identity & other) const +{ + return p->data == other.p->data; +} + +bool Identity::operator!=(const Identity & other) const +{ + return p->data != other.p->data; +} + optional Identity::ref() const { if (p->data.size() == 1) diff --git a/src/network.cpp b/src/network.cpp index f33c097..259ae5e 100644 --- a/src/network.cpp +++ b/src/network.cpp @@ -38,6 +38,11 @@ const Head & Server::localHead() const return p->localHead; } +const Bhv & Server::localState() const +{ + return p->localState; +} + const Identity & Server::identity() const { return p->self; @@ -203,6 +208,7 @@ Server::Priv::Priv(const Head & local, const Identity & self, vector> && svcs): self(self), // Watching needs to start after self is initialized + localState(local.behavior()), localHead(local.watch(std::bind(&Priv::handleLocalHeadChange, this, std::placeholders::_1))), services(std::move(svcs)) { diff --git a/src/network.h b/src/network.h index 6ebd60c..fe7d7b4 100644 --- a/src/network.h +++ b/src/network.h @@ -158,6 +158,7 @@ struct Server::Priv : enable_shared_from_this bool finish = false; Identity self; + Bhv localState; WatchedHead localHead; vector> services; -- cgit v1.2.3