]> git.tdb.fi Git - r2c2.git/commitdiff
Move block reservation signal serialization to Layout
authorMikko Rasa <tdb@tdb.fi>
Mon, 9 Feb 2015 17:27:38 +0000 (19:27 +0200)
committerMikko Rasa <tdb@tdb.fi>
Mon, 9 Feb 2015 17:27:38 +0000 (19:27 +0200)
It needs to be serialized between different blocks as well, since
TrainRouter at least expects to receive the signals in order.

source/libr2c2/block.cpp
source/libr2c2/block.h
source/libr2c2/layout.cpp
source/libr2c2/layout.h

index 25b3bc8b85163d0bfd2ece6a54718f02b840ec6d..289c4e54f6bd14de4107f1e4246144e8438c168b 100644 (file)
@@ -21,9 +21,7 @@ Block::Block(Layout &l, Track &start):
        turnout_addr(start.get_turnout_address()),
        conflict(false),
        sensor(0),
-       train(0),
-       pending_train(0),
-       emitting_reserve(false)
+       train(0)
 {
        add_track(start);
 
@@ -170,19 +168,10 @@ Block *Block::get_link(unsigned epi) const
 
 bool Block::reserve(Train *t)
 {
-       if(!t || !(emitting_reserve ? pending_train : train))
+       if(!t || !train)
        {
-               pending_train = t;
-               if(!emitting_reserve)
-               {
-                       while(pending_train!=train)
-                       {
-                               train = pending_train;
-                               SetFlag setf(emitting_reserve);
-                               signal_reserved.emit(train);
-                       }
-               }
-
+               train = t;
+               signal_reserved.emit(t);
                return true;
        }
        else
index 6823b2730343faa69c03b14b0f507701ad26b85d..f255e43f2e2421fb18b81bd9c0a6e862e0218a63 100644 (file)
@@ -38,8 +38,6 @@ private:
        TrackCircuit *sensor;
        std::vector<Endpoint> endpoints;
        Train *train;
-       Train *pending_train;
-       bool emitting_reserve;
 
 public:
        Block(Layout &, Track &);
index af7304da6aeef557f3f4f8119d08cc4aaca1ace4..72f083d57cc2da491dead13077ad495b716f5942 100644 (file)
@@ -1,5 +1,6 @@
 #include <algorithm>
 #include <msp/core/maputils.h>
+#include <msp/core/raii.h>
 #include <msp/core/refptr.h>
 #include <msp/datafile/parser.h>
 #include <msp/datafile/writer.h>
@@ -40,7 +41,8 @@ namespace R2C2 {
 Layout::Layout(Catalogue &c, Driver *d):
        catalogue(c),
        driver(d),
-       next_turnout_addr(0x800)
+       next_turnout_addr(0x800),
+       emitting_block_reserved(false)
 {
        clock.set_rate(60);
 }
@@ -121,7 +123,7 @@ void Layout::add(Block &b)
 {
        if(track_chains.insert(b))
        {
-               sigc::connection conn = b.signal_reserved.connect(sigc::bind<0>(signal_block_reserved, sigc::ref(b)));
+               sigc::connection conn = b.signal_reserved.connect(sigc::bind<0>(sigc::mem_fun(this, &Layout::block_reserved), sigc::ref(b)));
                try
                {
                        signal_track_chain_added.emit(b);
@@ -546,6 +548,23 @@ void Layout::sensor_state_changed(Sensor &sensor, Sensor::State state)
        }
 }
 
+void Layout::block_reserved(Block &block, Train *train)
+{
+       if(emitting_block_reserved)
+               block_reserve_queue.push_back(BlockReservation(block, train));
+       else
+       {
+               SetFlag setf(emitting_block_reserved);
+               signal_block_reserved.emit(block, train);
+               while(!block_reserve_queue.empty())
+               {
+                       BlockReservation br = block_reserve_queue.front();
+                       block_reserve_queue.pop_front();
+                       signal_block_reserved.emit(br.block, br.train);
+               }
+       }
+}
+
 
 template<typename B>
 Layout::Storage<B>::~Storage()
@@ -600,6 +619,12 @@ void Layout::Storage<B>::del()
 }
 
 
+Layout::BlockReservation::BlockReservation(Block &b, Train *t):
+       block(b),
+       train(t)
+{ }
+
+
 Layout::Loader::Loader(Layout &l):
        DataFile::ObjectLoader<Layout>(l)
 {
index 887b2d189f453dcc2dab13e59316f0642ce54df5..5a0804fc77329b1c8714b3ce2d27112d1ff14b13 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef LIBR2C2_LAYOUT_H_
 #define LIBR2C2_LAYOUT_H_
 
+#include <deque>
 #include <set>
 #include <sigc++/sigc++.h>
 #include <msp/datafile/objectloader.h>
@@ -67,6 +68,14 @@ private:
                void del();
        };
 
+       struct BlockReservation
+       {
+               Block &block;
+               Train *train;
+
+               BlockReservation(Block &, Train *);
+       };
+
 public:
        typedef std::vector<Zone *> ZoneArray;
 
@@ -91,6 +100,8 @@ private:
        std::map<unsigned, Train *> trains;
        Msp::Time::TimeStamp last_tick;
        unsigned next_turnout_addr;
+       bool emitting_block_reserved;
+       std::deque<BlockReservation> block_reserve_queue;
 
 public:
        Layout(Catalogue &, Driver * = 0);
@@ -145,6 +156,7 @@ public:
        void save_dynamic(const std::string &) const;
 private:
        void sensor_state_changed(Sensor &, Sensor::State);
+       void block_reserved(Block &, Train *);
 };
 
 } // namespace R2C2