removed using namespace

This commit is contained in:
rjawor 2015-07-31 14:45:49 +02:00
parent 596a440835
commit 8bc69c54f1
16 changed files with 74 additions and 279 deletions

View File

@ -1,15 +1,10 @@
add_executable(hello_world hello_world.cpp)
target_link_libraries(hello_world fcgi fcgi++)
add_executable(echo-cpp echo-cpp.cpp)
target_link_libraries(echo-cpp fcgi fcgi++)
add_executable(concordia_server_process
concordia_server_process.cpp
concordia_server.cpp
index_controller.cpp
searcher_controller.cpp
json_generator.cpp
unit_dao.cpp
)
target_link_libraries(concordia_server_process fcgi fcgi++ pq concordia config++ log4cpp ${Boost_LIBRARIES} divsufsort utf8case)

View File

@ -23,11 +23,11 @@ ConcordiaServer::ConcordiaServer(const std::string & configFilePath)
ConcordiaServer::~ConcordiaServer() {
}
string ConcordiaServer::handleRequest(string & requestString) {
std::string ConcordiaServer::handleRequest(std::string & requestString) {
rapidjson::StringBuffer outputJson;
rapidjson::Writer<rapidjson::StringBuffer> jsonWriter(outputJson);
stringstream outputString;
std::stringstream outputString;
try {
outputString << "Content-type: application/json\r\n\r\n";
@ -35,13 +35,13 @@ string ConcordiaServer::handleRequest(string & requestString) {
bool hasError = d.Parse(requestString.c_str()).HasParseError();
if (hasError) {
stringstream errorstream;
std::stringstream errorstream;
errorstream << "json parse error at offset: " << d.GetErrorOffset() <<
", description: " << GetParseError_En(d.GetParseError());
JsonGenerator::signalError(jsonWriter, errorstream.str());
} else { // json parsed
string operation = d[OPERATION_PARAM].GetString();
string sentence = d[SENTENCE_PARAM].GetString();
std::string operation = d[OPERATION_PARAM].GetString();
std::string sentence = d[SENTENCE_PARAM].GetString();
if (operation == ADD_SENTENCE_OP) {
_indexController->addSentence(jsonWriter, sentence);
} else if (operation == SIMPLE_SEARCH_OP) {
@ -54,7 +54,7 @@ string ConcordiaServer::handleRequest(string & requestString) {
}
} catch (ConcordiaException & e) {
stringstream errorstream;
std::stringstream errorstream;
errorstream << "concordia error: " << e.what();
JsonGenerator::signalError(jsonWriter, errorstream.str());
}

View File

@ -14,8 +14,6 @@
#include "index_controller.hpp"
#include "searcher_controller.hpp"
using namespace std;
class ConcordiaServer {
public:
/*! Constructor.
@ -28,7 +26,7 @@ public:
*/
virtual ~ConcordiaServer();
string handleRequest(string & requestString);
std::string handleRequest(std::string & requestString);
private:
boost::shared_ptr<IndexController> _indexController;

View File

@ -9,21 +9,18 @@
#include "config.hpp"
#include "concordia_server.hpp"
using namespace std;
static const unsigned long STDIN_MAX = 1000000;
static string get_request_content(const FCGX_Request & request) {
static std::string get_request_content(const FCGX_Request & request) {
char * content_length_str = FCGX_GetParam("CONTENT_LENGTH", request.envp);
unsigned long content_length = STDIN_MAX;
if (content_length_str) {
content_length = strtol(content_length_str, &content_length_str, 10);
if (*content_length_str) {
cerr << "Can't Parse 'CONTENT_LENGTH='"
<< FCGX_GetParam("CONTENT_LENGTH", request.envp)
<< "'. Consuming stdin up to " << STDIN_MAX << endl;
std::cerr << "Can't Parse 'CONTENT_LENGTH='"
<< FCGX_GetParam("CONTENT_LENGTH", request.envp)
<< "'. Consuming stdin up to " << STDIN_MAX << std::endl;
}
if (content_length > STDIN_MAX) {
@ -35,17 +32,17 @@ static string get_request_content(const FCGX_Request & request) {
}
char * content_buffer = new char[content_length];
cin.read(content_buffer, content_length);
content_length = cin.gcount();
std::cin.read(content_buffer, content_length);
content_length = std::cin.gcount();
// Chew up any remaining stdin - this shouldn't be necessary
// but is because mod_fastcgi doesn't handle it correctly.
// ignore() doesn't set the eof bit in some versions of glibc++
// so use gcount() instead of eof()...
do cin.ignore(1024); while (cin.gcount() == 1024);
do std::cin.ignore(1024); while (std::cin.gcount() == 1024);
string content(content_buffer, content_length);
std::string content(content_buffer, content_length);
delete [] content_buffer;
return content;
}
@ -53,9 +50,9 @@ static string get_request_content(const FCGX_Request & request) {
int main(int argc, char** argv) {
// Backup the stdio streambufs
streambuf * cin_streambuf = cin.rdbuf();
streambuf * cout_streambuf = cout.rdbuf();
streambuf * cerr_streambuf = cerr.rdbuf();
std::streambuf * cin_streambuf = std::cin.rdbuf();
std::streambuf * cout_streambuf = std::cout.rdbuf();
std::streambuf * cerr_streambuf = std::cerr.rdbuf();
ConcordiaServer concordiaServer(CONFIG_FILE_PATH);
@ -69,22 +66,22 @@ int main(int argc, char** argv) {
fcgi_streambuf cout_fcgi_streambuf(request.out);
fcgi_streambuf cerr_fcgi_streambuf(request.err);
cin.rdbuf(&cin_fcgi_streambuf);
cout.rdbuf(&cout_fcgi_streambuf);
cerr.rdbuf(&cerr_fcgi_streambuf);
std::cin.rdbuf(&cin_fcgi_streambuf);
std::cout.rdbuf(&cout_fcgi_streambuf);
std::cerr.rdbuf(&cerr_fcgi_streambuf);
string content = get_request_content(request);
std::string content = get_request_content(request);
string requestString(content);
cout << concordiaServer.handleRequest(requestString);
std::string requestString(content);
std::cout << concordiaServer.handleRequest(requestString);
// Note: the fcgi_streambuf destructor will auto flush
}
// restore stdio streambufs
cin.rdbuf(cin_streambuf);
cout.rdbuf(cout_streambuf);
cerr.rdbuf(cerr_streambuf);
std::cin.rdbuf(cin_streambuf);
std::cout.rdbuf(cout_streambuf);
std::cerr.rdbuf(cerr_streambuf);
return 0;
}

View File

@ -1,175 +0,0 @@
/*
* A simple FastCGI application example in C++.
*
* $Id: echo-cpp.cpp,v 1.10 2002/02/25 00:46:17 robs Exp $
*
* Copyright (c) 2001 Rob Saccoccio and Chelsea Networks
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdlib.h>
#ifdef _WIN32
#include <process.h>
#else
#include <unistd.h>
extern char ** environ;
#endif
#include "fcgio.h"
#include "fcgi_config.h" // HAVE_IOSTREAM_WITHASSIGN_STREAMBUF
using namespace std;
// Maximum number of bytes allowed to be read from stdin
static const unsigned long STDIN_MAX = 1000000;
static void penv(const char * const * envp)
{
cout << "<PRE>\n";
for ( ; *envp; ++envp)
{
cout << *envp << "\n";
}
cout << "</PRE>\n";
}
static long gstdin(FCGX_Request * request, char ** content)
{
char * clenstr = FCGX_GetParam("CONTENT_LENGTH", request->envp);
unsigned long clen = STDIN_MAX;
if (clenstr)
{
clen = strtol(clenstr, &clenstr, 10);
if (*clenstr)
{
cerr << "can't parse \"CONTENT_LENGTH="
<< FCGX_GetParam("CONTENT_LENGTH", request->envp)
<< "\"\n";
clen = STDIN_MAX;
}
// *always* put a cap on the amount of data that will be read
if (clen > STDIN_MAX) clen = STDIN_MAX;
*content = new char[clen];
cin.read(*content, clen);
clen = cin.gcount();
}
else
{
// *never* read stdin when CONTENT_LENGTH is missing or unparsable
*content = 0;
clen = 0;
}
// Chew up any remaining stdin - this shouldn't be necessary
// but is because mod_fastcgi doesn't handle it correctly.
// ignore() doesn't set the eof bit in some versions of glibc++
// so use gcount() instead of eof()...
do cin.ignore(1024); while (cin.gcount() == 1024);
return clen;
}
int main (void)
{
int count = 0;
long pid = getpid();
streambuf * cin_streambuf = cin.rdbuf();
streambuf * cout_streambuf = cout.rdbuf();
streambuf * cerr_streambuf = cerr.rdbuf();
FCGX_Request request;
FCGX_Init();
FCGX_InitRequest(&request, 0, 0);
while (FCGX_Accept_r(&request) == 0)
{
// Note that the default bufsize (0) will cause the use of iostream
// methods that require positioning (such as peek(), seek(),
// unget() and putback()) to fail (in favour of more efficient IO).
fcgi_streambuf cin_fcgi_streambuf(request.in);
fcgi_streambuf cout_fcgi_streambuf(request.out);
fcgi_streambuf cerr_fcgi_streambuf(request.err);
#if HAVE_IOSTREAM_WITHASSIGN_STREAMBUF
cin = &cin_fcgi_streambuf;
cout = &cout_fcgi_streambuf;
cerr = &cerr_fcgi_streambuf;
#else
cin.rdbuf(&cin_fcgi_streambuf);
cout.rdbuf(&cout_fcgi_streambuf);
cerr.rdbuf(&cerr_fcgi_streambuf);
#endif
// Although FastCGI supports writing before reading,
// many http clients (browsers) don't support it (so
// the connection deadlocks until a timeout expires!).
char * content;
unsigned long clen = gstdin(&request, &content);
cout << "Content-type: text/html\r\n"
"\r\n"
"<TITLE>echo-cpp</TITLE>\n"
"<H1>echo-cpp</H1>\n"
"<H4>PID: " << pid << "</H4>\n"
"<H4>Request Number: " << ++count << "</H4>\n";
cout << "<H4>Request Environment</H4>\n";
penv(request.envp);
cout << "<H4>Process/Initial Environment</H4>\n";
penv(environ);
cout << "<H4>Standard Input - " << clen;
if (clen == STDIN_MAX) cout << " (STDIN_MAX)";
cout << " bytes</H4>\n";
if (clen) cout.write(content, clen);
if (content) delete []content;
// If the output streambufs had non-zero bufsizes and
// were constructed outside of the accept loop (i.e.
// their destructor won't be called here), they would
// have to be flushed here.
}
#if HAVE_IOSTREAM_WITHASSIGN_STREAMBUF
cin = cin_streambuf;
cout = cout_streambuf;
cerr = cerr_streambuf;
#else
cin.rdbuf(cin_streambuf);
cout.rdbuf(cout_streambuf);
cerr.rdbuf(cerr_streambuf);
#endif
return 0;
}

View File

@ -1,46 +0,0 @@
#include <iostream>
#include "fcgio.h"
using namespace std;
int main(void) {
// Backup the stdio streambufs
streambuf * cin_streambuf = cin.rdbuf();
streambuf * cout_streambuf = cout.rdbuf();
streambuf * cerr_streambuf = cerr.rdbuf();
FCGX_Request request;
FCGX_Init();
FCGX_InitRequest(&request, 0, 0);
while (FCGX_Accept_r(&request) == 0) {
fcgi_streambuf cin_fcgi_streambuf(request.in);
fcgi_streambuf cout_fcgi_streambuf(request.out);
fcgi_streambuf cerr_fcgi_streambuf(request.err);
cin.rdbuf(&cin_fcgi_streambuf);
cout.rdbuf(&cout_fcgi_streambuf);
cerr.rdbuf(&cerr_fcgi_streambuf);
cout << "Content-type: text/html\r\n"
<< "\r\n"
<< "<html>\n"
<< " <head>\n"
<< " <title>Hello, World!</title>\n"
<< " </head>\n"
<< " <body>\n"
<< " <h1>Hello, World!</h1>\n"
<< " </body>\n"
<< "</html>\n";
// Note: the fcgi_streambuf destructor will auto flush
}
// restore stdio streambufs
cin.rdbuf(cin_streambuf);
cout.rdbuf(cout_streambuf);
cerr.rdbuf(cerr_streambuf);
return 0;
}

View File

@ -11,7 +11,7 @@ IndexController::~IndexController() {
}
void IndexController::addSentence(rapidjson::Writer<rapidjson::StringBuffer> & jsonWriter, string & sentence) {
void IndexController::addSentence(rapidjson::Writer<rapidjson::StringBuffer> & jsonWriter, std::string & sentence) {
try {
Example example(sentence, 0);
@ -23,7 +23,7 @@ void IndexController::addSentence(rapidjson::Writer<rapidjson::StringBuffer> & j
jsonWriter.String("success");
jsonWriter.EndObject();
} catch (ConcordiaException & e) {
stringstream errorstream;
std::stringstream errorstream;
errorstream << "concordia error: " << e.what();
JsonGenerator::signalError(jsonWriter, errorstream.str());
}

View File

@ -8,9 +8,6 @@
#include "rapidjson/writer.h"
using namespace std;
class IndexController {
public:
/*! Constructor.
@ -21,7 +18,7 @@ public:
*/
virtual ~IndexController();
void addSentence(rapidjson::Writer<rapidjson::StringBuffer> & jsonWriter, string & sentence);
void addSentence(rapidjson::Writer<rapidjson::StringBuffer> & jsonWriter, std::string & sentence);
private:
boost::shared_ptr<Concordia> _concordia;

View File

@ -8,7 +8,7 @@ JsonGenerator::~JsonGenerator() {
}
void JsonGenerator::signalError(rapidjson::Writer<rapidjson::StringBuffer> & jsonWriter, string message) {
void JsonGenerator::signalError(rapidjson::Writer<rapidjson::StringBuffer> & jsonWriter, std::string message) {
jsonWriter.StartObject();
jsonWriter.String("status");
jsonWriter.String("error");

View File

@ -5,9 +5,6 @@
#include "rapidjson/writer.h"
using namespace std;
class JsonGenerator {
public:
/*! Constructor.
@ -17,7 +14,7 @@ public:
*/
virtual ~JsonGenerator();
static void signalError(rapidjson::Writer<rapidjson::StringBuffer> & jsonWriter, string message);
static void signalError(rapidjson::Writer<rapidjson::StringBuffer> & jsonWriter, std::string message);
private:

View File

@ -12,8 +12,8 @@ SearcherController::~SearcherController() {
}
void SearcherController::simpleSearch(rapidjson::Writer<rapidjson::StringBuffer> & jsonWriter, string & pattern) {
vector<SubstringOccurence> result = _concordia->simpleSearch(pattern);
void SearcherController::simpleSearch(rapidjson::Writer<rapidjson::StringBuffer> & jsonWriter, std::string & pattern) {
std::vector<SubstringOccurence> result = _concordia->simpleSearch(pattern);
jsonWriter.StartObject();
jsonWriter.String("status");
@ -27,7 +27,7 @@ void SearcherController::simpleSearch(rapidjson::Writer<rapidjson::StringBuffer>
jsonWriter.EndObject();
}
void SearcherController::concordiaSearch(rapidjson::Writer<rapidjson::StringBuffer> & jsonWriter, string & pattern) {
void SearcherController::concordiaSearch(rapidjson::Writer<rapidjson::StringBuffer> & jsonWriter, std::string & pattern) {
jsonWriter.StartObject();
jsonWriter.String("status");
jsonWriter.String("error");

View File

@ -9,8 +9,6 @@
#include "rapidjson/writer.h"
using namespace std;
class SearcherController {
public:
/*! Constructor.
@ -21,9 +19,9 @@ public:
*/
virtual ~SearcherController();
void simpleSearch(rapidjson::Writer<rapidjson::StringBuffer> & jsonWriter, string & pattern);
void simpleSearch(rapidjson::Writer<rapidjson::StringBuffer> & jsonWriter, std::string & pattern);
void concordiaSearch(rapidjson::Writer<rapidjson::StringBuffer> & jsonWriter, string & pattern);
void concordiaSearch(rapidjson::Writer<rapidjson::StringBuffer> & jsonWriter, std::string & pattern);
private:
boost::shared_ptr<Concordia> _concordia;

View File

@ -0,0 +1,13 @@
#include "unit_dao.hpp"
UnitDAO::UnitDAO() {
}
UnitDAO::~UnitDAO() {
}
SUFFIX_MARKER_TYPE UnitDAO::addSentence(std::string & sentence) {
}

View File

@ -0,0 +1,22 @@
#ifndef UNIT_DAO_HDR
#define UNIT_DAO_HDR
#include <string>
#include <concordia/common/config.hpp>
class UnitDAO {
public:
/*! Constructor.
*/
UnitDAO();
/*! Destructor.
*/
virtual ~UnitDAO();
SUFFIX_MARKER_TYPE addSentence(std::string & sentence);
private:
};
#endif

View File

@ -1 +0,0 @@
14823

View File

@ -1,5 +1,5 @@
#!/bin/sh
#curl -H "Content-Type: application/json" -X POST -d '{"operation":"addSentence", "sentence":"zupełnie nowe zdanie"}' http://localhost
curl -H "Content-Type: application/json" -X POST -d '{"operation":"simpleSearch", "sentence":"zupełnie snowe"}' http://localhost
curl -H "Content-Type: application/json" -X POST -d '{"operation":"simpleSearch", "sentence":"zupełnie nowe"}' http://localhost