changeset 502:629bd77efb5f

Improved the implementation of the UiGUI server. As a next step the communication protocol needs to be implemented. git-svn-id: svn://svn.code.sf.net/p/universalindent/code/trunk@742 59b1889a-e5ac-428c-b0c7-476e01d41282
author thomas_-_s <thomas_-_s@59b1889a-e5ac-428c-b0c7-476e01d41282>
date Wed, 25 Jun 2008 09:37:21 +0000
parents a4d67b117238
children 4e1caba02d90
files src/UiguiIndentServer.cpp src/UiguiIndentServer.h
diffstat 2 files changed, 63 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/src/UiguiIndentServer.cpp	Wed Jun 25 09:33:33 2008 +0000
+++ b/src/UiguiIndentServer.cpp	Wed Jun 25 09:37:21 2008 +0000
@@ -17,11 +17,14 @@
  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
  ***************************************************************************/
 
+#include <QtDebug>
 
 #include "UiguiIndentServer.h"
 
 UiguiIndentServer::UiguiIndentServer(void) : QObject() {
     tcpServer = NULL;
+    currentClientConnection = NULL;
+    readyForHandleRequest = false;
 }
 
 
@@ -42,6 +45,8 @@
     }
 
     connect( tcpServer, SIGNAL(newConnection()), this, SLOT(handleNewConnection()) );
+    readyForHandleRequest = true;
+    blockSize = 0;
 }
 
 
@@ -51,6 +56,8 @@
         delete tcpServer;
         tcpServer = NULL;
     }
+    currentClientConnection = NULL;
+    readyForHandleRequest = false;
 }
 
 
@@ -64,27 +71,66 @@
 
 void UiguiIndentServer::handleReceivedData() {
 
-    QTcpSocket *clientConnection = qobject_cast<QTcpSocket*>( sender() );
-    quint16 blockSize = 0;
+    if ( !readyForHandleRequest ) {
+        return;
+    }
+
+    currentClientConnection = qobject_cast<QTcpSocket*>( sender() );
     QString receivedData = "";
 
-    if ( clientConnection != NULL ) {
-        QDataStream in(clientConnection);
-        //in.setVersion(QDataStream::Qt_4_0);
+    if ( currentClientConnection != NULL ) {
+        QDataStream in(currentClientConnection);
+        in.setVersion(QDataStream::Qt_4_0);
 
         if ( blockSize == 0 ) {
-            if ( clientConnection->bytesAvailable() < (int)sizeof(quint16) )
+            if ( currentClientConnection->bytesAvailable() < (int)sizeof(quint32) )
                 return;
 
             in >> blockSize;
         }
 
-        if ( clientConnection->bytesAvailable() < blockSize )
+        if ( currentClientConnection->bytesAvailable() < blockSize )
             return;
 
-        unsigned int l = clientConnection->bytesAvailable();
-        char *myData = new char[l];
-        in.readRawData( myData, l);
-        delete myData;
+        QString receivedMessage;
+        in >> receivedMessage;
+
+        blockSize = 0;
+
+        qDebug() << "receivedMessage: " << receivedMessage;
+
+        if ( receivedMessage == "ts" ) {
+            sendMessage("Toll");
+        }
+        else {
+            sendMessage("irgendwas");
+        }
     }
 }
+
+
+void UiguiIndentServer::sendMessage( const QString &message )
+{
+    readyForHandleRequest = false;
+
+    dataToSend = "";
+    QDataStream out(&dataToSend, QIODevice::WriteOnly);
+    out.setVersion(QDataStream::Qt_4_0);
+    out << (quint32)0;
+    out << message;
+    out.device()->seek(0);
+    out << (quint32)(dataToSend.size() - sizeof(quint32));
+
+    connect(currentClientConnection, SIGNAL(bytesWritten(qint64)), this, SLOT(checkIfReadyForHandleRequest()));
+    currentClientConnection->write(dataToSend);
+}
+
+
+void UiguiIndentServer::checkIfReadyForHandleRequest() {
+    if ( currentClientConnection->bytesToWrite() == 0 ) {
+        QString dataToSendStr = dataToSend.right( dataToSend.size() - sizeof(quint32) );
+        qDebug() << "checkIfReadyForHandleRequest dataToSend was: " << dataToSendStr;
+        disconnect(currentClientConnection, SIGNAL(bytesWritten(qint64)), this, SLOT(checkIfReadyForHandleRequest()));
+        readyForHandleRequest = true;
+    }
+}
--- a/src/UiguiIndentServer.h	Wed Jun 25 09:33:33 2008 +0000
+++ b/src/UiguiIndentServer.h	Wed Jun 25 09:37:21 2008 +0000
@@ -39,9 +39,15 @@
 private slots:
     void handleNewConnection();
     void handleReceivedData();
+    void sendMessage(const QString &message);
+    void checkIfReadyForHandleRequest();
 
 private:
     QTcpServer *tcpServer;
+    QByteArray dataToSend;
+    bool readyForHandleRequest;
+    QTcpSocket *currentClientConnection;
+    quint32 blockSize;
 };
 
 #endif // UIGUIINDENTSERVER_H
\ No newline at end of file