swift
Loading...
Searching...
No Matches
dupleximpl.cpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (C) 2020 swift Project Community / Contributors
2// SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3
5
7
8#include "misc/dbusserver.h"
9#include "misc/identifier.h"
11#include "misc/variantlist.h"
12#include "misc/verify.h"
13
14namespace swift::misc::shared_state::dbus
15{
17 : IDuplex(parent), m_hub(hub)
18 {
19 if (server)
20 {
22 }
23 }
24
26 {
27 while (!m_subscriptions.isEmpty())
28 {
29 const auto channel = m_subscriptions.firstKey(); // explicit copy because a reference would dangle
30 setSubscription(channel, {});
31 }
32 }
33
34 void CDuplex::postEvent(const QString &channel, const CVariant &param)
35 {
36 for (auto client : m_hub->clients())
37 {
38 if (client != this && client->m_subscriptions.value(channel).matches(param))
39 {
40 emit client->eventPosted(channel, param);
41 }
42 }
43 }
44
45 void CDuplex::setSubscription(const QString &channel, const CVariantList &filters)
46 {
47 if (filters.isEmpty())
48 {
49 m_subscriptions.remove(channel);
50 }
51 else
52 {
53 m_subscriptions.insert(channel, filters);
54 }
55
56 for (auto client : m_hub->clients())
57 {
58 if (client != this)
59 {
60 client->requestPeerSubscriptions(channel);
61 }
62 }
63 }
64
66 {
67 QSet<QString> channels;
68 for (const auto &client : m_hub->clients())
69 {
70 if (client != this)
71 {
72 const auto &keys = client->m_subscriptions.keys();
73 const QSet subscriptions(keys.begin(), keys.end());
74 channels.unite(subscriptions);
75 }
76 }
77 for (const auto &channel : channels)
78 {
80 }
81 }
82
84 {
85 CVariantList filters;
86 for (auto peer : m_hub->clients())
87 {
88 if (peer != this)
89 {
90 filters.push_back(peer->m_subscriptions.value(channel));
91 }
92 }
93
94 emit peerSubscriptionsReceived(channel, filters);
95 }
96
97 void CDuplex::submitRequest(const QString &channel, const CVariant &param, quint32 token)
98 {
99 for (auto handler : m_hub->clients())
100 {
101 if (handler != this && handler->m_handlingChannels.contains(channel))
102 {
103 doAfter(handler->receiveRequest(channel, param), this,
104 [this, channel, token](QFuture<CVariant> future) {
105 emit this->replyReceived(channel, future.result(), token);
106 });
107 return;
108 }
109 }
110 }
111
112 void CDuplex::advertise(const QString &channel) { m_handlingChannels.insert(channel); }
113
114 void CDuplex::withdraw(const QString &channel) { m_handlingChannels.remove(channel); }
115} // namespace swift::misc::shared_state::dbus
Custom DBusServer.
Definition dbusserver.h:34
void addObject(const QString &name, QObject *object)
Add a QObject to be exposed via DBus.
Value object encapsulating information identifying a component of a modular distributed swift process...
Definition identifier.h:29
QString toDBusObjectPath(const QString &root={}) const
Produces a DBus object path from the identifier.
void push_back(const T &value)
Appends an element at the end of the sequence.
Definition sequence.h:308
bool isEmpty() const
Synonym for empty.
Definition sequence.h:285
Wrapper around QVariant which provides transparent access to CValueObject methods of the contained ob...
Definition variant.h:69
Value object encapsulating a list of variants.
Definition variantlist.h:29
void postEvent(const QString &channel, const swift::misc::CVariant &param)
Client posts an event to the server.
void advertise(const QString &channel)
Client advertises that it can handle requests for the given channel.
void requestPeerSubscriptions()
Client requests to be notified of all other clients' subscriptions.
CDuplex(CHub *hub, const CIdentifier &client, CDBusServer *server, QObject *parent=nullptr)
Constructor.
void setSubscription(const QString &channel, const swift::misc::CVariantList &filters)
Client announces its subscription to an event channel.
void submitRequest(const QString &channel, const swift::misc::CVariant &param, quint32 token)
Client submits a request to the server. Reply is returned via a future.
void withdraw(const QString &channel)
Client advertises that it can no longer handle requests for the given channel.
Server side implementation of IHub.
Definition hubimpl.h:27
const auto & clients() const
Returns a range containing all duplex objects.
Definition hubimpl.h:39
IDuplex(QObject *parent=nullptr)
Constructor.
Definition duplex.cpp:10
void peerSubscriptionsReceived(const QString &channel, const swift::misc::CVariantList &filters)
Server has notified the client that other clients' event subscriptions have changed.
#define SWIFT_MISC_DUPLEX_PATH_ROOT
DBus object path root for sharedstate hub.
Definition duplex.h:22
void doAfter(QFuture< T > future, QObject *context, F &&func)
Connect a slot or function to be invoked in the given context when a QFuture is finished.
Definition promise.h:68
QObject(QObject *parent)
QObject * parent() const const
QSet< T > & unite(QSet< T > &&other)