diff options
Diffstat (limited to 'examples/network/network-chat')
-rw-r--r-- | examples/network/network-chat/chatdialog.cpp | 141 | ||||
-rw-r--r-- | examples/network/network-chat/chatdialog.h | 70 | ||||
-rw-r--r-- | examples/network/network-chat/chatdialog.ui | 79 | ||||
-rw-r--r-- | examples/network/network-chat/client.cpp | 140 | ||||
-rw-r--r-- | examples/network/network-chat/client.h | 83 | ||||
-rw-r--r-- | examples/network/network-chat/connection.cpp | 276 | ||||
-rw-r--r-- | examples/network/network-chat/connection.h | 108 | ||||
-rw-r--r-- | examples/network/network-chat/main.cpp | 52 | ||||
-rw-r--r-- | examples/network/network-chat/network-chat.pro | 21 | ||||
-rw-r--r-- | examples/network/network-chat/peermanager.cpp | 170 | ||||
-rw-r--r-- | examples/network/network-chat/peermanager.h | 85 | ||||
-rw-r--r-- | examples/network/network-chat/server.cpp | 58 | ||||
-rw-r--r-- | examples/network/network-chat/server.h | 63 |
13 files changed, 1346 insertions, 0 deletions
diff --git a/examples/network/network-chat/chatdialog.cpp b/examples/network/network-chat/chatdialog.cpp new file mode 100644 index 0000000..7e88e5b --- /dev/null +++ b/examples/network/network-chat/chatdialog.cpp @@ -0,0 +1,141 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtGui> + +#include "chatdialog.h" + +ChatDialog::ChatDialog(QWidget *parent) + : QDialog(parent) +{ + setupUi(this); + + lineEdit->setFocusPolicy(Qt::StrongFocus); + textEdit->setFocusPolicy(Qt::NoFocus); + textEdit->setReadOnly(true); + listWidget->setFocusPolicy(Qt::NoFocus); + + connect(lineEdit, SIGNAL(returnPressed()), this, SLOT(returnPressed())); + connect(&client, SIGNAL(newMessage(const QString &, const QString &)), + this, SLOT(appendMessage(const QString &, const QString &))); + connect(&client, SIGNAL(newParticipant(const QString &)), + this, SLOT(newParticipant(const QString &))); + connect(&client, SIGNAL(participantLeft(const QString &)), + this, SLOT(participantLeft(const QString &))); + + myNickName = client.nickName(); + newParticipant(myNickName); + tableFormat.setBorder(0); + QTimer::singleShot(10 * 1000, this, SLOT(showInformation())); +} + +void ChatDialog::appendMessage(const QString &from, const QString &message) +{ + if (from.isEmpty() || message.isEmpty()) + return; + + QTextCursor cursor(textEdit->textCursor()); + cursor.movePosition(QTextCursor::End); + QTextTable *table = cursor.insertTable(1, 2, tableFormat); + table->cellAt(0, 0).firstCursorPosition().insertText("<" + from + "> "); + table->cellAt(0, 1).firstCursorPosition().insertText(message); + QScrollBar *bar = textEdit->verticalScrollBar(); + bar->setValue(bar->maximum()); +} + +void ChatDialog::returnPressed() +{ + QString text = lineEdit->text(); + if (text.isEmpty()) + return; + + if (text.startsWith(QChar('/'))) { + QColor color = textEdit->textColor(); + textEdit->setTextColor(Qt::red); + textEdit->append(tr("! Unknown command: %1") + .arg(text.left(text.indexOf(' ')))); + textEdit->setTextColor(color); + } else { + client.sendMessage(text); + appendMessage(myNickName, text); + } + + lineEdit->clear(); +} + +void ChatDialog::newParticipant(const QString &nick) +{ + if (nick.isEmpty()) + return; + + QColor color = textEdit->textColor(); + textEdit->setTextColor(Qt::gray); + textEdit->append(tr("* %1 has joined").arg(nick)); + textEdit->setTextColor(color); + listWidget->addItem(nick); +} + +void ChatDialog::participantLeft(const QString &nick) +{ + if (nick.isEmpty()) + return; + + QList<QListWidgetItem *> items = listWidget->findItems(nick, + Qt::MatchExactly); + if (items.isEmpty()) + return; + + delete items.at(0); + QColor color = textEdit->textColor(); + textEdit->setTextColor(Qt::gray); + textEdit->append(tr("* %1 has left").arg(nick)); + textEdit->setTextColor(color); +} + +void ChatDialog::showInformation() +{ + if (listWidget->count() == 1) { + QMessageBox::information(this, tr("Chat"), + tr("Launch several instances of this " + "program on your local network and " + "start chatting!")); + } +} diff --git a/examples/network/network-chat/chatdialog.h b/examples/network/network-chat/chatdialog.h new file mode 100644 index 0000000..552d794 --- /dev/null +++ b/examples/network/network-chat/chatdialog.h @@ -0,0 +1,70 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef CHATDIALOG_H +#define CHATDIALOG_H + +#include "ui_chatdialog.h" +#include "client.h" + +class ChatDialog : public QDialog, private Ui::ChatDialog +{ + Q_OBJECT + +public: + ChatDialog(QWidget *parent = 0); + +public slots: + void appendMessage(const QString &from, const QString &message); + +private slots: + void returnPressed(); + void newParticipant(const QString &nick); + void participantLeft(const QString &nick); + void showInformation(); + +private: + Client client; + QString myNickName; + QTextTableFormat tableFormat; +}; + +#endif diff --git a/examples/network/network-chat/chatdialog.ui b/examples/network/network-chat/chatdialog.ui new file mode 100644 index 0000000..c85e0d0 --- /dev/null +++ b/examples/network/network-chat/chatdialog.ui @@ -0,0 +1,79 @@ +<ui version="4.0" > + <class>ChatDialog</class> + <widget class="QDialog" name="ChatDialog" > + <property name="geometry" > + <rect> + <x>0</x> + <y>0</y> + <width>513</width> + <height>349</height> + </rect> + </property> + <property name="windowTitle" > + <string>Chat</string> + </property> + <layout class="QVBoxLayout" > + <property name="margin" > + <number>9</number> + </property> + <property name="spacing" > + <number>6</number> + </property> + <item> + <layout class="QHBoxLayout" > + <property name="margin" > + <number>0</number> + </property> + <property name="spacing" > + <number>6</number> + </property> + <item> + <widget class="QTextEdit" name="textEdit" > + <property name="focusPolicy" > + <enum>Qt::NoFocus</enum> + </property> + <property name="readOnly" > + <bool>true</bool> + </property> + </widget> + </item> + <item> + <widget class="QListWidget" name="listWidget" > + <property name="maximumSize" > + <size> + <width>180</width> + <height>16777215</height> + </size> + </property> + <property name="focusPolicy" > + <enum>Qt::NoFocus</enum> + </property> + </widget> + </item> + </layout> + </item> + <item> + <layout class="QHBoxLayout" > + <property name="margin" > + <number>0</number> + </property> + <property name="spacing" > + <number>6</number> + </property> + <item> + <widget class="QLabel" name="label" > + <property name="text" > + <string>Message:</string> + </property> + </widget> + </item> + <item> + <widget class="QLineEdit" name="lineEdit" /> + </item> + </layout> + </item> + </layout> + </widget> + <resources/> + <connections/> +</ui> diff --git a/examples/network/network-chat/client.cpp b/examples/network/network-chat/client.cpp new file mode 100644 index 0000000..d5931a6 --- /dev/null +++ b/examples/network/network-chat/client.cpp @@ -0,0 +1,140 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtNetwork> + +#include "client.h" +#include "connection.h" +#include "peermanager.h" + +Client::Client() +{ + peerManager = new PeerManager(this); + peerManager->setServerPort(server.serverPort()); + peerManager->startBroadcasting(); + + QObject::connect(peerManager, SIGNAL(newConnection(Connection *)), + this, SLOT(newConnection(Connection *))); + QObject::connect(&server, SIGNAL(newConnection(Connection *)), + this, SLOT(newConnection(Connection *))); +} + +void Client::sendMessage(const QString &message) +{ + if (message.isEmpty()) + return; + + QList<Connection *> connections = peers.values(); + foreach (Connection *connection, connections) + connection->sendMessage(message); +} + +QString Client::nickName() const +{ + return QString(peerManager->userName()) + "@" + QHostInfo::localHostName() + + ":" + QString::number(server.serverPort()); +} + +bool Client::hasConnection(const QHostAddress &senderIp, int senderPort) const +{ + if (senderPort == -1) + return peers.contains(senderIp); + + if (!peers.contains(senderIp)) + return false; + + QList<Connection *> connections = peers.values(senderIp); + foreach (Connection *connection, connections) { + if (connection->peerPort() == senderPort) + return true; + } + + return false; +} + +void Client::newConnection(Connection *connection) +{ + connection->setGreetingMessage(peerManager->userName()); + + connect(connection, SIGNAL(error(QAbstractSocket::SocketError)), + this, SLOT(connectionError(QAbstractSocket::SocketError))); + connect(connection, SIGNAL(disconnected()), this, SLOT(disconnected())); + connect(connection, SIGNAL(readyForUse()), this, SLOT(readyForUse())); +} + +void Client::readyForUse() +{ + Connection *connection = qobject_cast<Connection *>(sender()); + if (!connection || hasConnection(connection->peerAddress(), + connection->peerPort())) + return; + + connect(connection, SIGNAL(newMessage(const QString &, const QString &)), + this, SIGNAL(newMessage(const QString &, const QString &))); + + peers.insert(connection->peerAddress(), connection); + QString nick = connection->name(); + if (!nick.isEmpty()) + emit newParticipant(nick); +} + +void Client::disconnected() +{ + if (Connection *connection = qobject_cast<Connection *>(sender())) + removeConnection(connection); +} + +void Client::connectionError(QAbstractSocket::SocketError /* socketError */) +{ + if (Connection *connection = qobject_cast<Connection *>(sender())) + removeConnection(connection); +} + +void Client::removeConnection(Connection *connection) +{ + if (peers.contains(connection->peerAddress())) { + peers.remove(connection->peerAddress()); + QString nick = connection->name(); + if (!nick.isEmpty()) + emit participantLeft(nick); + } + connection->deleteLater(); +} diff --git a/examples/network/network-chat/client.h b/examples/network/network-chat/client.h new file mode 100644 index 0000000..307df46 --- /dev/null +++ b/examples/network/network-chat/client.h @@ -0,0 +1,83 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef CLIENT_H +#define CLIENT_H + +#include <QAbstractSocket> +#include <QHash> +#include <QHostAddress> + +#include "server.h" + +class PeerManager; + +class Client : public QObject +{ + Q_OBJECT + +public: + Client(); + + void sendMessage(const QString &message); + QString nickName() const; + bool hasConnection(const QHostAddress &senderIp, int senderPort = -1) const; + +signals: + void newMessage(const QString &from, const QString &message); + void newParticipant(const QString &nick); + void participantLeft(const QString &nick); + +private slots: + void newConnection(Connection *connection); + void connectionError(QAbstractSocket::SocketError socketError); + void disconnected(); + void readyForUse(); + +private: + void removeConnection(Connection *connection); + + PeerManager *peerManager; + Server server; + QMultiHash<QHostAddress, Connection *> peers; +}; + +#endif diff --git a/examples/network/network-chat/connection.cpp b/examples/network/network-chat/connection.cpp new file mode 100644 index 0000000..116ca3a --- /dev/null +++ b/examples/network/network-chat/connection.cpp @@ -0,0 +1,276 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "connection.h" + +#include <QtNetwork> + +static const int TransferTimeout = 30 * 1000; +static const int PongTimeout = 60 * 1000; +static const int PingInterval = 5 * 1000; +static const char SeparatorToken = ' '; + +Connection::Connection(QObject *parent) + : QTcpSocket(parent) +{ + greetingMessage = tr("undefined"); + username = tr("unknown"); + state = WaitingForGreeting; + currentDataType = Undefined; + numBytesForCurrentDataType = -1; + transferTimerId = 0; + isGreetingMessageSent = false; + pingTimer.setInterval(PingInterval); + + QObject::connect(this, SIGNAL(readyRead()), this, SLOT(processReadyRead())); + QObject::connect(this, SIGNAL(disconnected()), &pingTimer, SLOT(stop())); + QObject::connect(&pingTimer, SIGNAL(timeout()), this, SLOT(sendPing())); + QObject::connect(this, SIGNAL(connected()), + this, SLOT(sendGreetingMessage())); +} + +QString Connection::name() const +{ + return username; +} + +void Connection::setGreetingMessage(const QString &message) +{ + greetingMessage = message; +} + +bool Connection::sendMessage(const QString &message) +{ + if (message.isEmpty()) + return false; + + QByteArray msg = message.toUtf8(); + QByteArray data = "MESSAGE " + QByteArray::number(msg.size()) + " " + msg; + return write(data) == data.size(); +} + +void Connection::timerEvent(QTimerEvent *timerEvent) +{ + if (timerEvent->timerId() == transferTimerId) { + abort(); + killTimer(transferTimerId); + transferTimerId = 0; + } +} + +void Connection::processReadyRead() +{ + if (state == WaitingForGreeting) { + if (!readProtocolHeader()) + return; + if (currentDataType != Greeting) { + abort(); + return; + } + state = ReadingGreeting; + } + + if (state == ReadingGreeting) { + if (!hasEnoughData()) + return; + + buffer = read(numBytesForCurrentDataType); + if (buffer.size() != numBytesForCurrentDataType) { + abort(); + return; + } + + username = QString(buffer) + "@" + peerAddress().toString() + ":" + + QString::number(peerPort()); + currentDataType = Undefined; + numBytesForCurrentDataType = 0; + buffer.clear(); + + if (!isValid()) { + abort(); + return; + } + + if (!isGreetingMessageSent) + sendGreetingMessage(); + + pingTimer.start(); + pongTime.start(); + state = ReadyForUse; + emit readyForUse(); + } + + do { + if (currentDataType == Undefined) { + if (!readProtocolHeader()) + return; + } + if (!hasEnoughData()) + return; + processData(); + } while (bytesAvailable() > 0); +} + +void Connection::sendPing() +{ + if (pongTime.elapsed() > PongTimeout) { + abort(); + return; + } + + write("PING 1 p"); +} + +void Connection::sendGreetingMessage() +{ + QByteArray greeting = greetingMessage.toUtf8(); + QByteArray data = "GREETING " + QByteArray::number(greeting.size()) + " " + greeting; + if (write(data) == data.size()) + isGreetingMessageSent = true; +} + +int Connection::readDataIntoBuffer(int maxSize) +{ + if (maxSize > MaxBufferSize) + return 0; + + int numBytesBeforeRead = buffer.size(); + if (numBytesBeforeRead == MaxBufferSize) { + abort(); + return 0; + } + + while (bytesAvailable() > 0 && buffer.size() < maxSize) { + buffer.append(read(1)); + if (buffer.endsWith(SeparatorToken)) + break; + } + return buffer.size() - numBytesBeforeRead; +} + +int Connection::dataLengthForCurrentDataType() +{ + if (bytesAvailable() <= 0 || readDataIntoBuffer() <= 0 + || !buffer.endsWith(SeparatorToken)) + return 0; + + buffer.chop(1); + int number = buffer.toInt(); + buffer.clear(); + return number; +} + +bool Connection::readProtocolHeader() +{ + if (transferTimerId) { + killTimer(transferTimerId); + transferTimerId = 0; + } + + if (readDataIntoBuffer() <= 0) { + transferTimerId = startTimer(TransferTimeout); + return false; + } + + if (buffer == "PING ") { + currentDataType = Ping; + } else if (buffer == "PONG ") { + currentDataType = Pong; + } else if (buffer == "MESSAGE ") { + currentDataType = PlainText; + } else if (buffer == "GREETING ") { + currentDataType = Greeting; + } else { + currentDataType = Undefined; + abort(); + return false; + } + + buffer.clear(); + numBytesForCurrentDataType = dataLengthForCurrentDataType(); + return true; +} + +bool Connection::hasEnoughData() +{ + if (transferTimerId) { + QObject::killTimer(transferTimerId); + transferTimerId = 0; + } + + if (numBytesForCurrentDataType <= 0) + numBytesForCurrentDataType = dataLengthForCurrentDataType(); + + if (bytesAvailable() < numBytesForCurrentDataType + || numBytesForCurrentDataType <= 0) { + transferTimerId = startTimer(TransferTimeout); + return false; + } + + return true; +} + +void Connection::processData() +{ + buffer = read(numBytesForCurrentDataType); + if (buffer.size() != numBytesForCurrentDataType) { + abort(); + return; + } + + switch (currentDataType) { + case PlainText: + emit newMessage(username, QString::fromUtf8(buffer)); + break; + case Ping: + write("PONG 1 p"); + break; + case Pong: + pongTime.restart(); + break; + default: + break; + } + + currentDataType = Undefined; + numBytesForCurrentDataType = 0; + buffer.clear(); +} diff --git a/examples/network/network-chat/connection.h b/examples/network/network-chat/connection.h new file mode 100644 index 0000000..0784154 --- /dev/null +++ b/examples/network/network-chat/connection.h @@ -0,0 +1,108 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef CONNECTION_H +#define CONNECTION_H + +#include <QHostAddress> +#include <QString> +#include <QTcpSocket> +#include <QTime> +#include <QTimer> + +static const int MaxBufferSize = 1024000; + +class Connection : public QTcpSocket +{ + Q_OBJECT + +public: + enum ConnectionState { + WaitingForGreeting, + ReadingGreeting, + ReadyForUse + }; + enum DataType { + PlainText, + Ping, + Pong, + Greeting, + Undefined + }; + + Connection(QObject *parent = 0); + + QString name() const; + void setGreetingMessage(const QString &message); + bool sendMessage(const QString &message); + +signals: + void readyForUse(); + void newMessage(const QString &from, const QString &message); + +protected: + void timerEvent(QTimerEvent *timerEvent); + +private slots: + void processReadyRead(); + void sendPing(); + void sendGreetingMessage(); + +private: + int readDataIntoBuffer(int maxSize = MaxBufferSize); + int dataLengthForCurrentDataType(); + bool readProtocolHeader(); + bool hasEnoughData(); + void processData(); + + QString greetingMessage; + QString username; + QTimer pingTimer; + QTime pongTime; + QByteArray buffer; + ConnectionState state; + DataType currentDataType; + int numBytesForCurrentDataType; + int transferTimerId; + bool isGreetingMessageSent; +}; + +#endif diff --git a/examples/network/network-chat/main.cpp b/examples/network/network-chat/main.cpp new file mode 100644 index 0000000..ffe28c9 --- /dev/null +++ b/examples/network/network-chat/main.cpp @@ -0,0 +1,52 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QApplication> + +#include "chatdialog.h" + +int main(int argc, char *argv[]) +{ + QApplication app(argc, argv); + ChatDialog dialog; + dialog.show(); + return app.exec(); +} diff --git a/examples/network/network-chat/network-chat.pro b/examples/network/network-chat/network-chat.pro new file mode 100644 index 0000000..6967228 --- /dev/null +++ b/examples/network/network-chat/network-chat.pro @@ -0,0 +1,21 @@ +HEADERS = chatdialog.h \ + client.h \ + connection.h \ + peermanager.h \ + server.h +SOURCES = chatdialog.cpp \ + client.cpp \ + connection.cpp \ + main.cpp \ + peermanager.cpp \ + server.cpp +FORMS = chatdialog.ui +QT += network + +# install +target.path = $$[QT_INSTALL_EXAMPLES]/network/network-chat +sources.files = $$SOURCES $$HEADERS $$RESOURCES $$FORMS network-chat.pro *.chat +sources.path = $$[QT_INSTALL_EXAMPLES]/network/network-chat +INSTALLS += target sources + +include($$QT_SOURCE_TREE/examples/examplebase.pri) diff --git a/examples/network/network-chat/peermanager.cpp b/examples/network/network-chat/peermanager.cpp new file mode 100644 index 0000000..4ed4c5a --- /dev/null +++ b/examples/network/network-chat/peermanager.cpp @@ -0,0 +1,170 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtNetwork> + +#include "client.h" +#include "connection.h" +#include "peermanager.h" + +static const qint32 BroadcastInterval = 2000; +static const unsigned broadcastPort = 45000; + +PeerManager::PeerManager(Client *client) + : QObject(client) +{ + this->client = client; + + QStringList envVariables; + envVariables << "USERNAME.*" << "USER.*" << "USERDOMAIN.*" + << "HOSTNAME.*" << "DOMAINNAME.*"; + + QStringList environment = QProcess::systemEnvironment(); + foreach (QString string, envVariables) { + int index = environment.indexOf(QRegExp(string)); + if (index != -1) { + QStringList stringList = environment.at(index).split("="); + if (stringList.size() == 2) { + username = stringList.at(1).toUtf8(); + break; + } + } + } + + if (username.isEmpty()) + username = "unknown"; + + updateAddresses(); + serverPort = 0; + + broadcastSocket.bind(QHostAddress::Any, broadcastPort, QUdpSocket::ShareAddress + | QUdpSocket::ReuseAddressHint); + connect(&broadcastSocket, SIGNAL(readyRead()), + this, SLOT(readBroadcastDatagram())); + + broadcastTimer.setInterval(BroadcastInterval); + connect(&broadcastTimer, SIGNAL(timeout()), + this, SLOT(sendBroadcastDatagram())); +} + +void PeerManager::setServerPort(int port) +{ + serverPort = port; +} + +QByteArray PeerManager::userName() const +{ + return username; +} + +void PeerManager::startBroadcasting() +{ + broadcastTimer.start(); +} + +bool PeerManager::isLocalHostAddress(const QHostAddress &address) +{ + foreach (QHostAddress localAddress, ipAddresses) { + if (address == localAddress) + return true; + } + return false; +} + +void PeerManager::sendBroadcastDatagram() +{ + QByteArray datagram(username); + datagram.append('@'); + datagram.append(QByteArray::number(serverPort)); + + bool validBroadcastAddresses = true; + foreach (QHostAddress address, broadcastAddresses) { + if (broadcastSocket.writeDatagram(datagram, address, + broadcastPort) == -1) + validBroadcastAddresses = false; + } + + if (!validBroadcastAddresses) + updateAddresses(); +} + +void PeerManager::readBroadcastDatagram() +{ + while (broadcastSocket.hasPendingDatagrams()) { + QHostAddress senderIp; + quint16 senderPort; + QByteArray datagram; + datagram.resize(broadcastSocket.pendingDatagramSize()); + if (broadcastSocket.readDatagram(datagram.data(), datagram.size(), + &senderIp, &senderPort) == -1) + continue; + + QList<QByteArray> list = datagram.split('@'); + if (list.size() != 2) + continue; + + int senderServerPort = list.at(1).toInt(); + if (isLocalHostAddress(senderIp) && senderServerPort == serverPort) + continue; + + if (!client->hasConnection(senderIp)) { + Connection *connection = new Connection(this); + emit newConnection(connection); + connection->connectToHost(senderIp, senderServerPort); + } + } +} + +void PeerManager::updateAddresses() +{ + broadcastAddresses.clear(); + ipAddresses.clear(); + foreach (QNetworkInterface interface, QNetworkInterface::allInterfaces()) { + foreach (QNetworkAddressEntry entry, interface.addressEntries()) { + QHostAddress broadcastAddress = entry.broadcast(); + if (broadcastAddress != QHostAddress::Null && + entry.ip() != QHostAddress::LocalHost) { + broadcastAddresses << broadcastAddress; + ipAddresses << entry.ip(); + } + } + } +} diff --git a/examples/network/network-chat/peermanager.h b/examples/network/network-chat/peermanager.h new file mode 100644 index 0000000..a729329 --- /dev/null +++ b/examples/network/network-chat/peermanager.h @@ -0,0 +1,85 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef PEERMANAGER_H +#define PEERMANAGER_H + +#include <QByteArray> +#include <QList> +#include <QObject> +#include <QTimer> +#include <QUdpSocket> + +class Client; +class Connection; + +class PeerManager : public QObject +{ + Q_OBJECT + +public: + PeerManager(Client *client); + + void setServerPort(int port); + QByteArray userName() const; + void startBroadcasting(); + bool isLocalHostAddress(const QHostAddress &address); + +signals: + void newConnection(Connection *connection); + +private slots: + void sendBroadcastDatagram(); + void readBroadcastDatagram(); + +private: + void updateAddresses(); + + Client *client; + QList<QHostAddress> broadcastAddresses; + QList<QHostAddress> ipAddresses; + QUdpSocket broadcastSocket; + QTimer broadcastTimer; + QByteArray username; + int serverPort; +}; + +#endif diff --git a/examples/network/network-chat/server.cpp b/examples/network/network-chat/server.cpp new file mode 100644 index 0000000..26f1e70 --- /dev/null +++ b/examples/network/network-chat/server.cpp @@ -0,0 +1,58 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtNetwork> + +#include "connection.h" +#include "server.h" + +Server::Server(QObject *parent) + : QTcpServer(parent) +{ + listen(QHostAddress::Any); +} + +void Server::incomingConnection(int socketDescriptor) +{ + Connection *connection = new Connection(this); + connection->setSocketDescriptor(socketDescriptor); + emit newConnection(connection); +} diff --git a/examples/network/network-chat/server.h b/examples/network/network-chat/server.h new file mode 100644 index 0000000..d297693 --- /dev/null +++ b/examples/network/network-chat/server.h @@ -0,0 +1,63 @@ +/**************************************************************************** +** +** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). +** Contact: Qt Software Information (qt-info@nokia.com) +** +** This file is part of the examples of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the either Technology Preview License Agreement or the +** Beta Release License Agreement. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain +** additional rights. These rights are described in the Nokia Qt LGPL +** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this +** package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** If you are unsure which license is appropriate for your use, please +** contact the sales department at qt-sales@nokia.com. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef SERVER_H +#define SERVER_H + +#include <QTcpServer> + +class Connection; + +class Server : public QTcpServer +{ + Q_OBJECT + +public: + Server(QObject *parent = 0); + +signals: + void newConnection(Connection *connection); + +protected: + void incomingConnection(int socketDescriptor); +}; + +#endif |