diff options
author | deva <deva> | 2008-08-15 09:37:36 +0000 |
---|---|---|
committer | deva <deva> | 2008-08-15 09:37:36 +0000 |
commit | 5ccdf6dd987be086a8712a4960f770122b1b499b (patch) | |
tree | 7999dd2a4b9e4e3aaccf3493cd7826528ddd1c01 /server/src/server.cc | |
parent | b7be78948cad7629427fd19769ddcfdfea3fb666 (diff) |
Changes for read-loop-server.
Diffstat (limited to 'server/src/server.cc')
-rw-r--r-- | server/src/server.cc | 103 |
1 files changed, 75 insertions, 28 deletions
diff --git a/server/src/server.cc b/server/src/server.cc index a419181..d2921b8 100644 --- a/server/src/server.cc +++ b/server/src/server.cc @@ -52,6 +52,8 @@ static std::string error_box(std::string message) { std::string errorbox = + "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<pracro version=\"1.0\">\n" " <course name=\"error\">\n" " <macro name=\"error\">\n" " <window caption=\"ERROR!\" height=\"300\" layout=\"vbox\" name=\"error\" width=\"480\">\n" @@ -59,7 +61,8 @@ static std::string error_box(std::string message) " <button action=\"cancel\" caption=\"Luk\" name=\"cancel\"/>\n" " </window>\n" " </macro>\n" - " </course>\n"; + " </course>\n" + "</pracro>\n"; return errorbox; } @@ -69,16 +72,13 @@ public: : Exception("Macro " + r.macro + " not found in course " + r.course) {} }; -static void connection(TCPSocket &socket) +static std::string handleTransaction(Transaction &transaction) { - socket.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); - socket.write("<pracro version=\"1.0\">\n"); + std::string answer; + answer += "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; + answer += "<pracro version=\"1.0\">\n"; try { - Transaction transaction; - TransactionParser parser(socket, transaction); - parser.parse(); - Database db; // @@ -112,8 +112,6 @@ static void connection(TCPSocket &socket) while(i != transaction.requests.end()) { Request &request = *i; - std::string answer; - printf("Handling request - macro: %s, course: %s\n", request.macro.c_str(), request.course.c_str()); @@ -201,26 +199,72 @@ static void connection(TCPSocket &socket) answer += " </course>\n"; - socket.write(answer); i++; } + + answer += "</pracro>\n"; + } catch( PGSTD::runtime_error &e ) { - socket.write(error_box(xml_encode(std::string("PostgreSQL server error:\n") + e.what()))); + answer = error_box(xml_encode(std::string("PostgreSQL server error:\n") + e.what())); } catch( std::exception &e ) { - socket.write(error_box(xml_encode(e.what()))); + answer = error_box(xml_encode(e.what())); } - socket.write("</pracro>\n"); + return answer; } +static void handleConnection(TCPSocket *socket) +{ + size_t bufsize = 256; + int size; + char buf[256]; + memset(buf, 0, bufsize); + + Transaction *transaction = NULL; + TransactionParser *parser = NULL; + + while((size = socket->read(buf, bufsize)) > 0) { + + if(transaction == NULL) { + transaction = new Transaction(); + parser = new TransactionParser(transaction); + } + + printf("Got %d bytes in read loop\n", size); + if(parser->parse(buf)) { + socket->write(handleTransaction(*transaction)); + + delete transaction; + transaction = NULL; + + delete parser; + parser = NULL; + } + memset(buf, 0, bufsize); + } + + if(transaction) { + delete transaction; + transaction = NULL; + + delete parser; + parser = NULL; + } + + printf("Out of read loop!\n"); +} + +//#define NON_FORKING +#include <sys/socket.h> +extern bool pracro_is_running; void server() { port_t port = Conf::server_port; TCPSocket *socket = NULL; try { - socket = new TCPSocket(); + socket = new TCPSocket("Listen socket"); socket->listen(port); } catch (Exception &e) { fprintf(stderr, "Error during parsing:\n%s\n", @@ -230,7 +274,7 @@ void server() return; } - while(socket->connected()) { + while(pracro_is_running && socket->connected()) { { // Reload if new port is assigned. int old_port = port; @@ -239,41 +283,44 @@ void server() if(port != old_port) { // Start listening on the new port delete socket; - socket = new TCPSocket(); + socket = new TCPSocket("Listen socket (reloaded)"); socket->listen(port); } } - TCPSocket child = socket->accept(); - if(child.connected()) { - //socket->disconnect(); - connection(child); - //delete socket; + TCPSocket *child = socket->accept(); + if(child) { - /* +#ifndef NON_FORKING switch(fork()) { case -1: // error fprintf(stderr, "Could not fork: %s\n", strerror(errno)); break; case 0: // child - socket->disconnect(); - connection(child); delete socket; +#endif/*NON_FORKING*/ + handleConnection(child); + delete child; +#ifndef NON_FORKING return; default: // parent - child.disconnect(); + delete child; break; } - */ +#endif/*NON_FORKING*/ + } } + //socket->shutdown(); delete socket; - fprintf(stderr, "Oups... dropped out of the main loop\n"); + + printf("Server gracefully shut down.\n"); } + #ifdef TEST_SERVER char request[] = |