[rygel/wip/basic-management: 47/49] core: Use Rygel.StateMachine in BasicManagementTest



commit 394d3f99b55602e4fcc2e3a0303249a6a0b4b07d
Author: Jussi Kukkonen <jussi kukkonen intel com>
Date:   Tue Oct 8 16:00:52 2013 +0300

    core: Use Rygel.StateMachine in BasicManagementTest

 src/librygel-core/rygel-basic-management-test.vala |   44 +++++++++++---------
 src/librygel-core/rygel-basic-management.vala      |   24 +++--------
 2 files changed, 30 insertions(+), 38 deletions(-)
---
diff --git a/src/librygel-core/rygel-basic-management-test.vala 
b/src/librygel-core/rygel-basic-management-test.vala
index 15020e4..c0f6bcd 100644
--- a/src/librygel-core/rygel-basic-management-test.vala
+++ b/src/librygel-core/rygel-basic-management-test.vala
@@ -23,11 +23,23 @@
 
 using GLib;
 
-internal errordomain Rygel.BasicManagementTestError {
-    NOT_POSSIBLE
-}
+internal abstract class Rygel.BasicManagementTest : Object, StateMachine {
+    protected Cancellable _cancellable;
+    public Cancellable cancellable {
+        get {
+            return this._cancellable;
+        }
+        set {
+            this._cancellable = value;
+            this._cancellable.cancelled.connect (() => {
+                if (this.execution_state == ExecutionState.IN_PROGRESS) {
+                    Posix.killpg (this.child_pid, Posix.SIGTERM);
+                    this.execution_state = ExecutionState.CANCELED;
+                }
+            });
+        }
+    }
 
-internal abstract class Rygel.BasicManagementTest : Object {
     protected enum InitState {
         OK,
         SPAWN_FAILED,
@@ -82,7 +94,7 @@ internal abstract class Rygel.BasicManagementTest : Object {
     private SourceFunc async_callback;
     private uint current_iteration;
 
-    /* These virtual/abstract functions will be called from execute():
+    /* These virtual/abstract functions will be called from run ():
      * - For every iteration:
      *    - init_iteration()
      *    - calls to handle_output() and handle_error(),
@@ -221,29 +233,21 @@ internal abstract class Rygel.BasicManagementTest : Object {
                this.execution_state == ExecutionState.IN_PROGRESS;
     }
 
-    public async virtual void execute () throws BasicManagementTestError {
+    public async virtual void run () {
         if (this.execution_state != ExecutionState.REQUESTED) {
-            throw new BasicManagementTestError.NOT_POSSIBLE
-                                        ("Already executing or executed");
+            warning ("Test already started");
+            return;
         }
-
+        if (this.cancellable == null)
+            this.cancellable = new Cancellable ();
         this.execution_state = ExecutionState.IN_PROGRESS;
         this.current_iteration = 0;
-        this.async_callback = execute.callback;
+        this.async_callback = run.callback;
 
         this.run_iteration ();
         yield;
 
+        this.completed ();
         return;
     }
-
-    public void cancel () throws BasicManagementTestError {
-        if (this.execution_state != ExecutionState.IN_PROGRESS) {
-            throw new BasicManagementTestError.NOT_POSSIBLE ("Not executing");
-        }
-
-        Posix.killpg (this.child_pid, Posix.SIGTERM);
-
-        this.execution_state = ExecutionState.CANCELED;
-    }
 }
diff --git a/src/librygel-core/rygel-basic-management.vala b/src/librygel-core/rygel-basic-management.vala
index 4af18ce..3916aac 100644
--- a/src/librygel-core/rygel-basic-management.vala
+++ b/src/librygel-core/rygel-basic-management.vala
@@ -113,11 +113,7 @@ public class Rygel.BasicManagement : Service {
         if (type_test_ids.size > this.max_history_size) {
             var old_id = type_test_ids.poll_head ();
 
-            try {
-                this.tests_map[old_id].cancel ();
-            } catch (BasicManagementTestError e) {
-                /* test was not running, not a problem */
-            }
+            this.tests_map[old_id].cancellable.cancel ();
             this.tests_map.unset (old_id);
         }
 
@@ -136,13 +132,8 @@ public class Rygel.BasicManagement : Service {
         /* NOTE: it might be useful queue the execution but this is not
          * currently done: if "BandwidthTest" is implemented queueing is
          * practically required. */
-        bm_test.execute.begin ((obj,res) => {
-            try {
-                bm_test.execute.end (res);
-            } catch (BasicManagementTestError e) {
-                /* already executing */
-            }
-
+        bm_test.run.begin ((obj,res) => {
+            bm_test.run.end (res);
             this.notify ("ActiveTestIDs",
                          typeof (string),
                          create_test_ids_list (true));
@@ -537,13 +528,10 @@ public class Rygel.BasicManagement : Service {
             return;
         }
 
-        try {
-            bm_test.cancel ();
-        } catch (BasicManagementTestError e) {
-            warning ("Canceled test was not running\n");
-        }
+        bm_test.cancellable.cancel ();
+
         /* ActiveTestIDs notification is handled by
-         * the tests' execute callback */
+         * the tests' run callback */
 
         action.return ();
     }


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