[gnome-games] glchess: Kill engine processes on new game/exit



commit 1eb18e95bad9135b516fbf6f77f516fa1ab8b402
Author: Robert Ancell <robert ancell canonical com>
Date:   Sat Jul 14 17:13:12 2012 +1200

    glchess: Kill engine processes on new game/exit

 glchess/src/chess-engine.vala |   19 +++++++++++++------
 glchess/src/glchess.vala      |   22 +++++++++++++++++++++-
 2 files changed, 34 insertions(+), 7 deletions(-)
---
diff --git a/glchess/src/chess-engine.vala b/glchess/src/chess-engine.vala
index fe12607..0792f3b 100644
--- a/glchess/src/chess-engine.vala
+++ b/glchess/src/chess-engine.vala
@@ -35,9 +35,9 @@ public class ChessEngine : Object
         try
         {
             Process.spawn_async_with_pipes (null, argv, null,
-                                                 SpawnFlags.SEARCH_PATH,
-                                                 null,
-                                                 out pid, out stdin_fd, out stdout_fd, out stderr_fd);
+                                            SpawnFlags.SEARCH_PATH | SpawnFlags.DO_NOT_REAP_CHILD,
+                                            null,
+                                            out pid, out stdin_fd, out stdout_fd, out stderr_fd);
         }
         catch (SpawnError e)
         {
@@ -45,6 +45,8 @@ public class ChessEngine : Object
             return false;
         }
 
+        ChildWatch.add (pid, engine_stopped_cb);
+
         stdout_channel = new IOChannel.unix_new (stdout_fd);
         try
         {
@@ -60,7 +62,12 @@ public class ChessEngine : Object
 
         return true;
     }
-    
+
+    private void engine_stopped_cb (Pid pid, int status)
+    {
+        stopped ();
+    }
+
     public virtual void start_game ()
     {
     }
@@ -79,8 +86,8 @@ public class ChessEngine : Object
 
     public void stop ()
     {
-        // FIXME
-        stopped ();
+        if (pid != 0)
+            Posix.kill (pid, Posix.SIGTERM);        
     }
 
     private bool read_cb (IOChannel source, IOCondition condition)
diff --git a/glchess/src/glchess.vala b/glchess/src/glchess.vala
index 41e624f..af9f4b3 100644
--- a/glchess/src/glchess.vala
+++ b/glchess/src/glchess.vala
@@ -124,6 +124,13 @@ public class Application : Gtk.Application
         settings_changed_cb (settings, "show-3d");
     }
 
+    protected override void shutdown ()
+    {
+        base.shutdown ();
+        if (opponent_engine != null)
+            opponent_engine.stop ();
+    }
+
     public void quit_game ()
     {
         if (save_duration_timeout != 0)
@@ -298,7 +305,14 @@ public class Application : Gtk.Application
             black_level = "normal";
 
         opponent = null;
-        opponent_engine = null;
+        if (opponent_engine != null)
+        {
+            opponent_engine.stop ();
+            opponent_engine.ready_changed.disconnect (engine_ready_cb);
+            opponent_engine.moved.disconnect (engine_move_cb);
+            opponent_engine.stopped.disconnect (engine_stopped_cb);
+            opponent_engine = null;
+        }
         if (white_engine != null)
         {
             opponent = game.white;
@@ -316,6 +330,7 @@ public class Application : Gtk.Application
         {
             opponent_engine.ready_changed.connect (engine_ready_cb);
             opponent_engine.moved.connect (engine_move_cb);
+            opponent_engine.stopped.connect (engine_stopped_cb);
             opponent_engine.start ();
         }
 
@@ -436,6 +451,11 @@ public class Application : Gtk.Application
         opponent.move (move);
     }
 
+    private void engine_stopped_cb (ChessEngine engine)
+    {
+        opponent.resign ();
+    }
+
     private void game_start_cb (ChessGame game)
     {
         if (opponent_engine != null)



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]