From 3c199adf6c317ade446120db1cdb51b473edca57 Mon Sep 17 00:00:00 2001 From: deva Date: Mon, 18 Aug 2008 14:54:03 +0000 Subject: Rewrite of all network code, with a reusable socket. Still needs some error checking. --- client/netcom.cc | 108 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 client/netcom.cc (limited to 'client/netcom.cc') 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 + +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); +} -- cgit v1.2.3