]> git.tdb.fi Git - r2c2.git/commitdiff
Stop all trains when Engineer exits
authorMikko Rasa <tdb@tdb.fi>
Sun, 27 Dec 2009 07:51:03 +0000 (07:51 +0000)
committerMikko Rasa <tdb@tdb.fi>
Sun, 27 Dec 2009 07:51:03 +0000 (07:51 +0000)
Handle some abnormal termination conditions as well

source/engineer/engineer.cpp
source/engineer/engineer.h
source/engineer/mainpanel.h
source/engineer/trainpanel.h
source/libmarklin/control.cpp
source/libmarklin/control.h

index 308e5b2d4db0e80437a790ac7cbd1bf8ae38075c..6a35a06a3317992dae0075dece5eecfe72e43ddd 100644 (file)
@@ -7,6 +7,7 @@ Distributed under the GPL
 
 #include <cmath>
 #include <limits>
+#include <signal.h>
 #include <GL/gl.h>
 #include <msp/core/except.h>
 #include <msp/core/getopt.h>
@@ -17,6 +18,7 @@ Distributed under the GPL
 #include <msp/gl/matrix.h>
 #include <msp/gl/projection.h>
 #include <msp/gl/transform.h>
+#include <msp/io/print.h>
 #include <msp/strings/formatter.h>
 #include <msp/strings/lexicalcast.h>
 #include <msp/strings/regex.h>
@@ -105,10 +107,24 @@ Engineer::Engineer(int argc, char **argv):
                i->second->signal_state_changed.connect(sigc::bind(sigc::mem_fun(this, &Engineer::sensor_event), i->second));
 
        view_all();
+
+       catch_signal(SIGINT);
+       catch_signal(SIGTERM);
+       catch_signal(SIGSEGV);
+       catch_signal(SIGILL);
+       catch_signal(SIGFPE);
+       catch_signal(SIGABRT);
 }
 
 Engineer::~Engineer()
 {
+       const list<Train *> &trains = trfc_mgr->get_trains();
+       for(list<Train *>::const_iterator i=trains.begin(); i!=trains.end(); ++i)
+               (*i)->set_speed(0);
+
+       while(control.get_queue_length())
+               control.tick();
+
        if(!simulate)
                trfc_mgr->save("engineer.state");
        delete trfc_mgr;
@@ -501,4 +517,20 @@ void Engineer::train_added(Train &train)
        place_train(train);
 }
 
+void Engineer::sighandler(int sig)
+{
+       if(sig==SIGSEGV || sig==SIGILL || sig==SIGFPE || sig==SIGABRT)
+       {
+               signal(sig, SIG_DFL);
+               IO::print(IO::cerr, "Fatal signal received, terminating\n");
+               const map<unsigned, Locomotive *> &locos = control.get_locomotives();
+               for(map<unsigned, Locomotive *>::const_iterator i=locos.begin(); i!=locos.end(); ++i)
+                       i->second->set_speed(0);
+               control.flush();
+               raise(sig);
+       }
+       else if(sig==SIGTERM || sig==SIGINT)
+               exit(0);
+}
+
 Application::RegApp<Engineer> Engineer::reg;
index 8e8e0216b21ef3a866c0d25b73a42e97e20e228e..31952ba1f11a9f61dccb9033b2d5be82c4e9ddb5 100644 (file)
@@ -83,6 +83,7 @@ private:
        void project_3d();
        Marklin::Track3D *pick_track(int, int);
        void train_added(Marklin::Train &);
+       virtual void sighandler(int);
 
        static Msp::Application::RegApp<Engineer> reg;
 };
index 56dd49450a6e9eb0ffb754e43cc86864ac4f6328..017617dafff437f9fdec9281b7337a10a5d2b7c8 100644 (file)
@@ -8,13 +8,14 @@ Distributed under the GPL
 #ifndef MAINPANEL_H_
 #define MAINPANEL_H_
 
+#include <sigc++/trackable.h>
 #include <msp/gltk/indicator.h>
 #include <msp/gltk/label.h>
 #include <msp/gltk/panel.h>
 
 class Engineer;
 
-class MainPanel: public Msp::GLtk::Panel
+class MainPanel: public Msp::GLtk::Panel, public sigc::trackable
 {
 private:
        Engineer &engineer;
index a76d907a1af33bcb994e365dafbc8a6e8e411b14..7b929856b840e6ff04e6eede5d338986f91f0332 100644 (file)
@@ -8,6 +8,7 @@ Distributed under the GPL
 #ifndef TRAINPANEL_H_
 #define TRAINPANEL_H_
 
+#include <sigc++/trackable.h>
 #include <msp/gltk/hslider.h>
 #include <msp/gltk/label.h>
 #include <msp/gltk/panel.h>
@@ -17,7 +18,7 @@ Distributed under the GPL
 
 class Engineer;
 
-class TrainPanel: public Msp::GLtk::Panel
+class TrainPanel: public Msp::GLtk::Panel, public sigc::trackable
 {
 private:
        Engineer &engineer;
index fa1117ccd0fa319dd6c15bf5c735092e8fe27ada..ae26a179a9aa2f4b51585584b4d289ff6e59d4cc 100644 (file)
@@ -123,6 +123,12 @@ Command &Control::command(Cmd cmd, const unsigned char *data, unsigned len)
        return queue.back();
 }
 
+void Control::flush()
+{
+       for(list<Command>::iterator i=queue.begin(); i!=queue.end(); ++i)
+               i->send(serial_fd);
+}
+
 void Control::add_turnout(Turnout &t)
 {
        turnouts[t.get_address()] = &t;
index 61469a8ea8c289f75316943e1404fa690a2c0d65..a041e957caaf1b009ee216b43ef34e1794cf52e7 100644 (file)
@@ -53,12 +53,14 @@ public:
        Command &command(Cmd, unsigned char);
        Command &command(Cmd, const unsigned char *, unsigned);
        unsigned get_queue_length() const { return queue.size(); }
+       void flush();
 
        void add_turnout(Turnout &);
        Turnout &get_turnout(unsigned) const;
        const std::map<unsigned, Turnout *> &get_turnouts() const { return turnouts; }
        void add_locomotive(Locomotive &);
        Locomotive &get_locomotive(unsigned) const;
+       const std::map<unsigned, Locomotive *> &get_locomotives() const { return locomotives; }
        void add_sensor(Sensor &);
        Sensor &get_sensor(unsigned) const;
        const std::map<unsigned, Sensor *> &get_sensors() const { return sensors; }