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 examples of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:BSD$ |
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 | ** BSD License Usage |
18 | ** Alternatively, you may use this file under the terms of the BSD license |
19 | ** as follows: |
20 | ** |
21 | ** "Redistribution and use in source and binary forms, with or without |
22 | ** modification, are permitted provided that the following conditions are |
23 | ** met: |
24 | ** * Redistributions of source code must retain the above copyright |
25 | ** notice, this list of conditions and the following disclaimer. |
26 | ** * Redistributions in binary form must reproduce the above copyright |
27 | ** notice, this list of conditions and the following disclaimer in |
28 | ** the documentation and/or other materials provided with the |
29 | ** distribution. |
30 | ** * Neither the name of The Qt Company Ltd nor the names of its |
31 | ** contributors may be used to endorse or promote products derived |
32 | ** from this software without specific prior written permission. |
33 | ** |
34 | ** |
35 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
36 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
37 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
38 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
39 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
40 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
41 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
42 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
43 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
44 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
45 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
46 | ** |
47 | ** $QT_END_LICENSE$ |
48 | ** |
49 | ****************************************************************************/ |
50 | |
51 | #include "server.h" |
52 | |
53 | #include <QtWidgets> |
54 | #include <QtNetwork> |
55 | |
56 | Server::Server(QWidget *parent) |
57 | : QDialog(parent) |
58 | { |
59 | setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint); |
60 | |
61 | server = new QLocalServer(this); |
62 | if (!server->listen("fortune" )) { |
63 | QMessageBox::critical(this, tr("Local Fortune Server" ), |
64 | tr("Unable to start the server: %1." ) |
65 | .arg(server->errorString())); |
66 | close(); |
67 | return; |
68 | } |
69 | |
70 | QLabel *statusLabel = new QLabel; |
71 | statusLabel->setWordWrap(true); |
72 | statusLabel->setText(tr("The server is running.\n" |
73 | "Run the Local Fortune Client example now." )); |
74 | |
75 | fortunes << tr("You've been leading a dog's life. Stay off the furniture." ) |
76 | << tr("You've got to think about tomorrow." ) |
77 | << tr("You will be surprised by a loud noise." ) |
78 | << tr("You will feel hungry again in another hour." ) |
79 | << tr("You might have mail." ) |
80 | << tr("You cannot kill time without injuring eternity." ) |
81 | << tr("Computers are not intelligent. They only think they are." ); |
82 | |
83 | QPushButton *quitButton = new QPushButton(tr("Quit" )); |
84 | quitButton->setAutoDefault(false); |
85 | connect(quitButton, &QPushButton::clicked, this, &Server::close); |
86 | connect(server, &QLocalServer::newConnection, this, &Server::sendFortune); |
87 | |
88 | QHBoxLayout *buttonLayout = new QHBoxLayout; |
89 | buttonLayout->addStretch(1); |
90 | buttonLayout->addWidget(quitButton); |
91 | buttonLayout->addStretch(1); |
92 | |
93 | QVBoxLayout *mainLayout = new QVBoxLayout(this); |
94 | mainLayout->addWidget(statusLabel); |
95 | mainLayout->addLayout(buttonLayout); |
96 | |
97 | setWindowTitle(QGuiApplication::applicationDisplayName()); |
98 | } |
99 | |
100 | void Server::sendFortune() |
101 | { |
102 | QByteArray block; |
103 | QDataStream out(&block, QIODevice::WriteOnly); |
104 | out.setVersion(QDataStream::Qt_5_10); |
105 | const int fortuneIndex = QRandomGenerator::global()->bounded(0, fortunes.size()); |
106 | const QString &message = fortunes.at(fortuneIndex); |
107 | out << quint32(message.size()); |
108 | out << message; |
109 | |
110 | QLocalSocket *clientConnection = server->nextPendingConnection(); |
111 | connect(clientConnection, &QLocalSocket::disconnected, |
112 | clientConnection, &QLocalSocket::deleteLater); |
113 | |
114 | clientConnection->write(block); |
115 | clientConnection->flush(); |
116 | clientConnection->disconnectFromServer(); |
117 | } |
118 | |