diff options
author | deva <deva> | 2008-09-22 12:17:51 +0000 |
---|---|---|
committer | deva <deva> | 2008-09-22 12:17:51 +0000 |
commit | 1b9cf53926a6627b9f8ed00a8dc1f4a784e6f295 (patch) | |
tree | 1a82a8e8ff50cc9d71e04867ca942e187605a1d9 /server | |
parent | 8352be99474fbbeed5795e8610b3d9a23e38b1cd (diff) |
Added resume storing to the db, and made the outputted xml use it. Added captions to the macro tags from the window-tag caption attribute. Made all template attributes fall through to the client.
Diffstat (limited to 'server')
-rw-r--r-- | server/src/database.cc | 76 | ||||
-rw-r--r-- | server/src/database.h | 12 | ||||
-rw-r--r-- | server/src/server.cc | 46 |
3 files changed, 121 insertions, 13 deletions
diff --git a/server/src/database.cc b/server/src/database.cc index 9cdbb7f..2338bb6 100644 --- a/server/src/database.cc +++ b/server/src/database.cc @@ -142,6 +142,76 @@ void Database::commit(std::string user, #endif/*0*/ } +void Database::putJournal(std::string user, + std::string cpr, + Macro &_macro, + std::string resume, + time_t now) +{ + std::string version = _macro.attributes["version"]; + 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 journal" + " VALUES('"+protect(cpr)+"', '"+protect(macro)+"', '"+protect(version)+ + "', '"+protect(timestamp.str())+"', '"+protect(user)+"', '"+protect(resume)+"')"; + + std::stringstream oid; + +#ifndef WITHOUT_DB + pqxx::result R = W.exec(ts); +#endif/*WITHOUT_DB*/ + +#ifdef WITH_DEBUG + printf("%s\n", ts.c_str()); +#endif/*WITH_DEBUG*/ + +#ifndef WITHOUT_DB + W.commit(); +#endif/*WITHOUT_DB*/ +} + + +std::string Database::getResume(std::string cpr, + std::string macro, + time_t oldest) +{ + std::string resume; + +#ifndef WITHOUT_DB + pqxx::work W(c); +#endif/*WITHOUT_DB*/ + + std::stringstream query; + query << "SELECT journal"; + query << " FROM journal"; + query << " WHERE cpr = '" << protect(cpr) << "'"; + query << " AND macro = '" << protect(macro) << "'"; + query << " ORDER BY timestamp;"; + +#ifdef WITH_DEBUG + printf("%s\n", query.str().c_str()); +#endif/*WITH_DEBUG*/ + +#ifndef WITHOUT_DB + pqxx::result R = W.exec(query.str()); + + pqxx::result::const_iterator ri = R.begin(); + while(ri != R.end()) { + pqxx::result::tuple t = *ri; + resume = t[0].c_str(); + ri++; + } +#endif/*WITHOUT_DB*/ + + return resume; +} + Values Database::getValues(std::string cpr, Fieldnames &fields, @@ -275,10 +345,10 @@ ALTER TABLE fields OWNER TO pracro; int main() { - Database db; + Database db("localhost", "pracro", "pracro"); time_t now = time(NULL); - + /* Macro macro; macro.attributes["name"] = "testmacro"; macro.attributes["version"] = "1.0"; @@ -300,6 +370,8 @@ int main() printf("%s -> %s (%u)\n", i->first.c_str(), v.value.c_str(), (unsigned int)v.timestamp); i++; } + */ + printf(db.getResume("1505050505", "B.2.5", now - 60 * 60 * 24).c_str()); } #endif/*TEST_DATABASE*/ diff --git a/server/src/database.h b/server/src/database.h index 332907f..684cd6c 100644 --- a/server/src/database.h +++ b/server/src/database.h @@ -71,6 +71,18 @@ public: bool checkMacro(std::string cpr, std::string macro, time_t oldest = 0); + + // Put an entry in the journal table + void putJournal(std::string user, + std::string cpr, + Macro &_macro, + std::string resume, + time_t now); + + // Get latest resume of a given macro + std::string getResume(std::string cpr, + std::string macro, + time_t oldest); // Connect to the db void connect() {} diff --git a/server/src/server.cc b/server/src/server.cc index 83d9157..ad7065e 100644 --- a/server/src/server.cc +++ b/server/src/server.cc @@ -97,10 +97,15 @@ static std::string handleTransaction(Transaction &transaction) std::string resume = resume_parser(macro->attributes["resume"].c_str(), commit); - journal_commit(transaction.cpr.c_str(), transaction.user.c_str(), - Conf::journal_commit_addr.c_str(), Conf::journal_commit_port, - resume.c_str(), resume.length()); - + // if(resume != "") { + journal_commit(transaction.cpr.c_str(), transaction.user.c_str(), + Conf::journal_commit_addr.c_str(), Conf::journal_commit_port, + resume.c_str(), resume.length()); + + db.putJournal(transaction.user, transaction.cpr, + *macro, resume, time(NULL)); + // } + i++; } } @@ -131,19 +136,31 @@ static std::string handleTransaction(Transaction &transaction) std::vector< Macro >::iterator mi2 = templ->course.macroes.begin(); while(mi2 != templ->course.macroes.end()) { Macro ¯o = (*mi2); + + bool completed = db.checkMacro(transaction.cpr, macro.attributes["name"]); - answer += " <macro name=\"" + macro.attributes["name"] + "\" completed="; - if(db.checkMacro(transaction.cpr, macro.attributes["name"])) answer += "\"true\""; + answer += " <macro completed="; + if(completed) answer += "\"true\""; else answer += "\"false\""; - - if(macro.attributes["name"] == request.macro) { - answer += ">\n"; + 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; - MacroParser mp(request.macro); + MacroParser mp(macro.attributes["name"]); mp.parse(); Macro *m = mp.getMacro(); + answer += " caption=\"" + m->window.attributes["caption"] + "\""; + answer += ">\n"; LUAQueryMapper lqm; @@ -197,10 +214,17 @@ static std::string handleTransaction(Transaction &transaction) MacroParser mp(macro.attributes["name"]); mp.parse(); Macro *m = mp.getMacro(); - answer += "caption=\"" + m->window.attributes["caption"] + "\""; + answer += " caption=\"" + m->window.attributes["caption"] + "\""; answer += ">\n"; } + + if(completed) { + answer += " <resume>"; + answer += db.getResume(transaction.cpr, macro.attributes["name"], time(NULL) - Conf::db_max_ttl); + answer += " </resume>\n"; + } + answer += " </macro>\n"; mi2++; |