summaryrefslogtreecommitdiffstats
path: root/src/jsonstream/doc/src/using.qdoc
blob: 174cfcb6306f1a7850f58748dc8d9fa073eb8d93 (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
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of the QtAddOn.JsonStream module of the Qt.
**
** $QT_BEGIN_LICENSE:FDL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia.  For licensing terms and
** conditions see http://qt.digia.com/licensing.  For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Free Documentation License Usage
** Alternatively, this file may be used under the terms of the GNU Free
** Documentation License version 1.3 as published by the Free Software
** Foundation and appearing in the file included in the packaging of
** this file.  Please review the following information to ensure
** the GNU Free Documentation License version 1.3 requirements
** will be met: http://www.gnu.org/copyleft/fdl.html.
** $QT_END_LICENSE$
**
****************************************************************************/

/*!
\title Using JSON stream classes
\target using
\page using.html

\section1 Using the JSON Stream Manager

The JSON Stream classes don't enforce a particular message protocol;
instead, they provide the base classes for implementing your own
client/server protocol using JSON serialization. In this section we
describe how you can use the classes to implement your own protocol.

First, an example of a simple server.  This server uses no authentication
protocol.

\code
  #include <JsonServer>

  void start()
  {
    QString socket = qgetenv("JSON_SOCKET");
    if (socket.isEmpty())
       socket = "/tmp/jsonsocket";
    JsonServer *server = new JsonServer;
    connect(server, SIGNAL(connectionAdded(const QString&)),  ...);
    connect(server, SIGNAL(messageReceived(const QString&, const QJsonObject&)),  ... );
    if (!server->listen(socket))
      qCritical() << Q_FUNC_INFO << "Unable to open socket" << socket;
  }
\endcode

Next, a client that connects to that server:

\code
  #include <JsonClient>

  void start()
  {
    JsonClient *client = new JsonClient();
    connect(client, SIGNAL(messageReceived(const QJsonObject&)), ...);

    QString socketname = qgetenv("JSON_SOCKET");
    if (socketname.isEmpty())
      socketname = "/tmp/jsonsocket";

    if (!client->connectLocal(socketname))
      qCritical() << Q_FUNC_INFO << "Unable to open socket " << socketname;
  }
\endcode

\section2 Authentication


If we want to add an authentication agent to the server, we only need to add
an appropriate JsonAuthority subclass to the server.  For example, to
authenticate clients based on strings tokens, we can modify
the server code as follows:

\code
  JsonAuthority *authority = new JsonTokenAuthority;
  JsonServer *server = new JsonServer(authority);

  // Before clients attempt to connect, set authorization:
  authority->authorize("XXXYYYZZZ", "valid-client");
\endcode

The client code needs to add an authorization token:

\code
  JsonClient *client = new JsonClient("XXXYYYZZZ");
\endcode

In many cases, this authorization token will be passed to the client
when the client application is started.  For example, the client token
could be passed as an environment variable \c {JSONTOKEN}.  Then the
client can be written as:

\code
  QString token = qgetenv("JSONTOKEN");
  JsonClient *client = new JsonClient(token);
\endcode

*/