summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--server/src/Makefile.am15
-rw-r--r--server/src/database.h1
-rw-r--r--server/src/dbtypes.h1
-rw-r--r--server/src/journal_commit.cc1
-rw-r--r--server/src/luaquerymapper.cc21
-rw-r--r--server/src/queryhandler.h10
-rw-r--r--server/src/queryhandlerpentominos.cc (renamed from server/src/queryhandler.cc)42
-rw-r--r--server/src/queryhandlerpentominos.h54
-rw-r--r--server/src/queryhandlerpracro.cc58
-rw-r--r--server/src/queryhandlerpracro.h54
-rw-r--r--server/src/queryparser.cc1
-rw-r--r--server/src/queryresult.h17
-rw-r--r--server/src/server.cc27
-rw-r--r--server/src/widgetgenerator.cc8
14 files changed, 272 insertions, 38 deletions
diff --git a/server/src/Makefile.am b/server/src/Makefile.am
index a4f2fd7..086905a 100644
--- a/server/src/Makefile.am
+++ b/server/src/Makefile.am
@@ -13,7 +13,8 @@ pracrod_SOURCES = \
configurationparser.cc \
debug.cc \
exception.cc \
- queryhandler.cc \
+ queryhandlerpentominos.cc \
+ queryhandlerpracro.cc \
queryparser.cc \
journal_commit.cc \
log.cc \
@@ -40,6 +41,8 @@ EXTRA_DIST = \
debug.h \
exception.h \
queryhandler.h \
+ queryhandlerpentominos.h \
+ queryhandlerpracro.h \
queryparser.h \
journal_commit.h \
log.h \
@@ -59,7 +62,8 @@ EXTRA_DIST = \
TESTFILES = \
test_formattools \
- test_queryhandler \
+ test_queryhandlerpentominos \
+ test_queryhandlerpracro \
test_queryparser \
test_luaquerymapper \
test_templateparser \
@@ -84,8 +88,11 @@ test_mltokenizer: mltokenizer.cc
test_formattools: formattools.cc luaformatmapper.cc luaformatmapperutils.cc mltokenizer.cc
@../../tools/test formattools.cc luaformatmapper.cc mltokenizer.cc luaformatmapperutils.cc exception.cc log.cc $(LUA_LIBS)
-test_queryhandler: queryhandler.cc
- @../../tools/test queryhandler.cc tcpsocket.cc exception.cc log.cc
+test_queryhandlerpentominos: queryhandlerpentominos.cc
+ @../../tools/test queryhandlerpentominos.cc tcpsocket.cc exception.cc log.cc
+
+test_queryhandlerpracro: queryhandlerpracro.cc
+ @../../tools/test queryhandlerpracro.cc tcpsocket.cc exception.cc log.cc
test_queryparser: queryparser.cc
@../../tools/test queryparser.cc queryhandler.cc tcpsocket.cc exception.cc log.cc saxparser.cc -lexpat
diff --git a/server/src/database.h b/server/src/database.h
index 85d4cc4..a5bed81 100644
--- a/server/src/database.h
+++ b/server/src/database.h
@@ -32,6 +32,7 @@
#include "pracrodao.h"
#include "transaction.h"
#include "template.h"
+
#include "debug.h"
class Database {
diff --git a/server/src/dbtypes.h b/server/src/dbtypes.h
index fc12728..79ac629 100644
--- a/server/src/dbtypes.h
+++ b/server/src/dbtypes.h
@@ -31,6 +31,7 @@ class Value {
public:
Value() : value(""), timestamp(0) {}
std::string value;
+ std::string source;
time_t timestamp;
};
typedef std::map< std::string, Value > Values;
diff --git a/server/src/journal_commit.cc b/server/src/journal_commit.cc
index cb9769e..9800fb7 100644
--- a/server/src/journal_commit.cc
+++ b/server/src/journal_commit.cc
@@ -45,6 +45,7 @@
#include <stdarg.h>
#include <fcntl.h>
#include <time.h>
+#include <errno.h>
static int mwrite(int sock, const char *fmt, ...)
{
diff --git a/server/src/luaquerymapper.cc b/server/src/luaquerymapper.cc
index 9f8293d..6a7282f 100644
--- a/server/src/luaquerymapper.cc
+++ b/server/src/luaquerymapper.cc
@@ -40,6 +40,7 @@ static std::string loadresultstring(QueryResult &res, std::string group = "")
s += group + (*v).first + " = {}\n";
s += group + (*v).first + ".value = \"" + (*v).second + "\"\n";
s += group + (*v).first + ".timestamp = " + timestamp.str() + "\n";
+ s += group + (*v).first + ".source = \"" + res.source + "\"\n";
v++;
}
@@ -75,6 +76,9 @@ void LUAQueryMapper::addQueryResult(QueryResult &result)
{
std::string preload = loadresultstring(result);
+ printf("Preload:\n%s\n", preload.c_str());
+
+
if(luaL_loadbuffer(L, preload.c_str(), preload.size(), "preload")) {
error(lua_tostring(L, lua_gettop(L)));
return;
@@ -103,6 +107,8 @@ Value LUAQueryMapper::map(const std::string &mapper)
return v;
}
+ printf("Mapper: %s\n", mapper.c_str());
+
// Load the mapper
if(luaL_loadbuffer(L, mapper.c_str(), mapper.size(), "mapper")) {
error(lua_tostring(L, lua_gettop(L)) + std::string(" in ") + mapper);
@@ -116,12 +122,23 @@ Value LUAQueryMapper::map(const std::string &mapper)
}
// Check if app messed up the stack.
- if(lua_gettop(L) != clean_top + 2) {
+ if(lua_gettop(L) != clean_top + 3) {
error("Wrong number of return values in " + mapper);
lua_pop(L, lua_gettop(L) - clean_top);
return v;
}
+ // value, timestamp, source
+
+ // Check if the types are right
+ if(lua_isstring(L, lua_gettop(L)) == false) {
+ error("Source is not a string in " + mapper);
+ lua_pop(L, 3);
+ return v;
+ }
+ v.source = lua_tostring(L, lua_gettop(L));
+ lua_pop(L, 1);
+
// Check if the types are right
if(lua_isnumber(L, lua_gettop(L)) == false) {
error("Timestamp is not an integer in " + mapper);
@@ -140,6 +157,8 @@ Value LUAQueryMapper::map(const std::string &mapper)
v.value = lua_tostring(L, lua_gettop(L));
lua_pop(L, 1);
+ printf("Result: value=%s, src=%s, time=%d\n", v.value.c_str(), v.source.c_str(), (int)v.timestamp);
+
return v;
}
diff --git a/server/src/queryhandler.h b/server/src/queryhandler.h
index bb0be8f..e8f2d1b 100644
--- a/server/src/queryhandler.h
+++ b/server/src/queryhandler.h
@@ -27,7 +27,6 @@
#ifndef __PRACRO_QUERYHANDLER_H__
#define __PRACRO_QUERYHANDLER_H__
-#include "tcpsocket.h"
#include "template.h"
#include "queryresult.h"
@@ -39,15 +38,10 @@
*/
class QueryHandler {
public:
- QueryHandler(TCPSocket *socket, std::string cpr);
+ QueryHandler() {}
// Execute all queries.
- // std::string exec();
- QueryResult exec(Query &query);
-
-private:
- TCPSocket *socket;
- std::string cpr;
+ virtual QueryResult exec(Query &query) { return QueryResult(); }
};
#endif/*__PRACRO_QUERYHANDLER_H__*/
diff --git a/server/src/queryhandler.cc b/server/src/queryhandlerpentominos.cc
index 7077ab8..4b98768 100644
--- a/server/src/queryhandler.cc
+++ b/server/src/queryhandlerpentominos.cc
@@ -1,9 +1,9 @@
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/***************************************************************************
- * queryhandler.cc
+ * queryhandlerpentominos.cc
*
- * Tue May 6 14:50:56 CEST 2008
- * Copyright 2008 Bent Bisballe Nyeng
+ * Thu Jan 15 11:35:31 CET 2009
+ * Copyright 2009 Bent Bisballe Nyeng
* deva@aasimon.org
****************************************************************************/
@@ -24,9 +24,7 @@
* along with Pracro; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/
-#include "debug.h"
-
-#include "queryhandler.h"
+#include "queryhandlerpentominos.h"
#include <config.h>
@@ -136,13 +134,14 @@ static std::string getUID(const char *interface)
}
-QueryHandler::QueryHandler(TCPSocket *socket, std::string cpr)
+QueryHandlerPentominos::QueryHandlerPentominos(TCPSocket *socket, std::string cpr)
+ : QueryHandler()
{
this->cpr = cpr;
this->socket = socket;
}
-QueryResult QueryHandler::exec(Query &query)
+QueryResult QueryHandlerPentominos::exec(Query &query)
{
time_t timestamp = time(NULL);
std::string uid = getUID("eth0");
@@ -158,7 +157,9 @@ QueryResult QueryHandler::exec(Query &query)
socket->write(header, strlen(header));
#endif/*WITHOUT_PENTOMINOS*/
- PRACRO_DEBUG(pentominos, header);
+#ifdef WITH_DEBUG
+ printf(header);
+#endif/*WITH_DEBUG*/
sprintf(buf, " <pentominos:entry cpr=\"%s\"\n"
" src_addr=\"%s\"\n"
@@ -179,7 +180,9 @@ QueryResult QueryHandler::exec(Query &query)
socket->write(buf, strlen(buf));
#endif/*WITHOUT_PENTOMINOS*/
- PRACRO_DEBUG(pentominos, buf);
+#ifdef WITH_DEBUG
+ printf(buf);
+#endif/*WITH_DEBUG*/
sprintf(buf, " <pentominos:query format=\"pracroxml\"\n"
" device_id=\"%s\"\n"
@@ -193,7 +196,9 @@ QueryResult QueryHandler::exec(Query &query)
socket->write(buf, strlen(buf));
#endif/*WITHOUT_PENTOMINOS*/
- PRACRO_DEBUG(pentominos, buf);
+#ifdef WITH_DEBUG
+ printf(buf);
+#endif/*WITH_DEBUG*/
sprintf(buf, "</artefact>");
@@ -201,10 +206,13 @@ QueryResult QueryHandler::exec(Query &query)
socket->write(buf, strlen(buf));
#endif/*WITHOUT_PENTOMINOS*/
- PRACRO_DEBUG(pentominos, buf);
+#ifdef WITH_DEBUG
+ printf(buf);
+ fflush(stdout);
+#endif/*WITH_DEBUG*/
QueryResult result;
-
+
#ifndef WITHOUT_PENTOMINOS
QueryParser parser;
@@ -219,7 +227,9 @@ QueryResult QueryHandler::exec(Query &query)
result = parser.result;
#endif/*WITHOUT_PENTOMINOS*/
- PRACRO_DEBUG(pentominos, "Done handling query\n");
+ printf("Done handling query\n");
+
+ result.print();
return result;
}
@@ -231,7 +241,7 @@ int main()
TCPSocket s;
s.connect("localhost", 11108);
- QueryHandler qh(&s, "2003791613");
+ QueryHandlerPentominos qh(&s, "2003791613");
Query q1;
q1.attributes["device_id"] = "lensmeter";
@@ -240,8 +250,6 @@ int main()
std::string res = qh.exec();
- printf("%s\n", res.c_str());
-
return 0;
}
diff --git a/server/src/queryhandlerpentominos.h b/server/src/queryhandlerpentominos.h
new file mode 100644
index 0000000..3d98b25
--- /dev/null
+++ b/server/src/queryhandlerpentominos.h
@@ -0,0 +1,54 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/***************************************************************************
+ * queryhandlerpentominos.h
+ *
+ * Thu Jan 15 11:35:31 CET 2009
+ * Copyright 2009 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.
+ */
+#ifndef __PRACRO_QUERYHANDLERPENTOMINOS_H__
+#define __PRACRO_QUERYHANDLERPENTOMINOS_H__
+
+#include "queryhandler.h"
+
+#include "tcpsocket.h"
+#include "template.h"
+#include "queryresult.h"
+
+#include <vector>
+#include <string>
+
+/**
+ * This class handles the query of external data.
+ */
+class QueryHandlerPentominos : public QueryHandler {
+public:
+ QueryHandlerPentominos(TCPSocket *socket, std::string cpr);
+
+ // Execute all queries.
+ QueryResult exec(Query &query);
+
+private:
+ TCPSocket *socket;
+ std::string cpr;
+};
+
+#endif/*__PRACRO_QUERYHANDLERPENTOMINOS_H__*/
diff --git a/server/src/queryhandlerpracro.cc b/server/src/queryhandlerpracro.cc
new file mode 100644
index 0000000..99c31b3
--- /dev/null
+++ b/server/src/queryhandlerpracro.cc
@@ -0,0 +1,58 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/***************************************************************************
+ * queryhandlerpracro.cc
+ *
+ * Thu Jan 15 11:35:34 CET 2009
+ * Copyright 2009 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 "queryhandlerpracro.h"
+
+QueryHandlerPracro::QueryHandlerPracro(Database *db, std::string cpr)
+{
+ this->cpr = cpr;
+ this->db = db;
+}
+
+QueryResult QueryHandlerPracro::exec(Query &query)
+{
+ QueryResult result;
+
+ std::string field = query.attributes["class"];
+ Fieldnames fields;
+ fields.push_back(field);
+
+ std::string ttl = query.attributes["ttl"];
+ time_t oldest = time(NULL) - atoi(ttl.c_str());
+
+ Values values = db->getValues(cpr, fields, oldest);
+
+ std::string value = values[field].value;
+ time_t timestamp = values[field].timestamp;
+
+ result.timestamp = timestamp;
+ result.values[field] = value;
+ result.source = "pracro";
+
+ printf("QHP: %s => %s (%lu)\n", field.c_str(), value.c_str(), timestamp);
+
+ return result;
+}
diff --git a/server/src/queryhandlerpracro.h b/server/src/queryhandlerpracro.h
new file mode 100644
index 0000000..8852868
--- /dev/null
+++ b/server/src/queryhandlerpracro.h
@@ -0,0 +1,54 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/***************************************************************************
+ * queryhandlerpracro.h
+ *
+ * Thu Jan 15 11:35:34 CET 2009
+ * Copyright 2009 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.
+ */
+#ifndef __PRACRO_QUERYHANDLERPRACRO_H__
+#define __PRACRO_QUERYHANDLERPRACRO_H__
+
+#include "queryhandler.h"
+
+#include "database.h"
+#include "template.h"
+#include "queryresult.h"
+
+#include <vector>
+#include <string>
+
+/**
+ * This class handles the query of pracro data.
+ */
+class QueryHandlerPracro : public QueryHandler {
+public:
+ QueryHandlerPracro(Database *db, std::string cpr);
+
+ // Execute all queries.
+ QueryResult exec(Query &query);
+
+private:
+ Database *db;
+ std::string cpr;
+};
+
+#endif/*__PRACRO_QUERYHANDLERPRACRO_H__*/
diff --git a/server/src/queryparser.cc b/server/src/queryparser.cc
index abb41b5..287f71c 100644
--- a/server/src/queryparser.cc
+++ b/server/src/queryparser.cc
@@ -45,6 +45,7 @@ void QueryParser::startTag(std::string name, std::map< std::string, std::string>
this->timestamp = atol(attributes["timestamp"].c_str());
QueryResult q;
+ q.source = "pentominos";
q.timestamp = this->timestamp;
stack.back()->groups[attributes["class"]] = q;
stack.push_back(&stack.back()->groups[attributes["class"]]);
diff --git a/server/src/queryresult.h b/server/src/queryresult.h
index 617d957..649f975 100644
--- a/server/src/queryresult.h
+++ b/server/src/queryresult.h
@@ -34,8 +34,25 @@
class QueryResult {
public:
time_t timestamp;
+ std::string source;
std::map< std::string, std::string > values;
std::map< std::string, QueryResult > groups;
+
+ void print(std::string tabs = "") {
+ printf("%sTimestamp: %d\n", tabs.c_str(), (int)timestamp);
+ printf("%sSource: %s\n", tabs.c_str(), source.c_str());
+ printf("%sValues:\n", tabs.c_str());
+ for(std::map< std::string, std::string >::iterator i = values.begin(); i != values.end(); i++) {
+ printf("%s[%s] => [%s]\n", tabs.c_str(), i->first.c_str(), i->second.c_str());
+ }
+ printf("%s{\n", tabs.c_str());
+ for(std::map< std::string, QueryResult >::iterator i = groups.begin(); i != groups.end(); i++) {
+ printf("%s[%s] =>:\n", tabs.c_str(), i->first.c_str());
+ i->second.print(tabs +" ");
+ }
+ printf("%s}\n", tabs.c_str());
+
+ }
};
#endif/*__PRACRO_QUERYRESULT_H__*/
diff --git a/server/src/server.cc b/server/src/server.cc
index b49f5b2..1ea2020 100644
--- a/server/src/server.cc
+++ b/server/src/server.cc
@@ -41,7 +41,11 @@
#include "transactionparser.h"
#include "templateparser.h"
#include "macroparser.h"
+
#include "queryhandler.h"
+#include "queryhandlerpracro.h"
+#include "queryhandlerpentominos.h"
+
#include "queryparser.h"
#include "luaquerymapper.h"
#include "database.h"
@@ -179,12 +183,25 @@ static std::string handleTransaction(Transaction *transaction,
////////////////////////
std::vector< Query >::iterator qi = m->queries.begin();
while(qi != m->queries.end()) {
- ///////////////////////////////
- // Send the queries to Pentominos (if any)
- QueryHandler qh(pentominos_socket, transaction->cpr);
- QueryResult queryresult = qh.exec(*qi);
- lqm.addQueryResult(queryresult);
+ Query &query = *qi;
+ std::string service = query.attributes["service"];
+
+ if(service == "pentominos") {
+ // Send the queries to Pentominos (if any)
+ QueryHandlerPentominos qh(pentominos_socket, transaction->cpr);
+
+ QueryResult queryresult = qh.exec(*qi);
+ lqm.addQueryResult(queryresult);
+ }
+
+ if(service == "pracro") {
+ // Send the queries to Pentominos (if any)
+ QueryHandlerPracro qh(db, transaction->cpr);
+
+ QueryResult queryresult = qh.exec(*qi);
+ lqm.addQueryResult(queryresult);
+ }
qi++;
}
diff --git a/server/src/widgetgenerator.cc b/server/src/widgetgenerator.cc
index 2e51b32..90d2b9e 100644
--- a/server/src/widgetgenerator.cc
+++ b/server/src/widgetgenerator.cc
@@ -40,20 +40,22 @@ static std::string automap(std::string name)
if(name[i] == '.') groupcheck += " and " + group;
else groupcheck += name[i];
}
- groupcheck += " and " + name + ".value and " + name + ".timestamp";
+ groupcheck += " and " + name + ".value and " + name + ".timestamp and " + name + ".source";
groupcheck += ")\n";
std::string automapstring =
"-- Returning 0, 0 invalidates the result\n"
"value = 0\n"
"timestamp = 0\n"
+ "source = 0\n"
"\n"
+ groupcheck +
"then\n"
" value = " + name + ".value\n"
" timestamp = " + name + ".timestamp\n"
+ " source = " + name + ".source\n"
"end\n"
- "return value, timestamp\n";
+ "return value, timestamp, source\n";
PRACRO_DEBUG(widget, "Automap:\n%s\n", automapstring.c_str());
@@ -99,7 +101,7 @@ static std::string send_macro_widget(Macro &macro,
if(value.timestamp > time(NULL) - Conf::pentominos_max_ttl) {
widget.attributes["value"] = xml_encode(value.value);
timestamp = value.timestamp;
- prefilled = "pentominos";
+ prefilled = xml_encode(value.source);
}
}
// widget.attributes.erase(widget.attributes.find("map"));