swift
hubimpl.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"
10 #include "misc/variantlist.h"
11 
12 namespace swift::misc::shared_state::dbus
13 {
14  CHub::CHub(CDBusServer *server, QObject *parent) : IHub(parent), m_server(server)
15  {
16  if (server) { server->addObject(SWIFT_MISC_HUB_PATH, this); }
17  }
18 
19  std::pair<QSharedPointer<IDuplex>, QFuture<bool>> CHub::getDuplex(const CIdentifier &identifier)
20  {
21  auto future = openDuplexAsync(identifier);
22  return std::make_pair(m_clients.value(identifier), future);
23  }
24 
26  {
27  if (!m_clients.contains(client))
28  {
29  m_clients.insert(client, QSharedPointer<CDuplex>::create(this, client, m_server, this));
30  }
31  return true;
32  }
33 
35  {
36  // Using take() instead of remove() because we need the
37  // destruction to happen after the removal, not before.
38  m_clients.take(client);
39  }
40 
41  QFuture<bool> CHub::openDuplexAsync(const CIdentifier &client)
42  {
43  openDuplex(client);
44 
45  CPromise<bool> promise;
46  promise.setResult(true);
47  return promise.future();
48  }
49 
51  {
52  // Disconnect clients from the hub before destroying them,
53  // to avoid thrashing peer event subscription updates.
54  const auto clients = std::move(m_clients);
55  Q_UNUSED(clients)
56  }
57 } // 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.
Definition: dbusserver.cpp:226
Value object encapsulating information identifying a component of a modular distributed swift process...
Definition: identifier.h:29
A promise-based interface to QFuture, similar to std::promise for std::future.
Definition: promise.h:78
QFuture< T > future()
Return a future that can be used to access the result.
Definition: promise.h:81
void setResult(const T &value)
Set the result value that will be made available through the future.
Definition: promise.h:91
virtual bool openDuplex(const swift::misc::CIdentifier &client)
Create a duplex object for the identified process.
Definition: hubimpl.cpp:25
const auto & clients() const
Returns a range containing all duplex objects.
Definition: hubimpl.h:39
CHub(CDBusServer *server, QObject *parent=nullptr)
Constructor.
Definition: hubimpl.cpp:14
virtual QFuture< bool > openDuplexAsync(const CIdentifier &client)
Create a duplex object for the identified process.
Definition: hubimpl.cpp:41
virtual std::pair< QSharedPointer< IDuplex >, QFuture< bool > > getDuplex(const CIdentifier &)
Create a duplex object for the identified process.
Definition: hubimpl.cpp:19
virtual void closeDuplex(const swift::misc::CIdentifier &client)
Destroy the duplex object for the identified process.
Definition: hubimpl.cpp:34
Abstract interface for the hub in a star topology.
Definition: hub.h:37
#define SWIFT_MISC_HUB_PATH
DBus object path root for sharedstate hub.
Definition: hub.h:20