1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 Alex Trotsenko <alex1973tr@gmail.com>
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 <QtWidgets>
52 #include <QtNetwork>
53 #include <QtAlgorithms>
54
55 #include "server.h"
56 #include "movieprovider.h"
57 #include "timeprovider.h"
58 #include "chatprovider.h"
59
60 #include "../shared/sctpchannels.h"
61
Server(QWidget * parent)62 Server::Server(QWidget *parent)
63 : QDialog(parent)
64 , providers(SctpChannels::NumberOfChannels)
65 {
66 setWindowTitle(tr("Multi-stream Server"));
67
68 sctpServer = new QSctpServer(this);
69 sctpServer->setMaximumChannelCount(NumberOfChannels);
70
71 statusLabel = new QLabel;
72 QPushButton *quitButton = new QPushButton(tr("Quit"));
73
74 providers[SctpChannels::Movie] = new MovieProvider(this);
75 providers[SctpChannels::Time] = new TimeProvider(this);
76 providers[SctpChannels::Chat] = new ChatProvider(this);
77
78 connect(sctpServer, &QSctpServer::newConnection, this, &Server::newConnection);
79 connect(quitButton, &QPushButton::clicked, this, &Server::accept);
80 connect(providers[SctpChannels::Movie], &Provider::writeDatagram, this, &Server::writeDatagram);
81 connect(providers[SctpChannels::Time], &Provider::writeDatagram, this, &Server::writeDatagram);
82 connect(providers[SctpChannels::Chat], &Provider::writeDatagram, this, &Server::writeDatagram);
83
84 QVBoxLayout *mainLayout = new QVBoxLayout;
85 mainLayout->addWidget(statusLabel);
86 mainLayout->addWidget(quitButton);
87 setLayout(mainLayout);
88 }
89
~Server()90 Server::~Server()
91 {
92 qDeleteAll(connections.begin(), connections.end());
93 }
94
exec()95 int Server::exec()
96 {
97 if (!sctpServer->listen()) {
98 QMessageBox::critical(this, windowTitle(),
99 tr("Unable to start the server: %1.")
100 .arg(sctpServer->errorString()));
101 return QDialog::Rejected;
102 }
103
104 QString ipAddress;
105 QList<QHostAddress> ipAddressesList = QNetworkInterface::allAddresses();
106 // use the first non-localhost IPv4 address
107 for (int i = 0; i < ipAddressesList.size(); ++i) {
108 if (ipAddressesList.at(i) != QHostAddress::LocalHost &&
109 ipAddressesList.at(i).toIPv4Address()) {
110 ipAddress = ipAddressesList.at(i).toString();
111 break;
112 }
113 }
114 // if we did not find one, use IPv4 localhost
115 if (ipAddress.isEmpty())
116 ipAddress = QHostAddress(QHostAddress::LocalHost).toString();
117 statusLabel->setText(tr("The server is running on\n\nIP: %1\nport: %2\n\n"
118 "Run the Multi-stream Client example now.")
119 .arg(ipAddress).arg(sctpServer->serverPort()));
120
121 return QDialog::exec();
122 }
123
newConnection()124 void Server::newConnection()
125 {
126 QSctpSocket *connection = sctpServer->nextPendingDatagramConnection();
127
128 connections.append(connection);
129 connect(connection, &QSctpSocket::channelReadyRead, this, &Server::readDatagram);
130 connect(connection, &QSctpSocket::disconnected, this, &Server::clientDisconnected);
131
132 for (Provider *provider : providers)
133 provider->newConnection(*connection);
134 }
135
clientDisconnected()136 void Server::clientDisconnected()
137 {
138 QSctpSocket *connection = static_cast<QSctpSocket *>(sender());
139
140 connections.removeOne(connection);
141 connection->disconnect();
142
143 for (Provider *provider : providers)
144 provider->clientDisconnected(*connection);
145
146 connection->deleteLater();
147 }
148
readDatagram(int channel)149 void Server::readDatagram(int channel)
150 {
151 QSctpSocket *connection = static_cast<QSctpSocket *>(sender());
152
153 connection->setCurrentReadChannel(channel);
154 providers[channel]->readDatagram(*connection, connection->readDatagram().data());
155 }
156
writeDatagram(QSctpSocket * to,const QByteArray & ba)157 void Server::writeDatagram(QSctpSocket *to, const QByteArray &ba)
158 {
159 int channel = providers.indexOf(static_cast<Provider *>(sender()));
160
161 if (to) {
162 to->setCurrentWriteChannel(channel);
163 to->writeDatagram(ba);
164 return;
165 }
166
167 for (QSctpSocket *connection : connections) {
168 connection->setCurrentWriteChannel(channel);
169 connection->writeDatagram(ba);
170 }
171 }
172