examples/network/network-chat/peermanager.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
child 7 f7bc934e204c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the examples of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    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 #ifndef Q_OS_SYMBIAN
       
    74         username = "unknown";
       
    75 #else
       
    76         username = "QtS60";
       
    77 #endif
       
    78 
       
    79     updateAddresses();
       
    80     serverPort = 0;
       
    81 
       
    82     broadcastSocket.bind(QHostAddress::Any, broadcastPort, QUdpSocket::ShareAddress
       
    83                          | QUdpSocket::ReuseAddressHint);
       
    84     connect(&broadcastSocket, SIGNAL(readyRead()),
       
    85             this, SLOT(readBroadcastDatagram()));
       
    86 
       
    87     broadcastTimer.setInterval(BroadcastInterval);
       
    88     connect(&broadcastTimer, SIGNAL(timeout()),
       
    89             this, SLOT(sendBroadcastDatagram()));
       
    90 }
       
    91 
       
    92 void PeerManager::setServerPort(int port)
       
    93 {
       
    94     serverPort = port;
       
    95 }
       
    96 
       
    97 QByteArray PeerManager::userName() const
       
    98 {
       
    99     return username;
       
   100 }
       
   101 
       
   102 void PeerManager::startBroadcasting()
       
   103 {
       
   104     broadcastTimer.start();
       
   105 }
       
   106 
       
   107 bool PeerManager::isLocalHostAddress(const QHostAddress &address)
       
   108 {
       
   109     foreach (QHostAddress localAddress, ipAddresses) {
       
   110         if (address == localAddress)
       
   111             return true;
       
   112     }
       
   113     return false;
       
   114 }
       
   115 
       
   116 void PeerManager::sendBroadcastDatagram()
       
   117 {
       
   118     QByteArray datagram(username);
       
   119     datagram.append('@');
       
   120     datagram.append(QByteArray::number(serverPort));
       
   121 
       
   122     bool validBroadcastAddresses = true;
       
   123     foreach (QHostAddress address, broadcastAddresses) {
       
   124         if (broadcastSocket.writeDatagram(datagram, address,
       
   125                                           broadcastPort) == -1)
       
   126             validBroadcastAddresses = false;
       
   127     }
       
   128 
       
   129     if (!validBroadcastAddresses)
       
   130         updateAddresses();
       
   131 }
       
   132 
       
   133 void PeerManager::readBroadcastDatagram()
       
   134 {
       
   135     while (broadcastSocket.hasPendingDatagrams()) {
       
   136         QHostAddress senderIp;
       
   137         quint16 senderPort;
       
   138         QByteArray datagram;
       
   139         datagram.resize(broadcastSocket.pendingDatagramSize());
       
   140         if (broadcastSocket.readDatagram(datagram.data(), datagram.size(),
       
   141                                          &senderIp, &senderPort) == -1)
       
   142             continue;
       
   143 
       
   144         QList<QByteArray> list = datagram.split('@');
       
   145         if (list.size() != 2)
       
   146             continue;
       
   147 
       
   148         int senderServerPort = list.at(1).toInt();
       
   149         if (isLocalHostAddress(senderIp) && senderServerPort == serverPort)
       
   150             continue;
       
   151 
       
   152         if (!client->hasConnection(senderIp)) {
       
   153             Connection *connection = new Connection(this);
       
   154             emit newConnection(connection);
       
   155             connection->connectToHost(senderIp, senderServerPort);
       
   156         }
       
   157     }
       
   158 }
       
   159 
       
   160 void PeerManager::updateAddresses()
       
   161 {
       
   162     broadcastAddresses.clear();
       
   163     ipAddresses.clear();
       
   164     foreach (QNetworkInterface interface, QNetworkInterface::allInterfaces()) {
       
   165         foreach (QNetworkAddressEntry entry, interface.addressEntries()) {
       
   166             QHostAddress broadcastAddress = entry.broadcast();
       
   167             if (broadcastAddress != QHostAddress::Null && entry.ip() != QHostAddress::LocalHost) {
       
   168                 broadcastAddresses << broadcastAddress;
       
   169                 ipAddresses << entry.ip();
       
   170             }
       
   171         }
       
   172     }
       
   173 }