Repository navigation
Expand file tree
/
Copy pathQMLRedisInterface.cpp
More file actions
229 lines (185 loc) · 6.71 KB
/
Copy pathQMLRedisInterface.cpp
File metadata and controls
229 lines (185 loc) · 6.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include "QMLRedisInterface.h"
/*!
\mainclass
\class QMLRedisInterface
\inmodule RedisInterface
\brief A simple wrapper class that enables a RedisInterface to be instantiated as a QML Item.
This QQuickItem-based wrapper provides call-throughs to a RedisInterface instance, which
provides a common interface for QObject-based objects to communicate via Redis + HTTP.
Subscribed/published events and properties are defined in a declarative fashion using the \c{variant} properties
\l{subscribedEvents}, \l{publishedEvents}, \l{subscribedProperties}, and \l{publishedProperties}.
Example:
\code
// Remote properties we wish to pull from Redis automatically
subscribedProperties: [
{ remote: "remote:published:double", local: "localSubscribedDouble" },
{ remote: "remote:published:string", local: "localSubscribedString" }
]
// Local properties we wish to push to Redis automatically
publishedProperties: [
{ local: "localPublishedDouble", remote: "remote:subscribed:double" },
{ local: "localPublishedString", remote: "remote:subscribed:string" }
]
// Remote events to which we wish to subscribe, triggering local signals/methods automatically
subscribedEvents: [
{ remote: "remote:signal:trigger", local: "localSignal" },
{ remote: "remote:method:trigger", local: "localMethod" },
{ remote: "remote:wildcard*", local: "localWildcard" }
]
// Local events (signals) we wish to publish to Redis when they are emitted
publishedEvents: [
{ local: "publishedSignal1", remote: "remote:subscribed:event1" },
{ local: "publishedSignal2", remote: "remote:subscribed:event2" }
]
// Local properties that are 'pulled' from Redis
property double localSubscribedDouble
property string localSubscribedString
// Local properties that are 'pushed' to Redis
property double localPublishedDouble: 0.00
property string localPublishedString: "."
// Signals we wish to publish to Redis
signal publishedSignal1
signal publishedSignal2
// Signal to trigger when "remote:signal:trigger" is emitted by Redis
signal localSignal
onLocalSignal: {
console.log("(QML) localSignal() emitted");
}
// Method to invoke when "remote:method:trigger" is emitted by Redis
function localMethod() {
console.log("(QML) localMethod() called");
}
// Method to invoke when "remote:wildcard*" is emitted by Redis
function localWildcard() {
console.log("(QML) localWildcard() called");
}
\endcode
\sa RedisInterface
*/
QMLRedisInterface::QMLRedisInterface(QQuickItem *parent) :
QQuickItem(parent)
{
setFlag(ItemHasContents, true);
}
QVariant QMLRedisInterface::subscribedEvents() const
{
return _subscribedEvents;
}
QVariant QMLRedisInterface::get(const QString &key) const
{
if(this->isComponentComplete())
return _redisInterface->get(key);
return QVariant();
}
void QMLRedisInterface::get(const QString &key, QJSValue callback) const
{
if(this->isComponentComplete())
_redisInterface->get(key, callback);
}
bool QMLRedisInterface::subscribeToEvent(const QString& remoteEventName, const QString& localMethodName)
{
if(this->isComponentComplete())
return _redisInterface->subscribeToEvent(remoteEventName, localMethodName);
return false;
}
void QMLRedisInterface::init()
{
_redisInterface = new RedisInterface(serverUrl(), this);
// Subscribe to events.
QListIterator<QVariant> subscribedEventsIter = subscribedEvents().toList();
while(subscribedEventsIter.hasNext())
{
QVariantMap element = subscribedEventsIter.next().toMap();
QString remoteEventName = element.value("remote").toString();
QString localMethodName = QString(element.value("local").toString());
_redisInterface->subscribeToEvent(remoteEventName, localMethodName);
}
// Publish events.
QListIterator<QVariant> publishedEventsIter = publishedEvents().toList();
while(publishedEventsIter.hasNext())
{
QVariantMap element = publishedEventsIter.next().toMap();
QString localSignalName = element.value("local").toString();
QString remoteEventName = element.value("remote").toString();
_redisInterface->publishEvent(localSignalName, remoteEventName);
}
// Subscribe to properties.
QListIterator<QVariant> subscribedPropertiesIter = subscribedProperties().toList();
while(subscribedPropertiesIter.hasNext())
{
QVariantMap element = subscribedPropertiesIter.next().toMap();
QString remotePropertyName = element.value("remote").toString();
QString localPropertyName = element.value("local").toString();
_redisInterface->subscribeToProperty(remotePropertyName, localPropertyName);
}
// Publish properties.
QListIterator<QVariant> publishedPropertiesIter = publishedProperties().toList();
while(publishedPropertiesIter.hasNext())
{
QVariantMap element = publishedPropertiesIter.next().toMap();
QString remotePropertyName = element.value("remote").toString();
QString localPropertyName = element.value("local").toString();
_redisInterface->publishProperty(localPropertyName, remotePropertyName);
}
}
QString QMLRedisInterface::serverUrl() const
{
return _serverUrl;
}
void QMLRedisInterface::set(const QString &key, const QVariant &value)
{
if(this->isComponentComplete())
_redisInterface->set(key, value);
}
QVariant QMLRedisInterface::publishedProperties() const
{
return _publishedProperties;
}
QVariant QMLRedisInterface::subscribedProperties() const
{
return _subscribedProperties;
}
QVariant QMLRedisInterface::publishedEvents() const
{
return _publishedEvents;
}
void QMLRedisInterface::setSubscribedEvents(const QVariant &value)
{
if(_subscribedEvents != value)
{
_subscribedEvents = value;
emit subscribedEventsChanged(value);
}
}
void QMLRedisInterface::setPublishedProperties(const QVariant &value)
{
if(_publishedProperties != value)
{
_publishedProperties = value;
emit publishedPropertiesChanged(value);
}
}
void QMLRedisInterface::setSubscribedProperties(const QVariant& value)
{
if(_subscribedProperties != value)
{
_subscribedProperties = value;
emit subscribedPropertiesChanged(value);
}
}
void QMLRedisInterface::setPublishedEvents(const QVariant& value)
{
if(_publishedEvents != value)
{
_publishedEvents = value;
emit publishedEventsChanged(value);
}
}
void QMLRedisInterface::setServerUrl(const QString& value)
{
if(_serverUrl != value)
{
_serverUrl = value;
emit serverUrlChanged(value);
}
}