From 4df8884f155f7558c07949426c248e066df7936f Mon Sep 17 00:00:00 2001 From: deva Date: Wed, 11 Feb 2009 07:14:22 +0000 Subject: Added QueryHandler for both Pentominos and Pracro. Added source string to all values, and use these to set the prefill value in the macros. --- server/src/Makefile.am | 15 +- server/src/database.h | 1 + server/src/dbtypes.h | 1 + server/src/journal_commit.cc | 1 + server/src/luaquerymapper.cc | 21 ++- server/src/queryhandler.cc | 248 --------------------------------- server/src/queryhandler.h | 10 +- server/src/queryhandlerpentominos.cc | 256 +++++++++++++++++++++++++++++++++++ server/src/queryhandlerpentominos.h | 54 ++++++++ server/src/queryhandlerpracro.cc | 58 ++++++++ server/src/queryhandlerpracro.h | 54 ++++++++ server/src/queryparser.cc | 1 + server/src/queryresult.h | 17 +++ server/src/server.cc | 27 +++- server/src/widgetgenerator.cc | 8 +- 15 files changed, 503 insertions(+), 269 deletions(-) delete mode 100644 server/src/queryhandler.cc create mode 100644 server/src/queryhandlerpentominos.cc create mode 100644 server/src/queryhandlerpentominos.h create mode 100644 server/src/queryhandlerpracro.cc create mode 100644 server/src/queryhandlerpracro.h (limited to 'server/src') 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 #include #include +#include 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.cc b/server/src/queryhandler.cc deleted file mode 100644 index 7077ab8..0000000 --- a/server/src/queryhandler.cc +++ /dev/null @@ -1,248 +0,0 @@ -/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/*************************************************************************** - * queryhandler.cc - * - * Tue May 6 14:50:56 CEST 2008 - * Copyright 2008 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 "debug.h" - -#include "queryhandler.h" - -#include - -// For time -#include - -#include -#include -#include - -// For getpid -#include -#include - -// For time -#include - -// For strerror and errno -#include - -// For socket and friends -#include -#include -#include -#include - -// For ioctl -#include - -#include "queryparser.h" - -typedef struct { - in_addr_t ip; - time_t time; - pid_t pid; - unsigned short int count; -} UID; - - -#define SIOCGIFCONF 0x8912 // get iface list - -static in_addr_t getIP(const char *interface) -{ - in_addr_t ret = 0; - int numreqs = 30, sd, n; - struct ifconf ifc; - struct ifreq *ifr; - struct in_addr *ia; - - sd = socket(AF_INET, SOCK_STREAM, 0); - if(sd == -1) { - // throw Pentominos::UIDCouldNotConnectException(strerror(errno)); - } - - ifc.ifc_buf = NULL; - ifc.ifc_len = sizeof(struct ifreq) * numreqs; - - ifc.ifc_buf = (char*)malloc(ifc.ifc_len); - if(ifc.ifc_buf == NULL) { - // throw Pentominos::UIDOutOfMemoryException(); - } - - if (ioctl(sd, SIOCGIFCONF, &ifc) < 0) { - // throw Pentominos::UIDInterfaceListException(strerror(errno)); - } - - ifr = ifc.ifc_req; - for (n = 0; n < ifc.ifc_len; n += sizeof(struct ifreq)) { - ia = (struct in_addr *)((ifr->ifr_ifru.ifru_addr.sa_data)+2); - if(!strcmp(ifr->ifr_ifrn.ifrn_name, interface)) { - ret = *(in_addr_t*)ia; - } - ifr++; - } - - if(!ret) { // Still no interface... We're fucked! - // throw Pentominos::UIDInterfaceException(interface); - } - - free(ifc.ifc_buf); - return ret; -} - - -static unsigned short counter = 0; -static unsigned short getCounter() -{ - return counter++; -} - -static UID uid = {0,0,0,0}; -static std::string getUID(const char *interface) -{ - if(!uid.ip) uid.ip = getIP(interface); - - time_t t = time(NULL); - if(uid.time != t) counter = 0; // If time differes, reset the counter - uid.time = t; // We need this value every time. - - if(!uid.pid) uid.pid = getpid(); - - uid.count = getCounter(); - - char buf[32]; - sprintf(buf, "%08x%08x%04x%04x", uid.ip, (unsigned int)uid.time, uid.pid, uid.count); - return std::string(buf); -} - - -QueryHandler::QueryHandler(TCPSocket *socket, std::string cpr) -{ - this->cpr = cpr; - this->socket = socket; -} - -QueryResult QueryHandler::exec(Query &query) -{ - time_t timestamp = time(NULL); - std::string uid = getUID("eth0"); - - char buf[512]; - char header[] = - "\n" - "\n"; -#ifndef WITHOUT_PENTOMINOS - socket->write(header, strlen(header)); -#endif/*WITHOUT_PENTOMINOS*/ - - PRACRO_DEBUG(pentominos, header); - - sprintf(buf, " \n", - cpr.c_str(), -#ifndef WITHOUT_PENTOMINOS - socket->srcaddr().c_str(), - socket->dstaddr().c_str(), -#else - "127.0.0.1", - "127.0.0.1", -#endif/*WITHOUT_PENTOMINOS*/ - (unsigned int)timestamp, - uid.c_str()); -#ifndef WITHOUT_PENTOMINOS - socket->write(buf, strlen(buf)); -#endif/*WITHOUT_PENTOMINOS*/ - - PRACRO_DEBUG(pentominos, buf); - - sprintf(buf, " \n", - query.attributes["class"].c_str(), - query.attributes["class"].c_str()); - -#ifndef WITHOUT_PENTOMINOS - socket->write(buf, strlen(buf)); -#endif/*WITHOUT_PENTOMINOS*/ - - PRACRO_DEBUG(pentominos, buf); - - sprintf(buf, ""); - -#ifndef WITHOUT_PENTOMINOS - socket->write(buf, strlen(buf)); -#endif/*WITHOUT_PENTOMINOS*/ - - PRACRO_DEBUG(pentominos, buf); - - QueryResult result; - -#ifndef WITHOUT_PENTOMINOS - QueryParser parser; - - ssize_t size; - - // Read until we've got the entire result. - while((size = socket->read(buf, sizeof(buf))) > 0) { - fwrite(buf, size, 1, stdout); fflush(stdout); - if(parser.parse(buf, size)) break; - } - - result = parser.result; -#endif/*WITHOUT_PENTOMINOS*/ - - PRACRO_DEBUG(pentominos, "Done handling query\n"); - - return result; -} - -#ifdef TEST_QUERYHANDLER - -int main() -{ - TCPSocket s; - s.connect("localhost", 11108); - - QueryHandler qh(&s, "2003791613"); - - Query q1; - q1.attributes["device_id"] = "lensmeter"; - q1.attributes["device_type"] = "lensmeter"; - qh.addQuery(q1); - - std::string res = qh.exec(); - - printf("%s\n", res.c_str()); - - return 0; -} - -#endif/*TEST_QUERYHANDLER*/ 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/queryhandlerpentominos.cc b/server/src/queryhandlerpentominos.cc new file mode 100644 index 0000000..4b98768 --- /dev/null +++ b/server/src/queryhandlerpentominos.cc @@ -0,0 +1,256 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/*************************************************************************** + * queryhandlerpentominos.cc + * + * 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. + */ +#include "queryhandlerpentominos.h" + +#include + +// For time +#include + +#include +#include +#include + +// For getpid +#include +#include + +// For time +#include + +// For strerror and errno +#include + +// For socket and friends +#include +#include +#include +#include + +// For ioctl +#include + +#include "queryparser.h" + +typedef struct { + in_addr_t ip; + time_t time; + pid_t pid; + unsigned short int count; +} UID; + + +#define SIOCGIFCONF 0x8912 // get iface list + +static in_addr_t getIP(const char *interface) +{ + in_addr_t ret = 0; + int numreqs = 30, sd, n; + struct ifconf ifc; + struct ifreq *ifr; + struct in_addr *ia; + + sd = socket(AF_INET, SOCK_STREAM, 0); + if(sd == -1) { + // throw Pentominos::UIDCouldNotConnectException(strerror(errno)); + } + + ifc.ifc_buf = NULL; + ifc.ifc_len = sizeof(struct ifreq) * numreqs; + + ifc.ifc_buf = (char*)malloc(ifc.ifc_len); + if(ifc.ifc_buf == NULL) { + // throw Pentominos::UIDOutOfMemoryException(); + } + + if (ioctl(sd, SIOCGIFCONF, &ifc) < 0) { + // throw Pentominos::UIDInterfaceListException(strerror(errno)); + } + + ifr = ifc.ifc_req; + for (n = 0; n < ifc.ifc_len; n += sizeof(struct ifreq)) { + ia = (struct in_addr *)((ifr->ifr_ifru.ifru_addr.sa_data)+2); + if(!strcmp(ifr->ifr_ifrn.ifrn_name, interface)) { + ret = *(in_addr_t*)ia; + } + ifr++; + } + + if(!ret) { // Still no interface... We're fucked! + // throw Pentominos::UIDInterfaceException(interface); + } + + free(ifc.ifc_buf); + return ret; +} + + +static unsigned short counter = 0; +static unsigned short getCounter() +{ + return counter++; +} + +static UID uid = {0,0,0,0}; +static std::string getUID(const char *interface) +{ + if(!uid.ip) uid.ip = getIP(interface); + + time_t t = time(NULL); + if(uid.time != t) counter = 0; // If time differes, reset the counter + uid.time = t; // We need this value every time. + + if(!uid.pid) uid.pid = getpid(); + + uid.count = getCounter(); + + char buf[32]; + sprintf(buf, "%08x%08x%04x%04x", uid.ip, (unsigned int)uid.time, uid.pid, uid.count); + return std::string(buf); +} + + +QueryHandlerPentominos::QueryHandlerPentominos(TCPSocket *socket, std::string cpr) + : QueryHandler() +{ + this->cpr = cpr; + this->socket = socket; +} + +QueryResult QueryHandlerPentominos::exec(Query &query) +{ + time_t timestamp = time(NULL); + std::string uid = getUID("eth0"); + + char buf[512]; + char header[] = + "\n" + "\n"; +#ifndef WITHOUT_PENTOMINOS + socket->write(header, strlen(header)); +#endif/*WITHOUT_PENTOMINOS*/ + +#ifdef WITH_DEBUG + printf(header); +#endif/*WITH_DEBUG*/ + + sprintf(buf, " \n", + cpr.c_str(), +#ifndef WITHOUT_PENTOMINOS + socket->srcaddr().c_str(), + socket->dstaddr().c_str(), +#else + "127.0.0.1", + "127.0.0.1", +#endif/*WITHOUT_PENTOMINOS*/ + (unsigned int)timestamp, + uid.c_str()); +#ifndef WITHOUT_PENTOMINOS + socket->write(buf, strlen(buf)); +#endif/*WITHOUT_PENTOMINOS*/ + +#ifdef WITH_DEBUG + printf(buf); +#endif/*WITH_DEBUG*/ + + sprintf(buf, " \n", + query.attributes["class"].c_str(), + query.attributes["class"].c_str()); + +#ifndef WITHOUT_PENTOMINOS + socket->write(buf, strlen(buf)); +#endif/*WITHOUT_PENTOMINOS*/ + +#ifdef WITH_DEBUG + printf(buf); +#endif/*WITH_DEBUG*/ + + sprintf(buf, ""); + +#ifndef WITHOUT_PENTOMINOS + socket->write(buf, strlen(buf)); +#endif/*WITHOUT_PENTOMINOS*/ + +#ifdef WITH_DEBUG + printf(buf); + fflush(stdout); +#endif/*WITH_DEBUG*/ + + QueryResult result; + +#ifndef WITHOUT_PENTOMINOS + QueryParser parser; + + ssize_t size; + + // Read until we've got the entire result. + while((size = socket->read(buf, sizeof(buf))) > 0) { + fwrite(buf, size, 1, stdout); fflush(stdout); + if(parser.parse(buf, size)) break; + } + + result = parser.result; +#endif/*WITHOUT_PENTOMINOS*/ + + printf("Done handling query\n"); + + result.print(); + + return result; +} + +#ifdef TEST_QUERYHANDLER + +int main() +{ + TCPSocket s; + s.connect("localhost", 11108); + + QueryHandlerPentominos qh(&s, "2003791613"); + + Query q1; + q1.attributes["device_id"] = "lensmeter"; + q1.attributes["device_type"] = "lensmeter"; + qh.addQuery(q1); + + std::string res = qh.exec(); + + return 0; +} + +#endif/*TEST_QUERYHANDLER*/ 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 +#include + +/** + * 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 +#include + +/** + * 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 ¯o, 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")); -- cgit v1.2.3