summaryrefslogtreecommitdiff
path: root/server/src/server.cc
diff options
context:
space:
mode:
Diffstat (limited to 'server/src/server.cc')
-rw-r--r--server/src/server.cc252
1 files changed, 120 insertions, 132 deletions
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*/