First implementation of moving the algorithmic core into a library

This commit is contained in:
Dennis Luxen 2013-06-26 19:48:02 -04:00
parent bfef8f39b7
commit 2c397bfa0b
19 changed files with 143 additions and 139 deletions

View File

@ -21,17 +21,17 @@ or see http://www.gnu.org/licenses/agpl.txt.
#ifndef BASEPARSER_H_
#define BASEPARSER_H_
#include "ExtractorCallbacks.h"
#include "ScriptingEnvironment.h"
extern "C" {
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
}
#include <boost/noncopyable.hpp>
#include "ExtractorCallbacks.h"
#include "ScriptingEnvironment.h"
class BaseParser : boost::noncopyable {
public:
BaseParser(ExtractorCallbacks* ec, ScriptingEnvironment& se);

View File

@ -18,19 +18,16 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
or see http://www.gnu.org/licenses/agpl.txt.
*/
#ifndef EXTRACTIONHELPERFUNCTIONS_H_
#define EXTRACTIONHELPERFUNCTIONS_H_
#include "../Util/StringUtil.h"
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string_regex.hpp>
#include <boost/regex.hpp>
#include <climits>
#include "../Util/StringUtil.h"
namespace qi = boost::spirit::qi;
//TODO: Move into LUA

View File

@ -40,7 +40,6 @@ or see http://www.gnu.org/licenses/agpl.txt.
#include "ExtractorCallbacks.h"
#include "ExtractionHelperFunctions.h"
ExtractorCallbacks::ExtractorCallbacks() {externalMemory = NULL; stringMap = NULL; }
ExtractorCallbacks::ExtractorCallbacks(ExtractionContainers * ext, StringMap * strMap) {

View File

@ -31,6 +31,7 @@ or see http://www.gnu.org/licenses/agpl.txt.
#include <boost/regex.hpp>
#include "ExtractionContainers.h"
#include "ExtractionHelperFunctions.h"
#include "ExtractorStructs.h"
class ExtractorCallbacks{

View File

@ -21,13 +21,6 @@ or see http://www.gnu.org/licenses/agpl.txt.
#ifndef EXTRACTORSTRUCTS_H_
#define EXTRACTORSTRUCTS_H_
#include <climits>
#include <string>
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/regex.hpp>
#include <boost/regex.hpp>
#include <boost/unordered_map.hpp>
#include "../DataStructures/Coordinate.h"
#include "../DataStructures/HashTable.h"
@ -37,6 +30,14 @@ or see http://www.gnu.org/licenses/agpl.txt.
#include "../DataStructures/TimingUtil.h"
#include "../typedefs.h"
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/regex.hpp>
#include <boost/regex.hpp>
#include <boost/unordered_map.hpp>
#include <climits>
#include <string>
typedef boost::unordered_map<std::string, NodeID > StringMap;
typedef boost::unordered_map<std::string, std::pair<int, short> > StringToIntPairMap;

View File

@ -21,13 +21,14 @@
#ifndef PBFPARSER_H_
#define PBFPARSER_H_
#include "BaseParser.h"
#include "../DataStructures/HashTable.h"
#include "../DataStructures/ConcurrentQueue.h"
#include "../Util/MachineInfo.h"
#include "../Util/OpenMPWrapper.h"
#include "../typedefs.h"
#include "BaseParser.h"
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>
#include <boost/ref.hpp>
@ -37,8 +38,6 @@
#include <zlib.h>
class PBFParser : public BaseParser {
enum EntityType {

View File

@ -21,20 +21,14 @@
#ifndef SCRIPTINGENVIRONMENT_H_
#define SCRIPTINGENVIRONMENT_H_
extern "C" {
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
}
#include <luabind/luabind.hpp>
#include "ExtractionHelperFunctions.h"
#include "ExtractorStructs.h"
#include "../typedefs.h"
#include "../DataStructures/ImportNode.h"
#include "../Util/LuaUtil.h"
#include "../Util/OpenMPWrapper.h"
#include "../typedefs.h"
#include <vector>
class ScriptingEnvironment {
public:

View File

@ -18,14 +18,13 @@
or see http://www.gnu.org/licenses/agpl.txt.
*/
#include <boost/ref.hpp>
#include "XMLParser.h"
#include "ExtractorStructs.h"
#include "../DataStructures/HashTable.h"
#include "../DataStructures/InputReaderFactory.h"
#include <boost/ref.hpp>
XMLParser::XMLParser(const char * filename, ExtractorCallbacks* ec, ScriptingEnvironment& se) : BaseParser(ec, se) {
WARN("Parsing plain .osm/.osm.bz2 is deprecated. Switch to .pbf");

View File

@ -21,13 +21,12 @@ or see http://www.gnu.org/licenses/agpl.txt.
#ifndef BASEPLUGIN_H_
#define BASEPLUGIN_H_
#include <cassert>
#include <string>
#include <vector>
#include "RouteParameters.h"
#include "../Server/BasicDatastructures.h"
#include <string>
#include <vector>
class BasePlugin {
public:
BasePlugin() { }

View File

@ -8,15 +8,15 @@
#ifndef HELLOWORLDPLUGIN_H_
#define HELLOWORLDPLUGIN_H_
#include <sstream>
#include "BasePlugin.h"
#include "RouteParameters.h"
#include <sstream>
class HelloWorldPlugin : public BasePlugin {
public:
HelloWorldPlugin() {}
virtual ~HelloWorldPlugin() { /*std::cout << GetDescriptor() << " destructor" << std::endl;*/ }
virtual ~HelloWorldPlugin() { }
std::string GetDescriptor() const { return std::string("hello"); }
std::string GetVersionString() const { return std::string("0.1a"); }

View File

@ -21,16 +21,15 @@ or see http://www.gnu.org/licenses/agpl.txt.
#ifndef NearestPlugin_H_
#define NearestPlugin_H_
#include <fstream>
#include "BasePlugin.h"
#include "RouteParameters.h"
#include "../Server/DataStructures/QueryObjectsStorage.h"
#include "../DataStructures/NodeInformationHelpDesk.h"
#include "../Server/DataStructures/QueryObjectsStorage.h"
#include "../Util/StringUtil.h"
#include <fstream>
/*
* This Plugin locates the nearest point on a street in the road network for a given coordinate.
*/

View File

@ -21,15 +21,25 @@ or see http://www.gnu.org/licenses/agpl.txt.
#ifndef ROUTE_PARAMETERS_H
#define ROUTE_PARAMETERS_H
#include "../DataStructures/Coordinate.h"
#include "../DataStructures/HashTable.h"
#include <boost/fusion/container/vector.hpp>
#include <boost/fusion/sequence/intrinsic.hpp>
#include <boost/fusion/include/at_c.hpp>
#include <string>
#include <vector>
#include <boost/fusion/sequence/intrinsic.hpp>
#include "../DataStructures/Coordinate.h"
struct RouteParameters {
RouteParameters() : zoomLevel(18), printInstructions(false), alternateRoute(true), geometry(true), compression(true), deprecatedAPI(false), checkSum(-1) {}
RouteParameters() :
zoomLevel(18),
printInstructions(false),
alternateRoute(true),
geometry(true),
compression(true),
deprecatedAPI(false),
checkSum(-1) {}
short zoomLevel;
bool printInstructions;
bool alternateRoute;

View File

@ -21,18 +21,22 @@ or see http://www.gnu.org/licenses/agpl.txt.
#ifndef TIMESTAMPPLUGIN_H_
#define TIMESTAMPPLUGIN_H_
#include <cassert>
#include "BasePlugin.h"
#include "RouteParameters.h"
#include <cassert>
class TimestampPlugin : public BasePlugin {
public:
TimestampPlugin(QueryObjectsStorage * o) : objects(o) {
}
~TimestampPlugin() {
std::cout << "shutdown time stamp" << std::endl;
}
std::string GetDescriptor() const { return std::string("timestamp"); }
std::string GetVersionString() const { return std::string("0.3 (DL)"); }
void HandleRequest(const RouteParameters & routeParameters, http::Reply& reply) {
std::cout << "handling request" << std::endl;
std::string tmp;
//json

View File

@ -21,29 +21,26 @@ or see http://www.gnu.org/licenses/agpl.txt.
#ifndef VIAROUTEPLUGIN_H_
#define VIAROUTEPLUGIN_H_
#include <cstdlib>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include "BasePlugin.h"
#include "RouteParameters.h"
#include "../Algorithms/ObjectToBase64.h"
#include "../Descriptors/BaseDescriptor.h"
#include "../Descriptors/GPXDescriptor.h"
#include "../Descriptors/JSONDescriptor.h"
#include "../DataStructures/HashTable.h"
#include "../DataStructures/QueryEdge.h"
#include "../DataStructures/StaticGraph.h"
#include "../DataStructures/SearchEngine.h"
#include "../Descriptors/BaseDescriptor.h"
#include "../Descriptors/GPXDescriptor.h"
#include "../Descriptors/JSONDescriptor.h"
#include "../Server/DataStructures/QueryObjectsStorage.h"
#include "../Util/StringUtil.h"
#include "../Server/DataStructures/QueryObjectsStorage.h"
#include <cstdlib>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
class ViaRoutePlugin : public BasePlugin {
private:

View File

@ -20,12 +20,16 @@ or see http://www.gnu.org/licenses/agpl.txt.
#ifndef BASIC_DATASTRUCTURES_H
#define BASIC_DATASTRUCTURES_H
#include <string>
#include <sstream>
#include <boost/foreach.hpp>
#include "../Util/StringUtil.h"
#include <boost/asio.hpp>
#include <boost/foreach.hpp>
#include <string>
#include <sstream>
#include <vector>
namespace http {
const std::string okString = "HTTP/1.0 200 OK\r\n";
@ -77,7 +81,7 @@ struct Reply {
BOOST_FOREACH ( Header& h, headers) {
if("Content-Length" == h.name) {
std::string sizeString;
intToString(size,h.value );
intToString(size,h.value);
}
}
}

View File

@ -21,7 +21,9 @@ or see http://www.gnu.org/licenses/agpl.txt.
#ifndef CONNECTION_H
#define CONNECTION_H
#include <vector>
#include "BasicDatastructures.h"
#include "RequestHandler.h"
#include "RequestParser.h"
#include <boost/asio.hpp>
#include <boost/array.hpp>
@ -30,11 +32,9 @@ or see http://www.gnu.org/licenses/agpl.txt.
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
#include "BasicDatastructures.h"
#include "RequestHandler.h"
#include "RequestParser.h"
#include <zlib.h>
#include "zlib.h"
#include <vector>
namespace http {

View File

@ -21,35 +21,27 @@ or see http://www.gnu.org/licenses/agpl.txt.
#ifndef REQUEST_HANDLER_H
#define REQUEST_HANDLER_H
#include <algorithm>
#include <cctype> // std::tolower
#include <string>
#include <iostream>
#include <boost/noncopyable.hpp>
#include "APIGrammar.h"
#include "BasicDatastructures.h"
#include "../DataStructures/HashTable.h"
#include "../Plugins/BasePlugin.h"
#include "../Library/OSRM.h"
#include "../Plugins/RouteParameters.h"
#include "../Util/StringUtil.h"
#include "../typedefs.h"
namespace http {
#include <boost/foreach.hpp>
#include <boost/noncopyable.hpp>
#include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
class RequestHandler : private boost::noncopyable {
public:
explicit RequestHandler() : _pluginCount(0) { }
typedef APIGrammar<std::string::iterator, RouteParameters> APIGrammarParser;
explicit RequestHandler() { }
~RequestHandler() {
for(unsigned i = 0; i < _pluginVector.size(); i++) {
BasePlugin * tempPointer = _pluginVector[i];
delete tempPointer;
}
}
void handle_request(const Request& req, Reply& rep){
void handle_request(const http::Request& req, http::Reply& rep){
//parse command
try {
std::string request(req.uri);
@ -66,10 +58,10 @@ public:
}
RouteParameters routeParameters;
APIGrammar<std::string::iterator, RouteParameters> apiParser(&routeParameters);
APIGrammarParser apiParser(&routeParameters);
std::string::iterator it = request.begin();
bool result = boost::spirit::qi::parse(it, request.end(), apiParser); // returns true if successful
bool result = boost::spirit::qi::parse(it, request.end(), apiParser);
if (!result || (it != request.end()) ) {
rep = http::Reply::stockReply(http::Reply::badRequest);
int position = std::distance(request.begin(), it);
@ -80,38 +72,29 @@ public:
rep.content += request;
rep.content += tmp_position_string;
rep.content += "<br>";
for(unsigned i = 0, end = std::distance(request.begin(), it); i < end; ++i)
unsigned end = std::distance(request.begin(), it);
for(unsigned i = 0; i < end; ++i) {
rep.content += "&nbsp;";
}
rep.content += "^<br></pre>";
} else {
//Finished parsing, lets call the right plugin to handle the request
if(pluginMap.Holds(routeParameters.service)) {
rep.status = Reply::ok;
_pluginVector[pluginMap.Find(routeParameters.service)]->HandleRequest(routeParameters, rep );
} else {
rep = Reply::stockReply(Reply::badRequest);
}
//parsing done, lets call the right plugin to handle the request
routing_machine->RunQuery(routeParameters, rep);
return;
}
} catch(std::exception& e) {
rep = Reply::stockReply(Reply::internalServerError);
std::cerr << "[server error] code: " << e.what() << ", uri: " << req.uri << std::endl;
rep = http::Reply::stockReply(http::Reply::internalServerError);
WARN("[server error] code: " << e.what() << ", uri: " << req.uri);
return;
}
};
void RegisterPlugin(BasePlugin * plugin) {
std::cout << "[handler] registering plugin " << plugin->GetDescriptor() << std::endl;
pluginMap.Add(plugin->GetDescriptor(), _pluginCount);
_pluginVector.push_back(plugin);
++_pluginCount;
void RegisterRoutingMachine(OSRM * osrm) {
routing_machine = osrm;
}
private:
HashTable<std::string, unsigned> pluginMap;
std::vector<BasePlugin *> _pluginVector;
unsigned _pluginCount;
OSRM * routing_machine;
};
} // namespace http
#endif // REQUEST_HANDLER_H

View File

@ -21,21 +21,29 @@ or see http://www.gnu.org/licenses/agpl.txt.
#ifndef SERVER_H
#define SERVER_H
#include <vector>
#include "Connection.h"
#include "RequestHandler.h"
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/noncopyable.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/thread.hpp>
#include "Connection.h"
#include "RequestHandler.h"
namespace http {
#include <vector>
class Server: private boost::noncopyable {
public:
explicit Server(const std::string& address, const std::string& port, unsigned thread_pool_size) : threadPoolSize(thread_pool_size), acceptor(ioService), newConnection(new Connection(ioService, requestHandler)), requestHandler(){
explicit Server(
const std::string& address,
const std::string& port,
unsigned thread_pool_size
) :
threadPoolSize(thread_pool_size),
acceptor(ioService),
newConnection(new http::Connection(ioService, requestHandler)),
requestHandler()
{
boost::asio::ip::tcp::resolver resolver(ioService);
boost::asio::ip::tcp::resolver::query query(address, port);
boost::asio::ip::tcp::endpoint endpoint = *resolver.resolve(query);
@ -44,7 +52,14 @@ public:
acceptor.set_option(boost::asio::ip::tcp::acceptor::reuse_address(true));
acceptor.bind(endpoint);
acceptor.listen();
acceptor.async_accept(newConnection->socket(), boost::bind(&Server::handleAccept, this, boost::asio::placeholders::error));
acceptor.async_accept(
newConnection->socket(),
boost::bind(
&Server::handleAccept,
this,
boost::asio::placeholders::error
)
);
}
void Run() {
@ -66,23 +81,28 @@ public:
}
private:
typedef boost::shared_ptr<Connection > ConnectionPtr;
void handleAccept(const boost::system::error_code& e) {
if (!e) {
newConnection->start();
newConnection.reset(new Connection(ioService, requestHandler));
acceptor.async_accept(newConnection->socket(), boost::bind(&Server::handleAccept, this, boost::asio::placeholders::error));
newConnection.reset(
new http::Connection(ioService, requestHandler)
);
acceptor.async_accept(
newConnection->socket(),
boost::bind(
&Server::handleAccept,
this,
boost::asio::placeholders::error
)
);
}
}
unsigned threadPoolSize;
boost::asio::io_service ioService;
boost::asio::ip::tcp::acceptor acceptor;
ConnectionPtr newConnection;
boost::shared_ptr<http::Connection> newConnection;
RequestHandler requestHandler;
};
} // namespace http
#endif // SERVER_H

View File

@ -25,21 +25,19 @@ or see http://www.gnu.org/licenses/agpl.txt.
#ifndef SERVERFACTORY_H_
#define SERVERFACTORY_H_
#include <zlib.h>
#include "Server.h"
#include "ServerConfiguration.h"
#include "../Util/BaseConfiguration.h"
#include "../Util/InputFileUtil.h"
#include "../Util/OpenMPWrapper.h"
#include "../Util/StringUtil.h"
#include "../typedefs.h"
typedef http::Server Server;
#include <zlib.h>
struct ServerFactory {
static Server * CreateServer(ServerConfiguration& serverConfig) {
static Server * CreateServer(BaseConfiguration& serverConfig) {
if(!testDataFile(serverConfig.GetParameter("nodesData"))) {
ERR("nodes file not found");
@ -76,7 +74,7 @@ struct ServerFactory {
}
static Server * CreateServer(const char * iniFile) {
ServerConfiguration serverConfig(iniFile);
BaseConfiguration serverConfig(iniFile);
return CreateServer(serverConfig);
}
};