* guest#running?

* guest?, iface? (also Guest.include? resp. guest.include?)
 * easy accessors for guests and ifaces (Guest.sun instead of Guest["sun"] and guest.eth0 instead of guest["eth0"])
 * if a block is given for iface#add or iface#del then the change is only temporary while executing the block and gets reverted afterwards
This commit is contained in:
Tobias Brunner
2008-08-27 07:35:20 +00:00
parent 9f9d6ece39
commit ca4f63383c
5 changed files with 139 additions and 12 deletions
+2 -1
View File
@@ -1,4 +1,5 @@
EXTRA_DIST = ext/dumm.c ext/extconf.rb ext/README
EXTRA_DIST = ext/dumm.c ext/extconf.rb ext/README \
ext/lib/dumm.rb ext/lib/dumm/guest.rb
lib_LTLIBRARIES = libdumm.la
ipsec_PROGRAMS = dumm irdumm
+75 -11
View File
@@ -87,14 +87,18 @@ static void sigchld_handler(int signal, siginfo_t *info, void* ptr)
enumerator->destroy(enumerator);
}
/**
* Guest bindings
*/
static VALUE guest_get(VALUE class, VALUE key)
static VALUE guest_find(VALUE class, VALUE key)
{
enumerator_t *enumerator;
guest_t *guest, *found = NULL;
if (TYPE(key) == T_SYMBOL) {
key = rb_convert_type(key, T_STRING, "String", "to_s");
}
enumerator = dumm->create_guest_enumerator(dumm);
while (enumerator->enumerate(enumerator, &guest))
{
@@ -107,11 +111,26 @@ static VALUE guest_get(VALUE class, VALUE key)
enumerator->destroy(enumerator);
if (!found)
{
rb_raise(rb_eRuntimeError, "guest not found");
return Qnil;
}
return Data_Wrap_Struct(class, NULL, NULL, found);
}
static VALUE guest_get(VALUE class, VALUE key)
{
VALUE guest = guest_find(class, key);
if (NIL_P(guest))
{
rb_raise(rb_eRuntimeError, "guest not found");
}
return guest;
}
static VALUE guest_exist(VALUE class, VALUE key)
{
return NIL_P(guest_find(class, key)) ? Qfalse : Qtrue;
}
static VALUE guest_each(int argc, VALUE *argv, VALUE class)
{
enumerator_t *enumerator;
@@ -174,6 +193,14 @@ static VALUE guest_stop(VALUE self)
return self;
}
static VALUE guest_running(VALUE self)
{
guest_t *guest;
Data_Get_Struct(self, guest_t, guest);
return guest->get_pid(guest) ? Qtrue : Qfalse;
}
static void exec_cb(void *data, char *buf)
{
rb_yield(rb_str_new2(buf));
@@ -209,12 +236,14 @@ static VALUE guest_add_iface(VALUE self, VALUE name)
return Data_Wrap_Struct(rbc_iface, NULL, NULL, iface);
}
static VALUE guest_get_iface(VALUE self, VALUE key)
static VALUE guest_find_iface(VALUE self, VALUE key)
{
enumerator_t *enumerator;
iface_t *iface, *found = NULL;
guest_t *guest;
if (TYPE(key) == T_SYMBOL) {
key = rb_convert_type(key, T_STRING, "String", "to_s");
}
Data_Get_Struct(self, guest_t, guest);
enumerator = guest->create_iface_enumerator(guest);
while (enumerator->enumerate(enumerator, &iface))
@@ -228,11 +257,26 @@ static VALUE guest_get_iface(VALUE self, VALUE key)
enumerator->destroy(enumerator);
if (!found)
{
rb_raise(rb_eRuntimeError, "interface not found");
return Qnil;
}
return Data_Wrap_Struct(rbc_iface, NULL, NULL, iface);
}
static VALUE guest_get_iface(VALUE self, VALUE key)
{
VALUE iface = guest_find_iface(self, key);
if (NIL_P(iface))
{
rb_raise(rb_eRuntimeError, "interface not found");
}
return iface;
}
static VALUE guest_exist_iface(VALUE self, VALUE key)
{
return NIL_P(guest_find_iface(self, key)) ? Qfalse : Qtrue;
}
static VALUE guest_each_iface(int argc, VALUE *argv, VALUE self)
{
enumerator_t *enumerator;
@@ -265,19 +309,26 @@ static VALUE guest_delete(VALUE self)
static void guest_init()
{
rbc_guest = rb_define_class_under(rbm_dumm , "Guest", rb_cObject);
rb_include_module(rb_class_of(rbc_guest), rb_mEnumerable);
rb_include_module(rbc_guest, rb_mEnumerable);
rb_define_singleton_method(rbc_guest, "[]", guest_get, 1);
rb_define_singleton_method(rbc_guest, "each", guest_each, -1);
rb_define_singleton_method(rbc_guest, "new", guest_new, 4);
rb_define_singleton_method(rbc_guest, "include?", guest_exist, 1);
rb_define_singleton_method(rbc_guest, "guest?", guest_exist, 1);
rb_define_method(rbc_guest, "to_s", guest_to_s, 0);
rb_define_method(rbc_guest, "start", guest_start, 0);
rb_define_method(rbc_guest, "stop", guest_stop, 0);
rb_define_method(rbc_guest, "running?", guest_running, 0);
rb_define_method(rbc_guest, "exec", guest_exec, 1);
rb_define_method(rbc_guest, "add", guest_add_iface, 1);
rb_define_method(rbc_guest, "[]", guest_get_iface, 1);
rb_define_method(rbc_guest, "each", guest_each_iface, -1);
rb_define_method(rbc_guest, "include?", guest_exist_iface, 1);
rb_define_method(rbc_guest, "iface?", guest_exist_iface, 1);
rb_define_method(rbc_guest, "delete", guest_delete, 0);
rb_include_module(rb_class_of(rbc_guest), rb_mEnumerable);
rb_include_module(rbc_guest, rb_mEnumerable);
}
/**
@@ -376,14 +427,16 @@ static VALUE bridge_delete(VALUE self)
static void bridge_init()
{
rbc_bridge = rb_define_class_under(rbm_dumm , "Bridge", rb_cObject);
rb_include_module(rb_class_of(rbc_bridge), rb_mEnumerable);
rb_include_module(rbc_bridge, rb_mEnumerable);
rb_define_singleton_method(rbc_bridge, "[]", bridge_get, 1);
rb_define_singleton_method(rbc_bridge, "each", bridge_each, -1);
rb_define_singleton_method(rbc_bridge, "new", bridge_new, 1);
rb_define_method(rbc_bridge, "to_s", bridge_to_s, 0);
rb_define_method(rbc_bridge, "each", bridge_each_iface, -1);
rb_define_method(rbc_bridge, "delete", bridge_delete, 0);
rb_include_module(rb_class_of(rbc_bridge), rb_mEnumerable);
rb_include_module(rbc_bridge, rb_mEnumerable);
}
/**
@@ -438,8 +491,13 @@ static VALUE iface_add_addr(VALUE self, VALUE name)
Data_Get_Struct(self, iface_t, iface);
if (!iface->add_address(iface, addr))
{
addr->destroy(addr);
rb_raise(rb_eRuntimeError, "adding address failed");
}
if (rb_block_given_p()) {
rb_yield(self);
iface->delete_address(iface, addr);
}
addr->destroy(addr);
return self;
}
@@ -482,6 +540,10 @@ static VALUE iface_del_addr(VALUE self, VALUE vaddr)
addr->destroy(addr);
rb_raise(rb_eRuntimeError, "address not found");
}
if (rb_block_given_p()) {
rb_yield(self);
iface->add_address(iface, addr);
}
addr->destroy(addr);
return self;
}
@@ -500,6 +562,8 @@ static VALUE iface_delete(VALUE self)
static void iface_init()
{
rbc_iface = rb_define_class_under(rbm_dumm , "Iface", rb_cObject);
rb_include_module(rbc_iface, rb_mEnumerable);
rb_define_method(rbc_iface, "to_s", iface_to_s, 0);
rb_define_method(rbc_iface, "connect", iface_connect, 1);
rb_define_method(rbc_iface, "disconnect", iface_disconnect, 0);
@@ -507,7 +571,6 @@ static void iface_init()
rb_define_method(rbc_iface, "del", iface_del_addr, 1);
rb_define_method(rbc_iface, "each", iface_each_addr, -1);
rb_define_method(rbc_iface, "delete", iface_delete, 0);
rb_include_module(rbc_iface, rb_mEnumerable);
}
static VALUE template_load(VALUE class, VALUE name)
@@ -531,6 +594,7 @@ static VALUE template_unload(VALUE class)
static void template_init()
{
rbc_template = rb_define_class_under(rbm_dumm , "Template", rb_cObject);
rb_define_singleton_method(rbc_template, "load", template_load, 1);
rb_define_singleton_method(rbc_template, "unload", template_unload, 0);
}
+21
View File
@@ -0,0 +1,21 @@
=begin
Copyright (C) 2008 Tobias Brunner
Hochschule fuer Technik Rapperswil
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2 of the License, or (at your
option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
$Id$
=end
require 'dumm.so'
require 'dumm/guest'
# vim:sw=2 ts=2 et
+40
View File
@@ -0,0 +1,40 @@
=begin
Copyright (C) 2008 Tobias Brunner
Hochschule fuer Technik Rapperswil
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2 of the License, or (at your
option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
$Id$
=end
module Dumm
class Guest
# accessor for guests
# e.g. Guest.sun instead of Guest["sun"]
def self.method_missing(id, *args)
unless guest? id
super(id, *args)
end
Guest[id]
end
# accessor for interfaces
# e.g. guest.eth0 instead of guest["eth0"]
def method_missing(id, *args)
unless iface? id
super(id, *args)
end
self[id]
end
end
end
# vim:sw=2 ts=2 et
+1
View File
@@ -214,6 +214,7 @@ static void stop(private_guest_t *this, idle_function_t idle)
}
}
unlinkat(this->dir, PID_FILE, 0);
this->pid = 0;
}
}