| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** Contact: Qt Software Information ([email protected])
|
|---|
| 5 | **
|
|---|
| 6 | ** This file is part of the examples of the Qt Toolkit.
|
|---|
| 7 | **
|
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 9 | ** Commercial Usage
|
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 13 | ** a written agreement between you and Nokia.
|
|---|
| 14 | **
|
|---|
| 15 | ** GNU Lesser General Public License Usage
|
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 19 | ** packaging of this file. Please review the following information to
|
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 22 | **
|
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain
|
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
|---|
| 26 | ** package.
|
|---|
| 27 | **
|
|---|
| 28 | ** GNU General Public License Usage
|
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 32 | ** packaging of this file. Please review the following information to
|
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
|---|
| 35 | **
|
|---|
| 36 | ** If you are unsure which license is appropriate for your use, please
|
|---|
| 37 | ** contact the sales department at [email protected].
|
|---|
| 38 | ** $QT_END_LICENSE$
|
|---|
| 39 | **
|
|---|
| 40 | ****************************************************************************/
|
|---|
| 41 |
|
|---|
| 42 | #include <QtNetwork>
|
|---|
| 43 |
|
|---|
| 44 | #include "client.h"
|
|---|
| 45 | #include "connection.h"
|
|---|
| 46 | #include "peermanager.h"
|
|---|
| 47 |
|
|---|
| 48 | static const qint32 BroadcastInterval = 2000;
|
|---|
| 49 | static const unsigned broadcastPort = 45000;
|
|---|
| 50 |
|
|---|
| 51 | PeerManager::PeerManager(Client *client)
|
|---|
| 52 | : QObject(client)
|
|---|
| 53 | {
|
|---|
| 54 | this->client = client;
|
|---|
| 55 |
|
|---|
| 56 | QStringList envVariables;
|
|---|
| 57 | envVariables << "USERNAME.*" << "USER.*" << "USERDOMAIN.*"
|
|---|
| 58 | << "HOSTNAME.*" << "DOMAINNAME.*";
|
|---|
| 59 |
|
|---|
| 60 | QStringList environment = QProcess::systemEnvironment();
|
|---|
| 61 | foreach (QString string, envVariables) {
|
|---|
| 62 | int index = environment.indexOf(QRegExp(string));
|
|---|
| 63 | if (index != -1) {
|
|---|
| 64 | QStringList stringList = environment.at(index).split("=");
|
|---|
| 65 | if (stringList.size() == 2) {
|
|---|
| 66 | username = stringList.at(1).toUtf8();
|
|---|
| 67 | break;
|
|---|
| 68 | }
|
|---|
| 69 | }
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | if (username.isEmpty())
|
|---|
| 73 | username = "unknown";
|
|---|
| 74 |
|
|---|
| 75 | updateAddresses();
|
|---|
| 76 | serverPort = 0;
|
|---|
| 77 |
|
|---|
| 78 | broadcastSocket.bind(QHostAddress::Any, broadcastPort, QUdpSocket::ShareAddress
|
|---|
| 79 | | QUdpSocket::ReuseAddressHint);
|
|---|
| 80 | connect(&broadcastSocket, SIGNAL(readyRead()),
|
|---|
| 81 | this, SLOT(readBroadcastDatagram()));
|
|---|
| 82 |
|
|---|
| 83 | broadcastTimer.setInterval(BroadcastInterval);
|
|---|
| 84 | connect(&broadcastTimer, SIGNAL(timeout()),
|
|---|
| 85 | this, SLOT(sendBroadcastDatagram()));
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | void PeerManager::setServerPort(int port)
|
|---|
| 89 | {
|
|---|
| 90 | serverPort = port;
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | QByteArray PeerManager::userName() const
|
|---|
| 94 | {
|
|---|
| 95 | return username;
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | void PeerManager::startBroadcasting()
|
|---|
| 99 | {
|
|---|
| 100 | broadcastTimer.start();
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | bool PeerManager::isLocalHostAddress(const QHostAddress &address)
|
|---|
| 104 | {
|
|---|
| 105 | foreach (QHostAddress localAddress, ipAddresses) {
|
|---|
| 106 | if (address == localAddress)
|
|---|
| 107 | return true;
|
|---|
| 108 | }
|
|---|
| 109 | return false;
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | void PeerManager::sendBroadcastDatagram()
|
|---|
| 113 | {
|
|---|
| 114 | QByteArray datagram(username);
|
|---|
| 115 | datagram.append('@');
|
|---|
| 116 | datagram.append(QByteArray::number(serverPort));
|
|---|
| 117 |
|
|---|
| 118 | bool validBroadcastAddresses = true;
|
|---|
| 119 | foreach (QHostAddress address, broadcastAddresses) {
|
|---|
| 120 | if (broadcastSocket.writeDatagram(datagram, address,
|
|---|
| 121 | broadcastPort) == -1)
|
|---|
| 122 | validBroadcastAddresses = false;
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | if (!validBroadcastAddresses)
|
|---|
| 126 | updateAddresses();
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | void PeerManager::readBroadcastDatagram()
|
|---|
| 130 | {
|
|---|
| 131 | while (broadcastSocket.hasPendingDatagrams()) {
|
|---|
| 132 | QHostAddress senderIp;
|
|---|
| 133 | quint16 senderPort;
|
|---|
| 134 | QByteArray datagram;
|
|---|
| 135 | datagram.resize(broadcastSocket.pendingDatagramSize());
|
|---|
| 136 | if (broadcastSocket.readDatagram(datagram.data(), datagram.size(),
|
|---|
| 137 | &senderIp, &senderPort) == -1)
|
|---|
| 138 | continue;
|
|---|
| 139 |
|
|---|
| 140 | QList<QByteArray> list = datagram.split('@');
|
|---|
| 141 | if (list.size() != 2)
|
|---|
| 142 | continue;
|
|---|
| 143 |
|
|---|
| 144 | int senderServerPort = list.at(1).toInt();
|
|---|
| 145 | if (isLocalHostAddress(senderIp) && senderServerPort == serverPort)
|
|---|
| 146 | continue;
|
|---|
| 147 |
|
|---|
| 148 | if (!client->hasConnection(senderIp)) {
|
|---|
| 149 | Connection *connection = new Connection(this);
|
|---|
| 150 | emit newConnection(connection);
|
|---|
| 151 | connection->connectToHost(senderIp, senderServerPort);
|
|---|
| 152 | }
|
|---|
| 153 | }
|
|---|
| 154 | }
|
|---|
| 155 |
|
|---|
| 156 | void PeerManager::updateAddresses()
|
|---|
| 157 | {
|
|---|
| 158 | broadcastAddresses.clear();
|
|---|
| 159 | ipAddresses.clear();
|
|---|
| 160 | foreach (QNetworkInterface interface, QNetworkInterface::allInterfaces()) {
|
|---|
| 161 | foreach (QNetworkAddressEntry entry, interface.addressEntries()) {
|
|---|
| 162 | QHostAddress broadcastAddress = entry.broadcast();
|
|---|
| 163 | if (broadcastAddress != QHostAddress::Null &&
|
|---|
| 164 | entry.ip() != QHostAddress::LocalHost) {
|
|---|
| 165 | broadcastAddresses << broadcastAddress;
|
|---|
| 166 | ipAddresses << entry.ip();
|
|---|
| 167 | }
|
|---|
| 168 | }
|
|---|
| 169 | }
|
|---|
| 170 | }
|
|---|