diff options
author | deva <deva> | 2008-10-08 10:47:25 +0000 |
---|---|---|
committer | deva <deva> | 2008-10-08 10:47:25 +0000 |
commit | 8bbcc59fd1488833a4d8dd346b6e8fec2a0021a8 (patch) | |
tree | 0e55747eba091461ec21f162b295ebb033666c44 /server/src/server.cc | |
parent | e2120257dda4d91b48bb031a96edda810ce30dfb (diff) |
Connections to Pentominos and Database are now persistent over the entire client connection.
Diffstat (limited to 'server/src/server.cc')
-rw-r--r-- | server/src/server.cc | 80 |
1 files changed, 46 insertions, 34 deletions
diff --git a/server/src/server.cc b/server/src/server.cc index 64ee709..92afbd0 100644 --- a/server/src/server.cc +++ b/server/src/server.cc @@ -71,37 +71,37 @@ public: : Exception("Macro " + r.macro + " not found in course " + r.course) {} }; -static std::string handleTransaction(Transaction &transaction) +static std::string handleTransaction(Transaction *transaction, + TCPSocket *pentominos_socket, + Database *db) { std::string answer; answer += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; answer += "<pracro version=\"1.0\">\n"; try { - Database db(Conf::database_addr, Conf::database_user, Conf::database_passwd); - // // Handle commits // { - Commits::iterator i = transaction.commits.begin(); - while(i != transaction.commits.end()) { + Commits::iterator i = transaction->commits.begin(); + while(i != transaction->commits.end()) { Commit &commit = *i; MacroParser mp(commit.macro); mp.parse(); Macro *macro = mp.getMacro(); - db.commit(transaction.user, transaction.cpr, *macro, commit.fields); + db->commit(transaction->user, transaction->cpr, *macro, commit.fields); std::string resume = resume_parser(macro->attributes["resume"].c_str(), commit); // if(resume != "") { - journal_commit(transaction.cpr.c_str(), transaction.user.c_str(), + 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, + db->putJournal(transaction->user, transaction->cpr, *macro, resume, time(NULL)); // } @@ -112,8 +112,8 @@ static std::string handleTransaction(Transaction &transaction) // // Handle requests // - Requests::iterator i = transaction.requests.begin(); - while(i != transaction.requests.end()) { + Requests::iterator i = transaction->requests.begin(); + while(i != transaction->requests.end()) { Request &request = *i; printf("Handling request - macro: %s, course: %s\n", @@ -136,7 +136,7 @@ static std::string handleTransaction(Transaction &transaction) while(mi2 != templ->course.macroes.end()) { Macro ¯o = (*mi2); - bool completed = db.checkMacro(transaction.cpr, macro.attributes["name"]); + bool completed = db->checkMacro(transaction->cpr, macro.attributes["name"]); answer += " <macro completed="; if(completed) answer += "\"true\""; @@ -168,11 +168,7 @@ static std::string handleTransaction(Transaction &transaction) while(qi != m->queries.end()) { /////////////////////////////// // 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); + QueryHandler qh(pentominos_socket, transaction->cpr); QueryResult queryresult = qh.exec(*qi); lqm.addQueryResult(queryresult); @@ -195,7 +191,7 @@ static std::string handleTransaction(Transaction &transaction) answer += " </scripts>\n"; } - answer += widgetgenerator(transaction.cpr, *m, lqm, db); + answer += widgetgenerator(transaction->cpr, *m, lqm, db); } else { // only find macro title MacroParser mp(macro.attributes["name"]); @@ -208,7 +204,7 @@ static std::string handleTransaction(Transaction &transaction) if(completed) { answer += " <resume>"; - answer += db.getResume(transaction.cpr, macro.attributes["name"], time(NULL) - Conf::db_max_ttl); + answer += db->getResume(transaction->cpr, macro.attributes["name"], time(NULL) - Conf::db_max_ttl); answer += " </resume>\n"; } @@ -233,26 +229,39 @@ static std::string handleTransaction(Transaction &transaction) answer = error_box(xml_encode(e.what())); } + printf("Done handling transaction\n"); + return answer; } static void handleConnection(TCPSocket *socket) { - size_t bufsize = 4096; - int size; - char buf[bufsize]; - memset(buf, 0, bufsize); + TCPSocket pentominos_socket; +#ifndef WITHOUT_PENTOMINOS + pentominos_socket.connect(Conf::pentominos_addr, Conf::pentominos_port); +#endif/*WITHOUT_PENTOMINOS*/ + + Database db(Conf::database_addr, Conf::database_user, Conf::database_passwd); + + ssize_t size; + char buf[4096]; Transaction *transaction = NULL; TransactionParser *parser = NULL; - while((size = socket->read(buf, bufsize)) > 0) { - + // while( (size = socket->read(buf, sizeof(buf))) != -1) { + while( (size = socket->read(buf, sizeof(buf))) > 0) { + + printf("Read %d bytes from network\n", size); + while(size) { if(transaction == NULL) { transaction = new Transaction(); + } + + if(parser == NULL) { parser = new TransactionParser(transaction); } @@ -260,27 +269,30 @@ static void handleConnection(TCPSocket *socket) if(parser->parse(buf, size)) { printf("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n" "!! Got complete XML document %d bytes used, %d bytes in current buffer.\n" - "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n", parser->usedBytes(), strlen(buf)); + "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n", parser->usedBytes(), size); - socket->write(handleTransaction(*transaction)); + socket->write(handleTransaction(transaction, &pentominos_socket, &db)); + size = size - parser->usedBytes(); - delete transaction; - transaction = NULL; - - delete parser; - parser = NULL; + delete transaction; transaction = NULL; + delete parser; parser = NULL; + } else { + size = size - parser->usedBytes(); } - size = size - parser->usedBytes(); - if(size) memcpy(buf, buf + parser->usedBytes(), size); + if(size > 0) { + strcpy(buf, buf + size); + printf("Replaying %d bytes.\n", size); + } } - memset(buf, 0, bufsize); } if(transaction) { delete transaction; transaction = NULL; + } + if(parser) { delete parser; parser = NULL; } |