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 | |
parent | e2120257dda4d91b48bb031a96edda810ce30dfb (diff) |
Connections to Pentominos and Database are now persistent over the entire client connection.
Diffstat (limited to 'server/src')
-rw-r--r-- | server/src/queryhandler.cc | 13 | ||||
-rw-r--r-- | server/src/saxparser.cc | 4 | ||||
-rw-r--r-- | server/src/server.cc | 80 | ||||
-rw-r--r-- | server/src/tcpsocket.cc | 10 | ||||
-rw-r--r-- | server/src/tcpsocket.h | 1 | ||||
-rw-r--r-- | server/src/widgetgenerator.cc | 4 | ||||
-rw-r--r-- | server/src/widgetgenerator.h | 2 |
7 files changed, 61 insertions, 53 deletions
diff --git a/server/src/queryhandler.cc b/server/src/queryhandler.cc index 248f1e6..a6b6e1b 100644 --- a/server/src/queryhandler.cc +++ b/server/src/queryhandler.cc @@ -207,6 +207,7 @@ QueryResult QueryHandler::exec(Query &query) #ifdef WITH_DEBUG printf(buf); + fflush(stdout); #endif/*WITH_DEBUG*/ QueryResult result; @@ -214,19 +215,19 @@ QueryResult QueryHandler::exec(Query &query) #ifndef WITHOUT_PENTOMINOS QueryParser parser; - int asize; - char abuf[64]; - memset(abuf, 0, sizeof(abuf)); + ssize_t size; // Read until we've got the entire result. - while((asize = socket->read(abuf, sizeof(abuf) - 1)) != -1 && - parser.parse(abuf, asize) == false) { - memset(abuf, 0, sizeof(abuf)); + while((size = socket->read(buf, sizeof(buf))) > 0) { + fwrite(buf, size, 1, stdout); fflush(stdout); + if(parser.parse(buf, size)) break; } result = parser.result; #endif/*WITHOUT_PENTOMINOS*/ + printf("Done handling query\n"); + return result; } diff --git a/server/src/saxparser.cc b/server/src/saxparser.cc index be7a541..4a69a6c 100644 --- a/server/src/saxparser.cc +++ b/server/src/saxparser.cc @@ -112,6 +112,8 @@ int SAXParser::parse() bool SAXParser::parse(char *data, size_t size) { + printf("parse %d bytes\n", size); + bufferbytes = size; totalbytes += bufferbytes; @@ -129,7 +131,7 @@ bool SAXParser::parse(char *data, size_t size) } } - printf("Got END_OF_DOCUMENT [%s] at %ld\n", outertag.c_str(), XML_GetCurrentByteIndex(p)); + if(done) printf("Got END_OF_DOCUMENT [%s] at %ld\n", outertag.c_str(), XML_GetCurrentByteIndex(p)); return done; } 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; } diff --git a/server/src/tcpsocket.cc b/server/src/tcpsocket.cc index b10f064..9d481ff 100644 --- a/server/src/tcpsocket.cc +++ b/server/src/tcpsocket.cc @@ -109,7 +109,7 @@ void TCPSocket::listen(unsigned short int port) { if(sock == -1) throw TCPListenException("Socket not initialized."); - if(isconnected) throw TCPListenException("Socket already connected."); + if(isconnected) throw TCPListenException("Socket alread connected."); struct sockaddr_in socketaddr; memset((char *) &socketaddr, sizeof(socketaddr), 0); @@ -319,14 +319,6 @@ int TCPSocket::write(char *data, int size) throw TCPWriteException(strerror(errno)); } -#ifdef WITH_DEBUG - std::string blah; - blah.append(data, size); - printf(blah.c_str()); - - // printf("Outputted %d bytes\n", res); -#endif/*WITH_DEBUG*/ - return res; } diff --git a/server/src/tcpsocket.h b/server/src/tcpsocket.h index 4771d10..6819537 100644 --- a/server/src/tcpsocket.h +++ b/server/src/tcpsocket.h @@ -147,6 +147,7 @@ public: * Reads bytes from the socket into a buffer. * @param buf The buffer into which the data will be written. * @param size The maximum number of bytes to read in (the size of the buffer). + * @parasm timeout The timeout in ms, -1 is no timeout. -1 is default. * @return The actual number of bytes read. */ int read(char *buf, int size, long timeout = -1) throw(TCPReadException); diff --git a/server/src/widgetgenerator.cc b/server/src/widgetgenerator.cc index a007002..f9feb19 100644 --- a/server/src/widgetgenerator.cc +++ b/server/src/widgetgenerator.cc @@ -152,12 +152,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 6a27ab0..2956d2b 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__*/ |