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