From ab96caac3c631ca452b21a0529372f1165638002 Mon Sep 17 00:00:00 2001 From: deva Date: Wed, 29 Jul 2009 07:25:25 +0000 Subject: Split up commits and requests to seperate functions. Changed Database * to Database & --- server/src/macrolist.cc | 8 +- server/src/queryhandlerpracro.cc | 8 +- server/src/queryhandlerpracro.h | 4 +- server/src/server.cc | 305 +++++++++++++++++++++------------------ server/src/widgetgenerator.cc | 4 +- server/src/widgetgenerator.h | 2 +- 6 files changed, 174 insertions(+), 157 deletions(-) diff --git a/server/src/macrolist.cc b/server/src/macrolist.cc index f067fb1..e8bc507 100644 --- a/server/src/macrolist.cc +++ b/server/src/macrolist.cc @@ -75,10 +75,10 @@ std::string MacroList::getLatestVersion(std::string macro) throw(Exception) if(find(macro) == end()) throw Exception("Macro ["+macro+"] does not exist"); MacroListItem mli = (*this)[macro]; if(mli.size() == 0) return ""; - printf("Search for %s - found %s v%s\n", - macro.c_str(), - (macropath + "/" + mli.begin()->second).c_str(), - ((std::string)mli.begin()->first).c_str()); + PRACRO_DEBUG(macrolist, "Search for %s - found %s v%s\n", + macro.c_str(), + (macropath + "/" + mli.begin()->second).c_str(), + ((std::string)mli.begin()->first).c_str()); return macropath + "/" + mli.begin()->second; } diff --git a/server/src/queryhandlerpracro.cc b/server/src/queryhandlerpracro.cc index ab1466e..682708c 100644 --- a/server/src/queryhandlerpracro.cc +++ b/server/src/queryhandlerpracro.cc @@ -30,10 +30,10 @@ #include -QueryHandlerPracro::QueryHandlerPracro(Database *db, std::string cpr) +QueryHandlerPracro::QueryHandlerPracro(Database &_db, std::string cpr) + : db(_db) { this->cpr = cpr; - this->db = db; } QueryResult QueryHandlerPracro::exec(Query &query) @@ -47,7 +47,7 @@ QueryResult QueryHandlerPracro::exec(Query &query) std::string ttl = query.attributes["ttl"]; time_t oldest = time(NULL) - atol(ttl.c_str()); - Values values = db->getValues(cpr, fields, oldest); + Values values = db.getValues(cpr, fields, oldest); std::string value = values[field].value; time_t timestamp = values[field].timestamp; @@ -69,7 +69,7 @@ int main() { Database db("pgsql", Conf::database_addr, "", Conf::database_user, Conf::database_passwd, ""); - QueryHandlerPracro qh(&db, "2003791613"); + QueryHandlerPracro qh(db, "2003791613"); Query q1; q1.attributes["device_id"] = "lensmeter"; diff --git a/server/src/queryhandlerpracro.h b/server/src/queryhandlerpracro.h index 73802f0..bdb85db 100644 --- a/server/src/queryhandlerpracro.h +++ b/server/src/queryhandlerpracro.h @@ -41,14 +41,14 @@ */ class QueryHandlerPracro : public QueryHandler { public: - QueryHandlerPracro(Database *db, std::string cpr); + QueryHandlerPracro(Database &db, std::string cpr); ~QueryHandlerPracro() {} // Execute all queries. QueryResult exec(Query &query); private: - Database *db; + Database &db; std::string cpr; }; diff --git a/server/src/server.cc b/server/src/server.cc index 8bfd827..1bfa2b0 100644 --- a/server/src/server.cc +++ b/server/src/server.cc @@ -79,186 +79,203 @@ public: : Exception("Macro " + r.macro + " not found in course " + r.course) {} }; -static std::string handleTransaction(Transaction *transaction, - TCPSocket *pentominos_socket, - Database *db, - JournalWriter &journalwriter, - MacroList ¯olist) + +static std::string handleCommits(Transaction *transaction, Database &db, + JournalWriter &journalwriter, MacroList ¯olist) { std::string answer; - answer += "\n"; - answer += "\n"; - - try { - // - // Handle commits - // - { - Commits::iterator i = transaction->commits.begin(); - while(i != transaction->commits.end()) { - Commit &commit = *i; - MacroParser mp(macrolist.getLatestVersion(commit.macro)); - mp.parse(); - Macro *macro = mp.getMacro(); - - std::string resume = resume_parser(macro->resume, commit); - commit.fields["journal.resume"] = resume; - db->commitTransaction(transaction->user, transaction->cpr, *macro, commit.fields); - - bool store_in_journal = true; - // We always need to store in journal! - // macro->resume.attributes.find("store_in_journal") != macro->resume.attributes.end() && - // macro->resume.attributes["store_in_journal"] == "true"; - - if(resume != "" && store_in_journal) { - journalwriter.addEntry(*transaction, commit, resume, commit.course); - } - - i++; - } + Commits::iterator i = transaction->commits.begin(); + while(i != transaction->commits.end()) { + Commit &commit = *i; + + MacroParser mp(macrolist.getLatestVersion(commit.macro)); + mp.parse(); + Macro *macro = mp.getMacro(); + + std::string resume = resume_parser(macro->resume, commit); + commit.fields["journal.resume"] = resume; + db.commitTransaction(transaction->user, transaction->cpr, *macro, commit.fields); + + if(resume != "") { + journalwriter.addEntry(*transaction, commit, resume, commit.course); } - - // - // Handle requests - // - Requests::iterator i = transaction->requests.begin(); - while(i != transaction->requests.end()) { - Request &request = *i; - - PRACRO_DEBUG(server, "Handling request - macro: %s, course: %s\n", - request.macro.c_str(), request.course.c_str()); - - // Read and parse the template file. - TemplateParser tp(request.course); - tp.parse(); - - Template *templ = tp.getTemplate(); + + i++; + } - answer += " course.attributes["name"]; - answer += "\" title=\""; - answer += templ->course.attributes["title"]; - answer += "\">\n"; - - bool foundmacro = false; + return answer; +} - // Generate the macro and return it to the client - std::vector< Macro >::iterator mi2 = templ->course.macros.begin(); - while(mi2 != templ->course.macros.end()) { - Macro ¯o = (*mi2); - if(macro.isHeader) { - answer += "
\n"; - mi2++; - continue; - } - - bool completed = db->checkMacro(transaction->cpr, macro.attributes["name"]); +static std::string handleRequest(Transaction *transaction, + TCPSocket *pentominos_socket, + Database &db, + JournalWriter &journalwriter, + MacroList ¯olist) +{ + std::string answer; - answer += " requests.begin(); + while(i != transaction->requests.end()) { + Request &request = *i; + + PRACRO_DEBUG(server, "Handling request - macro: %s, course: %s\n", + request.macro.c_str(), request.course.c_str()); + + // Read and parse the template file. + TemplateParser tp(request.course); + tp.parse(); + + Template *templ = tp.getTemplate(); + + answer += " course.attributes["name"]; + answer += "\" title=\""; + answer += templ->course.attributes["title"]; + answer += "\">\n"; + + bool foundmacro = false; + + // Generate the macro and return it to the client + std::vector< Macro >::iterator mi2 = templ->course.macros.begin(); + while(mi2 != templ->course.macros.end()) { + Macro ¯o = (*mi2); - std::map< std::string, std::string >::iterator ai = macro.attributes.begin(); - while(ai != macro.attributes.end()) { - std::string name = ai->first; - std::string value = ai->second; - answer += " "+name+"=\"" + value + "\""; - ai++; - } + if(macro.isHeader) { + answer += "
\n"; + mi2++; + continue; + } - if(macro.attributes["name"] == request.macro || - (macro.attributes.find("static") != macro.attributes.end() && - macro.attributes["static"] == "true") - ) { - foundmacro = true; + bool completed = db.checkMacro(transaction->cpr, macro.attributes["name"]); - MacroParser mp(macrolist.getLatestVersion(macro.attributes["name"])); - mp.parse(); - Macro *m = mp.getMacro(); - answer += " caption=\"" + m->window.attributes["caption"] + "\""; - answer += ">\n"; + answer += " ::iterator ai = macro.attributes.begin(); + while(ai != macro.attributes.end()) { + std::string name = ai->first; + std::string value = ai->second; + answer += " "+name+"=\"" + value + "\""; + ai++; + } - //////////////////////// - std::vector< Query >::iterator qi = m->queries.begin(); - while(qi != m->queries.end()) { + if(macro.attributes["name"] == request.macro || + (macro.attributes.find("static") != macro.attributes.end() && + macro.attributes["static"] == "true") + ) { + foundmacro = true; - Query &query = *qi; - std::string service = query.attributes["service"]; + MacroParser mp(macrolist.getLatestVersion(macro.attributes["name"])); + mp.parse(); + Macro *m = mp.getMacro(); + answer += " caption=\"" + m->window.attributes["caption"] + "\""; + answer += ">\n"; - if(service == "pentominos") { - // Send the queries to Pentominos (if any) - QueryHandlerPentominos qh(pentominos_socket, transaction->cpr); + LUAQueryMapper lqm; - QueryResult queryresult = qh.exec(*qi); - lqm.addQueryResult(queryresult); - } + //////////////////////// + std::vector< Query >::iterator qi = m->queries.begin(); + while(qi != m->queries.end()) { - if(service == "pracro") { - // Send the queries to Pentominos (if any) - QueryHandlerPracro qh(db, transaction->cpr); + Query &query = *qi; + std::string service = query.attributes["service"]; - QueryResult queryresult = qh.exec(*qi); - lqm.addQueryResult(queryresult); - } + if(service == "pentominos") { + // Send the queries to Pentominos (if any) + QueryHandlerPentominos qh(pentominos_socket, transaction->cpr); - qi++; + QueryResult queryresult = qh.exec(*qi); + lqm.addQueryResult(queryresult); } - // Handle scripts - if(m->scripts.size()) { - answer += " \n"; - - std::vector< Script >::iterator spi = m->scripts.begin(); - while(spi != m->scripts.end()) { - answer += " \n"; - spi++; - } - answer += " \n"; - } + if(service == "pracro") { + // Send the queries to Pentominos (if any) + QueryHandlerPracro qh(db, transaction->cpr); - answer += widgetgenerator(transaction->cpr, *m, lqm, db); - } else { - // only find macro title - MacroParser mp(macrolist.getLatestVersion(macro.attributes["name"])); - mp.parse(); - Macro *m = mp.getMacro(); - answer += " caption=\"" + m->window.attributes["caption"] + "\""; - answer += ">\n"; + QueryResult queryresult = qh.exec(*qi); + lqm.addQueryResult(queryresult); + } + qi++; } - if(completed) { - answer += " "; - answer += db->getResume(transaction->cpr, macro, time(NULL) - Conf::db_max_ttl); - answer += "\n"; + // Handle scripts + if(m->scripts.size()) { + answer += " \n"; + + std::vector< Script >::iterator spi = m->scripts.begin(); + while(spi != m->scripts.end()) { + answer += " \n"; + spi++; + } + answer += " \n"; } - answer += " \n"; - mi2++; + answer += widgetgenerator(transaction->cpr, *m, lqm, db); + } else { + // only find macro title + MacroParser mp(macrolist.getLatestVersion(macro.attributes["name"])); + mp.parse(); + Macro *m = mp.getMacro(); + answer += " caption=\"" + m->window.attributes["caption"] + "\""; + answer += ">\n"; } - - if(foundmacro == false && request.macro != "") - throw NotFoundException(request); - answer += " \n"; + if(completed) { + answer += " "; + answer += db.getResume(transaction->cpr, macro, time(NULL) - Conf::db_max_ttl); + answer += "\n"; + } + + answer += " \n"; + mi2++; - i++; } + + if(foundmacro == false && request.macro != "") + throw NotFoundException(request); + + answer += " \n"; + + i++; + } - answer += "\n"; + return answer; +} +static std::string handleTransaction(Transaction *transaction, + TCPSocket *pentominos_socket, + Database &db, + JournalWriter &journalwriter, + MacroList ¯olist) +{ + std::string answer; + answer += "\n"; + answer += "\n"; + + try { + answer += handleCommits(transaction, db, journalwriter, macrolist); } catch( std::exception &e ) { - answer = error_box(xml_encode(e.what())); + PRACRO_ERR(server, "Commit error: %s\n", e.what()); + return error_box(xml_encode(e.what())); } + try { + answer += handleRequest(transaction, pentominos_socket, db, journalwriter, macrolist); + } catch( std::exception &e ) { + PRACRO_ERR(server, "Request error: %s\n", e.what()); + return error_box(xml_encode(e.what())); + } + + answer += "\n"; + PRACRO_DEBUG(server, "Done handling transaction\n"); PRACRO_DEBUG(serverxml, "%s\n", answer.c_str()); return answer; @@ -272,7 +289,7 @@ static void handleConnection(TCPSocket *socket) pentominos_socket.connect(Conf::pentominos_addr, Conf::pentominos_port); #endif/*WITHOUT_PENTOMINOS*/ - Database *db = new Database("pgsql", Conf::database_addr, "", Conf::database_user, Conf::database_passwd, ""); + Database db("pgsql", Conf::database_addr, "", Conf::database_user, Conf::database_passwd, ""); JournalWriter journalwriter(Conf::journal_commit_addr.c_str(), Conf::journal_commit_port); diff --git a/server/src/widgetgenerator.cc b/server/src/widgetgenerator.cc index 6651fb1..b7d6ac3 100644 --- a/server/src/widgetgenerator.cc +++ b/server/src/widgetgenerator.cc @@ -189,12 +189,12 @@ static void get_fields(Widget &widget, Fieldnames &fields) } } -std::string widgetgenerator(std::string cpr, Macro ¯o, LUAQueryMapper &mapper, Database *db) +std::string widgetgenerator(std::string cpr, Macro ¯o, LUAQueryMapper &mapper, Database &db) { Fieldnames fields; get_fields(macro.window, fields); - Values values = db->getValues(cpr, fields); + Values values = db.getValues(cpr, fields); return send_macro_widget(macro, macro.window, " ", mapper, values); } diff --git a/server/src/widgetgenerator.h b/server/src/widgetgenerator.h index 2956d2b..6a27ab0 100644 --- a/server/src/widgetgenerator.h +++ b/server/src/widgetgenerator.h @@ -37,6 +37,6 @@ std::string widgetgenerator(std::string cpr, Macro ¯o, LUAQueryMapper &mapper, - Database *db); + Database &db); #endif/*__PRACRO_WIDGETGENERATOR_H__*/ -- cgit v1.2.3