summaryrefslogtreecommitdiffstats
path: root/src/jsonendpoint.cpp
blob: 5ef840bd66b456c6754499252dbb7968e14a7825 (plain)
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/****************************************************************************
**
** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
** All rights reserved.
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** This file is part of the QtAddOn.JsonStream module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** GNU Lesser General Public License Usage
** This file may be used under the terms of the GNU Lesser General Public
** License version 2.1 as published by the Free Software Foundation and
** appearing in the file LICENSE.LGPL included in the packaging of this
** file. Please review the following information to ensure the GNU Lesser
** General Public License version 2.1 requirements will be met:
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain additional
** rights. These rights are described in the Nokia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU General
** Public License version 3.0 as published by the Free Software Foundation
** and appearing in the file LICENSE.GPL included in the packaging of this
** file. Please review the following information to ensure the GNU General
** Public License version 3.0 requirements will be met:
** http://www.gnu.org/copyleft/gpl.html.
**
** Other Usage
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
**
**
**
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "jsonendpoint.h"
#include "jsonconnection.h"
#include "jsonconnectionprocessor_p.h"
#include <qjsonobject.h>
#include <QVariant>
#include <QDebug>

QT_BEGIN_NAMESPACE_JSONSTREAM

class JsonEndpointPrivate
{
public:
    JsonEndpointPrivate()
        : mConnection(0)
        , mEmittedReadyRead(false)
        , mMessageReady(false)
    {
    }

    QString             mName;
    JsonConnection     *mConnection;
    bool                mEmittedReadyRead;
    bool                mMessageReady;
};

/****************************************************************************/

/*!
    \class JsonEndpoint
    \brief The JsonEndpoint class ...

*/

/*!
  Constructs a \c JsonEndpoint object.
 */

JsonEndpoint::JsonEndpoint(const QString & name, JsonConnection *connection)
    : QObject(0)
    , d_ptr(new JsonEndpointPrivate())
{
    Q_D(JsonEndpoint);
    d->mName = name;

    setConnection(connection);
}

/*!
  Deletes the \c JsonEndpoint object.
 */

JsonEndpoint::~JsonEndpoint()
{
    Q_D(JsonEndpoint);
    if (d->mConnection)
        d->mConnection->removeEndpoint(this);
}

/*!
  Returns a name used for message multiplexing. A default endpoint should not
  have a name
 */
QString JsonEndpoint::name() const
{
    Q_D(const JsonEndpoint);
    return d->mName;
}

/*!
  Sets a \a name used for message multiplexing. A default endpoint should not
  have a name
 */
void JsonEndpoint::setName( const QString & name )
{
    Q_D(JsonEndpoint);
    d->mName = name;
    emit nameChanged();
}

/*!
  Returns a connection that is used by endpoint
 */
JsonConnection *JsonEndpoint::connection() const
{
    Q_D(const JsonEndpoint);
    return d->mConnection;
}

/*!
  Sets a \a connection to be used by endpoint
 */
void JsonEndpoint::setConnection(JsonConnection *connection)
{
    Q_D(JsonEndpoint);
    d->mConnection = connection;
    if (d->mConnection)
        d->mConnection->addEndpoint(this);

    emit connectionChanged();
}

/*!
  Send a QVariantMap \a message over the connection.
  Returns \b true if the entire message was sent or buffered or \b false otherwise.
*/
bool JsonEndpoint::send(const QVariantMap& message)
{
    return send(QJsonObject::fromVariantMap(message));
}

/*!
  Send a JsonObject \a message over the connection.
  Returns \b true if the entire message was sent or buffered or \b false otherwise.
*/
bool JsonEndpoint::send(const QJsonObject& message)
{
    bool ret = false;
    Q_D(const JsonEndpoint);
    if (d->mConnection) {
        if (!d->mConnection->useSeparateThreadForProcessing()) {
            ret = d->mConnection->processor()->send(message);
        }
        else {
            QMetaObject::invokeMethod(d->mConnection->processor(),
                                      "send",
                                      Qt::BlockingQueuedConnection,
                                      Q_RETURN_ARG(bool, ret),
                                      Q_ARG(QJsonObject, message));
        }
    }
    return ret;
}

/*!
  \internal
  Handle a notification from connection processor and emit the correct signals
*/
void JsonEndpoint::slotReadyReadMessage()
{
    Q_D(JsonEndpoint);
    d->mMessageReady = true;
    if (!d->mEmittedReadyRead) {
        d->mEmittedReadyRead = true;
        emit readyReadMessage();
        d->mEmittedReadyRead = false;
    }
}


/*!
  Returns \b true if a message is available to be read via \l{readMessage()}
  or \b false otherwise.
 */
bool JsonEndpoint::messageAvailable()
{
    Q_D(JsonEndpoint);
    bool ret = d->mMessageReady;
    if (!d->mMessageReady) {
        // check again
        if (d->mConnection) {
            ret = d->mConnection->processor()->messageAvailable(this);
            d->mMessageReady = ret;
        }
    }
    return ret;
}

/*!
  Returns a JSON object that has been received as a variant map.  If no message is
  available, an empty JSON object is returned.
 */
QVariantMap JsonEndpoint::readMessageMap()
{
    return readMessage().toVariantMap();
}

/*!
  Returns a JSON object that has been received.  If no message is
  available, an empty JSON object is returned.
 */
QJsonObject JsonEndpoint::readMessage()
{
    QJsonObject obj;
    Q_D(JsonEndpoint);
    if (d->mConnection) {
        obj = d->mConnection->processor()->readMessage(this);
        d->mMessageReady = false;
    }
    return obj;
}

/*!
    \fn void JsonEndpoint::readyReadMessage()

    This signal is emitted once every time new data arrives on the device
    and a message is ready. \l{readMessage()} should be used to retrieve a message
    and \l{messageAvailable()} to check for next available messages.
    The client code may look like this:

    \code
    ...
    connect(endpoint, SIGNAL(readyReadMessage()), this, SLOT(processMessages()));
    ...

    void Foo::processMessages()
    {
        while (endpoint->messageAvailable()) {
            QJsonObject obj = endpoint->readMessage();
            <process message>
        }
    }
    \endcode

    \b readyReadMessage() is not emitted recursively; if you reenter the event loop
    inside a slot connected to the \b readyReadMessage() signal, the signal will not
    be reemitted.

    \sa readMessage(), messageAvailable()
*/

#include "moc_jsonendpoint.cpp"

QT_END_NAMESPACE_JSONSTREAM