]> git.tdb.fi Git - r2c2.git/commitdiff
Abort route planning if it takes too long
authorMikko Rasa <tdb@tdb.fi>
Fri, 13 Feb 2015 00:47:01 +0000 (02:47 +0200)
committerMikko Rasa <tdb@tdb.fi>
Fri, 13 Feb 2015 00:47:01 +0000 (02:47 +0200)
source/libr2c2/trainrouteplanner.cpp
source/libr2c2/trainrouteplanner.h

index ca85ac74084dace04e65d24d48cec278b9c67350..90d769140725fdc00a708a8c55318f6fb2ea12b2 100644 (file)
@@ -1,4 +1,5 @@
 #include <msp/core/maputils.h>
+#include <msp/time/utils.h>
 #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;
index 9a9978137cfeb58caa9164e66f41ab3b2ee1c5cd..c7572c0df41cb48c548fa2c4b0cb40f6afdd4453 100644 (file)
@@ -137,6 +137,7 @@ private:
        std::list<RoutingStep> steps;
        std::list<RoutingStep> 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();