added a reset() method to Dumm and each client
authorMartin Willi <martin@strongswan.org>
Tue, 7 Apr 2009 14:22:02 +0000 (14:22 -0000)
committerMartin Willi <martin@strongswan.org>
Tue, 7 Apr 2009 14:22:02 +0000 (14:22 -0000)
identation cleanups

src/dumm/ext/dumm.c
src/dumm/ext/lib/dumm.rb

index adb89d6..222c57c 100644 (file)
@@ -24,6 +24,7 @@
 #include <library.h>
 #include <dumm.h>
 #include <debug.h>
+#include <utils/linked_list.h>
 
 #undef PACKAGE_NAME
 #undef PACKAGE_TARNAME
@@ -84,8 +85,6 @@ static void sigchld_handler(int signal, siginfo_t *info, void* ptr)
        enumerator->destroy(enumerator);
 }
 
-
-
 /**
  * Guest bindings
  */
@@ -127,15 +126,15 @@ static VALUE guest_each(int argc, VALUE *argv, VALUE class)
 {
        enumerator_t *enumerator;
        guest_t *guest;
-
+       
        if (!rb_block_given_p())
-    {
+       {
                rb_raise(rb_eArgError, "must be called with a block");
        }
        enumerator = dumm->create_guest_enumerator(dumm);
        while (enumerator->enumerate(enumerator, &guest))
        {
-               rb_yield(Data_Wrap_Struct(class, NULL, NULL, guest));
+               rb_yield(Data_Wrap_Struct(class, NULL, NULL, guest));
        }
        enumerator->destroy(enumerator);
        return class;
@@ -285,26 +284,57 @@ static VALUE guest_each_iface(int argc, VALUE *argv, VALUE self)
        enumerator_t *enumerator;
        guest_t *guest;
        iface_t *iface;
-
+       
        if (!rb_block_given_p())
-    {
+       {
                rb_raise(rb_eArgError, "must be called with a block");
        }
        Data_Get_Struct(self, guest_t, guest);
        enumerator = guest->create_iface_enumerator(guest);
        while (enumerator->enumerate(enumerator, &iface))
        {
-               rb_yield(Data_Wrap_Struct(rbc_iface, NULL, NULL, iface));
+               rb_yield(Data_Wrap_Struct(rbc_iface, NULL, NULL, iface));
        }
        enumerator->destroy(enumerator);
        return self;
 }
 
+static VALUE guest_reset(VALUE self)
+{
+       linked_list_t *list;
+       enumerator_t *enumerator;
+       guest_t *guest;
+       iface_t *iface;
+       
+       Data_Get_Struct(self, guest_t, guest);
+       if (!guest->get_pid(guest))
+       {
+               rb_raise(rb_eRuntimeError, "guest is not running");
+       }
+       list = linked_list_create();
+       enumerator = guest->create_iface_enumerator(guest);
+       while (enumerator->enumerate(enumerator, &iface))
+       {
+               list->insert_last(list, iface);
+       }
+       enumerator->destroy(enumerator);
+       while (list->remove_last(list, (void**)&iface) == SUCCESS)
+       {
+               guest->destroy_iface(guest, iface);
+       }
+       list->destroy(list);
+       return self;
+}
+
 static VALUE guest_delete(VALUE self)
 {
        guest_t *guest;
        
        Data_Get_Struct(self, guest_t, guest);
+       if (guest->get_pid(guest))
+       {
+               rb_raise(rb_eRuntimeError, "guest is running");
+       }
        dumm->delete_guest(dumm, guest);
        return Qnil;
 }
@@ -332,6 +362,7 @@ static void guest_init()
        rb_define_method(rbc_guest, "each", guest_each_iface, -1);
        rb_define_method(rbc_guest, "include?", guest_find_iface, 1);
        rb_define_method(rbc_guest, "iface?", guest_find_iface, 1);
+       rb_define_method(rbc_guest, "reset", guest_reset, 0);
        rb_define_method(rbc_guest, "delete", guest_delete, 0);
 }
 
@@ -364,15 +395,15 @@ static VALUE bridge_each(int argc, VALUE *argv, VALUE class)
 {
        enumerator_t *enumerator;
        bridge_t *bridge;
-
+       
        if (!rb_block_given_p())
-    {
+       {
                rb_raise(rb_eArgError, "must be called with a block");
        }
        enumerator = dumm->create_bridge_enumerator(dumm);
        while (enumerator->enumerate(enumerator, &bridge))
        {
-               rb_yield(Data_Wrap_Struct(class, NULL, NULL, bridge));
+               rb_yield(Data_Wrap_Struct(class, NULL, NULL, bridge));
        }
        enumerator->destroy(enumerator);
        return class;
@@ -404,16 +435,16 @@ static VALUE bridge_each_iface(int argc, VALUE *argv, VALUE self)
        enumerator_t *enumerator;
        bridge_t *bridge;
        iface_t *iface;
-
+       
        if (!rb_block_given_p())
-    {
+       {
                rb_raise(rb_eArgError, "must be called with a block");
        }
        Data_Get_Struct(self, bridge_t, bridge);
        enumerator = bridge->create_iface_enumerator(bridge);
        while (enumerator->enumerate(enumerator, &iface))
        {
-               rb_yield(Data_Wrap_Struct(rbc_iface, NULL, NULL, iface));
+               rb_yield(Data_Wrap_Struct(rbc_iface, NULL, NULL, iface));
        }
        enumerator->destroy(enumerator);
        return self;
@@ -512,9 +543,9 @@ static VALUE iface_each_addr(int argc, VALUE *argv, VALUE self)
        iface_t *iface;
        host_t *addr;
        char buf[64];
-
+       
        if (!rb_block_given_p())
-    {
+       {
                rb_raise(rb_eArgError, "must be called with a block");
        }
        Data_Get_Struct(self, iface_t, iface);
@@ -522,7 +553,7 @@ static VALUE iface_each_addr(int argc, VALUE *argv, VALUE self)
        while (enumerator->enumerate(enumerator, &addr))
        {
                snprintf(buf, sizeof(buf), "%H", addr);
-               rb_yield(rb_str_new2(buf));
+               rb_yield(rb_str_new2(buf));
        }
        enumerator->destroy(enumerator);
        return self;
index 719af76..f7d72f5 100644 (file)
 require 'dumm.so'
 require 'dumm/guest'
 
+module Dumm
+  # unload templates, reset all guests and delete bridges
+  def reset
+    Template.unload
+    Guest.each { |guest|
+      guest.reset if guest.running?
+    }
+    Bridge.each { |bridge|
+      bridge.delete
+    }
+  end
+end
+
 # vim:sw=2 ts=2 et