diff options
Diffstat (limited to 'server/src/transactionhandler.cc')
-rw-r--r-- | server/src/transactionhandler.cc | 280 |
1 files changed, 142 insertions, 138 deletions
diff --git a/server/src/transactionhandler.cc b/server/src/transactionhandler.cc index 06e5be4..65da013 100644 --- a/server/src/transactionhandler.cc +++ b/server/src/transactionhandler.cc @@ -36,6 +36,7 @@ #include "queryhandlerpracro.h" #include "xml_encode_decode.h" #include "widgetgenerator.h" +#include "journalwriter.h" static std::string error_box(std::string message) { @@ -47,205 +48,207 @@ static std::string error_box(std::string message) return errorbox; } -static std::string handleCommits(Transaction *transaction, Database &db, - JournalWriter &journalwriter, MacroList ¯olist, - TemplateList &templatelist) +static std::string handleCommits(Transaction &transaction, Environment &env, Session &session) { std::string answer; - 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 != "") { + if(transaction.commits.size() > 0) { + AutoBorrower<Database*> borrower(env.dbpool); + Database *db = borrower.get(); - TemplateParser tp(templatelist.getLatestVersion(commit.templ)); - tp.parse(); - Template *templ = tp.getTemplate(); - - journalwriter.addEntry(*transaction, commit, resume, templ); + Commits::iterator i = transaction.commits.begin(); + while(i != transaction.commits.end()) { + Commit &commit = *i; + + MacroParser mp(env.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 != "") { + + TemplateParser tp(env.templatelist.getLatestVersion(commit.templ)); + tp.parse(); + Template *templ = tp.getTemplate(); + + session.journal()->addEntry(transaction, commit, resume, templ); + } + + i++; } - - i++; } return answer; } - -static std::string handleRequest(Transaction *transaction, - TCPSocket &pentominos_socket, - Database &db, - MacroList ¯olist, - TemplateList &templatelist) +static std::string handleRequest(Transaction &transaction, Environment &env, Session &session) { std::string answer; - Requests::iterator i = transaction->requests.begin(); - while(i != transaction->requests.end()) { - Request &request = *i; + if(transaction.requests.size() > 0) { + + AutoBorrower<Database*> borrower(env.dbpool); + Database *db = borrower.get(); - PRACRO_DEBUG(server, "Handling request - macro: %s, template: %s\n", - request.macro.c_str(), request.templ.c_str()); + Requests::iterator i = transaction.requests.begin(); + while(i != transaction.requests.end()) { + Request &request = *i; - // Read and parse the template file. - TemplateParser tp(templatelist.getLatestVersion(request.templ)); - tp.parse(); + PRACRO_DEBUG(server, "Handling request - macro: %s, template: %s\n", + request.macro.c_str(), request.templ.c_str()); - Template *templ = tp.getTemplate(); + // Read and parse the template file. + TemplateParser tp(env.templatelist.getLatestVersion(request.templ)); + tp.parse(); + + Template *templ = tp.getTemplate(); - answer += " <template name=\""; - answer += templ->attributes["name"]; - answer += "\" title=\""; - answer += templ->attributes["title"]; - answer += "\">\n"; + answer += " <template name=\""; + answer += templ->attributes["name"]; + answer += "\" title=\""; + answer += templ->attributes["title"]; + answer += "\">\n"; - bool foundmacro = false; + bool foundmacro = false; - // Generate the macro and return it to the client - std::vector< Macro >::iterator mi2 = templ->macros.begin(); - while(mi2 != templ->macros.end()) { - Macro ¯o = (*mi2); + // Generate the macro and return it to the client + std::vector< Macro >::iterator mi2 = templ->macros.begin(); + while(mi2 != templ->macros.end()) { + Macro ¯o = (*mi2); + + if(macro.isHeader) { + answer += " <header caption=\"" + macro.attributes["caption"] + "\"/>\n"; + mi2++; + continue; + } - if(macro.isHeader) { - answer += " <header caption=\"" + macro.attributes["caption"] + "\"/>\n"; - mi2++; - continue; - } + bool completed = db->checkMacro(transaction.cpr, + macro.attributes["name"], + time(NULL)-Conf::db_max_ttl); - bool completed = db.checkMacro(transaction->cpr, - macro.attributes["name"], - time(NULL)-Conf::db_max_ttl); + answer += " <macro uid=\"42\" completed="; + if(completed) answer += "\"true\""; + else answer += "\"false\""; - answer += " <macro uid=\"42\" completed="; - if(completed) answer += "\"true\""; - else answer += "\"false\""; + 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++; + } - 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.attributes["name"] == request.macro || + (macro.attributes.find("static") != macro.attributes.end() && + macro.attributes["static"] == "true") + ) { + foundmacro = true; - if(macro.attributes["name"] == request.macro || - (macro.attributes.find("static") != macro.attributes.end() && - macro.attributes["static"] == "true") - ) { - foundmacro = true; + MacroParser mp(env.macrolist.getLatestVersion(macro.attributes["name"])); + mp.parse(); + Macro *m = mp.getMacro(); + answer += " caption=\"" + m->widgets.attributes["caption"] + "\""; + answer += ">\n"; - MacroParser mp(macrolist.getLatestVersion(macro.attributes["name"])); - mp.parse(); - Macro *m = mp.getMacro(); - answer += " caption=\"" + m->widgets.attributes["caption"] + "\""; - answer += ">\n"; + AutoBorrower<Artefact*> borrower(env.atfpool); + Artefact *atf = borrower.get(); + + LUAQueryMapper lqm; - LUAQueryMapper lqm; + //////////////////////// + std::vector< Query >::iterator qi = m->queries.begin(); + while(qi != m->queries.end()) { - //////////////////////// - std::vector< Query >::iterator qi = m->queries.begin(); - while(qi != m->queries.end()) { + Query &query = *qi; + std::string service = query.attributes["service"]; - Query &query = *qi; - std::string service = query.attributes["service"]; + if(service == "pentominos") { + // Send the queries to Pentominos (if any) + QueryHandlerPentominos qh(*atf, transaction.cpr); - if(service == "pentominos") { - // Send the queries to Pentominos (if any) - QueryHandlerPentominos qh(pentominos_socket, transaction->cpr); + QueryResult queryresult = qh.exec(*qi); + lqm.addQueryResult(queryresult); + } - QueryResult queryresult = qh.exec(*qi); - lqm.addQueryResult(queryresult); - } + if(service == "pracro") { + // Send the queries to Pentominos (if any) + QueryHandlerPracro qh(*db, transaction.cpr); - if(service == "pracro") { - // Send the queries to Pentominos (if any) - QueryHandlerPracro qh(db, transaction->cpr); + QueryResult queryresult = qh.exec(*qi); + lqm.addQueryResult(queryresult); + } - QueryResult queryresult = qh.exec(*qi); - lqm.addQueryResult(queryresult); + qi++; } - qi++; - } - - // Handle scripts - if(m->scripts.size()) { - answer += " <scripts>\n"; + // Handle scripts + if(m->scripts.size()) { + answer += " <scripts>\n"; - std::vector< Script >::iterator spi = m->scripts.begin(); - while(spi != m->scripts.end()) { - answer += " <script language=\"" + spi->attributes["language"] - + "\" name=\"" + spi->attributes["name"] + "\">\n"; - answer += xml_encode(spi->attributes["code"]); - answer += "\n </script>\n"; - spi++; + std::vector< Script >::iterator spi = m->scripts.begin(); + while(spi != m->scripts.end()) { + answer += " <script language=\"" + spi->attributes["language"] + + "\" name=\"" + spi->attributes["name"] + "\">\n"; + answer += xml_encode(spi->attributes["code"]); + answer += "\n </script>\n"; + spi++; + } + answer += " </scripts>\n"; } - answer += " </scripts>\n"; - } - 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->widgets.attributes["caption"] + "\""; - answer += ">\n"; + answer += widgetgenerator(transaction.cpr, *m, lqm, *db); + } else { + // only find macro title + MacroParser mp(env.macrolist.getLatestVersion(macro.attributes["name"])); + mp.parse(); + Macro *m = mp.getMacro(); + answer += " caption=\"" + m->widgets.attributes["caption"] + "\""; + answer += ">\n"; - } + } - if(completed) { - answer += " <resume>"; - answer += db.getResume(transaction->cpr, macro, time(NULL) - Conf::db_max_ttl); - answer += "</resume>\n"; - } + if(completed) { + answer += " <resume>"; + answer += db->getResume(transaction.cpr, macro, time(NULL) - Conf::db_max_ttl); + answer += "</resume>\n"; + } - answer += " </macro>\n"; - mi2++; + answer += " </macro>\n"; + mi2++; - } + } - if(foundmacro == false && request.macro != "") - throw NotFoundException(request); + if(foundmacro == false && request.macro != "") + throw NotFoundException(request); - answer += " </template>\n"; + answer += " </template>\n"; - i++; + i++; + } } return answer; } -std::string handleTransaction(Transaction *transaction, - TCPSocket &pentominos_socket, - Database &db, - JournalWriter &journalwriter, - MacroList ¯olist, - TemplateList &templatelist) +std::string handleTransaction(Transaction &transaction, Environment &env, Session &session) { std::string answer; + answer += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; answer += "<pracro version=\"1.0\">\n"; try { - answer += handleCommits(transaction, db, - journalwriter, macrolist, templatelist); + answer += handleCommits(transaction, env, session); } catch( std::exception &e ) { PRACRO_ERR(server, "Commit error: %s\n", e.what()); return error_box(xml_encode(e.what())); } try { - answer += handleRequest(transaction, pentominos_socket, db, macrolist, templatelist); + answer += handleRequest(transaction, env, session); } catch( std::exception &e ) { PRACRO_ERR(server, "Request error: %s\n", e.what()); return error_box(xml_encode(e.what())); @@ -255,6 +258,7 @@ std::string handleTransaction(Transaction *transaction, PRACRO_DEBUG(server, "Done handling transaction\n"); PRACRO_DEBUG(serverxml, "%s\n", answer.c_str()); + return answer; } |