diff options
author | deva <deva> | 2008-08-04 10:31:45 +0000 |
---|---|---|
committer | deva <deva> | 2008-08-04 10:31:45 +0000 |
commit | 98b90925d863531c3a6c75de15fb686b1fb1891f (patch) | |
tree | 9e6e54df55f4b7e30c4b9c9d447dcd4fe150ff35 | |
parent | bd8221d2791718f02ab1d067c797c62b29da9106 (diff) |
Made server able to run (compile-time) without postgres and the uploadserver. Fixed problems in the exclusion of pentominos support. Fixed bug in db query field iteration.
-rw-r--r-- | server/configure.in | 26 | ||||
-rw-r--r-- | server/src/database.cc | 32 | ||||
-rw-r--r-- | server/src/database.h | 2 | ||||
-rw-r--r-- | server/src/journal_commit.cc | 11 | ||||
-rw-r--r-- | server/src/queryhandler.cc | 28 | ||||
-rw-r--r-- | server/src/server.cc | 63 | ||||
-rw-r--r-- | server/src/widgetgenerator.cc | 2 |
7 files changed, 119 insertions, 45 deletions
diff --git a/server/configure.in b/server/configure.in index 79a14e1..9d6b803 100644 --- a/server/configure.in +++ b/server/configure.in @@ -19,7 +19,7 @@ dnl ====================== dnl Compile without pentominos support dnl ====================== AC_ARG_WITH(pentominos, - [ --with-pentominos build without pentominos support (default=yes)], + [ --with-pentominos build with pentominos support (default=yes)], [], [with_pentominos=yes]) if test x$with_pentominos == xno; then @@ -27,6 +27,30 @@ if test x$with_pentominos == xno; then CXXFLAGS="$CXXFLAGS -DWITHOUT_PENTOMINOS" fi +dnl ====================== +dnl Compile without uploadserver support +dnl ====================== +AC_ARG_WITH(uploadserver, + [ --with-uploadserver build with uploadserver support (default=yes)], + [], + [with_uploadserver=yes]) +if test x$with_uploadserver == xno; then + AC_MSG_WARN([*** Building without uploadserver support!]) + CXXFLAGS="$CXXFLAGS -DWITHOUT_UPLOADSERVER" +fi + +dnl ====================== +dnl Compile without db support +dnl ====================== +AC_ARG_WITH(db, + [ --with-db build with db support (default=yes)], + [], + [with_db=yes]) +if test x$with_db == xno; then + AC_MSG_WARN([*** Building without db support!]) + CXXFLAGS="$CXXFLAGS -DWITHOUT_DB" +fi + AC_PROG_CXX AC_PROG_LIBTOOL diff --git a/server/src/database.cc b/server/src/database.cc index 35d9214..2b8b83c 100644 --- a/server/src/database.cc +++ b/server/src/database.cc @@ -27,7 +27,9 @@ #include "database.h" Database::Database(std::string hostname, std::string user, std::string password) +#ifndef WITHOUT_DB : c("host=" + hostname + " user=" + user + " password=" + password + " dbname=pracro") +#endif/*WITHOUT_DB*/ { } @@ -57,15 +59,23 @@ void Database::commit(std::string user, std::string macro = _macro.attributes["name"]; std::stringstream timestamp; timestamp << now; +#ifndef WITHOUT_DB pqxx::work W(c); +#endif/*WITHOUT_DB*/ std::string ts = "INSERT INTO transactions" " VALUES('"+cpr+"', '"+macro+"', '"+version+"', '"+timestamp.str()+"', '"+user+"')"; - pqxx::result R = W.exec(ts); + std::stringstream oid; - std::stringstream oid; oid << R.inserted_oid(); +#ifndef WITHOUT_DB + pqxx::result R = W.exec(ts); + oid << R.inserted_oid(); +#else + oid << "###GENERATED_OID###"; + printf("%s\n", ts.c_str()); +#endif/*WITHOUT_DB*/ std::map< std::string, std::string >::iterator i = fields.begin(); while(i != fields.end()) { @@ -74,12 +84,18 @@ void Database::commit(std::string user, "INSERT INTO fields" " VALUES('"+oid.str()+"', '"+i->first+"', '"+i->second+"')"; +#ifndef WITHOUT_DB W.exec(fs); +#else + printf("%s\n", fs.c_str()); +#endif/*WITHOUT_DB*/ i++; } +#ifndef WITHOUT_DB W.commit(); +#endif/*WITHOUT_DB*/ #if 0 try { @@ -100,7 +116,9 @@ Values Database::getValues(std::string cpr, time_t oldest) { Values values; +#ifndef WITHOUT_DB pqxx::work W(c); +#endif/*WITHOUT_DB*/ std::stringstream query; query << "SELECT fields.name, fields.value, transactions.timestamp"; @@ -121,6 +139,7 @@ Values Database::getValues(std::string cpr, query << " ORDER BY transactions.timestamp"; +#ifndef WITHOUT_DB pqxx::result R = W.exec(query.str()); pqxx::result::const_iterator ri = R.begin(); @@ -139,6 +158,9 @@ Values Database::getValues(std::string cpr, ri++; } +#else + printf("%s\n", query.str().c_str()); +#endif/*WITHOUT_DB*/ return values; } @@ -147,7 +169,9 @@ bool Database::checkMacro(std::string cpr, std::string macro, time_t oldest) { +#ifndef WITHOUT_DB pqxx::work W(c); +#endif/*WITHOUT_DB*/ std::stringstream query; query << "SELECT oid"; @@ -157,6 +181,7 @@ bool Database::checkMacro(std::string cpr, query << " AND timestamp >= " << oldest; query << " ORDER BY timestamp"; +#ifndef WITHOUT_DB try { pqxx::result R = W.exec(query.str()); @@ -164,6 +189,9 @@ bool Database::checkMacro(std::string cpr, } catch( ... ) { return false; } +#else + return false; +#endif/*WITHOUT_DB*/ } /* diff --git a/server/src/database.h b/server/src/database.h index 85f8cd7..bd3139e 100644 --- a/server/src/database.h +++ b/server/src/database.h @@ -77,7 +77,9 @@ public: void disconnect() {} private: +#ifndef WITHOUT_DB pqxx::connection c; +#endif/*WITHOUT_DB*/ }; #endif/*__PRACRO_DATABASE_H__*/ diff --git a/server/src/journal_commit.cc b/server/src/journal_commit.cc index 95432b6..b1ae866 100644 --- a/server/src/journal_commit.cc +++ b/server/src/journal_commit.cc @@ -42,8 +42,6 @@ #include <fcntl.h> #include <time.h> -//#define NO_NETWORK - static int mwrite(int sock, const char *fmt, ...) { int l = 0; @@ -69,10 +67,11 @@ int journal_commit(const char *cpr, const char *user, const char *addr, unsigned short int port, const char *buf, size_t size) { - struct sockaddr_in sin; int sock = 1; -#ifndef NO_NETWORK +#ifndef WITHOUT_UPLOADSERVER + struct sockaddr_in sin; + // Do DNS lookup char *ip; struct in_addr **addr_list; @@ -104,7 +103,7 @@ int journal_commit(const char *cpr, const char *user, perror(":"); return -1; } -#endif/*NO_NETWORK*/ +#endif/*WITHOUT_UPLOADSERVER*/ // send header mwrite(sock, "PUT JOURNAL PROTO1.0 \r\n"); @@ -121,8 +120,10 @@ int journal_commit(const char *cpr, const char *user, fprintf(stderr, "write did not write all the bytes in the buffer.\n"); } +#ifndef WITHOUT_UPLOADSERVER // close socket close(sock); +#endif/*WITHOUT_UPLOADSERVER*/ return 0; } diff --git a/server/src/queryhandler.cc b/server/src/queryhandler.cc index cd96b03..573ce3a 100644 --- a/server/src/queryhandler.cc +++ b/server/src/queryhandler.cc @@ -153,7 +153,11 @@ std::string QueryHandler::exec() " xmlns:pentominos=\"http://www.aasimon.org/pentominos\"\n" " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" " xsi:schemaLocation=\"http://www.aasimon.org/pentominos schema.xsd\">\n"; +#ifndef WITHOUT_PENTOMINOS socket->write(header, strlen(header)); +#else + printf(header); +#endif/*WITHOUT_PENTOMINOS*/ sprintf(buf, " <pentominos:entry cpr=\"%s\"\n" " src_addr=\"%s\"\n" @@ -161,11 +165,20 @@ std::string QueryHandler::exec() " timestamp=\"%d\"\n" " uid=\"%s\"/>\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)); +#else + printf(buf); +#endif/*WITHOUT_PENTOMINOS*/ std::vector< Query >::iterator j = queries.begin(); while(j != queries.end()) { @@ -179,27 +192,38 @@ std::string QueryHandler::exec() query.attributes["class"].c_str(), query.attributes["class"].c_str()); +#ifndef WITHOUT_PENTOMINOS socket->write(buf, strlen(buf)); +#else + printf(buf); +#endif/*WITHOUT_PENTOMINOS*/ j++; } sprintf(buf, "</artefact>\n"); - socket->write(buf, strlen(buf)); +#ifndef WITHOUT_PENTOMINOS + socket->write(buf, strlen(buf)); // Terminate char term[] = "\0"; socket->write(term, 1); +#else + printf(buf); +#endif/*WITHOUT_PENTOMINOS*/ + std::string answer; + +#ifndef WITHOUT_PENTOMINOS // Wait for answer char abuf[64]; int res; - std::string answer; do { memset(abuf, 0, sizeof(abuf)); res = socket->read(abuf, sizeof(abuf) - 1); answer += abuf; } while(res); +#endif/*WITHOUT_PENTOMINOS*/ return answer; } diff --git a/server/src/server.cc b/server/src/server.cc index 6f875b8..353708f 100644 --- a/server/src/server.cc +++ b/server/src/server.cc @@ -123,39 +123,6 @@ static void connection(TCPSocket &socket) Template *templ = tp.getTemplate(); -#ifndef WITHOUT_PENTOMINOS - // Send the queries to Pentominos (if any) - TCPSocket s; - s.connect(Conf::pentominos_addr, Conf::pentominos_port); - QueryHandler qh(&s, transaction.cpr); - - std::vector< Macro >::iterator mi = templ->course.macroes.begin(); - while(mi != templ->course.macroes.end()) { - Macro ¯o = (*mi); - if(macro.attributes["name"] == request.macro) { - - std::vector< Query >::iterator qi = m->queries.begin(); - while(qi != m->queries.end()) { - qh.addQuery(*qi); - qi++; - } - } - mi++; - } - std::string result = qh.exec(); - - printf("Got result: [%s]\n", result.c_str()); -#else - std::string result = ""; -#endif/*WITHOUT_PENTOMINOS*/ - - // Parse the result from the queries to pentominos - QueryParser qp(result); - qp.parse(); - - // Map the results - LUAQueryMapper lqm(qp.result); - answer += " <course name=\""; answer += templ->course.attributes["name"]; answer += "\">\n"; @@ -172,6 +139,15 @@ static void connection(TCPSocket &socket) else answer += "\"false\""; answer += ">\n"; + /////////////////////////////// + // Send the queries to Pentominos (if any) + TCPSocket s; +#ifndef WITHOUT_PENTOMINOS + s.connect(Conf::pentominos_addr, Conf::pentominos_port); +#endif/*WITHOUT_PENTOMINOS*/ + QueryHandler qh(&s, transaction.cpr); + /////////////////////////////// + if(macro.attributes["name"] == request.macro) { foundmacro = true; @@ -180,7 +156,17 @@ static void connection(TCPSocket &socket) mp.parse(); Macro *m = mp.getMacro(); - // Handle lua programs + //////////////////////// + std::vector< Query >::iterator qi = m->queries.begin(); + while(qi != m->queries.end()) { + qh.addQuery(*qi); + qi++; + } + std::string result = qh.exec(); + printf("Got result: [%s]\n", result.c_str()); + ///////////////////////// + + // Handle scripts if(m->scripts.size()) { answer += " <scripts>\n"; @@ -194,6 +180,15 @@ static void connection(TCPSocket &socket) } answer += " </scripts>\n"; } + + ///////////////////////// + // Parse the result from the queries to pentominos + QueryParser qp(result); + qp.parse(); + // Map the results + LUAQueryMapper lqm(qp.result); + //////////////////////// + answer += widgetgenerator(*m, lqm, db); } answer += " </macro>\n"; diff --git a/server/src/widgetgenerator.cc b/server/src/widgetgenerator.cc index e9a8fee..ad9ffc6 100644 --- a/server/src/widgetgenerator.cc +++ b/server/src/widgetgenerator.cc @@ -106,7 +106,7 @@ static std::string send_macro_widget(Macro ¯o, static void get_fields(Widget &widget, Fieldnames &fields) { if(widget.attributes.find("value") != widget.attributes.end()) { - fields.push_back(widget.attributes["name"]); + if(widget.attributes["name"] != "") fields.push_back(widget.attributes["name"]); } std::vector< Widget >::iterator w = widget.widgets.begin(); |