From: Mikko Rasa Date: Fri, 13 Feb 2015 00:47:01 +0000 (+0200) Subject: Abort route planning if it takes too long X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=8776713b20fdc7cdd09f7e2363679ebd06665d32;p=r2c2.git Abort route planning if it takes too long --- diff --git a/source/libr2c2/trainrouteplanner.cpp b/source/libr2c2/trainrouteplanner.cpp index ca85ac7..90d7691 100644 --- a/source/libr2c2/trainrouteplanner.cpp +++ b/source/libr2c2/trainrouteplanner.cpp @@ -1,4 +1,5 @@ #include +#include #include "catalogue.h" #include "layout.h" #include "route.h" @@ -15,6 +16,7 @@ namespace R2C2 { TrainRoutePlanner::TrainRoutePlanner(Layout &layout): goal(0), + timeout(10*Time::sec), result(PENDING), thread(0) { @@ -36,6 +38,11 @@ TrainRoutePlanner::~TrainRoutePlanner() } } +void TrainRoutePlanner::set_timeout(const Time::TimeDelta &t) +{ + timeout = t; +} + TrainRoutePlanner::Result TrainRoutePlanner::plan() { prepare_plan(); @@ -112,6 +119,8 @@ void TrainRoutePlanner::prepare_plan() void TrainRoutePlanner::create_plan() { + Time::TimeStamp timeout_stamp = Time::now()+timeout; + unsigned count = 0; while(!queue.empty()) { const RoutingStep &step = get_step(); @@ -122,6 +131,13 @@ void TrainRoutePlanner::create_plan() } add_steps(step); + + if(++count>=1000) + { + if(Time::now()>timeout_stamp) + break; + count = 0; + } } result = FAILED; diff --git a/source/libr2c2/trainrouteplanner.h b/source/libr2c2/trainrouteplanner.h index 9a99781..c7572c0 100644 --- a/source/libr2c2/trainrouteplanner.h +++ b/source/libr2c2/trainrouteplanner.h @@ -137,6 +137,7 @@ private: std::list steps; std::list queue; const RoutingStep *goal; + Msp::Time::TimeDelta timeout; Result result; PlanningThread *thread; @@ -144,6 +145,7 @@ public: TrainRoutePlanner(Layout &); ~TrainRoutePlanner(); + void set_timeout(const Msp::Time::TimeDelta &); Result plan(); void plan_async(); Result check();