diff options
Diffstat (limited to 'framework/src/fabric.cpp')
-rw-r--r-- | framework/src/fabric.cpp | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/framework/src/fabric.cpp b/framework/src/fabric.cpp new file mode 100644 index 00000000..b14ed55d --- /dev/null +++ b/framework/src/fabric.cpp | |||
@@ -0,0 +1,83 @@ | |||
1 | /* | ||
2 | Copyright (c) 2016 Christian Mollekopf <mollekopf@kolabsys.com> | ||
3 | |||
4 | This library is free software; you can redistribute it and/or modify it | ||
5 | under the terms of the GNU Library General Public License as published by | ||
6 | the Free Software Foundation; either version 2 of the License, or (at your | ||
7 | option) any later version. | ||
8 | |||
9 | This library is distributed in the hope that it will be useful, but WITHOUT | ||
10 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | ||
11 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public | ||
12 | License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU Library General Public License | ||
15 | along with this library; see the file COPYING.LIB. If not, write to the | ||
16 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||
17 | 02110-1301, USA. | ||
18 | */ | ||
19 | |||
20 | #include "fabric.h" | ||
21 | |||
22 | #include <QDebug> | ||
23 | |||
24 | namespace Kube { | ||
25 | namespace Fabric { | ||
26 | |||
27 | class Bus { | ||
28 | public: | ||
29 | Bus() = default; | ||
30 | ~Bus() = default; | ||
31 | |||
32 | static Bus &instance() | ||
33 | { | ||
34 | static Bus bus; | ||
35 | return bus; | ||
36 | } | ||
37 | |||
38 | void registerListener(Listener *listener) | ||
39 | { | ||
40 | mListener << listener; | ||
41 | } | ||
42 | |||
43 | void unregisterListener(Listener *listener) | ||
44 | { | ||
45 | mListener.removeAll(listener); | ||
46 | } | ||
47 | |||
48 | void postMessage(const QString &id, const QVariantMap &message) | ||
49 | { | ||
50 | for (const auto &l : mListener) { | ||
51 | l->notify(id, message); | ||
52 | } | ||
53 | } | ||
54 | |||
55 | private: | ||
56 | QVector<Listener*> mListener; | ||
57 | }; | ||
58 | |||
59 | void Fabric::postMessage(const QString &id, const QVariantMap &msg) | ||
60 | { | ||
61 | Bus::instance().postMessage(id, msg); | ||
62 | } | ||
63 | |||
64 | Listener::Listener(QObject *parent) | ||
65 | : QObject(parent) | ||
66 | { | ||
67 | Bus::instance().registerListener(this); | ||
68 | } | ||
69 | |||
70 | Listener::~Listener() | ||
71 | { | ||
72 | Bus::instance().unregisterListener(this); | ||
73 | } | ||
74 | |||
75 | void Listener::notify(const QString &messageId, const QVariantMap &msg) | ||
76 | { | ||
77 | if (messageId == mFilter) { | ||
78 | emit messageReceived(msg); | ||
79 | } | ||
80 | } | ||
81 | |||
82 | } | ||
83 | } | ||