1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the examples of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:BSD$
|
---|
10 | ** You may use this file under the terms of the BSD license as follows:
|
---|
11 | **
|
---|
12 | ** "Redistribution and use in source and binary forms, with or without
|
---|
13 | ** modification, are permitted provided that the following conditions are
|
---|
14 | ** met:
|
---|
15 | ** * Redistributions of source code must retain the above copyright
|
---|
16 | ** notice, this list of conditions and the following disclaimer.
|
---|
17 | ** * Redistributions in binary form must reproduce the above copyright
|
---|
18 | ** notice, this list of conditions and the following disclaimer in
|
---|
19 | ** the documentation and/or other materials provided with the
|
---|
20 | ** distribution.
|
---|
21 | ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
|
---|
22 | ** the names of its contributors may be used to endorse or promote
|
---|
23 | ** products derived from this software without specific prior written
|
---|
24 | ** permission.
|
---|
25 | **
|
---|
26 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
27 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
28 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
29 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
---|
30 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
31 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
32 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
33 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
34 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
35 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
36 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
|
---|
37 | ** $QT_END_LICENSE$
|
---|
38 | **
|
---|
39 | ****************************************************************************/
|
---|
40 |
|
---|
41 | #include <QtNetwork>
|
---|
42 |
|
---|
43 | #include "client.h"
|
---|
44 | #include "connection.h"
|
---|
45 | #include "peermanager.h"
|
---|
46 |
|
---|
47 | static const qint32 BroadcastInterval = 2000;
|
---|
48 | static const unsigned broadcastPort = 45000;
|
---|
49 |
|
---|
50 | PeerManager::PeerManager(Client *client)
|
---|
51 | : QObject(client)
|
---|
52 | {
|
---|
53 | this->client = client;
|
---|
54 |
|
---|
55 | QStringList envVariables;
|
---|
56 | envVariables << "USERNAME.*" << "USER.*" << "USERDOMAIN.*"
|
---|
57 | << "HOSTNAME.*" << "DOMAINNAME.*";
|
---|
58 |
|
---|
59 | QStringList environment = QProcess::systemEnvironment();
|
---|
60 | foreach (QString string, envVariables) {
|
---|
61 | int index = environment.indexOf(QRegExp(string));
|
---|
62 | if (index != -1) {
|
---|
63 | QStringList stringList = environment.at(index).split('=');
|
---|
64 | if (stringList.size() == 2) {
|
---|
65 | username = stringList.at(1).toUtf8();
|
---|
66 | break;
|
---|
67 | }
|
---|
68 | }
|
---|
69 | }
|
---|
70 |
|
---|
71 | if (username.isEmpty())
|
---|
72 | #ifndef Q_OS_SYMBIAN
|
---|
73 | username = "unknown";
|
---|
74 | #else
|
---|
75 | username = "QtS60";
|
---|
76 | #endif
|
---|
77 |
|
---|
78 | updateAddresses();
|
---|
79 | serverPort = 0;
|
---|
80 |
|
---|
81 | broadcastSocket.bind(QHostAddress::Any, broadcastPort, QUdpSocket::ShareAddress
|
---|
82 | | QUdpSocket::ReuseAddressHint);
|
---|
83 | connect(&broadcastSocket, SIGNAL(readyRead()),
|
---|
84 | this, SLOT(readBroadcastDatagram()));
|
---|
85 |
|
---|
86 | broadcastTimer.setInterval(BroadcastInterval);
|
---|
87 | connect(&broadcastTimer, SIGNAL(timeout()),
|
---|
88 | this, SLOT(sendBroadcastDatagram()));
|
---|
89 | }
|
---|
90 |
|
---|
91 | void PeerManager::setServerPort(int port)
|
---|
92 | {
|
---|
93 | serverPort = port;
|
---|
94 | }
|
---|
95 |
|
---|
96 | QByteArray PeerManager::userName() const
|
---|
97 | {
|
---|
98 | return username;
|
---|
99 | }
|
---|
100 |
|
---|
101 | void PeerManager::startBroadcasting()
|
---|
102 | {
|
---|
103 | broadcastTimer.start();
|
---|
104 | }
|
---|
105 |
|
---|
106 | bool PeerManager::isLocalHostAddress(const QHostAddress &address)
|
---|
107 | {
|
---|
108 | foreach (QHostAddress localAddress, ipAddresses) {
|
---|
109 | if (address == localAddress)
|
---|
110 | return true;
|
---|
111 | }
|
---|
112 | return false;
|
---|
113 | }
|
---|
114 |
|
---|
115 | void PeerManager::sendBroadcastDatagram()
|
---|
116 | {
|
---|
117 | QByteArray datagram(username);
|
---|
118 | datagram.append('@');
|
---|
119 | datagram.append(QByteArray::number(serverPort));
|
---|
120 |
|
---|
121 | bool validBroadcastAddresses = true;
|
---|
122 | foreach (QHostAddress address, broadcastAddresses) {
|
---|
123 | if (broadcastSocket.writeDatagram(datagram, address,
|
---|
124 | broadcastPort) == -1)
|
---|
125 | validBroadcastAddresses = false;
|
---|
126 | }
|
---|
127 |
|
---|
128 | if (!validBroadcastAddresses)
|
---|
129 | updateAddresses();
|
---|
130 | }
|
---|
131 |
|
---|
132 | void PeerManager::readBroadcastDatagram()
|
---|
133 | {
|
---|
134 | while (broadcastSocket.hasPendingDatagrams()) {
|
---|
135 | QHostAddress senderIp;
|
---|
136 | quint16 senderPort;
|
---|
137 | QByteArray datagram;
|
---|
138 | datagram.resize(broadcastSocket.pendingDatagramSize());
|
---|
139 | if (broadcastSocket.readDatagram(datagram.data(), datagram.size(),
|
---|
140 | &senderIp, &senderPort) == -1)
|
---|
141 | continue;
|
---|
142 |
|
---|
143 | QList<QByteArray> list = datagram.split('@');
|
---|
144 | if (list.size() != 2)
|
---|
145 | continue;
|
---|
146 |
|
---|
147 | int senderServerPort = list.at(1).toInt();
|
---|
148 | if (isLocalHostAddress(senderIp) && senderServerPort == serverPort)
|
---|
149 | continue;
|
---|
150 |
|
---|
151 | if (!client->hasConnection(senderIp)) {
|
---|
152 | Connection *connection = new Connection(this);
|
---|
153 | emit newConnection(connection);
|
---|
154 | connection->connectToHost(senderIp, senderServerPort);
|
---|
155 | }
|
---|
156 | }
|
---|
157 | }
|
---|
158 |
|
---|
159 | void PeerManager::updateAddresses()
|
---|
160 | {
|
---|
161 | broadcastAddresses.clear();
|
---|
162 | ipAddresses.clear();
|
---|
163 | foreach (QNetworkInterface interface, QNetworkInterface::allInterfaces()) {
|
---|
164 | foreach (QNetworkAddressEntry entry, interface.addressEntries()) {
|
---|
165 | QHostAddress broadcastAddress = entry.broadcast();
|
---|
166 | if (broadcastAddress != QHostAddress::Null && entry.ip() != QHostAddress::LocalHost) {
|
---|
167 | broadcastAddresses << broadcastAddress;
|
---|
168 | ipAddresses << entry.ip();
|
---|
169 | }
|
---|
170 | }
|
---|
171 | }
|
---|
172 | }
|
---|