swift
datalinklocal.cpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (C) 2017 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/promise.h"
11 
13 {
14  CDataLinkLocal::CDataLinkLocal(QObject *parent) : QObject(parent) { setConnectionStatus(true); }
15 
17 
19  {
20  connect(mutator, &CPassiveMutator::eventPosted, this,
21  [this, channel = getChannelName(mutator)](const CVariant &param) { dispatchEvent(param, channel); });
22  }
23 
25  {
26  publish(static_cast<const CPassiveMutator *>(mutator));
27 
28  auto &channel = getChannel(mutator);
29  Q_ASSERT_X(!channel.activeMutator, Q_FUNC_INFO, "Tried to publish two active mutators on one channel");
30  channel.activeMutator = mutator->weakRef();
31  }
32 
34  {
35  getChannel(observer).passiveObservers.push_back(observer->weakRef());
36  }
37 
39  {
40  subscribe(static_cast<const CPassiveObserver *>(observer));
41 
42  connect(observer, &CActiveObserver::requestPosted, this,
43  [this, channel = getChannelName(observer)](const CVariant &param, CPromise<CVariant> reply) {
44  reply.chainResult(handleRequest(param, channel));
45  });
46  }
47 
48  void CDataLinkLocal::dispatchEvent(const CVariant &param, const QString &channel)
49  {
50  for (const auto &observerWeak : std::as_const(getChannel(channel).passiveObservers))
51  {
52  auto observer = observerWeak.lock();
53  if (observer && observer->eventSubscription().matches(param)) { observer->handleEvent(param); }
54  }
55  }
56 
57  QFuture<CVariant> CDataLinkLocal::handleRequest(const CVariant &param, const QString &channel)
58  {
59  auto mutator = getChannel(channel).activeMutator.lock();
60  if (mutator) { return mutator->handleRequest(param); }
61  return {};
62  }
63 
64  CDataLinkLocal::Channel &CDataLinkLocal::getChannel(const QString &name)
65  {
66  QMutexLocker lock(&m_channelsMutex);
67  return m_channels[name];
68  }
69 
70  CDataLinkLocal::Channel &CDataLinkLocal::getChannel(const QObject *object)
71  {
72  return getChannel(getChannelName(object));
73  }
74 } // namespace swift::misc::shared_state
A promise-based interface to QFuture, similar to std::promise for std::future.
Definition: promise.h:78
void chainResult(QFuture< U > future)
When the given future is ready, use its result to set the result of this promise.
Definition: promise.h:103
Wrapper around QVariant which provides transparent access to CValueObject methods of the contained ob...
Definition: variant.h:66
Extends CPassiveMutator with the ability to respond to requests.
Definition: activemutator.h:25
QWeakPointer< const CActiveMutator > weakRef() const
Get a QWeakPointer pointing to this object.
Definition: activemutator.h:48
Extends CPassiveObserver with the ability to send requests and receive replies.
void requestPosted(const swift::misc::CVariant &param, swift::misc::CPromise< swift::misc::CVariant > o_reply)
Emitted by request and requestAsync.
Endpoint which can emit events to subscribers.
void eventPosted(const swift::misc::CVariant &param)
Emitted by postEvent.
Endpoint which can subscribe to events emitted by CPassiveMutator.
QWeakPointer< const CPassiveObserver > weakRef() const
Get a QWeakPointer pointing to this object.
Utilities for sharing state between multiple objects.
Definition: application.h:48