aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/webserver
diff options
context:
space:
mode:
authorRenaud G <renaud@rolisteam.org>2016-12-04 12:50:35 +0100
committerRenaud G <renaud@rolisteam.org>2016-12-04 12:50:35 +0100
commitde3469b64b2ea11a426023fea5f11b03bd98750b (patch)
tree4154fd0bf248b4e9bc278b816a46168e5a5bbfcd /webserver
parentc3af56e2b43ee8007cf1b150c5484e991d04e59a (diff)
downloadOneRoll-de3469b64b2ea11a426023fea5f11b03bd98750b.tar.gz
OneRoll-de3469b64b2ea11a426023fea5f11b03bd98750b.zip
-First commit about webserver.
Diffstat (limited to 'webserver')
-rw-r--r--webserver/CMakeLists.txt78
-rw-r--r--webserver/diceserver.cpp230
-rw-r--r--webserver/diceserver.h19
-rw-r--r--webserver/main.cpp33
4 files changed, 360 insertions, 0 deletions
diff --git a/webserver/CMakeLists.txt b/webserver/CMakeLists.txt
new file mode 100644
index 0000000..7bfa2d2
--- /dev/null
+++ b/webserver/CMakeLists.txt
@@ -0,0 +1,78 @@
+cmake_minimum_required(VERSION 2.8)
+
+option(UPDATE_TRANSLATIONS "update Translation" OFF)
+MESSAGE(STATUS "UPDATE TRANSLATIONS: ${UPDATE_TRANSLATIONS}")
+
+
+project(diceserver)
+
+
+# Find includes in corresponding build directories
+set(CMAKE_INCLUDE_CURRENT_DIR ON)
+# Instruct CMake to run moc automatically when needed.
+set(CMAKE_AUTOMOC ON)
+
+# Find the QtWidgets library
+find_package(Qt5 COMPONENTS Core Network)
+#find_package(Qt5QuickCompiler)
+
+set(EXECUTABLE_OUTPUT_PATH bin/)
+
+include_directories(${Qt5Core_INCLUDES} ${Qt5Network_INCLUDES} webserver/qhttp/src ../)
+add_definitions(${Qt5Core_DEFINITIONS} ${Qt5Network_DEFINITIONS} )
+
+ADD_DEFINITIONS(
+ -std=c++11
+)
+
+#set(diceserver_RESOURCES diceparser.qrc)
+FIND_PACKAGE(Qt5LinguistTools)
+
+
+
+SET( diceserver_sources
+ ../diceparser.cpp
+ ../range.cpp
+ ../booleancondition.cpp
+ ../validator.cpp
+ ../compositevalidator.cpp
+ ../operationcondition.cpp
+ ../die.cpp
+ ../parsingtoolbox.cpp
+ ../dicealias.cpp
+ ../result/result.cpp
+ ../result/scalarresult.cpp
+ ../result/stringresult.cpp
+ ../result/diceresult.cpp
+ ../node/countexecutenode.cpp
+ ../node/dicerollernode.cpp
+ ../node/executionnode.cpp
+ ../node/explosedicenode.cpp
+ ../node/helpnode.cpp
+ ../node/mergenode.cpp
+ ../node/jumpbackwardnode.cpp
+ ../node/keepdiceexecnode.cpp
+ ../node/listaliasnode.cpp
+ ../node/listsetrollnode.cpp
+ ../node/numbernode.cpp
+ ../node/parenthesesnode.cpp
+ ../node/paintnode.cpp
+ ../node/rerolldicenode.cpp
+ ../node/scalaroperatornode.cpp
+ ../node/sortresult.cpp
+ ../node/startingnode.cpp
+ ../node/ifnode.cpp
+ ../node/filternode.cpp
+ ../node/stringnode.cpp
+ main.cpp
+ diceserver.cpp
+ ../highlightdice.cpp
+)
+#qt5_add_resources(RESOURCE_ADDED mobile.qrc)
+
+add_executable( diceserver ${diceserver_sources} )
+
+target_link_libraries(diceserver ${Qt5Core_LIBRARIES} ${Qt5Network_LIBRARIES} /home/renaud/application/mine/DiceParser/webserver/qhttp/xbin/libqhttp.so)
+INSTALL_TARGETS(/bin diceserver)
+
+#qt5_use_modules()
diff --git a/webserver/diceserver.cpp b/webserver/diceserver.cpp
new file mode 100644
index 0000000..7332f81
--- /dev/null
+++ b/webserver/diceserver.cpp
@@ -0,0 +1,230 @@
+#include "diceserver.h"
+#include "qhttp/src/qhttpserver.hpp"
+#include "qhttp/src/qhttpserverrequest.hpp"
+#include "qhttp/src/qhttpserverresponse.hpp"
+#include "qhttp/src/qhttpfwd.hpp"
+#include <QHostAddress>
+#include <QUrl>
+
+DiceServer::DiceServer(int port)
+ : QObject(),m_diceParser(new DiceParser())
+{
+ // using namespace ;
+ m_server = new qhttp::server::QHttpServer(this);
+ m_server->listen( // listening on 0.0.0.0:8080
+ QHostAddress::Any, port,
+ [=](qhttp::server::QHttpRequest* req, qhttp::server::QHttpResponse* res)
+ {
+ req->collectData(1024);
+
+ // qhttp::THeaderHash hash = req->headers();
+ // qDebug() << hash << res->headers() << qhttp::Stringify::toString(req->method()) << qPrintable(req->url().toString()) << req->collectedData().constData();
+ QString getArg = req->url().toString();
+ getArg=getArg.replace("/?","");
+ QStringList args = getArg.split('&');
+ QHash<QString,QString> m_hashArgs ;
+ for( auto argument : args)
+ {
+ QStringList keyValue = argument.split('=');
+ if(keyValue.size()==2)
+ {
+ m_hashArgs.insert(keyValue[0],keyValue[1]);
+ }
+ }
+
+ if(m_hashArgs.contains("cmd"))
+ {
+ qDebug() << QUrl::fromPercentEncoding(m_hashArgs["cmd"].toLocal8Bit());
+ QString result = startDiceParsing(QUrl::fromPercentEncoding(m_hashArgs["cmd"].toLocal8Bit()));
+
+ res->setStatusCode(qhttp::ESTATUS_OK);
+ QString html("<!doctype html>\n"
+ "<html>\n"
+ "<head>\n"
+ "</head>\n"
+ " <meta charset=\"utf-8\">\n"
+ " <title>Rolisteam Dice System Webservice</title>\n"
+ " <style>.dice {color:#FF0000;font-weight: bold;}</style>"
+ "<body>\n"
+ "%1\n"
+ "</body>\n"
+ "</html>\n");
+
+ res->end(html.arg(result).toLocal8Bit());
+ }
+ else
+ {
+ res->setStatusCode(qhttp::ESTATUS_OK);
+ res->end("No Command found!\n");
+ }
+
+ });
+ if ( !m_server->isListening() ) {
+ qDebug() << "failed to listen";
+
+ }
+ else
+ {
+ qDebug()<< "Server is On!!";
+ }
+}
+
+DiceServer::~DiceServer()
+{
+ qDebug()<< "destructor";
+}
+QString DiceServer::diceToText(ExportedDiceResult& dice,bool highlight,bool homogeneous)
+{
+ QStringList resultGlobal;
+ foreach(int face, dice.keys())
+ {
+ QStringList result;
+ QStringList currentStreak;
+ QList<QStringList> allStreakList;
+ ListDiceResult diceResult = dice.value(face);
+ bool previousHighlight=false;
+ QString previousColor;
+ QString patternColor("<span class=\"dice\">");
+ foreach (HighLightDice tmp, diceResult)
+ {
+ if(previousColor != tmp.getColor())
+ {
+ if(!currentStreak.isEmpty())
+ {
+ QStringList list;
+ list << patternColor+currentStreak.join(',')+"</span>";
+ allStreakList.append(list);
+ currentStreak.clear();
+ }
+ if(tmp.getColor().isEmpty())
+ {
+ patternColor = QStringLiteral("<span class=\"dice\">");
+ }
+ else
+ {
+ patternColor = QStringLiteral("<span style=\"color:%1;font-weight:bold\">").arg(tmp.getColor());
+ }
+ }
+ QStringList diceListStr;
+ if((previousHighlight)&&(!tmp.isHighlighted()))
+ {
+ if(!currentStreak.isEmpty())
+ {
+ QStringList list;
+ list << patternColor+currentStreak.join(',')+"</span>";
+ allStreakList.append(list);
+ currentStreak.clear();
+ }
+
+ }
+ else if((!previousHighlight)&&(tmp.isHighlighted()))
+ {
+ if(!currentStreak.isEmpty())
+ {
+ QStringList list;
+ list << currentStreak.join(',');
+ allStreakList.append(list);
+ currentStreak.clear();
+ }
+ }
+ previousHighlight = tmp.isHighlighted();
+ previousColor = tmp.getColor();
+ for(int i =0; i < tmp.getResult().size(); ++i)
+ {
+ qint64 dievalue = tmp.getResult()[i];
+ diceListStr << QString::number(dievalue);
+ }
+ if(diceListStr.size()>1)
+ {
+ QString first = diceListStr.takeFirst();
+ first = QString("%1 [%2]").arg(first).arg(diceListStr.join(','));
+ diceListStr.clear();
+ diceListStr << first;
+ }
+ currentStreak << diceListStr.join(' ');
+ }
+
+ if(previousHighlight)
+ {
+ QStringList list;
+ list << patternColor+currentStreak.join(',')+"</span>";
+ allStreakList.append(list);
+ }
+ else
+ {
+ if(!currentStreak.isEmpty())
+ {
+ QStringList list;
+ list << currentStreak.join(',');
+ allStreakList.append(list);
+ }
+ }
+ foreach(QStringList a, allStreakList)
+ {
+ result << a;
+ }
+ if(dice.keys().size()>1)
+ {
+ resultGlobal << QString(" d%2:(%1)").arg(result.join(",")).arg(face);
+ }
+ else
+ {
+ resultGlobal << result.join(",");
+ }
+ }
+ return resultGlobal.join("");
+}
+
+QString DiceServer::startDiceParsing(QString cmd)
+{
+ QString result("");
+ bool highlight = true;
+ if(m_diceParser->parseLine(cmd))
+ {
+ m_diceParser->Start();
+ if(!m_diceParser->getErrorMap().isEmpty())
+ {
+ result += "<span style=\"color: #FF0000\">Error:</span>" + m_diceParser->humanReadableError() + "<br/>";
+ }
+ else
+ {
+
+ ExportedDiceResult list;
+ bool homogeneous = true;
+ m_diceParser->getLastDiceResult(list,homogeneous);
+ QString diceText = diceToText(list,highlight,homogeneous);
+ QString scalarText;
+ QString str;
+
+ if(m_diceParser->hasIntegerResultNotInFirst())
+ {
+ scalarText = QString("%1").arg(m_diceParser->getLastIntegerResult());
+ }
+ else if(!list.isEmpty())
+ {
+ scalarText = QString("%1").arg(m_diceParser->getSumOfDiceResult());
+ }
+ if(highlight)
+ {
+ str = QString("Result: <span class=\"dice\">%1</span>, details:[%3 (%2)]").arg(scalarText).arg(diceText).arg(m_diceParser->getDiceCommand());
+ }
+ else
+ {
+ str = QString("Result: %1, details:[%3 (%2)]").arg(scalarText).arg(diceText).arg(m_diceParser->getDiceCommand());
+ }
+
+ if(m_diceParser->hasStringResult())
+ {
+ str = m_diceParser->getStringResult();
+ }
+ result += str + "<br/>";
+ }
+ }
+ else
+ {
+ result += "<span style=\"color: #00FF00\">Error:</span>" + m_diceParser->humanReadableError() + "<br/>";
+ }
+
+
+ return result;
+}
diff --git a/webserver/diceserver.h b/webserver/diceserver.h
new file mode 100644
index 0000000..330b71a
--- /dev/null
+++ b/webserver/diceserver.h
@@ -0,0 +1,19 @@
+#include <QObject>
+#include "diceparser.h"
+#include "qhttp/src/qhttpserver.hpp"
+
+
+class DiceServer : public QObject
+{
+ Q_OBJECT
+public:
+ DiceServer(int port = 8085);
+ virtual ~DiceServer();
+
+
+ QString startDiceParsing(QString cmd);
+ QString diceToText(ExportedDiceResult &dice, bool highlight, bool homogeneous);
+private:
+ DiceParser* m_diceParser;
+ qhttp::server::QHttpServer* m_server;
+};
diff --git a/webserver/main.cpp b/webserver/main.cpp
new file mode 100644
index 0000000..10952f1
--- /dev/null
+++ b/webserver/main.cpp
@@ -0,0 +1,33 @@
+/***************************************************************************
+ * Copyright (C) 2016 by Renaud Guezennec *
+ * http://www.rolisteam.org/contact *
+ * *
+ * rolisteam 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. *
+ * *
+ * This program 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 this program; if not, write to the *
+ * Free Software Foundation, Inc., *
+ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
+ ***************************************************************************/
+#include <QCoreApplication>
+
+#include "diceparser.h"
+
+#include "diceserver.h"
+
+int main(int argc, char *argv[])
+{
+ QCoreApplication app(argc, argv);
+
+ DiceServer diceServer;
+ diceServer.setParent(&app);
+ return app.exec();
+}