From: Mikko Rasa Date: Mon, 9 Feb 2015 17:27:38 +0000 (+0200) Subject: Move block reservation signal serialization to Layout X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=399b48a7a80dafde49e7530ff8dfd2820232a73f;p=r2c2.git Move block reservation signal serialization to Layout It needs to be serialized between different blocks as well, since TrainRouter at least expects to receive the signals in order. --- diff --git a/source/libr2c2/block.cpp b/source/libr2c2/block.cpp index 25b3bc8..289c4e5 100644 --- a/source/libr2c2/block.cpp +++ b/source/libr2c2/block.cpp @@ -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 diff --git a/source/libr2c2/block.h b/source/libr2c2/block.h index 6823b27..f255e43 100644 --- a/source/libr2c2/block.h +++ b/source/libr2c2/block.h @@ -38,8 +38,6 @@ private: TrackCircuit *sensor; std::vector endpoints; Train *train; - Train *pending_train; - bool emitting_reserve; public: Block(Layout &, Track &); diff --git a/source/libr2c2/layout.cpp b/source/libr2c2/layout.cpp index af7304d..72f083d 100644 --- a/source/libr2c2/layout.cpp +++ b/source/libr2c2/layout.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -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 Layout::Storage::~Storage() @@ -600,6 +619,12 @@ void Layout::Storage::del() } +Layout::BlockReservation::BlockReservation(Block &b, Train *t): + block(b), + train(t) +{ } + + Layout::Loader::Loader(Layout &l): DataFile::ObjectLoader(l) { diff --git a/source/libr2c2/layout.h b/source/libr2c2/layout.h index 887b2d1..5a0804f 100644 --- a/source/libr2c2/layout.h +++ b/source/libr2c2/layout.h @@ -1,6 +1,7 @@ #ifndef LIBR2C2_LAYOUT_H_ #define LIBR2C2_LAYOUT_H_ +#include #include #include #include @@ -67,6 +68,14 @@ private: void del(); }; + struct BlockReservation + { + Block █ + Train *train; + + BlockReservation(Block &, Train *); + }; + public: typedef std::vector ZoneArray; @@ -91,6 +100,8 @@ private: std::map trains; Msp::Time::TimeStamp last_tick; unsigned next_turnout_addr; + bool emitting_block_reserved; + std::deque 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