From 4861dce935bba282fe171e4b855216b6074962c5 Mon Sep 17 00:00:00 2001 From: deva Date: Fri, 18 Dec 2009 09:11:24 +0000 Subject: Moved transaction handling out of server.cc --- server/src/Makefile.am | 2 + server/src/server.cc | 220 +------------------------------ server/src/transactionhandler.cc | 276 +++++++++++++++++++++++++++++++++++++++ server/src/transactionhandler.h | 52 ++++++++ 4 files changed, 332 insertions(+), 218 deletions(-) create mode 100644 server/src/transactionhandler.cc create mode 100644 server/src/transactionhandler.h (limited to 'server/src') diff --git a/server/src/Makefile.am b/server/src/Makefile.am index 8f24e07..3d30adf 100644 --- a/server/src/Makefile.am +++ b/server/src/Makefile.am @@ -36,6 +36,7 @@ pracrod_SOURCES = \ templatelist.cc \ templateheaderparser.cc \ templateparser.cc \ + transactionhandler.cc \ transactionparser.cc \ tcpsocket.cc \ utf8.cc \ @@ -106,6 +107,7 @@ EXTRA_DIST = \ templatelist.h \ templateheaderparser.h \ templateparser.h \ + transactionhandler.h \ transactionparser.h \ tcpsocket.h \ utf8.h \ diff --git a/server/src/server.cc b/server/src/server.cc index b264426..747e88c 100644 --- a/server/src/server.cc +++ b/server/src/server.cc @@ -62,6 +62,8 @@ #include "mutex.h" #include "log.h" +#include "transactionhandler.h" + typedef long long unsigned int sessionid_t; typedef struct { @@ -84,224 +86,6 @@ static std::string error_box(std::string message) return errorbox; } -class NotFoundException : public Exception { -public: - NotFoundException(Request &r) - : Exception("Macro " + r.macro + " not found in template " + r.templ) {} -}; - - -static std::string handleCommits(Transaction *transaction, Database &db, - JournalWriter &journalwriter, MacroList ¯olist, - TemplateList &templatelist) -{ - 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 != "") { - - TemplateParser tp(templatelist.getLatestVersion(commit.templ)); - tp.parse(); - Template *templ = tp.getTemplate(); - - journalwriter.addEntry(*transaction, commit, resume, templ); - } - - i++; - } - - return answer; -} - - -static std::string handleRequest(Transaction *transaction, - TCPSocket &pentominos_socket, - Database &db, - MacroList ¯olist, - TemplateList &templatelist) -{ - std::string answer; - - Requests::iterator i = transaction->requests.begin(); - while(i != transaction->requests.end()) { - Request &request = *i; - - PRACRO_DEBUG(server, "Handling request - macro: %s, template: %s\n", - request.macro.c_str(), request.templ.c_str()); - - // Read and parse the template file. - TemplateParser tp(templatelist.getLatestVersion(request.templ)); - tp.parse(); - - Template *templ = tp.getTemplate(); - - answer += " \n"; - - i++; - } - - return answer; -} - -static std::string handleTransaction(Transaction *transaction, - TCPSocket &pentominos_socket, - Database &db, - JournalWriter &journalwriter, - MacroList ¯olist, - TemplateList &templatelist) -{ - std::string answer; - answer += "\n"; - answer += "\n"; - - try { - answer += handleCommits(transaction, db, - journalwriter, macrolist, templatelist); - } 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); - } 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; -} - static std::string handleConnection(const char *buf, size_t size, struct conn_t *conn, sessionid_t sid, bool commitsession) { diff --git a/server/src/transactionhandler.cc b/server/src/transactionhandler.cc new file mode 100644 index 0000000..fa7a419 --- /dev/null +++ b/server/src/transactionhandler.cc @@ -0,0 +1,276 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set et sw=2 ts=2: */ +/*************************************************************************** + * transactionhandler.cc + * + * Fri Dec 18 10:00:50 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 "transactionhandler.h" + +#include "macroparser.h" +#include "resumeparser.h" +#include "templateparser.h" +#include "configuration.h" +#include "luaquerymapper.h" +#include "queryhandlerpentominos.h" +#include "queryhandlerpracro.h" +#include "xml_encode_decode.h" +#include "widgetgenerator.h" + +static std::string error_box(std::string message) +{ + std::string errorbox = + "\n" + "\n" + " " + message + "\n" + "\n"; + return errorbox; +} + +static std::string handleCommits(Transaction *transaction, Database &db, + JournalWriter &journalwriter, MacroList ¯olist, + TemplateList &templatelist) +{ + 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 != "") { + + TemplateParser tp(templatelist.getLatestVersion(commit.templ)); + tp.parse(); + Template *templ = tp.getTemplate(); + + journalwriter.addEntry(*transaction, commit, resume, templ); + } + + i++; + } + + return answer; +} + + +static std::string handleRequest(Transaction *transaction, + TCPSocket &pentominos_socket, + Database &db, + MacroList ¯olist, + TemplateList &templatelist) +{ + std::string answer; + + Requests::iterator i = transaction->requests.begin(); + while(i != transaction->requests.end()) { + Request &request = *i; + + PRACRO_DEBUG(server, "Handling request - macro: %s, template: %s\n", + request.macro.c_str(), request.templ.c_str()); + + // Read and parse the template file. + TemplateParser tp(templatelist.getLatestVersion(request.templ)); + tp.parse(); + + Template *templ = tp.getTemplate(); + + answer += " \n"; + + i++; + } + + return answer; +} + +std::string handleTransaction(Transaction *transaction, + TCPSocket &pentominos_socket, + Database &db, + JournalWriter &journalwriter, + MacroList ¯olist, + TemplateList &templatelist) +{ + std::string answer; + answer += "\n"; + answer += "\n"; + + try { + answer += handleCommits(transaction, db, + journalwriter, macrolist, templatelist); + } 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); + } 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; +} + +#ifdef TEST_TRANSACTIONHANDLER +//Additional dependency files +//deps: +//Required cflags (autoconf vars may be used) +//cflags: +//Required link options (autoconf vars may be used) +//libs: +#include "test.h" + +TEST_BEGIN; + +// TODO: Put some testcode here (see test.h for usable macros). + +TEST_END; + +#endif/*TEST_TRANSACTIONHANDLER*/ diff --git a/server/src/transactionhandler.h b/server/src/transactionhandler.h new file mode 100644 index 0000000..9a2086a --- /dev/null +++ b/server/src/transactionhandler.h @@ -0,0 +1,52 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim: set et sw=2 ts=2: */ +/*************************************************************************** + * transactionhandler.h + * + * Fri Dec 18 10:00:50 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_TRANSACTIONHANDLER_H__ +#define __PRACRO_TRANSACTIONHANDLER_H__ + +#include "exception.h" +#include "transaction.h" +#include "tcpsocket.h" +#include "database.h" +#include "journal_commit.h" +#include "macrolist.h" +#include "templatelist.h" + +class NotFoundException : public Exception { +public: + NotFoundException(Request &r) + : Exception("Macro " + r.macro + " not found in template " + r.templ) {} +}; + +std::string handleTransaction(Transaction *transaction, + TCPSocket &pentominos_socket, + Database &db, + JournalWriter &journalwriter, + MacroList ¯olist, + TemplateList &templatelist); + +#endif/*__PRACRO_TRANSACTIONHANDLER_H__*/ -- cgit v1.2.3