summaryrefslogtreecommitdiff
path: root/client
diff options
context:
space:
mode:
authordeva <deva>2008-08-18 14:54:03 +0000
committerdeva <deva>2008-08-18 14:54:03 +0000
commit3c199adf6c317ade446120db1cdb51b473edca57 (patch)
tree8b865ad2fd9208be60333bf234bf8a8d5929d8b9 /client
parentcbb2adb8f35dfc11ac58bff6fa56b99f071a76cd (diff)
Rewrite of all network code, with a reusable socket. Still needs some error checking.
Diffstat (limited to 'client')
-rw-r--r--client/client.pro4
-rw-r--r--client/macro.cc87
-rw-r--r--client/macro.h15
-rw-r--r--client/macrowindow.cc15
-rw-r--r--client/netcom.cc108
-rw-r--r--client/netcom.h (renamed from client/sendrecieve.h)58
-rw-r--r--client/sendrecieve.cc161
7 files changed, 176 insertions, 272 deletions
diff --git a/client/client.pro b/client/client.pro
index c2e3ccf..e6d5be8 100644
--- a/client/client.pro
+++ b/client/client.pro
@@ -25,7 +25,7 @@ HEADERS += \
lua.h \
macro.h \
macrowindow.h \
- sendrecieve.h \
+ netcom.h \
widgetbuilder.h \
widgets.h \
widgets/widget.h \
@@ -49,7 +49,7 @@ SOURCES += \
lua.cc \
macro.cc \
macrowindow.cc \
- sendrecieve.cc \
+ netcom.cc \
widgetbuilder.cc \
widgets/widget.cc \
widgets/label.cc \
diff --git a/client/macro.cc b/client/macro.cc
index aa82d50..b0943d0 100644
--- a/client/macro.cc
+++ b/client/macro.cc
@@ -25,9 +25,15 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/
#include "macro.h"
-#include "sendrecieve.h"
-#include <QDomDocument>
+
#include <QApplication>
+#include <QDomDocument>
+#include <QObject>
+#include <QEvent>
+#include <QLinkedList>
+
+#include "macrowindow.h"
+#include "netcom.h"
#define MACRO_EVENT_ID 65432
@@ -63,63 +69,15 @@ protected:
* The single global macro event filter.
* It is created the first time new_macro is called.
*/
-MacroEventFilter *macro_event_filter = NULL;
-
-/**
- * This function sends a request to the praco server, and returns the
- * parsed answer.
- */
-static QDomDocument xml_request(QString course, QString macro)
-{
- // Create the xml request array
- QByteArray xml_array;
- printf("course: %s, macro: %s, cpr: %s, user: %s\n",
- course.toStdString().c_str(),
- macro.toStdString().c_str(),
- cpr.toStdString().c_str(),
- user.toStdString().c_str());
- xml_array.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
- xml_array.append("<pracro version=\"1.0\" cpr=\"" + cpr + "\" user=\"" + user + "\">\n");
- xml_array.append(" <request course=\"" + course + "\" macro=\"" + macro + "\"/>\n");
- xml_array.append("</pracro>");
-
- // Print to stdout for debug purposes
- char *test = xml_array.data();
- printf("%s\n", test);
-
- // Parse the XML document using setContent of QDomDocument
- QDomDocument xml_req;
- if (!xml_req.setContent(xml_array)) {
- printf("Error: Invalid XML found in request\n");
- }
-
- return xml_req;
-}
+static MacroEventFilter *macro_event_filter = NULL;
/**
* Create the new macro
*/
static void create_macro(QString course, QString macro)
{
- // Build the XML request
- QDomDocument xml_req = xml_request(course, macro);
-
- // Fetch the XML document
- SendRecieve xml_acquire(host, port);
- xml_acquire.makeConnection(&xml_req);
- QByteArray ba = xml_acquire.getResult();
-
- // Print to stdout, for debug purposes
- char *test = ba.data();
- printf("%s\n", test);
-
- // Parse the XML document using setContent of QDomDocument
- QDomDocument xml_doc;
- if (!xml_doc.setContent(ba)) {
- printf("ERROR: Invalid XML recieved!\n");
- fwrite(ba.data(), ba.size(), 1, stdout);
- return;
- }
+ NetCom netcom(host, port, user, cpr);
+ QDomDocument xml_doc = netcom.send(course, macro);
cleanup_macros();
@@ -130,7 +88,7 @@ static void create_macro(QString course, QString macro)
// Initiate the new macro window with the xml document and push
// it to the window list
QDomNodeList courses = xml_doc.documentElement().childNodes();
- QDomNode coursenode = courses.at(0); // There can be only one!
+ QDomNode coursenode = courses.at(0); // There can be only one! (Swush, flomp)
QDomNodeList macros = coursenode.childNodes();
for(int j = 0; j < macros.count(); j++) {
QDomNode macronode = macros.at(j);
@@ -140,9 +98,12 @@ static void create_macro(QString course, QString macro)
}
}
-bool MacroEventFilter::eventFilter( QObject *, QEvent *e )
+/**
+ * Event filter callback method
+ */
+bool MacroEventFilter::eventFilter(QObject *, QEvent *e)
{
- if ( e->type() == MACRO_EVENT_ID ) {
+ if(e->type() == MACRO_EVENT_ID) {
MacroEvent *event = (MacroEvent*)e;
create_macro(event->course, event->macro);
return TRUE; // eat event
@@ -151,26 +112,26 @@ bool MacroEventFilter::eventFilter( QObject *, QEvent *e )
}
}
-// Delete all closed windows from window list
+/**
+ * Delete all closed windows from window list
+ */
void cleanup_macros()
{
- int dead = 0;
- int live = 0;
-
QLinkedList< MacroWindow * >::iterator i = macrowindows.begin();
while(i != macrowindows.end()) {
if( (*i)->isClosed() ) {
- dead++;
delete *i;
i = macrowindows.erase(i);
} else {
- live++;
i++;
}
}
- printf("Found %d live ones and %d dead ones.\n", live, dead);
}
+/**
+ * Public macro creation function.
+ * Initiates the creation of a new macro.
+ */
void new_macro(QString course, QString macro)
{
if(macro_event_filter == NULL) {
diff --git a/client/macro.h b/client/macro.h
index 4cd7109..f00f172 100644
--- a/client/macro.h
+++ b/client/macro.h
@@ -28,15 +28,16 @@
#define __PRACRO_MACRO_H__
#include <QString>
-#include <QObject>
-#include <QEvent>
-#include <QLinkedList>
-
-#include "macrowindow.h"
-
-extern QLinkedList< MacroWindow * > macrowindows;
+/**
+ * Public macro creation function.
+ * Initiates the creation of a new macro.
+ */
void new_macro(QString course, QString name);
+
+/**
+ * Delete all closed windows from window list
+ */
void cleanup_macros();
#endif/*__PRACRO_MACRO_H__*/
diff --git a/client/macrowindow.cc b/client/macrowindow.cc
index 551c804..6837e1d 100644
--- a/client/macrowindow.cc
+++ b/client/macrowindow.cc
@@ -25,9 +25,7 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/
#include "macrowindow.h"
-#include "sendrecieve.h"
-#include "macro.h"
-#include "widgets/widget.h"
+
#include <QVBoxLayout>
#include <QMessageBox>
#include <QDomDocument>
@@ -35,9 +33,12 @@
#include <QDomNode>
#include <QByteArray>
+#include "macro.h"
+#include "widgets/widget.h"
#include "widgets/window.h"
#include "widgetbuilder.h"
#include "lua.h"
+#include "netcom.h"
extern QString cpr;
extern QString user;
@@ -129,6 +130,7 @@ bool MacroWindow::doCommit()
if(faulty == 0) {
printf("MacroWindow -> committing...\n");
+#if 0
// Build the XML commit
QString xml_string;
xml_string.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
@@ -137,7 +139,7 @@ bool MacroWindow::doCommit()
version + "\">\n");
// Iterate the different entries, and append their results to the commit string
- QVector< Widget* >::iterator i=widgets.begin();
+ QVector< Widget* >::iterator i = widgets.begin();
while (i != widgets.end()) {
Widget* w = *i;
@@ -168,6 +170,11 @@ bool MacroWindow::doCommit()
//QByteArray ba = macro_commit.getResult();
QString ba = macro_commit.getResult();
printf("Server returned result: %s", ba.toStdString().c_str());
+#endif/*0*/
+
+ NetCom netcom(host, port, user, cpr);
+ netcom.send(widgets, macro, version);
+
return true;
} else {
return false;
diff --git a/client/netcom.cc b/client/netcom.cc
new file mode 100644
index 0000000..cbe44d1
--- /dev/null
+++ b/client/netcom.cc
@@ -0,0 +1,108 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/***************************************************************************
+ * netcom.cc
+ *
+ * Mon Aug 18 09:33:26 CEST 2008
+ * Copyright 2008 Bent Bisballe Nyeng
+ * deva@aasimon.org
+ ****************************************************************************/
+
+/*
+ * This file is part of Pracro.
+ *
+ * Pracro is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Pracro is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Pracro; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+#include "netcom.h"
+
+#include <QApplication>
+
+NetCom::NetCom(QString host, quint16 port, QString user, QString cpr)
+{
+ this->user = user;
+ this->cpr = cpr;
+ socket.connectToHost(host, port);
+ connect(&socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
+}
+
+NetCom::~NetCom()
+{
+ socket.disconnectFromHost();
+}
+
+QDomDocument NetCom::send(QString course, QString macro)
+{
+ QDomDocument doc;
+
+ QDomElement pracro_elem = doc.createElement("pracro");
+ pracro_elem.setAttribute("version", "1.0");
+ pracro_elem.setAttribute("cpr", cpr);
+ pracro_elem.setAttribute("user", user);
+ doc.appendChild(pracro_elem);
+
+ QDomElement request_elem = doc.createElement("request");
+ request_elem.setAttribute("course", course);
+ request_elem.setAttribute("macro", macro);
+ pracro_elem.appendChild(request_elem);
+
+ socket.write(doc.toByteArray());
+
+ do {
+ qApp->processEvents();
+ } while(!res_doc.setContent(buffer));
+
+ buffer = "";
+
+ return res_doc;
+}
+
+void NetCom::readyRead()
+{
+ buffer.append(socket.readAll());
+}
+
+void NetCom::send(QVector< Widget* > widgets, QString macro, QString version)
+{
+ QDomDocument doc;
+
+ QDomElement pracro_elem = doc.createElement("pracro");
+ pracro_elem.setAttribute("version", "1.0");
+ pracro_elem.setAttribute("cpr", cpr);
+ pracro_elem.setAttribute("user", user);
+ doc.appendChild(pracro_elem);
+
+ QDomElement commit_elem = doc.createElement("commit");
+ commit_elem.setAttribute("macro", macro);
+ commit_elem.setAttribute("version", version);
+ pracro_elem.appendChild(commit_elem);
+
+ // Iterate the different entries, and append their results to the commit string
+ QVector< Widget* >::iterator i = widgets.begin();
+ while (i != widgets.end()) {
+ Widget* w = *i;
+
+ QDomElement field_elem = doc.createElement("field");
+ field_elem.setAttribute("name", w->getName());
+ field_elem.setAttribute("value", w->getValue());
+ commit_elem.appendChild(field_elem);
+
+ i++;
+ }
+
+ printf(doc.toString().toStdString().c_str());
+
+ socket.write(doc.toByteArray());
+ // qApp->processEvents();
+ socket.waitForBytesWritten(10000);
+}
diff --git a/client/sendrecieve.h b/client/netcom.h
index 7ee87ad..18d6dbe 100644
--- a/client/sendrecieve.h
+++ b/client/netcom.h
@@ -1,10 +1,10 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/***************************************************************************
- * sendrecieve.h
+ * netcom.h
*
- * Fri Jul 13 12:38:45 CEST 2007
- * Copyright 2007 Bent Bisballe Nyeng and Lars Bisballe Jensen
- * deva@aasimon.org and elsenator@gmail.com
+ * Mon Aug 18 09:33:26 CEST 2008
+ * Copyright 2008 Bent Bisballe Nyeng
+ * deva@aasimon.org
****************************************************************************/
/*
@@ -24,48 +24,36 @@
* along with Pracro; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/
-
-#ifndef _SENDRECIEVE_H
-#define _SENDRECIEVE_H
+#ifndef __PRACRO_NETCOM_H__
+#define __PRACRO_NETCOM_H__
#include <QObject>
+#include <QString>
#include <QTcpSocket>
#include <QDomDocument>
-#define TCP_CONNECTED 1
-#define TCP_DISCONNECTED 2
-#define TCP_CONNECTING 3
-#define TCP_ERROR -1
-#define TCP_ERROR_SERVER_NOT_FOUND -2
+#include "widgets/widget.h"
-class SendRecieve : public QObject
-{
- Q_OBJECT
+class NetCom : public QObject {
+Q_OBJECT
public:
- SendRecieve(QString host, quint16 port);
- void makeConnection(QDomDocument *xml_req);
- QByteArray getResult();
+ NetCom(QString host, quint16 port, QString user, QString cpr);
+ ~NetCom();
+
+ QDomDocument send(QString course, QString macro);
+ void send(QVector< Widget* > widgets, QString macro, QString version);
public slots:
- void tcpConnect();
- void tcpDisconnect();
- int tcpStatus();
- void myTcpWrite(char *msg, int len);
- void myHostFound();
- void myConnected();
- void myDisconnected();
- void myError(QAbstractSocket::SocketError);
- void myReadyReadHandler();
+ void readyRead();
private:
- QTcpSocket *tcpsocket;
- int tcpConnected;
- QByteArray ba_all;
- bool has_result;
- QDomDocument *xml_req;
+ QTcpSocket socket;
+
+ QByteArray buffer;
+ QDomDocument res_doc;
- QString host;
- quint16 port;
+ QString user;
+ QString cpr;
};
-#endif/*_SENDRECIEVE_H_*/
+#endif/*__PRACRO_NETCOM_H__*/
diff --git a/client/sendrecieve.cc b/client/sendrecieve.cc
deleted file mode 100644
index 454ce30..0000000
--- a/client/sendrecieve.cc
+++ /dev/null
@@ -1,161 +0,0 @@
-/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
-/***************************************************************************
- * sendrecieve.cc
- *
- * Fri Jul 13 12:38:45 CEST 2007
- * Copyright 2007 Bent Bisballe Nyeng, Lars Bisballe Jensen and Peter Skaarup
- * deva@aasimon.org, elsenator@gmail.com and piparum@piparum.dk
- ****************************************************************************/
-
-/*
- * This file is part of Pracro.
- *
- * Pracro is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * Pracro is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with Pracro; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
- */
-
-#include "sendrecieve.h"
-
-#include <stdio.h>
-#include <QApplication>
-#include <QMessageBox>
-#include <QByteArray>
-#include <QSettings>
-
-#ifdef HOST_WIN32
-#include <windows.h>
-#define sleep(x) Sleep(x * 1000)
-#else
-#include <unistd.h>
-#endif
-
-SendRecieve::SendRecieve(QString host, quint16 port)
- : QObject()
-{
- this->host = host;
- this->port = port;
- has_result = false;
-}
-
-void SendRecieve::tcpConnect()
-{
- printf("%s, %d\n", host.toStdString().c_str(), port);
- tcpsocket->connectToHost(host, port);
- tcpConnected = TCP_CONNECTING;
-}
-
-void SendRecieve::tcpDisconnect()
-{
- tcpsocket->disconnectFromHost();
- while(tcpConnected != TCP_DISCONNECTED) {
- if(tcpConnected == TCP_ERROR) {
- tcpConnected = TCP_DISCONNECTED;
- return;
- }
- sleep(1);
- printf("ERROR!!! Couldn't connect to host!\n");
- qApp->processEvents();
- }
-}
-
-int SendRecieve::tcpStatus()
-{
- return tcpConnected;
-}
-
-void SendRecieve::makeConnection(QDomDocument *xml_req)
-{
- this->xml_req = xml_req;
-
- tcpsocket = new QTcpSocket;
- connect(tcpsocket, SIGNAL(hostFound()), this, SLOT(myHostFound()));
- connect(tcpsocket, SIGNAL(readyRead()), this, SLOT(myReadyReadHandler()));
- connect(tcpsocket, SIGNAL(connected()), this, SLOT(myConnected()));
- connect(tcpsocket, SIGNAL(disconnected()), this, SLOT(myDisconnected()));
- connect(tcpsocket, SIGNAL(error(QAbstractSocket::SocketError)),
- this, SLOT(myError(QAbstractSocket::SocketError)));
- tcpConnect();
-}
-
-void SendRecieve::myTcpWrite(char *msg, int len)
-{
- if(tcpConnected == TCP_CONNECTED) {
- tcpsocket->write(msg, len);
- } else {
- printf("TCP socket not initialized!\n");
- }
-}
-
-void SendRecieve::myHostFound() // slot
-{
- printf("Host Found!\n");
-}
-
-void SendRecieve::myConnected() // slot
-{
- tcpConnected = TCP_CONNECTED;
- printf("TCP Connected!\n");
-
- QByteArray ba = xml_req->toByteArray();
- char *request = ba.data();
- myTcpWrite(request, ba.length());
-}
-
-void SendRecieve::myDisconnected() // slot
-{
- tcpConnected = TCP_DISCONNECTED;
- printf("TCP Disconnected!\n");
-
- // Result recieved, allow getResult to return ba_all
- has_result = true;
-}
-
-void SendRecieve::myError(QAbstractSocket::SocketError socketError) // slot
-{
- tcpConnected = TCP_ERROR;
- switch (socketError) {
- case 0 :
- QMessageBox::information(NULL, tr("Pracro"), tr("Couldn't connect to server.\nPlease contact a system administrator to resolve the problem."));
- exit(1);
- break;
- case 2 :
- QMessageBox::information(NULL, tr("Pracro"), tr("Couldn't connect to server.\nPlease contact a system administrator to resolve the problem."));
- exit(1);
- break;
- default:
- ;
- }
-}
-
-void SendRecieve::myReadyReadHandler()
-{
- QByteArray ba;
-
- ba = tcpsocket->readAll();
- ba_all.append(ba);
-
- // HACK: To test if the received data is a complete xml document.
- QDomDocument doc;
- if(doc.setContent(ba_all)) {
- has_result = true;
- }
-}
-
-QByteArray SendRecieve::getResult()
-{
- while(has_result == false) {
- qApp->processEvents();
- }
- return ba_all;
-}