diff options
Diffstat (limited to 'server/src/server.cc')
-rw-r--r-- | server/src/server.cc | 77 |
1 files changed, 50 insertions, 27 deletions
diff --git a/server/src/server.cc b/server/src/server.cc index 6783492..f6c811b 100644 --- a/server/src/server.cc +++ b/server/src/server.cc @@ -46,6 +46,8 @@ #include <fcntl.h> #include <unistd.h> +#include "transaction.h" +#include "xmlparser.h" /** \section{Data transmission} @@ -70,55 +72,76 @@ brugerens handling. \item Serveren lagrer dataene i en database hvis det gik godt. \end{itemize} */ -static void connection(Pentominos::TCPSocket &socket) +static void connection(TCPSocket &socket) { printf("Got connection...\n"); - std::string macro_request; + Transaction transaction; + parse(socket, transaction); - char inbuf[300]; // Must be at least 3 - memset(inbuf, 0, sizeof(inbuf)); // Ensure zero termination. + std::string xml_version = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; + socket.write((char*)xml_version.c_str(), xml_version.length()); - // Get request. - while(socket.read(inbuf, sizeof(inbuf) - 1) == sizeof(inbuf) - 1) { - macro_request += inbuf; - memset(inbuf, 0, sizeof(inbuf)); // Ensure zero termination. - } - macro_request += inbuf; + std::string header = "<pracro version=\"1.0\">\n"; + socket.write((char*)header.c_str(), header.length()); + + // Handle requests + Requests::iterator i = transaction.requests.begin(); + while(i != transaction.requests.end()) { + Request request = *i; - printf("Got request for [%s]\n", macro_request.c_str()); + printf("Request [%s]...\n", request.macro.c_str()); - // Now handle the request. - char outbuf[3]; - int bytes; + + // Now handle the request. + char outbuf[3]; + int bytes; - std::string macro = std::string(XML) + "/" + macro_request + ".xml"; + std::string macro = std::string(XML) + "/" + request.macro + ".xml"; + + int fd = open(macro.c_str(), O_RDONLY); + if(fd == -1) { + fprintf(stderr, "Aaargh... cannot open file...[%s]\n", macro.c_str()); + continue; + } - int fd = open(macro.c_str(), O_RDONLY); - if(fd == -1) return; + while((bytes = read(fd, outbuf, sizeof(outbuf))) ) { + socket.write(outbuf, bytes); + } + close(fd); + + i++; + } - while((bytes = read(fd, outbuf, sizeof(outbuf))) ) { - socket.write(outbuf, bytes); + // Handle commits + Commits::iterator j = transaction.commits.begin(); + while(j != transaction.commits.end()) { + printf("Commit...\n"); + j++; } - close(fd); + + std::string footer = "</pracro>\n"; + socket.write((char*)footer.c_str(), footer.length()); + + printf("done\n"); } void server() { int port; try { - port = Pentominos::config()->lookup("port"); + port = config()->lookup("port"); } catch( ... ) { fprintf(stderr, "Could not read port."); return; } - Pentominos::TCPSocket *socket; + TCPSocket *socket; try { - socket = new Pentominos::TCPSocket(); + socket = new TCPSocket(); socket->listen(port); - } catch (Pentominos::Exception &e) { + } catch (Exception &e) { fprintf(stderr, "Error during parsing:\n%s\n", e.what()); delete socket; @@ -130,7 +153,7 @@ void server() { // Reload if new port i assigned. int old_port = port; try { - port = Pentominos::config()->lookup("port"); + port = config()->lookup("port"); } catch( ... ) { fprintf(stderr, "Could not read port."); return; @@ -139,12 +162,12 @@ void server() if(port != old_port) { // Start listening on the new port delete socket; - socket = new Pentominos::TCPSocket(); + socket = new TCPSocket(); socket->listen(port); } } - Pentominos::TCPSocket child = socket->accept(); + TCPSocket child = socket->accept(); if(child.connected()) { switch(fork()) { case -1: // error |