Home · All Namespaces · All Classes · Main Classes · Grouped Classes · Modules · Functions

client.cpp Example File
network/network-chat/client.cpp

 /****************************************************************************
 **
 ** Copyright (C) 1992-2008 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
 **
 ** This file is part of the documentation of the Qt Toolkit.
 **
 ** Licensees holding a valid Qt License Agreement may use this file in
 ** accordance with the rights, responsibilities and obligations
 ** contained therein.  Please consult your licensing agreement or
 ** contact [email protected] if any conditions of this licensing
 ** agreement are not clear to you.
 **
 ** Further information about Qt licensing is available at:
 ** http://trolltech.com/products/appdev/licensing.
 **
 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 **
 ****************************************************************************/

 #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();
 }


Copyright © 2008 Nokia Corporation and/or its subsidiary(-ies) Trademarks
Qt 4.4.3