From a34402b79b38624a29ed8ea4e059af817266e6b8 Mon Sep 17 00:00:00 2001 From: deva Date: Mon, 3 Sep 2007 09:11:33 +0000 Subject: Implemented the first version of the XML request. Fixed eXpat incompatability with XML_Get/SetUserData and the void* in the handler functions. --- server/configure.in | 70 ++++++------ server/src/Makefile.am | 6 +- server/src/configuration.cc | 10 +- server/src/configuration.h | 82 +++++++------- server/src/exception.cc | 14 +-- server/src/exception.h | 45 ++++---- server/src/log.cc | 2 +- server/src/log.h | 12 +-- server/src/pracrod.cc | 14 +-- server/src/server.cc | 77 +++++++++----- server/src/tcpsocket.cc | 32 +++--- server/src/tcpsocket.h | 253 ++++++++++++++++++++++---------------------- server/src/tostring.cc | 44 ++++---- server/src/tostring.h | 140 ++++++++++++------------ server/src/transaction.h | 59 +++++++++++ server/src/xmlparser.cc | 116 ++++++++++++++++++++ server/src/xmlparser.h | 35 ++++++ server/xml/example.xml | 37 ++++--- 18 files changed, 638 insertions(+), 410 deletions(-) create mode 100644 server/src/transaction.h create mode 100644 server/src/xmlparser.cc create mode 100644 server/src/xmlparser.h (limited to 'server') diff --git a/server/configure.in b/server/configure.in index f32ad30..553eb16 100644 --- a/server/configure.in +++ b/server/configure.in @@ -60,41 +60,47 @@ PKG_CHECK_MODULES(PQXX, libpqxx >= 0.20) dnl ====================== dnl Check for libconfig++ dnl ====================== -PKG_CHECK_MODULES(CONFIG, libconfig++ = 1.0.1) - +PKG_CHECK_MODULES(CONFIG, libconfig++ >= 1.0.1) dnl ====================== -dnl Check for xerces-x +dnl Check for eXpat library dnl ====================== -AC_ARG_WITH(xercescinc, - [ --with-xercescinc Set the incude dir for xerces], - [if test -n ${with_xercescinc}; then - xercesc_inc=${with_xercescinc}; - dcheck="$dcheck --with-xercescinc=${with_xercescinc} "; - else - xercesc_inc=${oldincludedir}/xercesc; - fi - ], - [xercesc_inc=${oldincludedir}/xercesc;] -) -AC_ARG_WITH(xercesclib, - [ --with-xercesclib Set the lib dir for xerces], - [if test -n ${with_xercesclib}; then - xercesc_lib=${with_xercesclib}; - dcheck="$dcheck --with-xercesclib=${with_xercesclib} " - else - xercesc_lib=${libdir}; - fi - ], - [xercesc_lib=${libdir};] -) -CXXFLAGS="${CXXFLAGS} -I${xercesc_inc}" -LIBS="${LIBS} -L${xercesc_lib} -lxerces-c" -AC_SUBST(CXXFLAGS) -AC_SUBST(LIBS) -AC_CHECK_HEADER(xercesc/util/XercesVersion.hpp, , - AC_MSG_ERROR([*** libxerces-c headers not found!])) -AC_CHECK_LIB(xerces-c, main, , AC_MSG_ERROR([*** libxerces-c not found!])) +AC_CHECK_HEADER(expat.h, , AC_MSG_ERROR([*** eXpat header file not found!])) +AC_CHECK_LIB(expat, XML_ParserCreate, , AC_MSG_ERROR([*** eXpat library not found!])) + + +#dnl ====================== +#dnl Check for xerces-x +#dnl ====================== +#AC_ARG_WITH(xercescinc, +# [ --with-xercescinc Set the incude dir for xerces], +# [if test -n ${with_xercescinc}; then +# xercesc_inc=${with_xercescinc}; +# dcheck="$dcheck --with-xercescinc=${with_xercescinc} "; +# else +# xercesc_inc=${oldincludedir}/xercesc; +# fi +# ], +# [xercesc_inc=${oldincludedir}/xercesc;] +#) +#AC_ARG_WITH(xercesclib, +# [ --with-xercesclib Set the lib dir for xerces], +# [if test -n ${with_xercesclib}; then +# xercesc_lib=${with_xercesclib}; +# dcheck="$dcheck --with-xercesclib=${with_xercesclib} " +# else +# xercesc_lib=${libdir}; +# fi +# ], +# [xercesc_lib=${libdir};] +#) +#CXXFLAGS="${CXXFLAGS} -I${xercesc_inc}" +#LIBS="${LIBS} -L${xercesc_lib} -lxerces-c" +#AC_SUBST(CXXFLAGS) +#AC_SUBST(LIBS) +#AC_CHECK_HEADER(xercesc/util/XercesVersion.hpp, , +# AC_MSG_ERROR([*** libxerces-c headers not found!])) +#AC_CHECK_LIB(xerces-c, main, , AC_MSG_ERROR([*** libxerces-c not found!])) AC_SUBST(CFLAGS) AC_SUBST(CPPFLAGS) diff --git a/server/src/Makefile.am b/server/src/Makefile.am index 2ede22d..93c521c 100644 --- a/server/src/Makefile.am +++ b/server/src/Makefile.am @@ -12,7 +12,8 @@ pracrod_SOURCES = \ log.cc \ server.cc \ tcpsocket.cc \ - tostring.cc + tostring.cc \ + xmlparser.cc EXTRA_DIST = \ configuration.h \ @@ -21,4 +22,5 @@ EXTRA_DIST = \ log.h \ server.h \ tcpsocket.h \ - tostring.h + tostring.h \ + xmlparser.h diff --git a/server/src/configuration.cc b/server/src/configuration.cc index e6d24a9..3b59c93 100644 --- a/server/src/configuration.cc +++ b/server/src/configuration.cc @@ -30,14 +30,14 @@ #include "tostring.h" -Pentominos::Configuration::Configuration(std::string filename) +Configuration::Configuration(std::string filename) { this->filename = filename; reload(); } -void Pentominos::Configuration::reload() +void Configuration::reload() throw(ConfigurationException) { try { @@ -51,9 +51,9 @@ void Pentominos::Configuration::reload() } -static Pentominos::Configuration *_config = NULL; +static Configuration *_config = NULL; -Pentominos::Configuration *Pentominos::config() +Configuration *config() throw(ConfigurationException) { if(_config == NULL) @@ -61,7 +61,7 @@ Pentominos::Configuration *Pentominos::config() return _config; } -void Pentominos::initConfig(Pentominos::Configuration *config) +void initConfig(Configuration *config) { _config = config; } diff --git a/server/src/configuration.h b/server/src/configuration.h index 38dd233..1b851f3 100644 --- a/server/src/configuration.h +++ b/server/src/configuration.h @@ -32,57 +32,55 @@ #include "exception.h" -namespace Pentominos { +/** + * This exception is thrown by Configuration when reload fails. + */ +class ConfigurationException: public Exception { +public: + ConfigurationException(std::string reason) : + Exception(reason) {} +}; +/** + * This is the pentominos configuration class.\n + * It simply wraps the libconfig c++ interface. It can be found at + * http://www.hyperrealm.com/libconfig/libconfig.html\n + * To find out how the interface works, see + * http://www.hyperrealm.com/libconfig/libconfig_manual.html#The-C_002b_002b-API + */ +class Configuration : public libconfig::Config { +public: /** - * This exception is thrown by Configuration when reload fails. + * Constructor.\n + * @param filename The filename to be loaded. */ - class ConfigurationException: public Pentominos::Exception { - public: - ConfigurationException(std::string reason) : - Pentominos::Exception(reason) {} - }; + Configuration(std::string filename); /** - * This is the pentominos configuration class.\n - * It simply wraps the libconfig c++ interface. It can be found at - * http://www.hyperrealm.com/libconfig/libconfig.html\n - * To find out how the interface works, see - * http://www.hyperrealm.com/libconfig/libconfig_manual.html#The-C_002b_002b-API + * reload, simply reloads the configuration file attached to the configuration + * object. */ - class Configuration : public libconfig::Config { - public: - /** - * Constructor.\n - * @param filename The filename to be loaded. - */ - Configuration(std::string filename); + void reload() throw(ConfigurationException); - /** - * reload, simply reloads the configuration file attached to the configuration - * object. - */ - void reload() throw(ConfigurationException); +private: + std::string filename; +}; - private: - std::string filename; - }; +/** + * Initialize the global configuration.\n + * This function sets the Configuration global pointer, that can be reaced through + * the config variable. + * @param config The value of the Configuration pointer. + */ +void initConfig(Configuration *config); - /** - * Initialize the global configuration.\n - * This function sets the Configuration global pointer, that can be reaced through - * the Pentominos::config variable. - * @param config The value of the Configuration pointer. - */ - void initConfig(Configuration *config); +/** + * This function returns the global configuration object pointer.\n + * Use initConfig to set it.\n + * @return The pointer to the global configuration. + */ +Configuration *config() + throw(ConfigurationException); - /** - * This function returns the global configuration object pointer.\n - * Use Pentominos::initConfig to set it.\n - * @return The pointer to the global configuration. - */ - Configuration *config() - throw(ConfigurationException); -}; #endif/*__ARTEFACT_CONFIGURATION_H__*/ diff --git a/server/src/exception.cc b/server/src/exception.cc index 3e3bd46..57bd6bf 100644 --- a/server/src/exception.cc +++ b/server/src/exception.cc @@ -30,14 +30,14 @@ #include "log.h" -Pentominos::Exception::Exception(std::string what) +Exception::Exception(std::string what) { - Pentominos::log(what); + log(what); _what = what; } -const char* Pentominos::Exception::what() const throw() +const char* Exception::what() const throw() { return _what.c_str(); } @@ -45,16 +45,16 @@ const char* Pentominos::Exception::what() const throw() #ifdef TEST_EXCEPTION -class MyException : public Pentominos::Exception { +class MyException : public Exception { public: MyException() : - Pentominos::Exception("MyException has been thrown") {} + Exception("MyException has been thrown") {} }; -class MyExtException : public Pentominos::Exception { +class MyExtException : public Exception { public: MyExtException(std::string thingy) : - Pentominos::Exception("MyExtException has been thrown: " + thingy) {} + Exception("MyExtException has been thrown: " + thingy) {} }; int main() diff --git a/server/src/exception.h b/server/src/exception.h index 4a0ce23..e0a47fc 100644 --- a/server/src/exception.h +++ b/server/src/exception.h @@ -31,35 +31,32 @@ #include -namespace Pentominos { - +/** + * Exception is the base class for all Pentominos exceptions + */ +class Exception : public std::exception { +public: /** - * Exception is the base class for all Pentominos exceptions + * The constuctor sets the error message (retained by the what() call) and + * adds an entry to the syslog, using the Pentominos::log interface. */ - class Exception : public std::exception { - public: - /** - * The constuctor sets the error message (retained by the what() call) and - * adds an entry to the syslog, using the Pentominos::log interface. - */ - Exception(std::string what); + Exception(std::string what); - /** - * Destructor - */ - virtual ~Exception() throw() {} - - /** - * what is used to gain textual information about the exception. - * @return A const char pointer to a zero terminated string containing - * textual information about the exception. - */ - virtual const char* what() const throw(); + /** + * Destructor + */ + virtual ~Exception() throw() {} - private: - std::string _what; - }; + /** + * what is used to gain textual information about the exception. + * @return A const char pointer to a zero terminated string containing + * textual information about the exception. + */ + virtual const char* what() const throw(); +private: + std::string _what; }; + #endif/*__ARTEFACT_EXCEPTION_H__*/ diff --git a/server/src/log.cc b/server/src/log.cc index 384077b..92a77f6 100644 --- a/server/src/log.cc +++ b/server/src/log.cc @@ -36,7 +36,7 @@ void closelog(void); // Optional */ -void Pentominos::log(std::string message) +void log(std::string message) { syslog(LOG_CONS, // Write to console if error sending to system logger. message.c_str()); diff --git a/server/src/log.h b/server/src/log.h index e1c1be0..14a5585 100644 --- a/server/src/log.h +++ b/server/src/log.h @@ -29,12 +29,10 @@ #include -namespace Pentominos { - /** - * log appends a message to the syslog queue.\n - * @param message An STL string containing the string to be appended. - */ - void log(std::string message); -}; +/** + * log appends a message to the syslog queue.\n + * @param message An STL string containing the string to be appended. + */ +void log(std::string message); #endif/*__ARTEFACT_LOG_H__*/ diff --git a/server/src/pracrod.cc b/server/src/pracrod.cc index 7fce884..cd5bfe7 100644 --- a/server/src/pracrod.cc +++ b/server/src/pracrod.cc @@ -84,11 +84,11 @@ void reload(int fisk) { int port; printf("Reload!\n"); - port = Pentominos::config()->lookup("port"); - Pentominos::config()->reload(); + port = config()->lookup("port"); + config()->reload(); { // Force wake the server process for reinitialization. - Pentominos::TCPSocket socket; + TCPSocket socket; socket.connect("localhost", port); } } @@ -158,13 +158,13 @@ int main(int argc, char *argv[]) char *cfname = NULL; if(!configfile) cfname = ETC"/pracrod.conf"; else cfname = configfile; - Pentominos::Configuration conf(cfname); - Pentominos::initConfig(&conf); + Configuration conf(cfname); + initConfig(&conf); if(!user) { std::string userstr; try { - user = (char*)(const char*)Pentominos::config()->lookup("user"); + user = (char*)(const char*)config()->lookup("user"); } catch( ... ) { printf("User not found in config.\n"); } @@ -192,7 +192,7 @@ int main(int argc, char *argv[]) if(!group) { std::string groupstr; try { - group = (char*)(const char*)Pentominos::config()->lookup("group"); + group = (char*)(const char*)config()->lookup("group"); } catch( ... ) { printf("Group not found in config.\n"); } 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 #include +#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 = "\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 = "\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 = "\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 diff --git a/server/src/tcpsocket.cc b/server/src/tcpsocket.cc index 19470fe..6fc521f 100644 --- a/server/src/tcpsocket.cc +++ b/server/src/tcpsocket.cc @@ -76,7 +76,7 @@ #include #include -Pentominos::TCPSocket::TCPSocket() +TCPSocket::TCPSocket() throw(TCPSocketException) { if((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) { @@ -85,14 +85,14 @@ Pentominos::TCPSocket::TCPSocket() isconnected = false; } -Pentominos::TCPSocket::~TCPSocket() +TCPSocket::~TCPSocket() { disconnect(); } static int _listen(int sockfd, int backlog){return listen(sockfd, backlog);} -void Pentominos::TCPSocket::listen(unsigned short int port) +void TCPSocket::listen(unsigned short int port) throw(TCPListenException) { @@ -124,7 +124,7 @@ void Pentominos::TCPSocket::listen(unsigned short int port) static int _accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen) {return accept(sockfd, addr, addrlen);} -Pentominos::TCPSocket Pentominos::TCPSocket::accept() +TCPSocket TCPSocket::accept() throw(TCPAcceptException) { TCPSocket child; @@ -155,7 +155,7 @@ Pentominos::TCPSocket Pentominos::TCPSocket::accept() static int _connect(int sockfd, const struct sockaddr *serv_addr, socklen_t addrlen) {return connect(sockfd, serv_addr, addrlen);} -void Pentominos::TCPSocket::connect(std::string addr, unsigned short int port) +void TCPSocket::connect(std::string addr, unsigned short int port) throw(TCPConnectException) { @@ -194,7 +194,7 @@ void Pentominos::TCPSocket::connect(std::string addr, unsigned short int port) isconnected = true; } -void Pentominos::TCPSocket::disconnect() +void TCPSocket::disconnect() { if(sock != -1) { close(sock); @@ -203,12 +203,12 @@ void Pentominos::TCPSocket::disconnect() isconnected = false; } -bool Pentominos::TCPSocket::connected() +bool TCPSocket::connected() { return sock != -1 && isconnected; } -int Pentominos::TCPSocket::read(char *buf, int size) +int TCPSocket::read(char *buf, int size) throw(TCPReadException) { int res = 0; @@ -241,7 +241,7 @@ int Pentominos::TCPSocket::read(char *buf, int size) return res; } -int Pentominos::TCPSocket::write(char *data, int size) +int TCPSocket::write(char *data, int size) throw(TCPWriteException) { if(sock == -1) { @@ -260,7 +260,7 @@ int Pentominos::TCPSocket::write(char *data, int size) return res; } -std::string Pentominos::TCPSocket::srcaddr() +std::string TCPSocket::srcaddr() throw(TCPNameException) { std::string addr; @@ -280,7 +280,7 @@ std::string Pentominos::TCPSocket::srcaddr() return addr; } -std::string Pentominos::TCPSocket::dstaddr() +std::string TCPSocket::dstaddr() throw(TCPNameException) { std::string addr; @@ -313,13 +313,13 @@ int main() case 0: // child try { - Pentominos::TCPSocket client; + TCPSocket client; sleep(1); // We need to wait for the listen socket to be created. client.connect("localhost", 12345); sprintf(buf, "hello"); client.write(buf, sizeof(buf)); printf("Sent: [%s]\n", buf); - } catch( Pentominos::Exception &e ) { + } catch( Exception &e ) { fprintf(stderr, "%s\n", e.what()); return 1; } @@ -327,13 +327,13 @@ int main() default: // parent try { - Pentominos::TCPSocket listen_sock; + TCPSocket listen_sock; listen_sock.listen(12345); - Pentominos::TCPSocket sock = listen_sock.accept(); + TCPSocket sock = listen_sock.accept(); sock.read(buf, sizeof(buf)); printf("Got: [%s]\n", buf); if(std::string(buf) != "hello") return 1; - } catch( Pentominos::Exception &e ) { + } catch( Exception &e ) { fprintf(stderr, "%s\n", e.what()); return 1; } diff --git a/server/src/tcpsocket.h b/server/src/tcpsocket.h index f392a2c..45d94ee 100644 --- a/server/src/tcpsocket.h +++ b/server/src/tcpsocket.h @@ -31,153 +31,150 @@ #include "exception.h" -namespace Pentominos { - - /** - * This exception is thrown by TCPSocket when the socket creation fails. - */ - class TCPSocketException: public Pentominos::Exception { - public: - TCPSocketException(std::string reason) : - Pentominos::Exception("Could not create socket: " + reason) {} - }; +/** + * This exception is thrown by TCPSocket when the socket creation fails. + */ +class TCPSocketException: public Exception { +public: + TCPSocketException(std::string reason) : + Exception("Could not create socket: " + reason) {} +}; - /** - * This exception is thrown by TCPSocket when listen fails. - */ - class TCPListenException: public Pentominos::Exception { - public: - TCPListenException(std::string reason) : - Pentominos::Exception("Listen failed: " + reason) {} - }; +/** + * This exception is thrown by TCPSocket when listen fails. + */ +class TCPListenException: public Exception { +public: + TCPListenException(std::string reason) : + Exception("Listen failed: " + reason) {} +}; - /** - * This exception is thrown by TCPSocket when accept fails. - */ - class TCPAcceptException: public Pentominos::Exception { - public: - TCPAcceptException(std::string reason) : - Pentominos::Exception("Accept failed: " + reason) {} - }; +/** + * This exception is thrown by TCPSocket when accept fails. + */ +class TCPAcceptException: public Exception { +public: + TCPAcceptException(std::string reason) : + Exception("Accept failed: " + reason) {} +}; - /** - * This exception is thrown by TCPSocket when connection fails. - */ - class TCPConnectException: public Pentominos::Exception { - public: - TCPConnectException(std::string host, std::string port, std::string reason) : - Pentominos::Exception("Could not connect to " + host + ":" + port + ": " + reason) {} - }; +/** + * This exception is thrown by TCPSocket when connection fails. + */ +class TCPConnectException: public Exception { +public: + TCPConnectException(std::string host, std::string port, std::string reason) : + Exception("Could not connect to " + host + ":" + port + ": " + reason) {} +}; - /** - * This exception is thrown by TCPSocket when reading fails. - */ - class TCPReadException: public Pentominos::Exception { - public: - TCPReadException(std::string reason) : - Pentominos::Exception("TCPSocket could not read data: " + reason) {} - }; +/** + * This exception is thrown by TCPSocket when reading fails. + */ +class TCPReadException: public Exception { +public: + TCPReadException(std::string reason) : + Exception("TCPSocket could not read data: " + reason) {} +}; - /** - * This exception is thrown by TCPSocket when writing fails. - */ - class TCPWriteException: public Pentominos::Exception { - public: - TCPWriteException(std::string reason) : - Pentominos::Exception("TCPSocket could not write data: " + reason) {} - }; +/** + * This exception is thrown by TCPSocket when writing fails. + */ +class TCPWriteException: public Exception { +public: + TCPWriteException(std::string reason) : + Exception("TCPSocket could not write data: " + reason) {} +}; +/** + * This exception is thrown by TCPSocket when there is an error in name lookup. + */ +class TCPNameException: public Exception { +public: + TCPNameException(std::string reason) : + Exception("TCPSocket could not get name: " + reason) {} +}; + +/** + * This class is used to commumicate through a TCP/IP connection, wether it + * is a server (listening) or a client (transmitting). + */ +class TCPSocket { +public: /** - * This exception is thrown by TCPSocket when there is an error in name lookup. + * Constructor. Creates a new tcp socket. */ - class TCPNameException: public Pentominos::Exception { - public: - TCPNameException(std::string reason) : - Pentominos::Exception("TCPSocket could not get name: " + reason) {} - }; + TCPSocket() throw(TCPSocketException); /** - * This class is used to commumicate through a TCP/IP connection, wether it - * is a server (listening) or a client (transmitting). + * Destructor. Closes the tcp socket. */ - class TCPSocket { - public: - /** - * Constructor. Creates a new tcp socket. - */ - TCPSocket() throw(TCPSocketException); - - /** - * Destructor. Closes the tcp socket. - */ - ~TCPSocket(); + ~TCPSocket(); - /** - * Sets the socket in listen mode.\n - * @param port The port number on which to listen. - */ - void listen(unsigned short int port) throw(TCPListenException); + /** + * Sets the socket in listen mode.\n + * @param port The port number on which to listen. + */ + void listen(unsigned short int port) throw(TCPListenException); - /** - * Accept an incoming connection.\n - * The call is blocking and returns only when an incoming connection is received.\n - * The socket must be in listen mode in order for this call to work.\n - * Multiple accepts can be made on the same listening socket. - * @return A connected TCPSocket ready to communicate. - */ - TCPSocket accept() throw(TCPAcceptException); + /** + * Accept an incoming connection.\n + * The call is blocking and returns only when an incoming connection is received.\n + * The socket must be in listen mode in order for this call to work.\n + * Multiple accepts can be made on the same listening socket. + * @return A connected TCPSocket ready to communicate. + */ + TCPSocket accept() throw(TCPAcceptException); - /** - * Connects to a host for data transmission. - * @param addr The address of the host to connect to. - * @param port The portnumber of the host to connect to. - */ - void connect(std::string addr, unsigned short int port) throw(TCPConnectException); + /** + * Connects to a host for data transmission. + * @param addr The address of the host to connect to. + * @param port The portnumber of the host to connect to. + */ + void connect(std::string addr, unsigned short int port) throw(TCPConnectException); - /** - * Disconnect the socket. - */ - void disconnect(); + /** + * Disconnect the socket. + */ + void disconnect(); - /** - * Tells whether the socket is connected or not. - * @return true if the socket is connected, false if not. - */ - bool connected(); + /** + * Tells whether the socket is connected or not. + * @return true if the socket is connected, false if not. + */ + bool connected(); - /** - * 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). - * @return The actual number of bytes read. - */ - int read(char *buf, int size) throw(TCPReadException); + /** + * 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). + * @return The actual number of bytes read. + */ + int read(char *buf, int size) throw(TCPReadException); - /** - * Writes bytes from a buffer to the socket. - * @param data The buffer from which the data will be read. - * @param size The number of bytes to write. - * @return The actual number of bytes written. - */ - int write(char *data, int size) throw(TCPWriteException); + /** + * Writes bytes from a buffer to the socket. + * @param data The buffer from which the data will be read. + * @param size The number of bytes to write. + * @return The actual number of bytes written. + */ + int write(char *data, int size) throw(TCPWriteException); - /** - * Get the source address of the socket (IP address not DNS name). - * @return An STL string containing the source address. - */ - std::string srcaddr() throw(TCPNameException); - - /** - * Get the destination address of the socket (IP address not DNS name). - * @return An STL string containing the destination address. - */ - std::string dstaddr() throw(TCPNameException); - - private: - bool isconnected; - int sock; - }; - + /** + * Get the source address of the socket (IP address not DNS name). + * @return An STL string containing the source address. + */ + std::string srcaddr() throw(TCPNameException); + + /** + * Get the destination address of the socket (IP address not DNS name). + * @return An STL string containing the destination address. + */ + std::string dstaddr() throw(TCPNameException); + +private: + bool isconnected; + int sock; }; + #endif/*__ARTEFACT_TCPSOCKET_H__*/ diff --git a/server/src/tostring.cc b/server/src/tostring.cc index e218a32..bde5498 100644 --- a/server/src/tostring.cc +++ b/server/src/tostring.cc @@ -30,12 +30,12 @@ #include -std::string Pentominos::toString(std::string s) +std::string toString(std::string s) { return s; } -std::string Pentominos::toString(char c) +std::string toString(char c) { char buf[32]; sprintf(buf, "%c", c); @@ -43,7 +43,7 @@ std::string Pentominos::toString(char c) return buf; } -std::string Pentominos::toString(unsigned char c) +std::string toString(unsigned char c) { char buf[32]; sprintf(buf, "%c", c); @@ -51,7 +51,7 @@ std::string Pentominos::toString(unsigned char c) return buf; } -std::string Pentominos::toString(short int si) +std::string toString(short int si) { char buf[32]; sprintf(buf, "%d", si); @@ -59,7 +59,7 @@ std::string Pentominos::toString(short int si) return buf; } -std::string Pentominos::toString(short unsigned int su) +std::string toString(short unsigned int su) { char buf[32]; sprintf(buf, "%u", su); @@ -67,7 +67,7 @@ std::string Pentominos::toString(short unsigned int su) return buf; } -std::string Pentominos::toString(int li) +std::string toString(int li) { char buf[32]; sprintf(buf, "%ld", li); @@ -75,7 +75,7 @@ std::string Pentominos::toString(int li) return buf; } -std::string Pentominos::toString(unsigned int lu) +std::string toString(unsigned int lu) { char buf[32]; sprintf(buf, "%lu", lu); @@ -83,13 +83,13 @@ std::string Pentominos::toString(unsigned int lu) return buf; } -std::string Pentominos::toString(bool b) +std::string toString(bool b) { if(b) return "true"; else return "false"; } -std::string Pentominos::toString(float f, unsigned int precision) +std::string toString(float f, unsigned int precision) { char buf[100]; char format[12]; @@ -100,7 +100,7 @@ std::string Pentominos::toString(float f, unsigned int precision) return ""; } -std::string Pentominos::toString(double d, unsigned int precision) +std::string toString(double d, unsigned int precision) { char buf[100]; char format[12]; @@ -110,7 +110,7 @@ std::string Pentominos::toString(double d, unsigned int precision) return buf; } -std::string Pentominos::toString(long double ld, unsigned int precision) +std::string toString(long double ld, unsigned int precision) { char buf[100]; char format[12]; @@ -141,17 +141,17 @@ int main() long double ld = 0.1; std::string str = - "[" + Pentominos::toString(s) - + "] [" + Pentominos::toString(c) - + "] [" + Pentominos::toString(uc) - + "] [" + Pentominos::toString(si) - + "] [" + Pentominos::toString(su) - // + "] [" + Pentominos::toString(li) - // + "] [" + Pentominos::toString(lu) - + "] [" + Pentominos::toString(b) - + "] [" + Pentominos::toString(f, 10) - + "] [" + Pentominos::toString(d, 18) - + "] [" + Pentominos::toString(ld, 36) + "[" + toString(s) + + "] [" + toString(c) + + "] [" + toString(uc) + + "] [" + toString(si) + + "] [" + toString(su) + // + "] [" + toString(li) + // + "] [" + toString(lu) + + "] [" + toString(b) + + "] [" + toString(f, 10) + + "] [" + toString(d, 18) + + "] [" + toString(ld, 36) + "]"; printf("%s\n", str.c_str()); diff --git a/server/src/tostring.h b/server/src/tostring.h index 0b63fb3..137969f 100644 --- a/server/src/tostring.h +++ b/server/src/tostring.h @@ -29,86 +29,84 @@ #include -namespace Pentominos { - /** - * toString converts a nonstring variable into an STL string. - * @param s A string, converted into a... string... - * @return The STL string containing the converted value. - */ - std::string toString(std::string s); +/** + * toString converts a nonstring variable into an STL string. + * @param s A string, converted into a... string... + * @return The STL string containing the converted value. + */ +std::string toString(std::string s); - /** - * toString converts a nonstring variable into an STL string. - * @param c A char, converted into a string. - * @return The STL string containing the converted value. - */ - std::string toString(char c); +/** + * toString converts a nonstring variable into an STL string. + * @param c A char, converted into a string. + * @return The STL string containing the converted value. + */ +std::string toString(char c); - /** - * toString converts a nonstring variable into an STL string. - * @param c A unsigned char, converted into a string. - * @return The STL string containing the converted value. - */ - std::string toString(unsigned char c); +/** + * toString converts a nonstring variable into an STL string. + * @param c A unsigned char, converted into a string. + * @return The STL string containing the converted value. + */ +std::string toString(unsigned char c); - /** - * toString converts a nonstring variable into an STL string. - * @param si A short integer, converted into a string. - * @return The STL string containing the converted value. - */ - std::string toString(short int si); +/** + * toString converts a nonstring variable into an STL string. + * @param si A short integer, converted into a string. + * @return The STL string containing the converted value. + */ +std::string toString(short int si); - /** - * toString converts a nonstring variable into an STL string. - * @param su An unsigned short integer, converted into a string. - * @return The STL string containing the converted value. - */ - std::string toString(short unsigned int su); +/** + * toString converts a nonstring variable into an STL string. + * @param su An unsigned short integer, converted into a string. + * @return The STL string containing the converted value. + */ +std::string toString(short unsigned int su); - /** - * toString converts a nonstring variable into an STL string. - * @param li A long integer, converted into a string. - * @return The STL string containing the converted value. - */ - std::string toString(int li); +/** + * toString converts a nonstring variable into an STL string. + * @param li A long integer, converted into a string. + * @return The STL string containing the converted value. + */ +std::string toString(int li); - /** - * toString converts a nonstring variable into an STL string. - * @param lu An unsigned long integer, converted into a string. - * @return The STL string containing the converted value. - */ - std::string toString(unsigned int lu); +/** + * toString converts a nonstring variable into an STL string. + * @param lu An unsigned long integer, converted into a string. + * @return The STL string containing the converted value. + */ +std::string toString(unsigned int lu); - /** - * toString converts a nonstring variable into an STL string. - * @param b A boolean value, converted into a string (true/false). - * @return The STL string containing the converted value. - */ - std::string toString(bool b); +/** + * toString converts a nonstring variable into an STL string. + * @param b A boolean value, converted into a string (true/false). + * @return The STL string containing the converted value. + */ +std::string toString(bool b); - /** - * toString converts a nonstring variable into an STL string. - * @param f A floating point value, converted into a string. - * @param precision The precision to use when converting. - * @return The STL string containing the converted value. - */ - std::string toString(float f, unsigned int precision = 8); +/** + * toString converts a nonstring variable into an STL string. + * @param f A floating point value, converted into a string. + * @param precision The precision to use when converting. + * @return The STL string containing the converted value. + */ +std::string toString(float f, unsigned int precision = 8); - /** - * toString converts a nonstring variable into an STL string. - * @param d A double precision floating point value, converted into a string. - * @param precision The precision to use when converting. - * @return The STL string containing the converted value. - */ - std::string toString(double d, unsigned int precision = 16); +/** + * toString converts a nonstring variable into an STL string. + * @param d A double precision floating point value, converted into a string. + * @param precision The precision to use when converting. + * @return The STL string containing the converted value. + */ +std::string toString(double d, unsigned int precision = 16); - /** - * toString converts a nonstring variable into an STL string. - * @param ld A long double precision floating point value, converted into a string. - * @param precision The precision to use when converting. - * @return The STL string containing the converted value. - */ - std::string toString(long double ld, unsigned int precision = 32); -}; +/** + * toString converts a nonstring variable into an STL string. + * @param ld A long double precision floating point value, converted into a string. + * @param precision The precision to use when converting. + * @return The STL string containing the converted value. + */ +std::string toString(long double ld, unsigned int precision = 32); #endif/*__ARTEFACT_TOSTRING_H__*/ diff --git a/server/src/transaction.h b/server/src/transaction.h new file mode 100644 index 0000000..41559e0 --- /dev/null +++ b/server/src/transaction.h @@ -0,0 +1,59 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/*************************************************************************** + * transaction.h + * + * Fri Aug 31 09:52:27 CEST 2007 + * Copyright 2007 Bent Bisballe Nyeng, Lars Bisballe Jensen and Peter Skaarup + * deva@aasimon.org, elsenator@gmail.com and piparum@piparum.dk + ****************************************************************************/ + +/* + * This file is part of Pracro. + * + * Pracro is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Pracro is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Pracro; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ +#ifndef __PRACRO_TRANSACTION_H__ +#define __PRACRO_TRANSACTION_H__ + +#include +#include + +class Request { +public: + std::string macro; +}; +typedef std::vector< Request > Requests; + +class CommitValue { +public: + std::string name; + std::string value; +}; + +class Commit { +public: + std::vector< CommitValue > values; +}; +typedef std::vector< Commit > Commits; + +class Transaction { +public: + std::string cpr; + + Requests requests; + Commits commits; +}; + +#endif/*__PRACRO_TRANSACTION_H__*/ diff --git a/server/src/xmlparser.cc b/server/src/xmlparser.cc new file mode 100644 index 0000000..8020a80 --- /dev/null +++ b/server/src/xmlparser.cc @@ -0,0 +1,116 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/*************************************************************************** + * xmlparser.cc + * + * Fri Aug 31 09:30:06 CEST 2007 + * Copyright 2007 Bent Bisballe Nyeng, Lars Bisballe Jensen and Peter Skaarup + * deva@aasimon.org, elsenator@gmail.com and piparum@piparum.dk + ****************************************************************************/ + +/* + * This file is part of Pracro. + * + * Pracro is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Pracro is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Pracro; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ +#include "xmlparser.h" + +#include +#include +#include + +#include +#include + +bool done = false; + +void start_hndl(void *p, const char *el, const char **attr) +{ + Transaction *transaction = (Transaction*)XML_GetUserData(p); + + printf("Data %p\n", transaction); + + printf("Start tag [%s]\n", el); + + // Convert to comfy C++ values... + std::string name = el; + std::map< std::string, std::string > attributes; + + while(*attr) { + std::string at_name = *attr; + attr++; + std::string at_value = *attr; + attr++; + + attributes.insert(make_pair(at_name, at_value)); + } + /* + std::map< std::string, std::string >::iterator i = attributes.begin(); + while(i != attributes.end()) { + printf("%s=%s\n", i->first.c_str(), i->second.c_str()); + i++; + } + */ + + // Do something reasonable with them... + if(name == "request") { + Request r; + r.macro = attributes["macro"]; + printf("%s\n", r.macro.c_str()); + transaction->requests.push_back(r); + } + +} + +void end_hndl(void *p, const char *el) +{ + printf("End tag [%s]\n", el); + if(!strcmp(el, "pracro")) done = true; +} + +void parse(TCPSocket &socket, Transaction &transaction) +{ + + XML_Parser p = XML_ParserCreate(NULL); + if (! p) { + fprintf(stderr, "Couldn't allocate memory for parser\n"); + exit(-1); + } + + XML_SetUserData(p, &transaction); + XML_UseParserAsHandlerArg(p); + XML_SetElementHandler(p, start_hndl, end_hndl); + + while(!done) { + char buf[32]; + int len; + + memset(buf, 0, sizeof(buf)); + len = socket.read(buf, sizeof(buf) - 1); + + done = len == 0; + + if (! XML_Parse(p, buf, len, done)) { + fprintf(stderr, "Parse error at line %d:\n%s\n", + XML_GetCurrentLineNumber(p), + XML_ErrorString(XML_GetErrorCode(p))); + // exit(-1); + // throw Exception(...); + return; + } + } + + printf("%d requests\n", transaction.requests.size()); + +} diff --git a/server/src/xmlparser.h b/server/src/xmlparser.h new file mode 100644 index 0000000..8e6b7aa --- /dev/null +++ b/server/src/xmlparser.h @@ -0,0 +1,35 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/*************************************************************************** + * xmlparser.h + * + * Fri Aug 31 09:30:06 CEST 2007 + * Copyright 2007 Bent Bisballe Nyeng, Lars Bisballe Jensen and Peter Skaarup + * deva@aasimon.org, elsenator@gmail.com and piparum@piparum.dk + ****************************************************************************/ + +/* + * This file is part of Pracro. + * + * Pracro is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Pracro is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Pracro; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. + */ +#ifndef __PRACRO_XMLPARSER_H__ +#define __PRACRO_XMLPARSER_H__ + +#include "tcpsocket.h" +#include "transaction.h" + +void parse(TCPSocket &socket, Transaction &transaction); + +#endif/*__PRACRO_XMLPARSER_H__*/ diff --git a/server/xml/example.xml b/server/xml/example.xml index 03e2d59..f472bd0 100644 --- a/server/xml/example.xml +++ b/server/xml/example.xml @@ -1,19 +1,18 @@ - - - - - -