receiver.cpp

Go to the documentation of this file.
00001 #include <QtGui>
00002 #include <QtNetwork>
00003 
00004 #include "receiver.h"
00005 
00006 Receiver::Receiver(QWidget *parent)
00007     : QWidget(parent)
00008 {
00009     udpSocket = new QUdpSocket(this);
00010     
00011     connect(udpSocket, SIGNAL(readyRead()), this, SLOT(processPendingDatagrams()));
00012     connect(udpSocket, SIGNAL(stateChanged(QAbstractSocket::SocketState)), SLOT(udp_stateChanged(QAbstractSocket::SocketState)));
00013 }
00014 
00015 void Receiver::udp_stateChanged( QAbstractSocket::SocketState state )
00016 {
00017     if (state == QAbstractSocket::UnconnectedState)
00018         emit connectionClose();
00019 //  if (state == QAbstractSocket::ConnectedState)
00020 //      emit connectionOpen();
00021     if (state == QAbstractSocket::BoundState)
00022         emit connectionBound();
00023 }
00024 
00025 void Receiver::processPendingDatagrams()
00026 {
00027     while (udpSocket->hasPendingDatagrams()) 
00028     {
00029         QByteArray datagram;
00030         datagram.resize(udpSocket->pendingDatagramSize());
00031         udpSocket->readDatagram(datagram.data(), datagram.size());
00032         //statusLabel->setText(tr("Received datagram: \"%1\"") .arg(datagram.data()));
00033         emit textReceived(datagram);
00034     }
00035 }
00036 
00037 bool Receiver::isListening()
00038 {
00039     QAbstractSocket::SocketState state = udpSocket->state();
00040     
00041     if (state == QAbstractSocket::BoundState)
00042         return true;
00043     
00044     return false;
00045 }
00046 
00047 void Receiver::bindUrl(QUrl url)
00048 {
00049     udpSocket->bind( QHostAddress(url.host()), quint16(url.port()) );
00050 }
00051 
00052 void Receiver::sendText(QString text)
00053 {
00054     if ( isListening() )
00055         udpSocket->write( text.toAscii() );
00056     else
00057         emit connectionClose();
00058 }
00059 
00060 void Receiver::disconnect()
00061 {
00062     udpSocket->close();
00063 }
00064 
00065 void Receiver::udp_connected()
00066 {
00067     emit connectionBound();
00068 }
00069 
00070 void Receiver::udp_disconnected()
00071 {
00072     emit connectionClose();
00073 }
00074 

Generated on Sun Oct 28 22:51:29 2007 for BBoB by  doxygen 1.5.3