From d22c5966c130ef7768bac7914cb4dafa74088036 Mon Sep 17 00:00:00 2001
From: deva <deva>
Date: Fri, 16 May 2008 15:00:12 +0000
Subject: Worked on the connection of the various elements of the server.

---
 server/src/Makefile.am          |   6 +-
 server/src/luaquerymapper.cc    |   5 +-
 server/src/queryhandler.cc      |   8 +-
 server/src/queryhandler.h       |  13 +--
 server/src/queryparser.cc       |   5 +-
 server/src/saxparser.cc         |   9 ++
 server/src/saxparser.h          |   1 +
 server/src/server.cc            | 192 +++++++++++++++++++++++++++-------------
 server/src/template.h           |   4 +-
 server/src/templateparser.cc    |  87 +++++++++++++++---
 server/src/templateparser.h     |   3 +-
 server/src/transaction.h        |  13 +--
 server/src/transactionparser.cc |   3 +-
 13 files changed, 235 insertions(+), 114 deletions(-)

(limited to 'server/src')

diff --git a/server/src/Makefile.am b/server/src/Makefile.am
index 7b66663..7077301 100644
--- a/server/src/Makefile.am
+++ b/server/src/Makefile.am
@@ -51,7 +51,8 @@ EXTRA_DIST = \
 TESTFILES = \
 	test_queryhandler \
 	test_queryparser \
-	test_luaquerymapper
+	test_luaquerymapper \
+	test_templateparser
 
 TESTLOGS = `for F in ${TESTFILES}; do echo $$F.log; done`
 
@@ -70,4 +71,7 @@ test_queryparser: queryparser.cc
 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
+
 CLEANFILES = $(TESTFILES) $(TESTLOGS)
diff --git a/server/src/luaquerymapper.cc b/server/src/luaquerymapper.cc
index af6074d..a09333b 100644
--- a/server/src/luaquerymapper.cc
+++ b/server/src/luaquerymapper.cc
@@ -115,8 +115,9 @@ int main()
 
   QueryHandler qh(&s, "2003791613");
 
-  Query q1("lensmeter", "lensmeter");
-  qh.addQuery(q1);
+  Query q1;
+  q1.attributes["device_id"] = "lensmeter";
+  q1.attributes["device_type"] = "lensmeter";
 
   std::string res = qh.exec();
   
diff --git a/server/src/queryhandler.cc b/server/src/queryhandler.cc
index e21d820..b78f8c4 100644
--- a/server/src/queryhandler.cc
+++ b/server/src/queryhandler.cc
@@ -176,8 +176,8 @@ std::string QueryHandler::exec()
             "                    device_type=\"%s\"\n"
             "                    filter=\"latest\"\n"
             "                    location=\"all\"/>\n",
-            query.device_id.c_str(),
-            query.device_type.c_str());
+            query.attributes["device_id"].c_str(),
+            query.attributes["device_type"].c_str());
 
     socket->write(buf, strlen(buf));
 
@@ -212,7 +212,9 @@ int main()
 
   QueryHandler qh(&s, "2003791613");
 
-  Query q1("lensmeter", "lensmeter");
+  Query q1;
+  q1.attributes["device_id"] = "lensmeter";
+  q1.attributes["device_type"] = "lensmeter";
   qh.addQuery(q1);
 
   std::string res = qh.exec();
diff --git a/server/src/queryhandler.h b/server/src/queryhandler.h
index 14e97c2..9ddca60 100644
--- a/server/src/queryhandler.h
+++ b/server/src/queryhandler.h
@@ -28,22 +28,11 @@
 #define __PRACRO_QUERYHANDLER_H__
 
 #include "tcpsocket.h"
+#include "template.h"
 
 #include <vector>
 #include <string>
 
-/**
- * Query specific values. This is the in-memory representation of a query transaction.
- */
-class Query {
-public:
-  Query(std::string id, std::string type) :
-    device_id(id), device_type(type) {}
-  std::string device_id;
-  std::string device_type;
-};
-
-
 /**
  * This class handles the query of external data.
  */
diff --git a/server/src/queryparser.cc b/server/src/queryparser.cc
index 34c7aa3..9649f1f 100644
--- a/server/src/queryparser.cc
+++ b/server/src/queryparser.cc
@@ -93,8 +93,9 @@ int main()
 
   QueryHandler qh(&s, "2003791613");
 
-  Query q1("lensmeter", "lensmeter");
-  qh.addQuery(q1);
+  Query q1;
+  q1.attributes["device_id"] = "lensmeter";
+  q1.attributes["device_type"] = "lensmeter";
 
   std::string res = qh.exec();
   
diff --git a/server/src/saxparser.cc b/server/src/saxparser.cc
index 2eabf38..e8e9eb4 100644
--- a/server/src/saxparser.cc
+++ b/server/src/saxparser.cc
@@ -26,6 +26,14 @@
  */
 #include "saxparser.h"
 
+static void character_hndl(void *p, const XML_Char *s, int len)
+{
+  SAXParser *parser = (SAXParser*)XML_GetUserData(p);
+  std::string chars;
+  chars.append(s, len);
+  parser->characterData(chars);
+}
+
 static void start_hndl(void *p, const char *el, const char **attr)
 {
   SAXParser *parser = (SAXParser*)XML_GetUserData(p);
@@ -66,6 +74,7 @@ SAXParser::SAXParser()
   XML_SetUserData(p, this);
   XML_UseParserAsHandlerArg(p);
   XML_SetElementHandler(p, start_hndl, end_hndl);
+  XML_SetCharacterDataHandler(p, character_hndl);
 }
 
 SAXParser::~SAXParser()
diff --git a/server/src/saxparser.h b/server/src/saxparser.h
index e15b38c..3cb7997 100644
--- a/server/src/saxparser.h
+++ b/server/src/saxparser.h
@@ -38,6 +38,7 @@ public:
 
   int parse();
 
+  virtual void characterData(std::string &data) {}
   virtual void startTag(std::string name, std::map< std::string, std::string> attributes) {}
   virtual void endTag(std::string name) {}
 
diff --git a/server/src/server.cc b/server/src/server.cc
index f6c1771..4254f47 100644
--- a/server/src/server.cc
+++ b/server/src/server.cc
@@ -26,6 +26,9 @@
  */
 #include "server.h"
 
+// For XML
+#include <config.h>
+
 #include "tcpsocket.h"
 #include <errno.h>
 
@@ -39,9 +42,134 @@
 
 #include "transaction.h"
 #include "transactionparser.h"
+#include "templateparser.h"
+#include "queryhandler.h"
+#include "queryparser.h"
+#include "luaquerymapper.h"
 
 #include "database.h"
 
+static void connection(TCPSocket &socket)
+{
+  Transaction t;
+  TransactionParser parser(socket, t);
+  parser.parse();
+
+  // Handle requests
+  Requests::iterator i = t.requests.begin();
+  while(i != t.requests.end()) {
+    Request request = *i;
+
+    // Read and parse the template file.
+    TemplateParser tp(XML"/example2.xml");
+    tp.parse();
+
+    // Send the queries to Pentominos (if any)
+    TCPSocket s;
+    s.connect("localhost", 11108);
+    QueryHandler qh(&s, t.cpr);
+    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);
+    
+    
+    i++;
+  }
+
+}
+
+
+void server()
+{
+  int port;
+  try {
+    port = config()->lookup("port");
+  } catch( ... ) {
+    fprintf(stderr, "Could not read port.");
+    return;
+  }
+
+  TCPSocket *socket;
+  
+  try {
+    socket = new TCPSocket();
+    socket->listen(port);
+  } catch (Exception &e) {
+    fprintf(stderr, "Error during parsing:\n%s\n",
+            e.what());
+    delete socket;
+    return;
+  }
+
+  while(socket->connected()) {
+
+    { // Reload if new port i assigned.
+      int old_port = port;
+      try {
+        port = config()->lookup("port");
+      } catch( ... ) {
+        fprintf(stderr, "Could not read port.");
+        return;
+      }
+
+      if(port != old_port) {
+        // Start listening on the new port
+        delete socket;
+        socket = new TCPSocket();
+        socket->listen(port);
+      }
+    }
+
+    TCPSocket child = socket->accept();
+    if(child.connected()) {
+      switch(fork()) {
+      case -1: // error
+        fprintf(stderr, "Could not fork: %s\n", strerror(errno));
+        break;
+        
+      case 0: // child
+        socket->disconnect();
+        connection(child);
+        delete socket;
+        return;
+        
+      default: // parent
+        break;
+      }
+    }
+  }
+
+  delete socket;
+  fprintf(stderr, "Oups... dropped out of the main loop\n");
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+//
+//
+//     OLD CODE!
+//
+//
+#if 0
+
 #include "macro.h"
 #include "macro_parser.h"
 
@@ -145,66 +273,4 @@ static void connection(TCPSocket &socket)
   printf("Done with connection.\n");
 }
 
-void server()
-{
-  int port;
-  try {
-    port = config()->lookup("port");
-  } catch( ... ) {
-    fprintf(stderr, "Could not read port.");
-    return;
-  }
-
-  TCPSocket *socket;
-  
-  try {
-    socket = new TCPSocket();
-    socket->listen(port);
-  } catch (Exception &e) {
-    fprintf(stderr, "Error during parsing:\n%s\n",
-            e.what());
-    delete socket;
-    return;
-  }
-
-  while(socket->connected()) {
-
-    { // Reload if new port i assigned.
-      int old_port = port;
-      try {
-        port = config()->lookup("port");
-      } catch( ... ) {
-        fprintf(stderr, "Could not read port.");
-        return;
-      }
-
-      if(port != old_port) {
-        // Start listening on the new port
-        delete socket;
-        socket = new TCPSocket();
-        socket->listen(port);
-      }
-    }
-
-    TCPSocket child = socket->accept();
-    if(child.connected()) {
-      switch(fork()) {
-      case -1: // error
-        fprintf(stderr, "Could not fork: %s\n", strerror(errno));
-        break;
-        
-      case 0: // child
-        socket->disconnect();
-        connection(child);
-        delete socket;
-        return;
-        
-      default: // parent
-        break;
-      }
-    }
-  }
-
-  delete socket;
-  fprintf(stderr, "Oups... dropped out of the main loop\n");
-}
+#endif/*0*/
diff --git a/server/src/template.h b/server/src/template.h
index e3b030c..0884c08 100644
--- a/server/src/template.h
+++ b/server/src/template.h
@@ -54,7 +54,7 @@ public:
   std::map< std::string, std::string > attributes;
 };
 
-class MacroSequence {
+class Course {
 public:
   std::vector< Macro > macroes;
   std::map< std::string, std::string > attributes;
@@ -62,7 +62,7 @@ public:
 
 class Template {
 public:
-  MacroSequence macrosequence;
+  Course course;
   std::map< std::string, std::string > attributes;
 };
 
diff --git a/server/src/templateparser.cc b/server/src/templateparser.cc
index 1e6ddc7..956a0e8 100644
--- a/server/src/templateparser.cc
+++ b/server/src/templateparser.cc
@@ -67,9 +67,14 @@ TemplateParser::~TemplateParser()
   if(fd != -1) close(fd);
 }
 
+void TemplateParser::characterData(std::string &data)
+{
+  if(state == MAP) current_macro->maps.back().attributes["lua"].append(data);
+}
+
 void TemplateParser::startTag(std::string name, std::map< std::string, std::string> attributes)
 {
-  // Create template and enable parsing of macrosequences
+  // Create template and enable parsing of courses
   if(name == "template") {
     if(state != UNDEFINED) error("template found not in outer level.");
     state = TEMPLATE;
@@ -83,35 +88,35 @@ void TemplateParser::startTag(std::string name, std::map< std::string, std::stri
   }
 
   // Enable macro parsing
-  if(name == "macrosequence") {
-    if(state != TEMPLATE) error("macrosequence found outside template.");
-    state = MACROSEQUENCE;
+  if(name == "course") {
+    if(state != TEMPLATE) error("course found outside template.");
+    state = COURSE;
 
-    assert(t); // A Template has not yet been allocated, cannot create macrosequence!
+    assert(t); // A Template has not yet been allocated, cannot create course!
 
-    t->macrosequence.attributes = attributes;
+    t->course.attributes = attributes;
 
     return;
   }
 
   // Create macro and enable parsing of queries, maps and window
   if(name == "macro") {
-    if(state != MACROSEQUENCE) error("macro found outside macrosequence.");
+    if(state != COURSE) error("macro found outside course.");
     state = MACRO;
 
     assert(t); // A Template has not yet been allocated, cannot create macro!
     
     Macro m;
     m.attributes = attributes;
-    t->macrosequence.macroes.push_back(m);
-    current_macro = &(t->macrosequence.macroes.back());
+    t->course.macroes.push_back(m);
+    current_macro = &(t->course.macroes.back());
 
     return;
   }
 
   // Enable Query parsing
   if(name == "queries") {
-    if(state != TEMPLATE) error("queries found outside template.");
+    if(state != MACRO) error("queries found outside macro.");
     state = QUERIES;
     
     assert(current_macro); // No macro is currently available, cannot create queries!
@@ -135,7 +140,7 @@ void TemplateParser::startTag(std::string name, std::map< std::string, std::stri
 
   // Enable Map parsing
   if(name == "maps") {
-    if(state != TEMPLATE) error("maps found outside template.");
+    if(state != MACRO) error("maps found outside macro.");
     state = MAPS;
 
     assert(current_macro); // No macro is currently available, cannot create maps!
@@ -159,7 +164,7 @@ void TemplateParser::startTag(std::string name, std::map< std::string, std::stri
 
   // Enable widget parsing
   if(name == "window") {
-    if(state != TEMPLATE) error("window found outside template.");
+    if(state != MACRO) error("window found outside macro.");
     state = WINDOW;
 
     assert(current_macro); // No macro is currently available, cannot create window!
@@ -196,8 +201,8 @@ void TemplateParser::startTag(std::string name, std::map< std::string, std::stri
 void TemplateParser::endTag(std::string name)
 {
   if(name == "template") state = UNDEFINED;
-  if(name == "macrosequence") state = TEMPLATE;
-  if(name == "macro") state = MACROSEQUENCE;
+  if(name == "course") state = TEMPLATE;
+  if(name == "macro") state = COURSE;
   if(name == "queries") state = MACRO;
   if(name == "query") state = QUERIES;
   if(name == "maps") state = MACRO;
@@ -221,3 +226,57 @@ Template *TemplateParser::getTemplate()
 {
   return t;
 }
+
+#ifdef TEST_TEMPLATEPARSER
+
+void print_attributes(std::string prefix,
+                      std::map< std::string, std::string > &att)
+{
+  std::map< std::string, std::string >::iterator i = att.begin();
+  while(i != att.end()) {
+    printf("%s %s = \"%s\"\n", prefix.c_str(), (*i).first.c_str(), (*i).second.c_str());
+    i++;
+  }
+}
+
+int main()
+{
+  TemplateParser parser("../xml/example2.xml");
+  parser.parse();
+
+  Template *t = parser.getTemplate();
+
+  printf("[Template]:\n");
+  print_attributes("\t-", t->attributes);
+
+  printf("\t[Course]:\n");
+  print_attributes("\t\t-", t->course.attributes);
+  
+  printf("\t\t[Macroes]:\n");
+  std::vector< Macro >::iterator i = t->course.macroes.begin();
+
+  while(i != t->course.macroes.end()) {
+    printf("\t\t\t[Macro]:\n");
+    print_attributes("\t\t\t\t-", (*i).attributes);
+    
+    std::vector< Query >::iterator qi = (*i).queries.begin();
+    while(qi != (*i).queries.end()) {
+      printf("\t\t\t\t[Query]:\n");
+      print_attributes("\t\t\t\t\t-", (*qi).attributes);
+      qi++;
+    }
+
+    std::vector< Map >::iterator mi = (*i).maps.begin();
+    while(mi != (*i).maps.end()) {
+      printf("\t\t\t\t[Map]:\n");
+      print_attributes("\t\t\t\t\t-", (*mi).attributes);
+      mi++;
+    }
+
+    i++;
+  }
+
+  return 0;
+}
+
+#endif/*TEST_TEMPLATEPARSER*/
diff --git a/server/src/templateparser.h b/server/src/templateparser.h
index d5c09b1..8570234 100644
--- a/server/src/templateparser.h
+++ b/server/src/templateparser.h
@@ -33,7 +33,7 @@
 typedef enum {
   UNDEFINED,
   TEMPLATE,
-  MACROSEQUENCE,
+  COURSE,
   MACRO,
   QUERIES,
   QUERY,
@@ -47,6 +47,7 @@ public:
   TemplateParser(std::string templatefile);
   ~TemplateParser();
 
+  void characterData(std::string &data);
   void startTag(std::string name, std::map< std::string, std::string> attributes);
   void endTag(std::string name);
 
diff --git a/server/src/transaction.h b/server/src/transaction.h
index daf0de3..4cc633d 100644
--- a/server/src/transaction.h
+++ b/server/src/transaction.h
@@ -34,21 +34,12 @@
 class Request {
 public:
   std::string macro;
+  std::string course;
 };
 typedef std::vector< Request > Requests;
 
-
-/*
-class Field {
-public:
-  std::string name;
-  std::string value;
-};
-*/
-//typedef std::vector< Field > Fields;
 typedef std::map< std::string, std::string > Fields;
 
-
 class Commit {
 public:
   std::string macro;
@@ -57,7 +48,6 @@ public:
 };
 typedef std::vector< Commit > Commits;
 
-
 class Transaction {
 public:
   std::string user;
@@ -68,5 +58,4 @@ public:
   Commits commits;
 };
 
-
 #endif/*__PRACRO_TRANSACTION_H__*/
diff --git a/server/src/transactionparser.cc b/server/src/transactionparser.cc
index 9ca6288..547a9a7 100644
--- a/server/src/transactionparser.cc
+++ b/server/src/transactionparser.cc
@@ -33,8 +33,6 @@
 #include <string>
 #include <map>
 
-static bool done = false;
-
 TransactionParser::TransactionParser(TCPSocket &socket, Transaction &transaction)
 {
   this->transaction = &transaction;
@@ -52,6 +50,7 @@ void TransactionParser::startTag(std::string name, std::map< std::string, std::s
 
   if(name == "request") {
     Request r;
+    r.course = attributes["course"];
     r.macro = attributes["macro"];
     transaction->requests.push_back(r);
   }
-- 
cgit v1.2.3