summaryrefslogtreecommitdiff
path: root/server
diff options
context:
space:
mode:
authordeva <deva>2008-05-20 10:17:14 +0000
committerdeva <deva>2008-05-20 10:17:14 +0000
commit91d56787bec83a3a7c2ad6afd84d2b82c5d869b9 (patch)
tree32cfe22c3024de280c7516b561b8bfd98b266ac4 /server
parent7d81d456702e3c5b0caead6e44f2c2f236d2fa41 (diff)
Removed obsolete macro.h and macro_parser.cc,h
Diffstat (limited to 'server')
-rw-r--r--server/src/Makefile.am22
-rw-r--r--server/src/database.cc6
-rw-r--r--server/src/database.h3
-rw-r--r--server/src/macro.h63
-rw-r--r--server/src/macro_parser.cc205
-rw-r--r--server/src/server.cc252
-rw-r--r--server/src/tcpsocket.cc4
-rw-r--r--server/src/templateparser.cc91
-rw-r--r--server/src/templateparser.h2
-rw-r--r--server/src/widgetgenerator.cc77
-rw-r--r--server/src/widgetgenerator.h (renamed from server/src/macro_parser.h)38
11 files changed, 308 insertions, 455 deletions
diff --git a/server/src/Makefile.am b/server/src/Makefile.am
index 7077301..52eade2 100644
--- a/server/src/Makefile.am
+++ b/server/src/Makefile.am
@@ -1,7 +1,7 @@
bin_PROGRAMS = pracrod
-pracrod_LDADD = $(LD_EFENCE) $(PQXX_LIBS) $(CONFIG_LIBS) $(LUA_LIBS)
+pracrod_LDADD = $(LD_EFENCE) $(PQXX_LIBS) $(CONFIG_LIBS) $(LUA_LIBS) -lpthread
pracrod_CXXFLAGS = $(PQXX_CXXFLAGS) $(CONFIG_CXXFLAGS) $(LUA_CXXFLAGS)
@@ -16,7 +16,6 @@ pracrod_SOURCES = \
journal_commit.cc \
log.cc \
luaquerymapper.cc \
- macro_parser.cc \
resumeparser.cc \
saxparser.cc \
server.cc \
@@ -24,7 +23,8 @@ pracrod_SOURCES = \
transactionparser.cc \
tcpsocket.cc \
tostring.cc \
- uid.cc
+ uid.cc \
+ widgetgenerator.cc
EXTRA_DIST = \
configuration.h \
@@ -37,8 +37,6 @@ EXTRA_DIST = \
journal_commit.h \
log.h \
luaquerymapper.h \
- macro.h \
- macro_parser.h \
resumeparser.h \
saxparser.h \
server.h \
@@ -46,13 +44,15 @@ EXTRA_DIST = \
transactionparser.h \
tcpsocket.h \
tostring.h \
- uid.h
+ uid.h \
+ widgetgenerator.h
TESTFILES = \
test_queryhandler \
test_queryparser \
test_luaquerymapper \
- test_templateparser
+ test_templateparser \
+ test_server
TESTLOGS = `for F in ${TESTFILES}; do echo $$F.log; done`
@@ -72,6 +72,10 @@ test_luaquerymapper: luaquerymapper.cc
@../../tools/test luaquerymapper.cc queryparser.cc queryhandler.cc tcpsocket.cc exception.cc tostring.cc uid.cc log.cc saxparser.cc -lexpat $(LUA_LIBS)
test_templateparser: templateparser.cc
- @../../tools/test templateparser.cc saxparser.cc -lexpat
+ @../../tools/test templateparser.cc saxparser.cc -lexpat -DXML="\"../xml\""
-CLEANFILES = $(TESTFILES) $(TESTLOGS)
+test_server: server.cc
+ @../../tools/test server.cc templateparser.cc saxparser.cc queryparser.cc queryhandler.cc luaquerymapper.cc tcpsocket.cc exception.cc log.cc configuration.cc tostring.cc transactionparser.cc widgetgenerator.cc -lexpat $(LUA_LIBS) $(CONFIG_LIBS) -DXML="\"../xml\""
+ killall -9 test_server
+
+CLEANFILES = $(TESTFILES) $(TESTLOGS) *~
diff --git a/server/src/database.cc b/server/src/database.cc
index ad6bbe0..b2d5589 100644
--- a/server/src/database.cc
+++ b/server/src/database.cc
@@ -41,8 +41,10 @@ Database::~Database()
{
}
-int Database::post(std::string &user, std::string &cpr, std::string &time, Commit &commit)
+int Database::post(std::string &user, std::string &cpr, time_t now, Commit &commit)
{
+ char timestamp[32];
+ sprintf(timestamp, "%u", now);
UID uid;
try {
@@ -56,7 +58,7 @@ int Database::post(std::string &user, std::string &cpr, std::string &time, Commi
uid.toString() + "', '" +
commit.macro + "', '" +
commit.version + "', '" +
- time + "', '" +
+ timestamp + "', '" +
user + "')";
W.exec(sql);
diff --git a/server/src/database.h b/server/src/database.h
index a841188..91a09f4 100644
--- a/server/src/database.h
+++ b/server/src/database.h
@@ -30,6 +30,7 @@
#include <pqxx/pqxx>
#include <string>
#include "transaction.h"
+#include <time.h>
class Database {
public:
@@ -38,7 +39,7 @@ public:
std::string password = "pracro");
~Database();
- int post(std::string &user, std::string &cpr, std::string &time, Commit &commit);
+ int post(std::string &user, std::string &cpr, time_t now, Commit &commit);
private:
pqxx::connection c;
diff --git a/server/src/macro.h b/server/src/macro.h
deleted file mode 100644
index 9d4692f..0000000
--- a/server/src/macro.h
+++ /dev/null
@@ -1,63 +0,0 @@
-/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
-/***************************************************************************
- * macro.h
- *
- * Mon Sep 24 10:51:43 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.
- */
-#ifndef __PRACRO_MACRO_H__
-#define __PRACRO_MACRO_H__
-
-#include <string>
-#include <vector>
-#include <map>
-
-/*
-class WidgetProperty {
-public:
- std::string name;
- std::string value;
-};
-*/
-//typedef std::vector< WidgetProperty > WidgetPropertyList;
-typedef std::map< std::string, std::string > WidgetPropertyList;
-
-class Widget;
-typedef std::vector< Widget > WidgetList;
-
-class Widget {
-public:
- std::string type;
- WidgetPropertyList properties;
- WidgetList widgets;
-};
-
-class Macro {
-public:
- std::string name;
- std::string version;
- std::string format;
-
- WidgetList widgets;
-};
-
-#endif/*__PRACRO_MACRO_H__*/
diff --git a/server/src/macro_parser.cc b/server/src/macro_parser.cc
deleted file mode 100644
index 84572d3..0000000
--- a/server/src/macro_parser.cc
+++ /dev/null
@@ -1,205 +0,0 @@
-/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
-/***************************************************************************
- * macro_parser.cc
- *
- * Mon Sep 24 10:49:55 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 "macro_parser.h"
-
-// For XML
-#include <config.h>
-
-// For open, read and close
-#include <sys/types.h>
-#include <sys/stat.h>
-#include <fcntl.h>
-#include <unistd.h>
-
-#include <stdio.h>
-#include <string.h>
-
-MacroParser::MacroParser(std::string name, Macro &macro)
-{
- this->macro = &macro;
-
- std::string macrofile = std::string(XML) + "/" + name + ".xml";
- fd = open(macrofile.c_str(), O_RDONLY);
-
- if(fd == -1) {
- printf("Cannot open file \"%s\"...", macrofile.c_str());
- printf("failed!\n");
- return;
- }
-}
-
-MacroParser::~MacroParser()
-{
- if(fd != -1) close(fd);
-}
-
-int MacroParser::readData(char *data, size_t size)
-{
- return read(fd, data, size);
-}
-
-void MacroParser::startTag(std::string name, std::map< std::string, std::string> attributes)
-{
- if(name == "include") {
- Macro inc;
- MacroParser parser(attributes["name"], inc);
- parser.parse();
-
- WidgetList::iterator w = inc.widgets.front().widgets.begin();
- while(w != inc.widgets.front().widgets.end()) {
- stack.back()->widgets.push_back(*w);
- w++;
- }
-
- return; // Don't do further parsing of this tag.
- }
-
- if(name == "macro") {
-
- macro->name = attributes["name"];
- macro->version = attributes["version"];
- macro->format = attributes["resume"];
-
- return; // Don't do further parsing of this tag.
- }
-
- if(name == "macrolist") {
- std::map< std::string, int > macro_usage;
-
- macro_usage["example"] = 3;
- macro_usage["pimmelsus"] = 2;
- macro_usage["dimmer"] = 1;
- macro_usage["fnullemam"] = 2;
- macro_usage["ektosup"] = 0;
- macro_usage["fillehej"] = 0;
- macro_usage["heppelis"] = 0;
- macro_usage["simmermuh"] = 0;
- macro_usage["skilletran"] = 0;
- macro_usage["futterbah"] = 0;
-
- Widget macrolist;
- Widget *wp;
-
- if(stack.size() > 0) {// We only pushback the child if there is a parent.
- stack.back()->widgets.push_back(macrolist);
- wp = &stack.back()->widgets.back();
- } else {
- macro->widgets.push_back(macrolist);
- wp = &macro->widgets.back();
- }
- stack.push_back(wp);
-
- wp->type = "listbox";
- wp->properties["name"] = attributes["name"];
- wp->properties["value"] = "example";
- wp->properties["help"] = attributes["Noget info tekst."];
-
- {
- Widget item;
- item.type = "item";
- item.properties["type"] = "header";
- item.properties["caption"] = "Oftest brugte makroer:";
- wp->widgets.push_back(item);
- }
-
- std::map< std::string, std::string > macro_usage_most;
- std::map< std::string, int >::iterator i = macro_usage.begin();
- while(i != macro_usage.end()) {
- char buf[32];
- // FIXME: Ugly hack to make sorting work on equal usage number (sub sort alphabetically)
- // This only works when usage count is less than 999.
- sprintf(buf, "%03d", 999 - (*i).second);
- std::string key = std::string(buf) + "-" + (*i).first;
- if((*i).second != 0) macro_usage_most[key] = (*i).first;
- i++;
- }
-
- std::map< std::string, std::string >::iterator j = macro_usage_most.begin();
- while(j != macro_usage_most.end()) {
- Widget item;
- item.type = "item";
- item.properties["value"] = (*j).second;
- item.properties["caption"] = (*j).second;
- wp->widgets.push_back(item);
- j++;
- }
-
- {
- Widget item;
- item.type = "item";
- item.properties["type"] = "separator";
- wp->widgets.push_back(item);
- }
-
- {
- Widget item;
- item.type = "item";
- item.properties["type"] = "header";
- item.properties["caption"] = "Alle makroer alfabetisk:";
- wp->widgets.push_back(item);
- }
-
- std::map< std::string, int >::iterator k = macro_usage.begin();
- while(k != macro_usage.end()) {
- Widget item;
- item.type = "item";
- item.properties["value"] = (*k).first;
- item.properties["caption"] = (*k).first;
- wp->widgets.push_back(item);
- k++;
- }
-
- return;// Don't do further parsing of this tag.
- }
-
- Widget widget;
- widget.type = name;
-
- Widget *wp;
-
- if(stack.size() > 0) {// We only pushback the child if there is a parent.
- stack.back()->widgets.push_back(widget);
- wp = &stack.back()->widgets.back();
- } else {
- macro->widgets.push_back(widget);
- wp = &macro->widgets.back();
- }
- stack.push_back(wp);
-
- std::map< std::string, std::string >::iterator i = attributes.begin();
- while(i != attributes.end()) {
- wp->properties[i->first] = i->second;
-
- i++;
- }
-}
-
-void MacroParser::endTag(std::string name)
-{
- if(name != "include") stack.pop_back();
- // if(!strcmp(el, "macro")) done = true;
-}
diff --git a/server/src/server.cc b/server/src/server.cc
index 4254f47..1786f34 100644
--- a/server/src/server.cc
+++ b/server/src/server.cc
@@ -26,9 +26,6 @@
*/
#include "server.h"
-// For XML
-#include <config.h>
-
#include "tcpsocket.h"
#include <errno.h>
@@ -39,53 +36,120 @@
#include <unistd.h>
#include "configuration.h"
-
#include "transaction.h"
#include "transactionparser.h"
#include "templateparser.h"
#include "queryhandler.h"
#include "queryparser.h"
#include "luaquerymapper.h"
-
#include "database.h"
+#include "widgetgenerator.h"
+#include "resumeparser.h"
+#include "journal_commit.h"
static void connection(TCPSocket &socket)
{
- Transaction t;
- TransactionParser parser(socket, t);
+ Transaction transaction;
+ TransactionParser parser(socket, transaction);
parser.parse();
+ socket.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
+ socket.write("<pracro version=\"1.0\">\n");
+
+ /*
+ //
+ // Handle commits
+ //
+ if(transaction.commits.size() > 0) {
+ time_t now = time(NULL);
+
+ Commits::iterator i = transaction.commits.begin();
+ while(i != transaction.commits.end()) {
+ Commit &commit = *i;
+
+ Database db;
+ db.post(transaction.user, transaction.cpr, now, commit);
+
+ Macro macro;
+ MacroParser parser(commit.macro, macro);
+ parser.parse();
+
+ std::string resume = resume_parser(macro.format.c_str(), commit);
+
+ std::string journal_commit_addr = config()->lookup("journal_commit_addr");
+ int journal_commit_port = config()->lookup("journal_commit_port");
+
+ journal_commit(transaction.cpr.c_str(), transaction.user.c_str(),
+ journal_commit_addr.c_str(), journal_commit_port,
+ resume.c_str(), resume.length());
+
+ i++;
+ }
+ }
+ */
+
+ //
// Handle requests
- Requests::iterator i = t.requests.begin();
- while(i != t.requests.end()) {
- Request request = *i;
+ //
+ Requests::iterator i = transaction.requests.begin();
+ while(i != transaction.requests.end()) {
+ Request &request = *i;
+
+ printf("Handling request - macro: %s, course: %s\n", request.macro.c_str(), request.course.c_str());
// Read and parse the template file.
- TemplateParser tp(XML"/example2.xml");
+ TemplateParser tp(request.course);
tp.parse();
+ Template *templ = tp.getTemplate();
+
// Send the queries to Pentominos (if any)
TCPSocket s;
s.connect("localhost", 11108);
- QueryHandler qh(&s, t.cpr);
- std::string result = qh.exec();
+ QueryHandler qh(&s, transaction.cpr);
+ std::vector< Macro >::iterator mi = templ->course.macroes.begin();
+ while(mi != templ->course.macroes.end()) {
+ printf("!!\n"); fflush(stdout);
+ Macro &macro = (*mi);
+ if(macro.attributes["name"] == request.macro) {
+ std::vector< Query >::iterator qi = macro.queries.begin();
+ while(qi != macro.queries.end()) {
+ qh.addQuery(*qi);
+ qi++;
+ }
+ }
+ mi++;
+ }
+ std::string result = qh.exec();
+
// Parse the result from the queries to pentominos
QueryParser qp(result);
qp.parse();
// Map the results
LUAQueryMapper lqm(qp.result);
-
-
+
+ // Generate the macro and return it to the client
+ std::vector< Macro >::iterator mi2 = templ->course.macroes.begin();
+ while(mi2 != templ->course.macroes.end()) {
+ Macro &macro = (*mi2);
+ if(macro.attributes["name"] == request.macro) {
+ widgetgenerator(socket, macro, lqm);
+ }
+ mi2++;
+ }
+
i++;
}
+ socket.write("</pracro>\n");
}
void server()
{
+ int PauseMode = 1;
int port;
try {
port = config()->lookup("port");
@@ -108,7 +172,7 @@ void server()
while(socket->connected()) {
- { // Reload if new port i assigned.
+ { // Reload if new port is assigned.
int old_port = port;
try {
port = config()->lookup("port");
@@ -122,11 +186,17 @@ void server()
delete socket;
socket = new TCPSocket();
socket->listen(port);
+ PauseMode = 0;
}
}
TCPSocket child = socket->accept();
if(child.connected()) {
+ socket->disconnect();
+ connection(child);
+ delete socket;
+
+ /*
switch(fork()) {
case -1: // error
fprintf(stderr, "Could not fork: %s\n", strerror(errno));
@@ -139,8 +209,10 @@ void server()
return;
default: // parent
+ child.disconnect();
break;
}
+ */
}
}
@@ -148,129 +220,45 @@ void server()
fprintf(stderr, "Oups... dropped out of the main loop\n");
}
+#ifdef TEST_SERVER
+char request[] =
+ "<?xml version='1.0' encoding='UTF-8'?>\n"
+ "<pracro cpr=\"2003791613\" version=\"1.0\">\n"
+ " <request macro=\"example2\" course=\"example2\"/>\n"
+ "</pracro>\n";
-
-
-
-
-
-
-
-
-
-
-
-
-
-//
-//
-// OLD CODE!
-//
-//
-#if 0
-
-#include "macro.h"
-#include "macro_parser.h"
-
-#include "resumeparser.h"
-
-#include "tostring.h"
-#include <time.h>
-
-#include "journal_commit.h"
-
-static void send_macro_widget(Widget &widget, TCPSocket &socket, std::string tabs)
+int main()
{
- socket.write(tabs + "<" + widget.type);
- WidgetPropertyList::iterator p = widget.properties.begin();
- while(p != widget.properties.end()) {
- socket.write(" " + p->first + "=\"" + p->second + "\"");
- p++;
- }
-
- if(widget.widgets.size() == 0) { // If node is empty, use short tag form
- socket.write("/>\n");
- return;
- }
-
- socket.write(">\n");
+ Configuration conf("../etc/pracrod.conf");
+ initConfig(&conf);
- WidgetList::iterator w = widget.widgets.begin();
- while(w != widget.widgets.end()) {
- send_macro_widget(*w, socket, tabs + " ");
- w++;
- }
- socket.write(tabs + "</" + widget.type + ">\n");
-}
-
-static void connection(TCPSocket &socket)
-{
- printf("Got connection...\n");
-
- Transaction transaction;
- TransactionParser parser(socket, transaction);
- parser.parse();
-
- socket.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
- socket.write("<pracro version=\"1.0\">\n");
-
- // Handle requests
- Requests::iterator i = transaction.requests.begin();
- while(i != transaction.requests.end()) {
- Request request = *i;
-
- printf("Handling request for \"%s\"...", request.macro.c_str());
+ switch(fork()) {
+ case -1: // error
+ return 1;
- Macro macro;
- MacroParser parser(request.macro, macro);
- parser.parse();
-
- socket.write(" <macro version=\"" + macro.version + "\" name=\""
- + macro.name + "\" resume=\"" + macro.format + "\">\n");
- WidgetList::iterator w = macro.widgets.begin();
- while(w != macro.widgets.end()) {
- send_macro_widget(*w, socket, " ");
- w++;
- }
- socket.write(" </macro>\n");
-
- printf("done.\n");
+ case 0: // child
+ server();
+ return 0;
- i++;
- }
-
- // Handle commits
- if(transaction.commits.size() > 0) {
- std::string now = toString((unsigned int)time(NULL));
-
- Commits::iterator i = transaction.commits.begin();
- while(i != transaction.commits.end()) {
- Commit &commit = *i;
-
- Database db;
- db.post(transaction.user, transaction.cpr, now, commit);
-
- Macro macro;
- MacroParser parser(commit.macro, macro);
- parser.parse();
-
- std::string resume = resume_parser(macro.format.c_str(), commit);
-
- std::string journal_commit_addr = config()->lookup("journal_commit_addr");
- int journal_commit_port = config()->lookup("journal_commit_port");
-
- journal_commit(transaction.cpr.c_str(), transaction.user.c_str(),
- journal_commit_addr.c_str(), journal_commit_port,
- resume.c_str(), resume.length());
-
- i++;
+ default: // parent
+ {
+ TCPSocket socket;
+ int port = config()->lookup("port");
+ socket.connect("localhost", port);
+
+ socket.write(request);
+ char buf[32];
+ memset(buf, 0, sizeof(buf));
+ while(socket.read(buf, 31)) {
+ printf(buf); fflush(stdout);
+ memset(buf, 0, sizeof(buf));
+ }
}
+ return 0;
}
-
- socket.write("</pracro>\n");
-
- printf("Done with connection.\n");
+
+ return 1;
}
-#endif/*0*/
+#endif/*TEST_SERVER*/
diff --git a/server/src/tcpsocket.cc b/server/src/tcpsocket.cc
index 90f71a5..d361b7b 100644
--- a/server/src/tcpsocket.cc
+++ b/server/src/tcpsocket.cc
@@ -230,7 +230,7 @@ int TCPSocket::read(char *buf, int size)
// Wait until something is ready to be read ( peek'a'loop ).
errno = EAGAIN;
while( recv(sock, buf, 1, MSG_PEEK) == -1 && errno == EAGAIN) {
- sleep(1);
+ usleep(10);
}
// Now read it
@@ -238,6 +238,8 @@ int TCPSocket::read(char *buf, int size)
throw TCPReadException(strerror(errno));
}
+ // fwrite(buf, size, 1, stdout); fflush(stdout);
+
return res;
}
diff --git a/server/src/templateparser.cc b/server/src/templateparser.cc
index 956a0e8..652a870 100644
--- a/server/src/templateparser.cc
+++ b/server/src/templateparser.cc
@@ -38,6 +38,13 @@
// For vprintf and friends
#include <stdarg.h>
+#ifndef XML
+// For XML
+#include <config.h>
+#endif/*XML*/
+
+#include <errno.h>
+
void TemplateParser::error(char* fmt, ...)
{
// TODO: Throw exception here.
@@ -52,24 +59,37 @@ void TemplateParser::error(char* fmt, ...)
fprintf(stderr, "\n");
}
-TemplateParser::TemplateParser(std::string templatefile)
+TemplateParser::TemplateParser(std::string course)
{
state = UNDEFINED;
t = NULL;
current_macro = NULL;
current_map = NULL;
- fd = open(templatefile.c_str(), O_RDONLY);
- if(fd == -1) error("Could not open file %s", templatefile.c_str());
+
+ std::string file = XML"/";
+ file.append(course);
+ file.append(".xml");
+
+ printf("Using template file: %s\n", file.c_str());
+
+ fd = open(file.c_str(), O_RDONLY);
+ if(fd == -1) error("Could not open file %s", file.c_str());
}
TemplateParser::~TemplateParser()
{
if(fd != -1) close(fd);
+ if(t) delete t;
}
void TemplateParser::characterData(std::string &data)
{
- if(state == MAP) current_macro->maps.back().attributes["lua"].append(data);
+ if(state == MAP) {
+ // assert(current_macro->maps.size()); // No maps present!
+ // current_macro->maps.back().attributes["lua"].append(data);
+ assert(current_map); // No map present!
+ current_map->attributes["lua"].append(data);
+ }
}
void TemplateParser::startTag(std::string name, std::map< std::string, std::string> attributes)
@@ -107,7 +127,7 @@ void TemplateParser::startTag(std::string name, std::map< std::string, std::stri
assert(t); // A Template has not yet been allocated, cannot create macro!
Macro m;
- m.attributes = attributes;
+ // m.attributes = attributes;
t->course.macroes.push_back(m);
current_macro = &(t->course.macroes.back());
@@ -158,19 +178,27 @@ void TemplateParser::startTag(std::string name, std::map< std::string, std::stri
Map m;
m.attributes = attributes;
current_macro->maps.push_back(m);
+ current_map = &(current_macro->maps.back());
return;
}
// Enable widget parsing
if(name == "window") {
+
if(state != MACRO) error("window found outside macro.");
state = WINDOW;
assert(current_macro); // No macro is currently available, cannot create window!
current_macro->window.attributes = attributes;
- widgetstack.push_back(&(current_macro->window));
+ current_macro->window.attributes["type"] = name;
+
+ Widget *current = &(current_macro->window);
+
+ printf("%p %p\n", current); fflush(stdout);
+
+ widgetstack.push_back(current);
return;
}
@@ -183,8 +211,22 @@ void TemplateParser::startTag(std::string name, std::map< std::string, std::stri
Widget w;
w.attributes = attributes;
- widgetstack.back()->widgets.push_back(w);
- widgetstack.push_back(&(widgetstack.back()->widgets.back()));
+ w.attributes["type"] = name;
+
+ printf("1 %d\n", widgetstack.size() ); fflush(stdout);
+
+ Widget *parent = widgetstack.back();
+
+ printf("%d\n", parent->widgets.size()); fflush(stdout);
+
+ parent->widgets.push_back(w);
+
+ printf("2\n"); fflush(stdout);
+
+ Widget *current = &(parent->widgets.back());
+ widgetstack.push_back(current);
+
+ printf("3\n"); fflush(stdout);
return;
}
@@ -202,24 +244,43 @@ void TemplateParser::endTag(std::string name)
{
if(name == "template") state = UNDEFINED;
if(name == "course") state = TEMPLATE;
- if(name == "macro") state = COURSE;
+ if(name == "macro") {
+ current_macro = NULL;
+ state = COURSE;
+ }
if(name == "queries") state = MACRO;
if(name == "query") state = QUERIES;
if(name == "maps") state = MACRO;
- if(name == "map") state = MAPS;
+ if(name == "map") {
+ current_map = NULL;
+ state = MAPS;
+ }
if(name == "window") state = MACRO;
if(state == WINDOW) {
- assert(widgetstack.size()); // Widget stack is empty, cannot pop!
- widgetstack.pop_back();
- }
+ printf("4\n"); fflush(stdout);
+
+ assert(widgetstack.size()); // Widget stack is empty, cannot pop!
+ widgetstack.pop_back();
+
+ printf("5\n"); fflush(stdout);
+
+ if(widgetstack.size() == 0) state = MACRO;
+
+ printf("6\n"); fflush(stdout);
+ }
}
int TemplateParser::readData(char *data, size_t size)
{
if(fd == -1) return 0;
- return read(fd, data, size);
+ ssize_t r = read(fd, data, size);
+ if(r == -1) {
+ printf("Could not read...%s\n", strerror(errno)); fflush(stdout);
+ return 0;
+ }
+ return r;
}
Template *TemplateParser::getTemplate()
@@ -241,7 +302,7 @@ void print_attributes(std::string prefix,
int main()
{
- TemplateParser parser("../xml/example2.xml");
+ TemplateParser parser("example2");
parser.parse();
Template *t = parser.getTemplate();
diff --git a/server/src/templateparser.h b/server/src/templateparser.h
index 8570234..6e5fb1a 100644
--- a/server/src/templateparser.h
+++ b/server/src/templateparser.h
@@ -44,7 +44,7 @@ typedef enum {
class TemplateParser : public SAXParser {
public:
- TemplateParser(std::string templatefile);
+ TemplateParser(std::string course);
~TemplateParser();
void characterData(std::string &data);
diff --git a/server/src/widgetgenerator.cc b/server/src/widgetgenerator.cc
new file mode 100644
index 0000000..ced804a
--- /dev/null
+++ b/server/src/widgetgenerator.cc
@@ -0,0 +1,77 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/***************************************************************************
+ * widgetgenerator.cc
+ *
+ * Mon May 19 09:58:41 CEST 2008
+ * Copyright 2008 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 "widgetgenerator.h"
+
+static void send_macro_widget(Macro &macro, Widget &widget, TCPSocket &socket, std::string tabs, LUAQueryMapper &mapper)
+{
+ socket.write(tabs + "<" + widget.attributes["type"]);
+ std::map< std::string, std::string >::iterator p = widget.attributes.begin();
+
+ if(widget.attributes.find("map") != widget.attributes.end()) {
+ std::string luamap;
+
+ std::vector< Map >::iterator li = macro.maps.begin();
+ while(li != macro.maps.end()) {
+ Map &map = *li;
+ if(map.attributes["name"] == widget.attributes["map"]) {
+ luamap = map.attributes["lua"];
+ }
+ li++;
+ }
+
+ // printf("LUAMAP: %s\n", luamap.c_str()); fflush(stdout);
+
+ if(luamap != "") widget.attributes["value"] = mapper.map(luamap);
+ // widget.attributes.erase(widget.attributes.find("map"));
+ }
+
+ while(p != widget.attributes.end()) {
+ if(p->first != "type" && p->first != "map") {
+ socket.write(" " + p->first + "=\"" + p->second + "\"");
+ }
+ p++;
+ }
+
+ if(widget.widgets.size() == 0) { // If node is empty, use short tag form
+ socket.write("/>\n");
+ return;
+ }
+
+ socket.write(">\n");
+
+ std::vector< Widget >::iterator w = widget.widgets.begin();
+ while(w != widget.widgets.end()) {
+ send_macro_widget(macro, *w, socket, tabs + " ", mapper);
+ w++;
+ }
+ socket.write(tabs + "</" + widget.attributes["type"] + ">\n");
+}
+
+void widgetgenerator(TCPSocket &socket, Macro &macro, LUAQueryMapper &mapper)
+{
+ send_macro_widget(macro, macro.window, socket, " ", mapper);
+}
diff --git a/server/src/macro_parser.h b/server/src/widgetgenerator.h
index 7200551..4918848 100644
--- a/server/src/macro_parser.h
+++ b/server/src/widgetgenerator.h
@@ -1,9 +1,9 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/***************************************************************************
- * macro_parser.h
+ * widgetgenerator.h
*
- * Mon Sep 24 10:49:55 CEST 2007
- * Copyright 2007 Bent Bisballe Nyeng, Lars Bisballe Jensen and Peter Skaarup
+ * Mon May 19 09:58:41 CEST 2008
+ * Copyright 2008 Bent Bisballe Nyeng, Lars Bisballe Jensen and Peter Skaarup
* deva@aasimon.org, elsenator@gmail.com and piparum@piparum.dk
****************************************************************************/
@@ -24,31 +24,17 @@
* along with Pracro; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/
-#ifndef __PRACRO_MACRO_PARSER_H__
-#define __PRACRO_MACRO_PARSER_H__
+#ifndef __PRACRO_WIDGETGENERATOR_H__
+#define __PRACRO_WIDGETGENERATOR_H__
#include <string>
-#include "saxparser.h"
-#include "macro.h"
+#include "tcpsocket.h"
+#include "template.h"
+#include "luaquerymapper.h"
-class MacroParser : public SAXParser {
-public:
- MacroParser(std::string name, Macro &macro);
- ~MacroParser();
+void widgetgenerator(TCPSocket &socket,
+ Macro &macro,
+ LUAQueryMapper &mapper);
- void startTag(std::string name, std::map< std::string, std::string> attributes);
- void endTag(std::string name);
-
-protected:
- int readData(char *data, size_t size);
-
-private:
- Macro *macro;
- std::vector< Widget* > stack;
- bool done;
-
- int fd;
-};
-
-#endif/*__PRACRO_MACRO_PARSER_H__*/
+#endif/*__PRACRO_WIDGETGENERATOR_H__*/