1#!/usr/bin/env python
2
3
4#############################################################################
5##
6## Copyright (C) 2013 Riverbank Computing Limited.
7## Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
8## All rights reserved.
9##
10## This file is part of the examples of PyQt.
11##
12## $QT_BEGIN_LICENSE:BSD$
13## You may use this file under the terms of the BSD license as follows:
14##
15## "Redistribution and use in source and binary forms, with or without
16## modification, are permitted provided that the following conditions are
17## met:
18##   * Redistributions of source code must retain the above copyright
19##     notice, this list of conditions and the following disclaimer.
20##   * Redistributions in binary form must reproduce the above copyright
21##     notice, this list of conditions and the following disclaimer in
22##     the documentation and/or other materials provided with the
23##     distribution.
24##   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
25##     the names of its contributors may be used to endorse or promote
26##     products derived from this software without specific prior written
27##     permission.
28##
29## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30## "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31## LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32## A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33## OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34## SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35## LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36## DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37## THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38## (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39## OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
40## $QT_END_LICENSE$
41##
42#############################################################################
43
44
45import random
46
47from PyQt5.QtCore import QByteArray, QDataStream, QIODevice, QSettings
48from PyQt5.QtWidgets import (QApplication, QDialog, QHBoxLayout, QLabel,
49        QMessageBox, QPushButton, QVBoxLayout)
50from PyQt5.QtNetwork import (QHostAddress, QNetworkConfiguration,
51        QNetworkConfigurationManager, QNetworkInterface, QNetworkSession,
52        QTcpServer)
53
54
55class Server(QDialog):
56    FORTUNES = (
57        "You've been leading a dog's life. Stay off the furniture.",
58        "You've got to think about tomorrow.",
59        "You will be surprised by a loud noise.",
60        "You will feel hungry again in another hour.",
61        "You might have mail.",
62        "You cannot kill time without injuring eternity.",
63        "Computers are not intelligent. They only think they are.")
64
65    def __init__(self, parent=None):
66        super(Server, self).__init__(parent)
67
68        self.tcpServer = None
69        self.networkSession = None
70
71        self.statusLabel = QLabel()
72        quitButton = QPushButton("Quit")
73        quitButton.setAutoDefault(False)
74
75        manager = QNetworkConfigurationManager()
76        if manager.capabilities() & QNetworkConfigurationManager.NetworkSessionRequired:
77            settings = QSettings(QSettings.UserScope, 'QtProject')
78            settings.beginGroup('QtNetwork')
79            id = settings.value('DefaultNetworkConfiguration', '')
80            settings.endGroup()
81
82            config = manager.configurationFromIdentifier(id)
83            if config.state() & QNetworkConfiguration.Discovered == 0:
84                config = manager.defaultConfiguration()
85
86            self.networkSession = QNetworkSession(config, self)
87            self.networkSession.opened.connect(self.sessionOpened)
88
89            self.statusLabel.setText("Opening network session.")
90            self.networkSession.open()
91        else:
92            self.sessionOpened()
93
94        quitButton.clicked.connect(self.close)
95        self.tcpServer.newConnection.connect(self.sendFortune)
96
97        buttonLayout = QHBoxLayout()
98        buttonLayout.addStretch(1)
99        buttonLayout.addWidget(quitButton)
100        buttonLayout.addStretch(1)
101
102        mainLayout = QVBoxLayout()
103        mainLayout.addWidget(self.statusLabel)
104        mainLayout.addLayout(buttonLayout)
105        self.setLayout(mainLayout)
106
107        self.setWindowTitle("Fortune Server")
108
109    def sessionOpened(self):
110        if self.networkSession is not None:
111            config = self.networkSession.configuration()
112
113            if config.type() == QNetworkConfiguration.UserChoice:
114                id = self.networkSession.sessionProperty('UserChoiceConfiguration')
115            else:
116                id = config.identifier()
117
118            settings = QSettings(QSettings.UserScope, 'QtProject')
119            settings.beginGroup('QtNetwork')
120            settings.setValue('DefaultNetworkConfiguration', id)
121            settings.endGroup();
122
123        self.tcpServer = QTcpServer(self)
124        if not self.tcpServer.listen():
125            QMessageBox.critical(self, "Fortune Server",
126                    "Unable to start the server: %s." % self.tcpServer.errorString())
127            self.close()
128            return
129
130        for ipAddress in QNetworkInterface.allAddresses():
131            if ipAddress != QHostAddress.LocalHost and ipAddress.toIPv4Address() != 0:
132                break
133        else:
134            ipAddress = QHostAddress(QHostAddress.LocalHost)
135
136        ipAddress = ipAddress.toString()
137
138        self.statusLabel.setText("The server is running on\n\nIP: %s\nport %d\n\n"
139                "Run the Fortune Client example now." % (ipAddress, self.tcpServer.serverPort()))
140
141    def sendFortune(self):
142        fortune = self.FORTUNES[random.randint(0, len(self.FORTUNES) - 1)]
143
144        block = QByteArray()
145        out = QDataStream(block, QIODevice.WriteOnly)
146        out.setVersion(QDataStream.Qt_4_0)
147        out.writeUInt16(0)
148        out.writeQString(fortune)
149        out.device().seek(0)
150        out.writeUInt16(block.size() - 2)
151
152        clientConnection = self.tcpServer.nextPendingConnection()
153        clientConnection.disconnected.connect(clientConnection.deleteLater)
154
155        clientConnection.write(block)
156        clientConnection.disconnectFromHost()
157
158
159if __name__ == '__main__':
160
161    import sys
162
163    app = QApplication(sys.argv)
164    server = Server()
165    random.seed(None)
166    sys.exit(server.exec_())
167