API calls now get parsed through a grammar using boost::spirit::qi

This commit is contained in:
DennisOSRM
2012-09-12 15:01:37 +02:00
parent f575da2ca8
commit 1ec49c3914
8 changed files with 194 additions and 141 deletions
+8 -10
View File
@@ -25,16 +25,14 @@ public:
reply.status = http::Reply::ok;
reply.content.append("<html><head><title>Hello World Demonstration Document</title></head><body><h1>Hello, World!</h1>");
std::stringstream content;
content << "Number of parameters: " << routeParameters.parameters.size() << "<br>";
for(unsigned i = 0; i < routeParameters.parameters.size(); i++) {
content << routeParameters.parameters[i] << "<br>";
}
content << "Number Of Options: " << routeParameters.options.Size() << "<br>";
RouteParameters::OptionsIterator optionsIT = routeParameters.options.begin();
for(;optionsIT != routeParameters.options.end(); optionsIT++) {
content << "param = " << optionsIT->first << ": ";
content << "option = " << optionsIT->second << "<br>";
}
content << "Number of locations: " << routeParameters.coordinates.size() << "<br>\n";
for(unsigned i = 0; i < routeParameters.coordinates.size(); ++i) {
content << " [" << i << "] " << routeParameters.coordinates[i].lat/100000. << "," << routeParameters.coordinates[i].lon/100000. << "<br>\n";
}
content << "Number of hints: " << routeParameters.hints.size() << "<br>\n";
for(unsigned i = 0; i < routeParameters.hints.size(); ++i) {
content << " [" << i << "] " << routeParameters.hints[i] << "<br>\n";
}
reply.content.append(content.str());
reply.content.append("</body></html>");
}
+5 -15
View File
@@ -41,21 +41,11 @@ public:
std::string GetVersionString() const { return std::string("0.3 (DL)"); }
void HandleRequest(const RouteParameters & routeParameters, http::Reply& reply) {
//check number of parameters
if(!routeParameters.viaPoints.size()) {
if(!routeParameters.coordinates.size()) {
reply = http::Reply::stockReply(http::Reply::badRequest);
return;
}
std::vector<std::string> textCoord;
stringSplit (routeParameters.viaPoints[0], ',', textCoord);
if(textCoord.size() != 2) {
reply = http::Reply::stockReply(http::Reply::badRequest);
return;
}
int lat = 100000.*atof(textCoord[0].c_str());
int lon = 100000.*atof(textCoord[1].c_str());
_Coordinate myCoordinate(lat, lon);
if(false == checkCoord(myCoordinate)) {
if(false == checkCoord(routeParameters.coordinates[0])) {
reply = http::Reply::stockReply(http::Reply::badRequest);
return;
}
@@ -65,15 +55,15 @@ public:
std::string JSONParameter, tmp;
//json
JSONParameter = routeParameters.options.Find("jsonp");
if("" != JSONParameter) {
// JSONParameter = routeParameters.options.Find("jsonp");
if("" != routeParameters.jsonpParameter) {
reply.content += JSONParameter;
reply.content += "(";
}
reply.status = http::Reply::ok;
reply.content += ("{");
reply.content += ("\"version\":0.3,");
if(!nodeHelpDesk->FindNearestNodeCoordForLatLon(myCoordinate, result)) {
if(!nodeHelpDesk->FindNearestNodeCoordForLatLon(routeParameters.coordinates[0], result)) {
reply.content += ("\"status\":207,");
reply.content += ("\"mapped_coordinate\":[]");
} else {
+5 -24
View File
@@ -29,7 +29,6 @@ or see http://www.gnu.org/licenses/agpl.txt.
#include "../Server/DataStructures/QueryObjectsStorage.h"
#include "../DataStructures/NodeInformationHelpDesk.h"
#include "../DataStructures/HashTable.h"
#include "../Util/StringUtil.h"
/*
@@ -47,43 +46,25 @@ public:
std::string GetVersionString() const { return std::string("0.3 (DL)"); }
void HandleRequest(const RouteParameters & routeParameters, http::Reply& reply) {
//check number of parameters
if(!routeParameters.viaPoints.size()) {
if(!routeParameters.coordinates.size()) {
reply = http::Reply::stockReply(http::Reply::badRequest);
return;
}
std::vector<std::string> textCoord;
stringSplit (routeParameters.viaPoints[0], ',', textCoord);
if(textCoord.size() != 2) {
reply = http::Reply::stockReply(http::Reply::badRequest);
return;
}
int lat = 100000.*atof(textCoord[0].c_str());
int lon = 100000.*atof(textCoord[1].c_str());
_Coordinate myCoordinate(lat, lon);
if(false == checkCoord(myCoordinate)) {
if(false == checkCoord(routeParameters.coordinates[0])) {
reply = http::Reply::stockReply(http::Reply::badRequest);
return;
}
unsigned zoomLevel = 18;
if(routeParameters.options.Find("z") != ""){
zoomLevel = atoi(routeParameters.options.Find("z").c_str());
if(18 < zoomLevel)
zoomLevel = 18;
}
//query to helpdesk
PhantomNode result;
nodeHelpDesk->FindPhantomNodeForCoordinate(myCoordinate, result, zoomLevel);
nodeHelpDesk->FindPhantomNodeForCoordinate(routeParameters.coordinates[0], result, routeParameters.zoomLevel);
std::string tmp;
std::string JSONParameter;
//json
JSONParameter = routeParameters.options.Find("jsonp");
if("" != JSONParameter) {
reply.content += JSONParameter;
if("" != routeParameters.jsonpParameter) {
reply.content += routeParameters.jsonpParameter;
reply.content += "(";
}
+63 -4
View File
@@ -23,14 +23,73 @@ or see http://www.gnu.org/licenses/agpl.txt.
#include <string>
#include <vector>
#include "../DataStructures/HashTable.h"
#include <boost/fusion/sequence/intrinsic.hpp>
#include "../DataStructures/Coordinate.h"
struct RouteParameters {
RouteParameters() : zoomLevel(18), printInstructions(false), geometry(true), compression(true), checkSum(-1) {}
short zoomLevel;
bool printInstructions;
bool geometry;
bool compression;
int checkSum;
std::string service;
std::string outputFormat;
std::string jsonpParameter;
std::string language;
std::vector<std::string> hints;
std::vector<std::string> parameters;
std::vector<std::string> viaPoints;
HashTable<std::string, std::string> options;
std::vector<_Coordinate> coordinates;
typedef HashTable<std::string, std::string>::MyIterator OptionsIterator;
void setZoomLevel(const short i) {
if (18 > i && 0 < i)
zoomLevel = i;
}
void setChecksum(const int c) {
checkSum = c;
}
void setInstructionFlag(const bool b) {
printInstructions = b;
}
void printService( const std::string & s) {
service = s;
}
void setOutputFormat(const std::string & s) {
outputFormat = s;
}
void setJSONpParameter(const std::string & s) {
jsonpParameter = s;
}
void addHint(const std::string & s) {
hints.push_back(s);
}
void setLanguage(const std::string & s) {
language = s;
}
void setGeometryFlag(const bool b) {
geometry = b;
}
void setCompressionFlag(const bool b) {
compression = b;
}
void addCoordinate(boost::fusion::vector < double, double > arg_) {
int lat = 100000.*boost::fusion::at_c < 0 > (arg_);
int lon = 100000.*boost::fusion::at_c < 1 > (arg_);
_Coordinate myCoordinate(lat, lon);
coordinates.push_back(_Coordinate(lat, lon));
}
};
+2 -3
View File
@@ -37,9 +37,8 @@ public:
std::string JSONParameter;
//json
JSONParameter = routeParameters.options.Find("jsonp");
if("" != JSONParameter) {
reply.content += JSONParameter;
if("" != routeParameters.jsonpParameter) {
reply.content += routeParameters.jsonpParameter;
reply.content += "(";
}
+17 -40
View File
@@ -74,37 +74,21 @@ public:
std::string GetVersionString() const { return std::string("0.3 (DL)"); }
void HandleRequest(const RouteParameters & routeParameters, http::Reply& reply) {
//check number of parameters
if( 2 > routeParameters.viaPoints.size() ) {
if( 2 > routeParameters.coordinates.size() ) {
reply = http::Reply::stockReply(http::Reply::badRequest);
return;
}
unsigned zoomLevel = 18;
if(routeParameters.options.Find("z") != ""){
zoomLevel = atoi(routeParameters.options.Find("z").c_str());
if(18 < zoomLevel)
zoomLevel = 18;
}
RawRouteData rawRoute;
rawRoute.checkSum = nodeHelpDesk->GetCheckSum();
bool checksumOK = ((unsigned)atoi(routeParameters.options.Find("checksum").c_str()) == rawRoute.checkSum);
bool checksumOK = (routeParameters.checkSum == rawRoute.checkSum);
std::vector<std::string> textCoord;
for(unsigned i = 0; i < routeParameters.viaPoints.size(); ++i) {
textCoord.resize(0);
stringSplit (routeParameters.viaPoints[i], ',', textCoord);
if(textCoord.size() != 2) {
for(unsigned i = 0; i < routeParameters.coordinates.size(); ++i) {
if(false == checkCoord(routeParameters.coordinates[i])) {
reply = http::Reply::stockReply(http::Reply::badRequest);
return;
}
int vialat = static_cast<int>(100000.*atof(textCoord[0].c_str()));
int vialon = static_cast<int>(100000.*atof(textCoord[1].c_str()));
_Coordinate viaCoord(vialat, vialon);
if(false == checkCoord(viaCoord)) {
reply = http::Reply::stockReply(http::Reply::badRequest);
return;
}
rawRoute.rawViaNodeCoordinates.push_back(viaCoord);
rawRoute.rawViaNodeCoordinates.push_back(routeParameters.coordinates[i]);
}
std::vector<PhantomNode> phantomNodeVector(rawRoute.rawViaNodeCoordinates.size());
for(unsigned i = 0; i < rawRoute.rawViaNodeCoordinates.size(); ++i) {
@@ -117,9 +101,8 @@ public:
}
}
// INFO("Brute force lookup of coordinate " << i);
searchEngine->FindPhantomNodeForCoordinate( rawRoute.rawViaNodeCoordinates[i], phantomNodeVector[i], zoomLevel);
searchEngine->FindPhantomNodeForCoordinate( rawRoute.rawViaNodeCoordinates[i], phantomNodeVector[i], routeParameters.zoomLevel);
}
//unsigned distance = 0;
for(unsigned i = 0; i < phantomNodeVector.size()-1; ++i) {
PhantomNodes segmentPhantomNodes;
@@ -143,24 +126,18 @@ public:
//TODO: Move to member as smart pointer
BaseDescriptor<SearchEngine<QueryEdge::EdgeData, StaticGraph<QueryEdge::EdgeData> > > * desc;
std::string JSONParameter = routeParameters.options.Find("jsonp");
if("" != JSONParameter) {
reply.content += JSONParameter;
if("" != routeParameters.jsonpParameter) {
reply.content += routeParameters.jsonpParameter;
reply.content += "(";
}
_DescriptorConfig descriptorConfig;
unsigned descriptorType = descriptorTable[routeParameters.options.Find("output")];
descriptorConfig.z = zoomLevel;
if(routeParameters.options.Find("instructions") == "false") {
descriptorConfig.instructions = false;
}
if(routeParameters.options.Find("geometry") == "false" ) {
descriptorConfig.geometry = false;
}
if("cmp" == routeParameters.options.Find("no") || "cmp6" == routeParameters.options.Find("no") ) {
descriptorConfig.encodeGeometry = false;
}
unsigned descriptorType = descriptorTable[routeParameters.outputFormat];
descriptorConfig.z = routeParameters.zoomLevel;
descriptorConfig.instructions = routeParameters.printInstructions;
descriptorConfig.geometry = routeParameters.geometry;
descriptorConfig.encodeGeometry = routeParameters.compression;
switch(descriptorType){
case 0:
desc = new JSONDescriptor<SearchEngine<QueryEdge::EdgeData, StaticGraph<QueryEdge::EdgeData> > >();
@@ -185,7 +162,7 @@ public:
desc->SetConfig(descriptorConfig);
desc->Run(reply, rawRoute, phantomNodes, *searchEngine);
if("" != JSONParameter) {
if("" != routeParameters.jsonpParameter) {
reply.content += ")\n";
}
reply.headers.resize(3);
@@ -195,7 +172,7 @@ public:
reply.headers[0].value = tmp;
switch(descriptorType){
case 0:
if("" != JSONParameter){
if("" != routeParameters.jsonpParameter){
reply.headers[1].name = "Content-Type";
reply.headers[1].value = "text/javascript";
reply.headers[2].name = "Content-Disposition";
@@ -216,7 +193,7 @@ public:
break;
default:
if("" != JSONParameter){
if("" != routeParameters.jsonpParameter){
reply.headers[1].name = "Content-Type";
reply.headers[1].value = "text/javascript";
reply.headers[2].name = "Content-Disposition";