Client Class Reference

#include <client.h>

Inheritance diagram for Client:

Inheritance graph
[legend]
Collaboration diagram for Client:

Collaboration graph
[legend]
List of all members.

Detailed Description

Definition at line 35 of file client.h.

Signals

void newMessage (const QString &from, const QString &message)
void newParticipant (const QString &nick)
void participantLeft (const QString &nick)

Public Member Functions

 Client ()
void sendMessage (const QString &message)
QString nickName () const
bool hasConnection (const QHostAddress &senderIp, int senderPort=-1) const
 Client (QWidget *parent=0)

Private Slots

void newConnection (Connection *connection)
void connectionError (QAbstractSocket::SocketError socketError)
void disconnected ()
void readyForUse ()
void requestNewFortune ()
void readFortune ()
void displayError (QAbstractSocket::SocketError socketError)
void enableGetFortuneButton ()

Private Member Functions

void removeConnection (Connection *connection)

Private Attributes

PeerManagerpeerManager
Server server
QMultiHash< QHostAddress,
Connection * > 
peers
QLabelhostLabel
QLabelportLabel
QLineEdithostLineEdit
QLineEditportLineEdit
QLabelstatusLabel
QPushButtongetFortuneButton
QPushButtonquitButton
QDialogButtonBoxbuttonBox
QTcpSockettcpSocket
QString currentFortune
quint16 blockSize


Constructor & Destructor Documentation

Client::Client (  ) 

Definition at line 30 of file client.cpp.

References QObject::connect(), newConnection(), peerManager, server, QTcpServer::serverPort(), PeerManager::setServerPort(), SIGNAL, SLOT, and PeerManager::startBroadcasting().

Here is the call graph for this function:

Client::Client ( QWidget parent = 0  ) 

Definition at line 29 of file client.cpp.

References QDialogButtonBox::ActionRole, QDialogButtonBox::addButton(), QGridLayout::addWidget(), buttonBox, QWidget::close(), QObject::connect(), displayError(), enableGetFortuneButton(), error, getFortuneButton, hostLabel, hostLineEdit, portLabel, portLineEdit, QDialog::QPushButton, quitButton, readFortune(), QDialogButtonBox::RejectRole, requestNewFortune(), QLabel::setBuddy(), QPushButton::setDefault(), QWidget::setEnabled(), QWidget::setFocus(), QWidget::setLayout(), QLineEdit::setValidator(), QWidget::setWindowTitle(), SIGNAL, SLOT, statusLabel, and tcpSocket.

00030     : QDialog(parent)
00031 {
00032     hostLabel = new QLabel(tr("&Server name:"));
00033     portLabel = new QLabel(tr("S&erver port:"));
00034 
00035     hostLineEdit = new QLineEdit("Localhost");
00036     portLineEdit = new QLineEdit;
00037     portLineEdit->setValidator(new QIntValidator(1, 65535, this));
00038 
00039     hostLabel->setBuddy(hostLineEdit);
00040     portLabel->setBuddy(portLineEdit);
00041 
00042     statusLabel = new QLabel(tr("This examples requires that you run the "
00043                                 "Fortune Server example as well."));
00044 
00045     getFortuneButton = new QPushButton(tr("Get Fortune"));
00046     getFortuneButton->setDefault(true);
00047     getFortuneButton->setEnabled(false);
00048 
00049     quitButton = new QPushButton(tr("Quit"));
00050 
00051     buttonBox = new QDialogButtonBox;
00052     buttonBox->addButton(getFortuneButton, QDialogButtonBox::ActionRole);
00053     buttonBox->addButton(quitButton, QDialogButtonBox::RejectRole);
00054 
00055     tcpSocket = new QTcpSocket(this);
00056 
00057     connect(hostLineEdit, SIGNAL(textChanged(const QString &)),
00058             this, SLOT(enableGetFortuneButton()));
00059     connect(portLineEdit, SIGNAL(textChanged(const QString &)),
00060             this, SLOT(enableGetFortuneButton()));
00061     connect(getFortuneButton, SIGNAL(clicked()),
00062             this, SLOT(requestNewFortune()));
00063     connect(quitButton, SIGNAL(clicked()), this, SLOT(close()));
00064     connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readFortune()));
00065     connect(tcpSocket, SIGNAL(error(QAbstractSocket::SocketError)),
00066             this, SLOT(displayError(QAbstractSocket::SocketError)));
00067 
00068     QGridLayout *mainLayout = new QGridLayout;
00069     mainLayout->addWidget(hostLabel, 0, 0);
00070     mainLayout->addWidget(hostLineEdit, 0, 1);
00071     mainLayout->addWidget(portLabel, 1, 0);
00072     mainLayout->addWidget(portLineEdit, 1, 1);
00073     mainLayout->addWidget(statusLabel, 2, 0, 1, 2);
00074     mainLayout->addWidget(buttonBox, 3, 0, 1, 2);
00075     setLayout(mainLayout);
00076 
00077     setWindowTitle(tr("Fortune Client"));
00078     portLineEdit->setFocus();
00079 }

Here is the call graph for this function:


Member Function Documentation

void Client::sendMessage ( const QString message  ) 

Definition at line 42 of file client.cpp.

References connection, message, peers, and QHash< Key, T >::values().

Referenced by ChatDialog::returnPressed().

00043 {
00044     if (message.isEmpty())
00045         return;
00046 
00047     QList<Connection *> connections = peers.values();
00048     foreach (Connection *connection, connections)
00049         connection->sendMessage(message);
00050 }

Here is the call graph for this function:

QString Client::nickName (  )  const

Definition at line 52 of file client.cpp.

References QHostInfo::localHostName(), QString::number(), peerManager, server, QTcpServer::serverPort(), and PeerManager::userName().

Referenced by ChatDialog::ChatDialog().

00053 {
00054     return QString(peerManager->userName()) + "@" + QHostInfo::localHostName()
00055            + ":" + QString::number(server.serverPort());
00056 }

Here is the call graph for this function:

bool Client::hasConnection ( const QHostAddress senderIp,
int  senderPort = -1 
) const

Definition at line 58 of file client.cpp.

References connection, QHash< Key, T >::contains(), peers, and QHash< Key, T >::values().

Referenced by PeerManager::readBroadcastDatagram(), and readyForUse().

00059 {
00060     if (senderPort == -1)
00061         return peers.contains(senderIp);
00062 
00063     if (!peers.contains(senderIp))
00064         return false;
00065 
00066     QList<Connection *> connections = peers.values(senderIp);
00067     foreach (Connection *connection, connections) {
00068         if (connection->peerPort() == senderPort)
00069             return true;
00070     }
00071 
00072     return false;
00073 }

Here is the call graph for this function:

void Client::newMessage ( const QString from,
const QString message 
) [signal]

Referenced by readyForUse().

void Client::newParticipant ( const QString nick  )  [signal]

Referenced by readyForUse().

void Client::participantLeft ( const QString nick  )  [signal]

Referenced by removeConnection().

void Client::newConnection ( Connection connection  )  [private, slot]

Definition at line 75 of file client.cpp.

References QObject::connect(), connection, connectionError(), disconnected(), error, peerManager, readyForUse(), SIGNAL, SLOT, and PeerManager::userName().

Referenced by Client().

00076 {
00077     connection->setGreetingMessage(peerManager->userName());
00078 
00079     connect(connection, SIGNAL(error(QAbstractSocket::SocketError)),
00080             this, SLOT(connectionError(QAbstractSocket::SocketError)));
00081     connect(connection, SIGNAL(disconnected()), this, SLOT(disconnected()));
00082     connect(connection, SIGNAL(readyForUse()), this, SLOT(readyForUse()));
00083 }

void Client::connectionError ( QAbstractSocket::SocketError  socketError  )  [private, slot]

Definition at line 107 of file client.cpp.

References connection, removeConnection(), and QObject::sender().

Referenced by newConnection().

00108 {
00109     if (Connection *connection = qobject_cast<Connection *>(sender()))
00110         removeConnection(connection);
00111 }

void Client::disconnected (  )  [private, slot]

Definition at line 101 of file client.cpp.

References connection, removeConnection(), and QObject::sender().

Referenced by newConnection().

00102 {
00103     if (Connection *connection = qobject_cast<Connection *>(sender()))
00104         removeConnection(connection);
00105 }

void Client::readyForUse (  )  [private, slot]

Definition at line 85 of file client.cpp.

References QObject::connect(), connection, emit, hasConnection(), QMultiHash< Key, T >::insert(), QString::isEmpty(), newMessage(), newParticipant(), peers, QObject::sender(), and SIGNAL.

Referenced by newConnection().

00086 {
00087     Connection *connection = qobject_cast<Connection *>(sender());
00088     if (!connection || hasConnection(connection->peerAddress(),
00089                                      connection->peerPort()))
00090         return;
00091 
00092     connect(connection, SIGNAL(newMessage(const QString &, const QString &)),
00093             this, SIGNAL(newMessage(const QString &, const QString &)));
00094 
00095     peers.insert(connection->peerAddress(), connection);
00096     QString nick = connection->name();
00097     if (!nick.isEmpty())
00098         emit newParticipant(nick);
00099 }

void Client::removeConnection ( Connection connection  )  [private]

Definition at line 113 of file client.cpp.

References connection, QHash< Key, T >::contains(), emit, QString::isEmpty(), participantLeft(), peers, and QHash< Key, T >::remove().

Referenced by connectionError(), and disconnected().

00114 {
00115     if (peers.contains(connection->peerAddress())) {
00116         peers.remove(connection->peerAddress());
00117         QString nick = connection->name();
00118         if (!nick.isEmpty())
00119             emit participantLeft(nick);
00120     }
00121     connection->deleteLater();
00122 }

Here is the call graph for this function:

void Client::requestNewFortune (  )  [private, slot]

Definition at line 81 of file client.cpp.

References QAbstractSocket::abort(), blockSize, QAbstractSocket::connectToHost(), getFortuneButton, hostLineEdit, portLineEdit, QWidget::setEnabled(), tcpSocket, QLineEdit::text(), and QString::toInt().

Referenced by Client(), and readFortune().

00082 {
00083     getFortuneButton->setEnabled(false);
00084     blockSize = 0;
00085     tcpSocket->abort();
00086     tcpSocket->connectToHost(hostLineEdit->text(),
00087                              portLineEdit->text().toInt());
00088 }

void Client::readFortune (  )  [private, slot]

Definition at line 90 of file client.cpp.

References blockSize, QAbstractSocket::bytesAvailable(), currentFortune, getFortuneButton, int, QDataStream::Qt_4_0, requestNewFortune(), QWidget::setEnabled(), QLabel::setText(), QDataStream::setVersion(), QTimer::singleShot(), SLOT, statusLabel, and tcpSocket.

Referenced by Client().

00091 {
00092     QDataStream in(tcpSocket);
00093     in.setVersion(QDataStream::Qt_4_0);
00094 
00095     if (blockSize == 0) {
00096         if (tcpSocket->bytesAvailable() < (int)sizeof(quint16))
00097             return;
00098 
00099         in >> blockSize;
00100     }
00101 
00102     if (tcpSocket->bytesAvailable() < blockSize)
00103         return;
00104 
00105     QString nextFortune;
00106     in >> nextFortune;
00107 
00108     if (nextFortune == currentFortune) {
00109         QTimer::singleShot(0, this, SLOT(requestNewFortune()));
00110         return;
00111     }
00112 
00113     currentFortune = nextFortune;
00114     statusLabel->setText(currentFortune);
00115     getFortuneButton->setEnabled(true);
00116 }

void Client::displayError ( QAbstractSocket::SocketError  socketError  )  [private, slot]

Definition at line 118 of file client.cpp.

References QAbstractSocket::ConnectionRefusedError, QIODevice::errorString(), getFortuneButton, QAbstractSocket::HostNotFoundError, QMessageBox::information(), QAbstractSocket::RemoteHostClosedError, QWidget::setEnabled(), and tcpSocket.

Referenced by Client().

00119 {
00120     switch (socketError) {
00121     case QAbstractSocket::RemoteHostClosedError:
00122         break;
00123     case QAbstractSocket::HostNotFoundError:
00124         QMessageBox::information(this, tr("Fortune Client"),
00125                                  tr("The host was not found. Please check the "
00126                                     "host name and port settings."));
00127         break;
00128     case QAbstractSocket::ConnectionRefusedError:
00129         QMessageBox::information(this, tr("Fortune Client"),
00130                                  tr("The connection was refused by the peer. "
00131                                     "Make sure the fortune server is running, "
00132                                     "and check that the host name and port "
00133                                     "settings are correct."));
00134         break;
00135     default:
00136         QMessageBox::information(this, tr("Fortune Client"),
00137                                  tr("The following error occurred: %1.")
00138                                  .arg(tcpSocket->errorString()));
00139     }
00140 
00141     getFortuneButton->setEnabled(true);
00142 }

void Client::enableGetFortuneButton (  )  [private, slot]

Definition at line 144 of file client.cpp.

References getFortuneButton, hostLineEdit, QString::isEmpty(), portLineEdit, QWidget::setEnabled(), and QLineEdit::text().

Referenced by Client().

00145 {
00146     getFortuneButton->setEnabled(!hostLineEdit->text().isEmpty()
00147                                  && !portLineEdit->text().isEmpty());
00148 }


Member Data Documentation

PeerManager* Client::peerManager [private]

Definition at line 60 of file client.h.

Referenced by Client(), newConnection(), and nickName().

Server Client::server [private]

Definition at line 61 of file client.h.

Referenced by Client(), and nickName().

QMultiHash<QHostAddress, Connection *> Client::peers [private]

Definition at line 62 of file client.h.

Referenced by hasConnection(), readyForUse(), removeConnection(), and sendMessage().

QLabel* Client::hostLabel [private]

Definition at line 50 of file client.h.

Referenced by Client().

QLabel* Client::portLabel [private]

Definition at line 51 of file client.h.

Referenced by Client().

QLineEdit* Client::hostLineEdit [private]

Definition at line 52 of file client.h.

Referenced by Client(), enableGetFortuneButton(), and requestNewFortune().

QLineEdit* Client::portLineEdit [private]

Definition at line 53 of file client.h.

Referenced by Client(), enableGetFortuneButton(), and requestNewFortune().

QLabel* Client::statusLabel [private]

Definition at line 54 of file client.h.

Referenced by Client(), and readFortune().

QPushButton* Client::getFortuneButton [private]

Definition at line 55 of file client.h.

Referenced by Client(), displayError(), enableGetFortuneButton(), readFortune(), and requestNewFortune().

QPushButton* Client::quitButton [private]

Definition at line 56 of file client.h.

Referenced by Client().

QDialogButtonBox* Client::buttonBox [private]

Definition at line 57 of file client.h.

Referenced by Client().

QTcpSocket* Client::tcpSocket [private]

Definition at line 59 of file client.h.

Referenced by Client(), displayError(), readFortune(), and requestNewFortune().

QString Client::currentFortune [private]

Definition at line 60 of file client.h.

Referenced by readFortune().

quint16 Client::blockSize [private]

Definition at line 61 of file client.h.

Referenced by readFortune(), and requestNewFortune().


The documentation for this class was generated from the following files:
Generated on Thu Mar 15 14:48:19 2007 for Qt 4.2 User's Guide by  doxygen 1.5.1