| 1 | /**************************************************************************** |
|---|---|
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the test suite of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:GPL-EXCEPT$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT |
| 21 | ** included in the packaging of this file. Please review the following |
| 22 | ** information to ensure the GNU General Public License requirements will |
| 23 | ** be met: https://www.gnu.org/licenses/gpl-3.0.html. |
| 24 | ** |
| 25 | ** $QT_END_LICENSE$ |
| 26 | ** |
| 27 | ****************************************************************************/ |
| 28 | |
| 29 | #include "qnmeapositioninfosourceproxyfactory.h" |
| 30 | #include "../utils/qlocationtestutils_p.h" |
| 31 | |
| 32 | #include <QIODevice> |
| 33 | #include <QTcpServer> |
| 34 | #include <QTcpSocket> |
| 35 | |
| 36 | |
| 37 | QNmeaPositionInfoSourceProxy::QNmeaPositionInfoSourceProxy(QNmeaPositionInfoSource *source, QIODevice *outDevice) |
| 38 | : m_source(source), |
| 39 | m_outDevice(outDevice) |
| 40 | { |
| 41 | } |
| 42 | |
| 43 | QNmeaPositionInfoSourceProxy::~QNmeaPositionInfoSourceProxy() |
| 44 | { |
| 45 | m_outDevice->close(); |
| 46 | delete m_outDevice; |
| 47 | } |
| 48 | |
| 49 | QGeoPositionInfoSource *QNmeaPositionInfoSourceProxy::source() const |
| 50 | { |
| 51 | return m_source; |
| 52 | } |
| 53 | |
| 54 | void QNmeaPositionInfoSourceProxy::feedUpdate(const QDateTime &dt) |
| 55 | { |
| 56 | m_outDevice->write(data: QLocationTestUtils::createRmcSentence(dt).toLatin1()); |
| 57 | } |
| 58 | |
| 59 | void QNmeaPositionInfoSourceProxy::feedBytes(const QByteArray &bytes) |
| 60 | { |
| 61 | m_outDevice->write(data: bytes); |
| 62 | } |
| 63 | |
| 64 | |
| 65 | QNmeaPositionInfoSourceProxyFactory::QNmeaPositionInfoSourceProxyFactory() |
| 66 | : m_server(new QTcpServer(this)) |
| 67 | { |
| 68 | bool b = m_server->listen(address: QHostAddress::LocalHost); |
| 69 | Q_ASSERT(b); |
| 70 | } |
| 71 | |
| 72 | QNmeaPositionInfoSourceProxy *QNmeaPositionInfoSourceProxyFactory::createProxy(QNmeaPositionInfoSource *source) |
| 73 | { |
| 74 | QTcpSocket *client = new QTcpSocket; |
| 75 | client->connectToHost(address: m_server->serverAddress(), port: m_server->serverPort()); |
| 76 | qDebug() << "listening on"<< m_server->serverAddress() << m_server->serverPort(); |
| 77 | bool b = m_server->waitForNewConnection(msec: 15000); |
| 78 | if (!b) |
| 79 | qWarning() << "Server didin't receive new connection"; |
| 80 | b = client->waitForConnected(); |
| 81 | if (!b) |
| 82 | qWarning() << "Client could not connect to server"; |
| 83 | |
| 84 | //QNmeaPositionInfoSource *source = new QNmeaPositionInfoSource(m_mode); |
| 85 | QIODevice *device = m_server->nextPendingConnection(); |
| 86 | if (!device) |
| 87 | qWarning() << "Missing pending connection. Test is going to fail."; |
| 88 | else |
| 89 | qWarning() << "Received pending connection:"<< device << b; |
| 90 | source->setDevice(device); |
| 91 | Q_ASSERT(source->device() != 0); |
| 92 | QNmeaPositionInfoSourceProxy *proxy = new QNmeaPositionInfoSourceProxy(source, client); |
| 93 | proxy->setParent(source); |
| 94 | return proxy; |
| 95 | } |
| 96 |
