0fb9b5b9b02eace9b18024dce4cea246d53c4ca8
[profile/ivi/qtdeclarative.git] / src / plugins / qmltooling / qmldbg_tcp / qtcpserverconnection.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
6 **
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qtcpserverconnection.h"
43
44 #include <QtCore/qplugin.h>
45 #include <QtNetwork/qtcpserver.h>
46 #include <QtNetwork/qtcpsocket.h>
47
48 #include <private/qdeclarativedebugserver_p.h>
49 #include <private/qpacketprotocol_p.h>
50
51 QT_BEGIN_NAMESPACE
52
53 class QTcpServerConnectionPrivate {
54 public:
55     QTcpServerConnectionPrivate();
56
57     int port;
58     bool block;
59     QTcpSocket *socket;
60     QPacketProtocol *protocol;
61     QTcpServer *tcpServer;
62
63     QDeclarativeDebugServer *debugServer;
64 };
65
66 QTcpServerConnectionPrivate::QTcpServerConnectionPrivate() :
67     port(0),
68     block(false),
69     socket(0),
70     protocol(0),
71     tcpServer(0),
72     debugServer(0)
73 {
74 }
75
76 QTcpServerConnection::QTcpServerConnection() :
77     d_ptr(new QTcpServerConnectionPrivate)
78 {
79
80 }
81
82 QTcpServerConnection::~QTcpServerConnection()
83 {
84     delete d_ptr;
85 }
86
87 void QTcpServerConnection::setServer(QDeclarativeDebugServer *server)
88 {
89     Q_D(QTcpServerConnection);
90     d->debugServer = server;
91 }
92
93 bool QTcpServerConnection::isConnected() const
94 {
95     Q_D(const QTcpServerConnection);
96     return d->socket && d->socket->state() == QTcpSocket::ConnectedState;
97 }
98
99 void QTcpServerConnection::send(const QList<QByteArray> &messages)
100 {
101     Q_D(QTcpServerConnection);
102
103     if (!isConnected()
104             || !d->protocol || !d->socket)
105         return;
106
107     foreach (const QByteArray &message, messages) {
108         QPacket pack;
109         pack.writeRawData(message.data(), message.length());
110         d->protocol->send(pack);
111     }
112     d->socket->flush();
113 }
114
115 void QTcpServerConnection::disconnect()
116 {
117     Q_D(QTcpServerConnection);
118
119     // protocol might still be processing packages at this point
120     d->protocol->deleteLater();
121     d->protocol = 0;
122     d->socket->deleteLater();
123     d->socket = 0;
124 }
125
126 bool QTcpServerConnection::waitForMessage()
127 {
128     Q_D(QTcpServerConnection);
129     return d->protocol->waitForReadyRead(-1);
130 }
131
132 void QTcpServerConnection::setPort(int port, bool block)
133 {
134     Q_D(QTcpServerConnection);
135     d->port = port;
136     d->block = block;
137
138     listen();
139     if (block)
140         d->tcpServer->waitForNewConnection(-1);
141 }
142
143 void QTcpServerConnection::listen()
144 {
145     Q_D(QTcpServerConnection);
146
147     d->tcpServer = new QTcpServer(this);
148     QObject::connect(d->tcpServer, SIGNAL(newConnection()), this, SLOT(newConnection()));
149     if (d->tcpServer->listen(QHostAddress::Any, d->port))
150         qWarning("QDeclarativeDebugServer: Waiting for connection on port %d...", d->port);
151     else
152         qWarning("QDeclarativeDebugServer: Unable to listen on port %d", d->port);
153 }
154
155
156 void QTcpServerConnection::readyRead()
157 {
158     Q_D(QTcpServerConnection);
159     if (!d->protocol)
160         return;
161
162     QPacket packet = d->protocol->read();
163
164     QByteArray content = packet.data();
165     d->debugServer->receiveMessage(content);
166 }
167
168 void QTcpServerConnection::newConnection()
169 {
170     Q_D(QTcpServerConnection);
171
172     if (d->socket && d->socket->peerPort()) {
173         qWarning("QDeclarativeDebugServer: Another client is already connected");
174         QTcpSocket *faultyConnection = d->tcpServer->nextPendingConnection();
175         delete faultyConnection;
176         return;
177     }
178
179     delete d->socket;
180     d->socket = d->tcpServer->nextPendingConnection();
181     d->socket->setParent(this);
182     d->protocol = new QPacketProtocol(d->socket, this);
183     QObject::connect(d->protocol, SIGNAL(readyRead()), this, SLOT(readyRead()));
184     QObject::connect(d->protocol, SIGNAL(invalidPacket()), this, SLOT(invalidPacket()));
185
186     if (d->block) {
187         d->protocol->waitForReadyRead(-1);
188     }
189 }
190
191 void QTcpServerConnection::invalidPacket()
192 {
193     qWarning("QDeclarativeDebugServer: Received a corrupted packet! Giving up ...");
194 }
195
196 Q_EXPORT_PLUGIN2(tcpserver, QTcpServerConnection)
197
198 QT_END_NAMESPACE
199