API Breaking change. Location of nodes can be specified by a hint.

This commit is contained in:
DennisOSRM 2012-03-05 19:08:10 +01:00
parent 91c9cb2114
commit e83891b4fc
7 changed files with 132 additions and 109 deletions

View File

@ -32,27 +32,36 @@ or see http://www.gnu.org/licenses/agpl.txt.
#include "../Util/StringUtil.h" #include "../Util/StringUtil.h"
typedef typedef
boost::archive::iterators::base64_from_binary< boost::archive::iterators::base64_from_binary<
boost::archive::iterators::transform_width<string::const_iterator, 6, 8> boost::archive::iterators::transform_width<string::const_iterator, 6, 8>
> base64_t; > base64_t;
typedef typedef
boost::archive::iterators::transform_width< boost::archive::iterators::transform_width<
boost::archive::iterators::binary_from_base64<string::const_iterator>, 8, 6 boost::archive::iterators::binary_from_base64<string::const_iterator>, 8, 6
> binary_t; > binary_t;
template<class ToEncodeT> template<class ToEncodeT>
static void EncodeObjectToBase64(const ToEncodeT & object, std::string& encoded) { static void EncodeObjectToBase64(const ToEncodeT & object, std::string& encoded) {
assert(0 == encoded.length()); assert(0 == encoded.length());
char * pointerToOriginalObject = (char *)&object; char * pointerToOriginalObject = (char *)&object;
encoded = std::string(base64_t(pointerToOriginalObject), base64_t(pointerToOriginalObject+sizeof(ToEncodeT))); encoded = std::string(base64_t(pointerToOriginalObject), base64_t(pointerToOriginalObject+sizeof(ToEncodeT)));
//replace "+" with "-" and "/" with "_"
replaceAll(encoded, "+", "-");
replaceAll(encoded, "/", "_");
} }
template<class ToEncodeT> template<class ToEncodeT>
static void DecodeObjectFraBase64(ToEncodeT & object, const std::string& encoded) { static void DecodeObjectFromBase64(ToEncodeT & object, const std::string& _encoded) {
char * pointerToDecodedObject = (char *)&object; try {
std::string dec(binary_t(encoded.begin()), binary_t(encoded.begin() + encoded.length() - 1)); string encoded(_encoded);
std::copy ( dec.begin(), dec.end(), pointerToDecodedObject ); //replace "-" with "+" and "_" with "/"
replaceAll(encoded, "-", "+");
replaceAll(encoded, "_", "/");
char * pointerToDecodedObject = (char *)&object;
std::string dec(binary_t(encoded.begin()), binary_t(encoded.begin() + encoded.length() - 1));
std::copy ( dec.begin(), dec.end(), pointerToDecodedObject );
} catch(...) {}
} }
#endif /* OBJECTTOBASE64_H_ */ #endif /* OBJECTTOBASE64_H_ */

View File

@ -42,6 +42,9 @@ struct PhantomNode {
bool isBidirected() const { bool isBidirected() const {
return weight2 != INT_MAX; return weight2 != INT_MAX;
} }
bool isValid(const unsigned numberOfNodes) const {
return location.isValid() && (edgeBasedNode < numberOfNodes) && (weight1 != INT_MAX) && (ratio >= 0.) && (ratio <= 1.) && (nodeBasedEdgeNameID != UINT_MAX);
}
}; };
struct PhantomNodes { struct PhantomNodes {

View File

@ -91,6 +91,23 @@ public:
_nodes.swap(nodes); _nodes.swap(nodes);
_edges.swap(edges); _edges.swap(edges);
#ifndef NDEBUG
Percent p(GetNumberOfNodes());
for(unsigned u = 0; u < GetNumberOfNodes(); ++u) {
for(unsigned eid = BeginEdges(u); eid < EndEdges(u); ++eid) {
unsigned v = GetTarget(eid);
EdgeData & data = GetEdgeData(eid);
if(data.shortcut) {
unsigned eid2 = FindEdgeInEitherDirection(u, data.via);
assert(eid2 != UINT_MAX);
eid2 = FindEdgeInEitherDirection(data.via, v);
assert(eid2 != UINT_MAX);
}
}
p.printIncrement();
}
#endif
} }
unsigned GetNumberOfNodes() const { unsigned GetNumberOfNodes() const {

View File

@ -167,11 +167,11 @@ public:
} }
} }
reply.content += "],"; reply.content += "],";
reply.content += "\"route_data\": {"; reply.content += "\"hint_data\": {";
reply.content += "\"checksum\":"; reply.content += "\"checksum\":";
intToString(rawRoute.checkSum, tmp); intToString(rawRoute.checkSum, tmp);
reply.content += tmp; reply.content += tmp;
reply.content += ", \"hint_array\": ["; reply.content += ", \"locations\": [";
for(unsigned i = 0; i < rawRoute.segmentEndCoordinates.size(); ++i) { for(unsigned i = 0; i < rawRoute.segmentEndCoordinates.size(); ++i) {
std::string hint; std::string hint;

View File

@ -25,7 +25,7 @@ struct RawRouteData {
std::vector< _PathData > computedRouted; std::vector< _PathData > computedRouted;
std::vector< PhantomNodes > segmentEndCoordinates; std::vector< PhantomNodes > segmentEndCoordinates;
std::vector< _Coordinate > rawViaNodeCoordinates; std::vector< _Coordinate > rawViaNodeCoordinates;
unsigned checkSum; int checkSum;
}; };
#endif /* RAWROUTEDATA_H_ */ #endif /* RAWROUTEDATA_H_ */

View File

@ -31,6 +31,9 @@ or see http://www.gnu.org/licenses/agpl.txt.
#include "BasePlugin.h" #include "BasePlugin.h"
#include "RouteParameters.h" #include "RouteParameters.h"
#include "../Algorithms/ObjectToBase64.h"
#include "../Descriptors/BaseDescriptor.h" #include "../Descriptors/BaseDescriptor.h"
#include "../Descriptors/GPXDescriptor.h" #include "../Descriptors/GPXDescriptor.h"
#include "../Descriptors/JSONDescriptor.h" #include "../Descriptors/JSONDescriptor.h"
@ -48,7 +51,7 @@ private:
StaticGraph<EdgeData> * graph; StaticGraph<EdgeData> * graph;
HashTable<std::string, unsigned> descriptorTable; HashTable<std::string, unsigned> descriptorTable;
std::string pluginDescriptorString; std::string pluginDescriptorString;
bool checksumOK;
SearchEngine<EdgeData, StaticGraph<EdgeData> > * searchEngine; SearchEngine<EdgeData, StaticGraph<EdgeData> > * searchEngine;
public: public:
@ -62,6 +65,7 @@ public:
descriptorTable.Set("", 0); //default descriptor descriptorTable.Set("", 0); //default descriptor
descriptorTable.Set("json", 0); descriptorTable.Set("json", 0);
descriptorTable.Set("gpx", 1); descriptorTable.Set("gpx", 1);
checksumOK = false;
} }
virtual ~ViaRoutePlugin() { virtual ~ViaRoutePlugin() {
@ -72,35 +76,19 @@ public:
std::string GetVersionString() const { return std::string("0.3 (DL)"); } std::string GetVersionString() const { return std::string("0.3 (DL)"); }
void HandleRequest(const RouteParameters & routeParameters, http::Reply& reply) { void HandleRequest(const RouteParameters & routeParameters, http::Reply& reply) {
//check number of parameters //check number of parameters
if(0 == routeParameters.options["start"].size() || 0 == routeParameters.options["dest"].size() ) { if( 2 > routeParameters.viaPoints.size() ) {
reply = http::Reply::stockReply(http::Reply::badRequest); reply = http::Reply::stockReply(http::Reply::badRequest);
return; return;
} }
//Get start and end Coordinate
std::string start = routeParameters.options["start"];
std::string dest = routeParameters.options["dest"];
std::vector<std::string> textCoord = split (start, ',');
int lat1 = static_cast<int>(100000.*atof(textCoord[0].c_str()));
int lon1 = static_cast<int>(100000.*atof(textCoord[1].c_str()));
textCoord = split (dest, ',');
int lat2 = static_cast<int>(100000.*atof(textCoord[0].c_str()));
int lon2 = static_cast<int>(100000.*atof(textCoord[1].c_str()));
_Coordinate startCoord(lat1, lon1);
_Coordinate targetCoord(lat2, lon2);
RawRouteData rawRoute; RawRouteData rawRoute;
rawRoute.checkSum = nodeHelpDesk->GetCheckSum(); rawRoute.checkSum = nodeHelpDesk->GetCheckSum();
if(false == checkCoord(startCoord) || false == checkCoord(targetCoord)) { checksumOK = (atoi(routeParameters.options.Find("checksum").c_str()) == rawRoute.checkSum);
reply = http::Reply::stockReply(http::Reply::badRequest); if(!checksumOK) {
return; INFO(atoi(routeParameters.options.Find("checksum").c_str()) << "!=" << rawRoute.checkSum);
INFO("mismatching checksum");
} }
rawRoute.rawViaNodeCoordinates.push_back(startCoord); std::vector<std::string> textCoord;
for(unsigned i = 0; i < routeParameters.viaPoints.size(); ++i) { for(unsigned i = 0; i < routeParameters.viaPoints.size(); ++i) {
textCoord = split (routeParameters.viaPoints[i], ','); textCoord = split (routeParameters.viaPoints[i], ',');
if(textCoord.size() != 2) { if(textCoord.size() != 2) {
@ -109,7 +97,6 @@ public:
} }
int vialat = static_cast<int>(100000.*atof(textCoord[0].c_str())); int vialat = static_cast<int>(100000.*atof(textCoord[0].c_str()));
int vialon = static_cast<int>(100000.*atof(textCoord[1].c_str())); int vialon = static_cast<int>(100000.*atof(textCoord[1].c_str()));
// INFO("[debug] via" << i << ": " << vialat << "," << vialon);
_Coordinate viaCoord(vialat, vialon); _Coordinate viaCoord(vialat, vialon);
if(false == checkCoord(viaCoord)) { if(false == checkCoord(viaCoord)) {
reply = http::Reply::stockReply(http::Reply::badRequest); reply = http::Reply::stockReply(http::Reply::badRequest);
@ -117,11 +104,18 @@ public:
} }
rawRoute.rawViaNodeCoordinates.push_back(viaCoord); rawRoute.rawViaNodeCoordinates.push_back(viaCoord);
} }
rawRoute.rawViaNodeCoordinates.push_back(targetCoord); std::vector<PhantomNode> phantomNodeVector(rawRoute.rawViaNodeCoordinates.size());
vector<PhantomNode> phantomNodeVector(rawRoute.rawViaNodeCoordinates.size());
for(unsigned i = 0; i < rawRoute.rawViaNodeCoordinates.size(); ++i) { for(unsigned i = 0; i < rawRoute.rawViaNodeCoordinates.size(); ++i) {
if(checksumOK && i < routeParameters.hints.size() && "" != routeParameters.hints[i]) {
INFO("Decoding hint: " << routeParameters.hints[i] << " for location index " << i);
DecodeObjectFromBase64(phantomNodeVector[i], routeParameters.hints[i]);
if(phantomNodeVector[i].isValid(nodeHelpDesk->getNumberOfNodes())) {
INFO("Decoded hint " << i << " successfully");
continue;
}
}
INFO("Brute force lookup of coordinate " << i);
searchEngine->FindPhantomNodeForCoordinate( rawRoute.rawViaNodeCoordinates[i], phantomNodeVector[i]); searchEngine->FindPhantomNodeForCoordinate( rawRoute.rawViaNodeCoordinates[i], phantomNodeVector[i]);
// INFO("found coord [" << i << "|" << rawRoute.rawViaNodeCoordinates.size());
} }
unsigned distance = 0; unsigned distance = 0;
//single route or via point routing //single route or via point routing

View File

@ -38,88 +38,88 @@ namespace http {
class RequestHandler : private boost::noncopyable { class RequestHandler : private boost::noncopyable {
public: public:
explicit RequestHandler() : _pluginCount(0) { } explicit RequestHandler() : _pluginCount(0) { }
~RequestHandler() { ~RequestHandler() {
for(unsigned i = 0; i < _pluginVector.size(); i++) { for(unsigned i = 0; i < _pluginVector.size(); i++) {
BasePlugin * tempPointer = _pluginVector[i]; BasePlugin * tempPointer = _pluginVector[i];
DELETE( tempPointer ); DELETE( tempPointer );
} }
} }
void handle_request(const Request& req, Reply& rep){ void handle_request(const Request& req, Reply& rep){
//parse command //parse command
std::string request(req.uri); std::string request(req.uri);
// time_t ltime; time_t ltime;
// struct tm *Tm; struct tm *Tm;
//
// ltime=time(NULL);
// Tm=localtime(&ltime);
//
// INFO( Tm->tm_mday << "-" << (Tm->tm_mon < 10 ? "0" : "" ) << Tm->tm_mon << "-" << 1900+Tm->tm_year << " " << Tm->tm_hour << ":" << Tm->tm_min << ":" << Tm->tm_sec << " " <<
// req.endpoint.to_string() << " " << request );
std::string command;
std::size_t firstAmpPosition = request.find_first_of("?");
command = request.substr(1,firstAmpPosition-1);
// DEBUG("[debug] looking for handler for command: " << command);
try {
if(pluginMap.Holds(command)) {
RouteParameters routeParameters; ltime=time(NULL);
std::stringstream ss(( firstAmpPosition == std::string::npos ? "" : request.substr(firstAmpPosition+1) )); Tm=localtime(&ltime);
std::string item;
while(std::getline(ss, item, '&')) {
size_t found = item.find('=');
if(found == std::string::npos) {
routeParameters.parameters.push_back(item);
} else {
std::string p = item.substr(0, found);
std::transform(p.begin(), p.end(), p.begin(), (int(*)(int)) std::tolower);
std::string o = item.substr(found+1);
if("jsonp" != p)
std::transform(o.begin(), o.end(), o.begin(), (int(*)(int)) std::tolower);
if("via" == p ) {
if(25 >= routeParameters.viaPoints.size()) {
routeParameters.viaPoints.push_back(o);
}
} else if("hint" == p) {
routeParameters.hints.resize(routeParameters.viaPoints.size(), 0);
routeParameters.hints.back() = o;
} else {
routeParameters.options.Set(p, o);
}
}
}
// std::cout << "[debug] found handler for '" << command << "' at version: " << pluginMap.Find(command)->GetVersionString() << std::endl; INFO((Tm->tm_mday < 10 ? "0" : "" ) << Tm->tm_mday << "-" << (Tm->tm_mon < 10 ? "0" : "" ) << Tm->tm_mon << "-" << 1900+Tm->tm_year << " " << (Tm->tm_hour < 10 ? "0" : "" ) << Tm->tm_hour << ":" << (Tm->tm_min < 10 ? "0" : "" ) << Tm->tm_min << ":" << (Tm->tm_sec < 10 ? "0" : "" ) << Tm->tm_sec << " " <<
// std::cout << "[debug] remaining parameters: " << parameters.size() << std::endl; req.endpoint.to_string() << " " << request );
rep.status = Reply::ok; std::string command;
_pluginVector[pluginMap.Find(command)]->HandleRequest(routeParameters, rep ); std::size_t firstAmpPosition = request.find_first_of("?");
command = request.substr(1,firstAmpPosition-1);
//DEBUG("[debug] looking for handler for command: " << command);
try {
if(pluginMap.Holds(command)) {
// std::cout << rep.content << std::endl; RouteParameters routeParameters;
} else { std::stringstream ss(( firstAmpPosition == std::string::npos ? "" : request.substr(firstAmpPosition+1) ));
rep = Reply::stockReply(Reply::badRequest); std::string item;
} while(std::getline(ss, item, '&')) {
return; size_t found = item.find('=');
} catch(std::exception& e) { if(found == std::string::npos) {
rep = Reply::stockReply(Reply::internalServerError); routeParameters.parameters.push_back(item);
std::cerr << "[server error] code: " << e.what() << ", uri: " << req.uri << std::endl; } else {
return; std::string p = item.substr(0, found);
} std::transform(p.begin(), p.end(), p.begin(), (int(*)(int)) std::tolower);
}; std::string o = item.substr(found+1);
if("jsonp" != p && "hint" != p)
std::transform(o.begin(), o.end(), o.begin(), (int(*)(int)) std::tolower);
if("loc" == p) {
if(25 >= routeParameters.viaPoints.size()) {
routeParameters.viaPoints.push_back(o);
}
} else if("hint" == p) {
routeParameters.hints.resize(routeParameters.viaPoints.size());
if(routeParameters.viaPoints.size())
routeParameters.hints.back() = o;
} else {
routeParameters.options.Set(p, o);
}
}
}
// std::cout << "[debug] found handler for '" << command << "' at version: " << pluginMap.Find(command)->GetVersionString() << std::endl;
// std::cout << "[debug] remaining parameters: " << parameters.size() << std::endl;
rep.status = Reply::ok;
_pluginVector[pluginMap.Find(command)]->HandleRequest(routeParameters, rep );
void RegisterPlugin(BasePlugin * plugin) { // std::cout << rep.content << std::endl;
std::cout << "[handler] registering plugin " << plugin->GetDescriptor() << std::endl; } else {
pluginMap.Add(plugin->GetDescriptor(), _pluginCount); rep = Reply::stockReply(Reply::badRequest);
_pluginVector.push_back(plugin); }
_pluginCount++; return;
} } catch(std::exception& e) {
rep = Reply::stockReply(Reply::internalServerError);
std::cerr << "[server error] code: " << e.what() << ", uri: " << req.uri << std::endl;
return;
}
};
void RegisterPlugin(BasePlugin * plugin) {
std::cout << "[handler] registering plugin " << plugin->GetDescriptor() << std::endl;
pluginMap.Add(plugin->GetDescriptor(), _pluginCount);
_pluginVector.push_back(plugin);
_pluginCount++;
}
private: private:
HashTable<std::string, unsigned> pluginMap; HashTable<std::string, unsigned> pluginMap;
std::vector<BasePlugin *> _pluginVector; std::vector<BasePlugin *> _pluginVector;
unsigned _pluginCount; unsigned _pluginCount;
}; };
} // namespace http } // namespace http