From e2120257dda4d91b48bb031a96edda810ce30dfb Mon Sep 17 00:00:00 2001 From: deva Date: Tue, 30 Sep 2008 12:49:34 +0000 Subject: Changed SAXParser parse method to take a buffer and a size instead of a c++ string. Made the queryhandler use a read loop, parsing on-the-fly to determine when the entire document has been read. --- server/src/queryhandler.cc | 62 ++++++++++++++++++++-------------------------- 1 file changed, 27 insertions(+), 35 deletions(-) (limited to 'server/src/queryhandler.cc') diff --git a/server/src/queryhandler.cc b/server/src/queryhandler.cc index 29e8fda..248f1e6 100644 --- a/server/src/queryhandler.cc +++ b/server/src/queryhandler.cc @@ -54,6 +54,8 @@ // For ioctl #include +#include "queryparser.h" + typedef struct { in_addr_t ip; time_t time; @@ -138,12 +140,7 @@ QueryHandler::QueryHandler(TCPSocket *socket, std::string cpr) this->socket = socket; } -void QueryHandler::addQuery(Query &query) -{ - queries.push_back(query); -} - -std::string QueryHandler::exec() +QueryResult QueryHandler::exec(Query &query) { time_t timestamp = time(NULL); std::string uid = getUID("eth0"); @@ -186,56 +183,51 @@ std::string QueryHandler::exec() printf(buf); #endif/*WITH_DEBUG*/ - std::vector< Query >::iterator j = queries.begin(); - while(j != queries.end()) { - Query query = *j; - - sprintf(buf, " \n", - query.attributes["class"].c_str(), - query.attributes["class"].c_str()); - + sprintf(buf, " \n", + query.attributes["class"].c_str(), + query.attributes["class"].c_str()); + #ifndef WITHOUT_PENTOMINOS - socket->write(buf, strlen(buf)); + socket->write(buf, strlen(buf)); #endif/*WITHOUT_PENTOMINOS*/ #ifdef WITH_DEBUG - printf(buf); + printf(buf); #endif/*WITH_DEBUG*/ - j++; - } - - sprintf(buf, "\n"); + sprintf(buf, ""); #ifndef WITHOUT_PENTOMINOS socket->write(buf, strlen(buf)); - // Terminate - char term[] = "\0"; - socket->write(term, 1); #endif/*WITHOUT_PENTOMINOS*/ #ifdef WITH_DEBUG printf(buf); #endif/*WITH_DEBUG*/ - std::string answer; + QueryResult result; #ifndef WITHOUT_PENTOMINOS - // Wait for answer + QueryParser parser; + + int asize; char abuf[64]; - int res; - do { + memset(abuf, 0, sizeof(abuf)); + + // 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)); - res = socket->read(abuf, sizeof(abuf) - 1); - answer += abuf; - } while(res); + } + + result = parser.result; #endif/*WITHOUT_PENTOMINOS*/ - return answer; + return result; } #ifdef TEST_QUERYHANDLER -- cgit v1.2.3