From 86293ff20d9f8625615e855d98249175e8cd5cd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Roman=20Smr=C5=BE?= Date: Sun, 18 Dec 2022 18:56:19 +0100 Subject: Contact service and contacts using stored set --- src/contact.cpp | 173 ++++++++++++++++++++++++++++++++------------------------ src/contact.h | 14 +---- src/main.cpp | 49 ++++++++++++++-- src/set.cpp | 21 ++++++- 4 files changed, 165 insertions(+), 92 deletions(-) (limited to 'src') diff --git a/src/contact.cpp b/src/contact.cpp index edc33ea..796e896 100644 --- a/src/contact.cpp +++ b/src/contact.cpp @@ -6,42 +6,44 @@ using namespace erebos; using std::move; -DEFINE_SHARED_TYPE(List, +DEFINE_SHARED_TYPE(Set, "34fbb61e-6022-405f-b1b3-a5a1abecd25e", - &Contact::loadList, - [](const List & list) { - if (list.empty()) - return vector(); - return list.front().refs(); + &Set::load, + [](const Set & set) { + return set.store(); }) +static const UUID serviceUUID("d9c37368-0da1-4280-93e9-d9bd9a198084"); -List Contact::prepend(const Storage & st, Identity id, List list) +Contact::Contact(vector> data): + p(shared_ptr(new Priv { + .data = data, + })) { - auto cd = st.store(ContactData { - .prev = list.empty() ? vector>() : list.front().p->data, - .identity = id.data(), - .name = nullopt, - }); - return list.push_front( - Contact(shared_ptr(new Priv { - .data = { cd }, - .identity = move(id), - })) - ); } -Identity Contact::identity() const +optional Contact::identity() const { + p->init(); return p->identity; } -optional Contact::name() const +optional Contact::customName() const { p->init(); return p->name; } +string Contact::name() const +{ + if (auto cust = customName()) + return *cust; + if (auto id = p->identity) + if (auto idname = id->name()) + return *idname; + return ""; +} + bool Contact::operator==(const Contact & other) const { return p->data == other.p->data; @@ -52,66 +54,18 @@ bool Contact::operator!=(const Contact & other) const return p->data != other.p->data; } -List Contact::loadList(const vector & refs) -{ - vector> cdata; - cdata.reserve(refs.size()); - - for (const auto & r : refs) - cdata.push_back(Stored::load(r)); - return Priv::loadList(move(cdata), {}); -} - -List Contact::Priv::loadList(vector> && cdata, vector && seen) -{ - if (cdata.empty()) - return {}; - - filterAncestors(cdata); - - for (size_t i = 0; i < cdata.size(); i++) { - auto id = Identity::load(cdata[i]->identity); - if (!id) - continue; - - bool skip = false; - for (const auto & sid : seen) { - if (id->sameAs(sid)) { - skip = true; - break; - } - } - if (skip) - continue; - - vector> next; - next.reserve(cdata.size() - i - 1 + cdata[i]->prev.size()); - for (size_t j = i + 1; j < cdata.size(); j++) - next.push_back(cdata[j]); - for (const auto & x : cdata[i]->prev) - next.push_back(x); - - seen.push_back(*id); - auto p = shared_ptr(new Priv { .data = move(cdata), .identity = move(*id) }); - return List(Contact(p), loadList(move(next), move(seen))); - } - - return {}; -} - -vector Contact::refs() const +vector> Contact::data() const { - vector res; - res.reserve(p->data.size()); - for (const auto & x : p->data) - res.push_back(x.ref()); - return res; + return p->data; } void Contact::Priv::init() { std::call_once(initFlag, [this]() { - name = identity.name(); + // TODO: property lookup + identity = Identity::load(data[0]->identity); + if (identity) + name = identity->name(); }); } @@ -151,3 +105,74 @@ Ref ContactData::store(const Storage & st) const return st.storeObject(Record(std::move(items))); } + +ContactService::ContactService() = default; +ContactService::~ContactService() = default; + +UUID ContactService::uuid() const +{ + return serviceUUID; +} + +void ContactService::serverStarted(const Server & s) +{ + PairingService::serverStarted(s); + server = &s; +} + +void ContactService::request(const Peer & peer) +{ + requestPairing(serviceUUID, peer); +} + +Stored ContactService::handlePairingComplete(const Peer & peer) +{ + server->localHead().update([&] (const Stored & local) { + auto cdata = local.ref().storage().store(ContactData { + .prev = {}, + .identity = peer.identity()->finalOwner().data(), + .name = std::nullopt, + }); + + Contact contact(shared_ptr(new Contact::Priv { + .data = { cdata }, + })); + + auto contacts = local->shared>(); + + return local.ref().storage().store(local->shared>( + contacts.add(local.ref().storage(), contact))); + }); + + return peer.tempStorage().store(ContactAccepted {}); +} + +void ContactService::handlePairingResult(Context & ctx, Stored) +{ + auto cdata = ctx.local().ref().storage().store(ContactData { + .prev = {}, + .identity = ctx.peer().identity()->finalOwner().data(), + .name = std::nullopt, + }); + + Contact contact(shared_ptr(new Contact::Priv { + .data = { cdata }, + })); + + auto contacts = ctx.local()->shared>(); + + ctx.local(ctx.local()->shared>( + contacts.add(ctx.local().ref().storage(), contact))); +} + +ContactAccepted ContactAccepted::load(const Ref &) +{ + return ContactAccepted {}; +} + +Ref ContactAccepted::store(const Storage & st) const +{ + vector items; + items.emplace_back("accept", ""); + return st.storeObject(Record(std::move(items))); +} diff --git a/src/contact.h b/src/contact.h index 31deceb..6fc0219 100644 --- a/src/contact.h +++ b/src/contact.h @@ -19,24 +19,12 @@ struct IdentityData; struct Contact::Priv { vector> data; - Identity identity; void init(); std::once_flag initFlag {}; + optional identity {}; optional name {}; - - static List loadList(vector> &&, vector &&); -}; - -struct ContactData -{ - static ContactData load(const Ref &); - Ref store(const Storage &) const; - - vector> prev; - vector>> identity; - optional name; }; } diff --git a/src/main.cpp b/src/main.cpp index 387fe4a..de4ffbc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -74,7 +75,7 @@ struct TestPeer Peer peer; size_t id; bool deleted = false; - promise attachAnswer {}; + promise pairingAnswer {}; }; vector testPeers; @@ -245,7 +246,7 @@ future confirmPairing(string prefix, const Peer & peer, string confirm, fu promise promise; auto input = promise.get_future(); - getPeer(peer).attachAnswer = move(promise); + getPeer(peer).pairingAnswer = move(promise); ostringstream ss; ss << prefix << " " << getPeer(peer).id << " " << confirm; @@ -264,6 +265,11 @@ void startServer(const vector &) atts->onResponse(bind(confirmPairing, "attach-response", _1, _2, _3)); services.push_back(move(atts)); + auto conts = make_unique(); + conts->onRequest(bind(confirmPairing, "contact-request", _1, _2, _3)); + conts->onResponse(bind(confirmPairing, "contact-response", _1, _2, _3)); + services.push_back(move(conts)); + services.push_back(make_unique()); server.emplace(*h, move(services)); @@ -416,12 +422,43 @@ void attachTo(const vector & params) void attachAccept(const vector & params) { - getPeer(params.at(0)).attachAnswer.set_value(true); + getPeer(params.at(0)).pairingAnswer.set_value(true); } void attachReject(const vector & params) { - getPeer(params.at(0)).attachAnswer.set_value(false); + getPeer(params.at(0)).pairingAnswer.set_value(false); +} + +void contactRequest(const vector & params) +{ + server->svc().request(getPeer(params.at(0)).peer); +} + +void contactAccept(const vector & params) +{ + getPeer(params.at(0)).pairingAnswer.set_value(true); +} + +void contactReject(const vector & params) +{ + getPeer(params.at(0)).pairingAnswer.set_value(false); +} + +void contactList(const vector &) +{ + auto cmp = [](const Contact & x, const Contact & y) { + return x.data() < y.data(); + }; + for (const auto & c : h->behavior().lens().lens>().get().view(cmp)) { + ostringstream ss; + ss << "contact-list-item " << c.name(); + if (auto id = c.identity()) + if (auto iname = id->name()) + ss << " " << *iname; + printLine(ss.str()); + } + printLine("contact-list-done"); } vector commands = { @@ -442,6 +479,10 @@ vector commands = { { "attach-to", attachTo }, { "attach-accept", attachAccept }, { "attach-reject", attachReject }, + { "contact-request", contactRequest }, + { "contact-accept", contactAccept }, + { "contact-reject", contactReject }, + { "contact-list", contactList }, }; } diff --git a/src/set.cpp b/src/set.cpp index 001bce3..d224af3 100644 --- a/src/set.cpp +++ b/src/set.cpp @@ -32,7 +32,7 @@ SetBase::SetBase(shared_ptr p_): { } -shared_ptr SetBase::add(Storage & st, const vector & refs) const +shared_ptr SetBase::add(const Storage & st, const vector & refs) const { auto item = st.store(SetItem { .prev = p->items, @@ -122,6 +122,16 @@ vector> SetBase::toList() const return res; } +bool SetBase::operator==(const SetBase & other) const +{ + return p->items == other.p->items; +} + +bool SetBase::operator!=(const SetBase & other) const +{ + return !(*this == other); +} + vector SetBase::digests() const { vector res; @@ -131,6 +141,15 @@ vector SetBase::digests() const return res; } +vector SetBase::store() const +{ + vector res; + res.reserve(p->items.size()); + for (const auto & i : p->items) + res.push_back(i.ref()); + return res; +} + SetItem SetItem::load(const Ref & ref) { if (auto rec = ref->asRecord()) { -- cgit v1.2.3