UniversalIndentGUI 1.2.0
|
00001 /*************************************************************************** 00002 * Copyright (C) 2006-2012 by Thomas Schweitzer * 00003 * thomas-schweitzer(at)arcor.de * 00004 * * 00005 * This program is free software; you can redistribute it and/or modify * 00006 * it under the terms of the GNU General Public License version 2.0 as * 00007 * published by the Free Software Foundation. * 00008 * * 00009 * This program is distributed in the hope that it will be useful, * 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00012 * GNU General Public License for more details. * 00013 * * 00014 * You should have received a copy of the GNU General Public License * 00015 * along with this program in the file LICENSE.GPL; if not, write to the * 00016 * Free Software Foundation, Inc., * 00017 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 00018 ***************************************************************************/ 00019 00020 #include "UiGuiIndentServer.h" 00021 00022 #include <QTcpServer> 00023 #include <QTcpSocket> 00024 #include <QMessageBox> 00025 #include <QtDebug> 00026 00028 00046 UiGuiIndentServer::UiGuiIndentServer(void) : QObject() { 00047 _tcpServer = NULL; 00048 _currentClientConnection = NULL; 00049 _readyForHandleRequest = false; 00050 } 00051 00052 00053 UiGuiIndentServer::~UiGuiIndentServer(void) { 00054 } 00055 00056 00057 void UiGuiIndentServer::startServer() { 00058 if ( _tcpServer == NULL ) { 00059 _tcpServer = new QTcpServer(this); 00060 } 00061 00062 if ( !_tcpServer->isListening() ) { 00063 if ( !_tcpServer->listen(QHostAddress::Any, quint16(84484)) ) { 00064 QMessageBox::critical( NULL, tr("UiGUI Server"), tr("Unable to start the server: %1.").arg(_tcpServer->errorString()) ); 00065 return; 00066 } 00067 } 00068 00069 connect( _tcpServer, SIGNAL(newConnection()), this, SLOT(handleNewConnection()) ); 00070 _readyForHandleRequest = true; 00071 _blockSize = 0; 00072 } 00073 00074 00075 void UiGuiIndentServer::stopServer() { 00076 if ( _tcpServer != NULL ) { 00077 _tcpServer->close(); 00078 delete _tcpServer; 00079 _tcpServer = NULL; 00080 } 00081 _currentClientConnection = NULL; 00082 _readyForHandleRequest = false; 00083 } 00084 00085 00086 void UiGuiIndentServer::handleNewConnection() { 00087 QTcpSocket *clientConnection = _tcpServer->nextPendingConnection(); 00088 connect( clientConnection, SIGNAL(disconnected()), clientConnection, SLOT(deleteLater()) ); 00089 00090 connect( clientConnection, SIGNAL(readyRead()), this, SLOT(handleReceivedData()) ); 00091 } 00092 00093 00094 void UiGuiIndentServer::handleReceivedData() { 00095 if ( !_readyForHandleRequest ) { 00096 return; 00097 } 00098 00099 _currentClientConnection = qobject_cast<QTcpSocket*>( sender() ); 00100 QString receivedData = ""; 00101 00102 if ( _currentClientConnection != NULL ) { 00103 QDataStream in(_currentClientConnection); 00104 in.setVersion(QDataStream::Qt_4_0); 00105 00106 if ( _blockSize == 0 ) { 00107 if ( _currentClientConnection->bytesAvailable() < (int)sizeof(quint32) ) 00108 return; 00109 00110 in >> _blockSize; 00111 } 00112 00113 if ( _currentClientConnection->bytesAvailable() < _blockSize ) 00114 return; 00115 00116 QString receivedMessage; 00117 in >> receivedMessage; 00118 00119 _blockSize = 0; 00120 00121 qDebug() << "receivedMessage: " << receivedMessage; 00122 00123 if ( receivedMessage == "ts" ) { 00124 sendMessage("Toll"); 00125 } 00126 else { 00127 sendMessage("irgendwas"); 00128 } 00129 } 00130 } 00131 00132 00133 void UiGuiIndentServer::sendMessage( const QString &message ) { 00134 _readyForHandleRequest = false; 00135 00136 _dataToSend = ""; 00137 QDataStream out(&_dataToSend, QIODevice::WriteOnly); 00138 out.setVersion(QDataStream::Qt_4_0); 00139 out << (quint32)0; 00140 out << message; 00141 out.device()->seek(0); 00142 out << (quint32)(_dataToSend.size() - sizeof(quint32)); 00143 00144 connect(_currentClientConnection, SIGNAL(bytesWritten(qint64)), this, SLOT(checkIfReadyForHandleRequest())); 00145 _currentClientConnection->write(_dataToSend); 00146 } 00147 00148 00149 void UiGuiIndentServer::checkIfReadyForHandleRequest() { 00150 if ( _currentClientConnection->bytesToWrite() == 0 ) { 00151 QString dataToSendStr = _dataToSend.right( _dataToSend.size() - sizeof(quint32) ); 00152 qDebug() << "checkIfReadyForHandleRequest _dataToSend was: " << dataToSendStr; 00153 disconnect(_currentClientConnection, SIGNAL(bytesWritten(qint64)), this, SLOT(checkIfReadyForHandleRequest())); 00154 _readyForHandleRequest = true; 00155 } 00156 }