swift
duplex.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 namespace swift::misc::shared_state::dbus
9 {
10  IDuplex::IDuplex(QObject *parent) : QObject(parent)
11  {
12  connect(this, &IDuplex::replyReceived, this, [this](const QString &, const CVariant &param, quint32 token) {
13  const auto it = m_submittedRequests.find(token);
14  if (it == m_submittedRequests.end()) { return; }
15  it->setResult(param);
16  m_submittedRequests.erase(it);
17  });
18  }
19 
20  QFuture<CVariant> IDuplex::submitRequest(const QString &channel, const CVariant &param)
21  {
22  const auto token = getToken();
23  auto future = m_submittedRequests.insert(token, {})->future();
24  submitRequest(channel, param, token);
25  return future;
26  }
27 
28  QFuture<CVariant> IDuplex::receiveRequest(const QString &channel, const swift::misc::CVariant &param)
29  {
30  const auto token = getToken();
31  auto future = m_receivedRequests.insert(token, {})->future();
32  emit requestReceived(channel, param, token, QPrivateSignal {});
33  return future;
34  }
35 
36  void IDuplex::reply(const swift::misc::CVariant &param, quint32 token)
37  {
38  const auto it = m_receivedRequests.find(token);
39  if (it == m_receivedRequests.end()) { return; }
40  it->setResult(param);
41  m_receivedRequests.erase(it);
42  }
43 
44  quint32 IDuplex::getToken() { return m_token++; }
45 } // namespace swift::misc::shared_state::dbus
Wrapper around QVariant which provides transparent access to CValueObject methods of the contained ob...
Definition: variant.h:66
IDuplex(QObject *parent=nullptr)
Constructor.
Definition: duplex.cpp:10
void requestReceived(const QString &channel, const swift::misc::CVariant &param, quint32 token, QPrivateSignal)
Server has submitted a request to be handled by the client.
QFuture< CVariant > receiveRequest(const QString &channel, const swift::misc::CVariant &param)
Server submits a request to the client. Reply is returned via a future.
Definition: duplex.cpp:28
virtual void reply(const swift::misc::CVariant &param, quint32 token)
Client replies to a submitted request.
Definition: duplex.cpp:36
QFuture< CVariant > submitRequest(const QString &channel, const swift::misc::CVariant &param)
Client submits a request to the server. Reply is returned via a future.
Definition: duplex.cpp:20