2022-05-10 10:06:48 +00:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
#include "fortuneserver.h"
|
|
|
|
#include "fortunethread.h"
|
|
|
|
|
2017-04-14 04:13:52 +00:00
|
|
|
#include <QRandomGenerator>
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
//! [0]
|
|
|
|
FortuneServer::FortuneServer(QObject *parent)
|
|
|
|
: QTcpServer(parent)
|
|
|
|
{
|
|
|
|
fortunes << tr("You've been leading a dog's life. Stay off the furniture.")
|
|
|
|
<< tr("You've got to think about tomorrow.")
|
|
|
|
<< tr("You will be surprised by a loud noise.")
|
|
|
|
<< tr("You will feel hungry again in another hour.")
|
|
|
|
<< tr("You might have mail.")
|
|
|
|
<< tr("You cannot kill time without injuring eternity.")
|
|
|
|
<< tr("Computers are not intelligent. They only think they are.");
|
|
|
|
}
|
|
|
|
//! [0]
|
|
|
|
|
|
|
|
//! [1]
|
2012-01-11 15:06:14 +00:00
|
|
|
void FortuneServer::incomingConnection(qintptr socketDescriptor)
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
2021-03-14 15:56:46 +00:00
|
|
|
QString fortune = fortunes.at(QRandomGenerator::global()->bounded(fortunes.size()));
|
2011-04-27 10:05:43 +00:00
|
|
|
FortuneThread *thread = new FortuneThread(socketDescriptor, fortune, this);
|
2019-11-01 12:21:32 +00:00
|
|
|
connect(thread, &FortuneThread::finished, thread, &FortuneThread::deleteLater);
|
2011-04-27 10:05:43 +00:00
|
|
|
thread->start();
|
|
|
|
}
|
|
|
|
//! [1]
|