ruby-watchcat 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/examples/example.rb +17 -0
- data/ext/extconf.rb +5 -0
- data/ext/watchcat.c +123 -0
- metadata +47 -0
data/examples/example.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'watchcat'
|
2
|
+
|
3
|
+
# Create a new cat.
|
4
|
+
cat = Watchcat.new(:timeout => 5, :signal => 'KILL', :info => 'rubykill!')
|
5
|
+
loop do
|
6
|
+
# Do something that might be slow.
|
7
|
+
cat.heartbeat
|
8
|
+
end
|
9
|
+
cat.close # clean cat's litter box.
|
10
|
+
|
11
|
+
# If you call it with a block, the cat cleans its own litter box.
|
12
|
+
Watchcat.new do |cat|
|
13
|
+
loop do
|
14
|
+
# Do something that might be slow.
|
15
|
+
cat.heartbeat
|
16
|
+
end
|
17
|
+
end
|
data/ext/extconf.rb
ADDED
data/ext/watchcat.c
ADDED
@@ -0,0 +1,123 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (c) 2006 Andre Nathan <andre@digirati.com.br>
|
3
|
+
*
|
4
|
+
* Permission to use, copy, modify, and distribute this software for any
|
5
|
+
* purpose with or without fee is hereby granted, provided that the above
|
6
|
+
* copyright notice and this permission notice appear in all copies.
|
7
|
+
*
|
8
|
+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
9
|
+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
10
|
+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
11
|
+
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
12
|
+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
13
|
+
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
14
|
+
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
15
|
+
*
|
16
|
+
* $Id: watchcat.c 6 2006-04-21 02:19:26Z andre $
|
17
|
+
*
|
18
|
+
*/
|
19
|
+
#include <ruby.h>
|
20
|
+
#include <watchcat.h>
|
21
|
+
|
22
|
+
static VALUE
|
23
|
+
rb_wcat_open(int argc, VALUE *argv, VALUE self)
|
24
|
+
{
|
25
|
+
int cat, id, timeout, signal;
|
26
|
+
char *signame;
|
27
|
+
const char *info;
|
28
|
+
VALUE opt, vtimeout, vsignal, vinfo, vsiglist, mSignal;
|
29
|
+
|
30
|
+
rb_scan_args(argc, argv, "01", &opt);
|
31
|
+
if (NIL_P(opt)) {
|
32
|
+
cat = cat_open();
|
33
|
+
if (cat == -1)
|
34
|
+
rb_sys_fail("cat_open");
|
35
|
+
rb_iv_set(self, "@cat", INT2NUM(cat));
|
36
|
+
return(self);
|
37
|
+
}
|
38
|
+
|
39
|
+
/* Defaults. */
|
40
|
+
timeout = 60;
|
41
|
+
signal = SIGKILL;
|
42
|
+
info = NULL;
|
43
|
+
|
44
|
+
vtimeout = rb_hash_aref(opt, ID2SYM(rb_intern("timeout")));
|
45
|
+
if (!NIL_P(vtimeout)) {
|
46
|
+
if (FIXNUM_P(vtimeout))
|
47
|
+
timeout = NUM2INT(vtimeout);
|
48
|
+
else
|
49
|
+
rb_raise(rb_eTypeError, "timeout must be an integer");
|
50
|
+
}
|
51
|
+
|
52
|
+
vsignal = rb_hash_aref(opt, ID2SYM(rb_intern("signal")));
|
53
|
+
if (!NIL_P(vsignal)) {
|
54
|
+
switch (TYPE(vsignal)) {
|
55
|
+
case T_FIXNUM:
|
56
|
+
signal = NUM2INT(vsignal);
|
57
|
+
break;
|
58
|
+
case T_STRING:
|
59
|
+
signame = StringValuePtr(vsignal);
|
60
|
+
if (strncmp("SIG", signame, 3) == 0) {
|
61
|
+
signame += 3;
|
62
|
+
vsignal = rb_str_new2(signame);
|
63
|
+
}
|
64
|
+
id = rb_intern("Signal");
|
65
|
+
mSignal = rb_const_get(rb_cObject, id);
|
66
|
+
id = rb_intern("list");
|
67
|
+
vsiglist = rb_funcall(mSignal, id, 0);
|
68
|
+
id = rb_intern("fetch");
|
69
|
+
vsignal = rb_funcall(vsiglist, id, 1, vsignal);
|
70
|
+
signal = NUM2INT(vsignal);
|
71
|
+
break;
|
72
|
+
default:
|
73
|
+
rb_raise(rb_eTypeError,
|
74
|
+
"signal must be an integer or a string");
|
75
|
+
}
|
76
|
+
}
|
77
|
+
|
78
|
+
vinfo = rb_hash_aref(opt, ID2SYM(rb_intern("info")));
|
79
|
+
if (!NIL_P(vinfo))
|
80
|
+
info = StringValuePtr(vinfo);
|
81
|
+
|
82
|
+
cat = cat_open1(timeout, signal, info);
|
83
|
+
if (cat == -1)
|
84
|
+
rb_sys_fail("cat_open");
|
85
|
+
|
86
|
+
rb_iv_set(self, "@cat", INT2NUM(cat));
|
87
|
+
|
88
|
+
if (rb_block_given_p()) {
|
89
|
+
rb_yield(self);
|
90
|
+
cat_close(cat);
|
91
|
+
}
|
92
|
+
|
93
|
+
return(self);
|
94
|
+
}
|
95
|
+
|
96
|
+
static VALUE
|
97
|
+
rb_wcat_heartbeat(VALUE self)
|
98
|
+
{
|
99
|
+
VALUE cCat = rb_iv_get(self, "@cat");
|
100
|
+
if (cat_heartbeat(NUM2INT(cCat)) == -1)
|
101
|
+
rb_sys_fail("cat_heartbeat");
|
102
|
+
return(Qnil);
|
103
|
+
}
|
104
|
+
|
105
|
+
static VALUE
|
106
|
+
rb_wcat_close(VALUE self)
|
107
|
+
{
|
108
|
+
VALUE cCat = rb_iv_get(self, "@cat");
|
109
|
+
if (cat_close(NUM2INT(cCat)) == -1)
|
110
|
+
rb_sys_fail("cat_close");
|
111
|
+
return(Qnil);
|
112
|
+
}
|
113
|
+
|
114
|
+
void
|
115
|
+
Init_watchcat(void)
|
116
|
+
{
|
117
|
+
VALUE cWCat = rb_define_class("Watchcat", rb_cObject);
|
118
|
+
|
119
|
+
rb_define_method(cWCat, "initialize", rb_wcat_open, -1);
|
120
|
+
rb_define_alias(cWCat, "open", "initialize");
|
121
|
+
rb_define_method(cWCat, "heartbeat", rb_wcat_heartbeat, 0);
|
122
|
+
rb_define_method(cWCat, "close", rb_wcat_close, 0);
|
123
|
+
}
|
metadata
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
rubygems_version: 0.8.11
|
3
|
+
specification_version: 1
|
4
|
+
name: ruby-watchcat
|
5
|
+
version: !ruby/object:Gem::Version
|
6
|
+
version: 1.0.0
|
7
|
+
date: 2006-04-20 00:00:00 -03:00
|
8
|
+
summary: A Ruby extension for libwatchcat
|
9
|
+
require_paths:
|
10
|
+
- lib
|
11
|
+
email: andre@digirati.com.br
|
12
|
+
homepage: http://watchcat.rubyforge.org
|
13
|
+
rubyforge_project: ruby-watchcat
|
14
|
+
description: Ruby/Watchcat is an extension for the Ruby programming language for the development of watchcatd-aware applications.
|
15
|
+
autorequire: watchcat
|
16
|
+
default_executable:
|
17
|
+
bindir: bin
|
18
|
+
has_rdoc: false
|
19
|
+
required_ruby_version: !ruby/object:Gem::Version::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">"
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.0.0
|
24
|
+
version:
|
25
|
+
platform: ruby
|
26
|
+
signing_key:
|
27
|
+
cert_chain:
|
28
|
+
authors:
|
29
|
+
- Andre Nathan
|
30
|
+
files:
|
31
|
+
- ext/watchcat.c
|
32
|
+
- ext/extconf.rb
|
33
|
+
- examples/example.rb
|
34
|
+
test_files: []
|
35
|
+
|
36
|
+
rdoc_options: []
|
37
|
+
|
38
|
+
extra_rdoc_files: []
|
39
|
+
|
40
|
+
executables: []
|
41
|
+
|
42
|
+
extensions:
|
43
|
+
- ext/extconf.rb
|
44
|
+
requirements:
|
45
|
+
- libwcat and watchcatd.
|
46
|
+
dependencies: []
|
47
|
+
|