]> git.tdb.fi Git - r2c2.git/blobdiff - source/libr2c2/trainrouteplanner.cpp
Abort route planning if it takes too long
[r2c2.git] / source / libr2c2 / trainrouteplanner.cpp
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;