cool.io 1.2.0-x86-mswin32-60
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +26 -0
- data/.rspec +3 -0
- data/.travis.yml +5 -0
- data/CHANGES.md +177 -0
- data/Gemfile +4 -0
- data/LICENSE +20 -0
- data/README.md +172 -0
- data/Rakefile +81 -0
- data/cool.io.gemspec +28 -0
- data/examples/dslified_echo_client.rb +34 -0
- data/examples/dslified_echo_server.rb +24 -0
- data/examples/echo_client.rb +38 -0
- data/examples/echo_server.rb +27 -0
- data/examples/google.rb +9 -0
- data/examples/httpclient.rb +38 -0
- data/ext/cool.io/.gitignore +5 -0
- data/ext/cool.io/cool.io.h +58 -0
- data/ext/cool.io/cool.io_ext.c +25 -0
- data/ext/cool.io/ev_wrap.h +8 -0
- data/ext/cool.io/extconf.rb +73 -0
- data/ext/cool.io/iowatcher.c +189 -0
- data/ext/cool.io/libev.c +8 -0
- data/ext/cool.io/loop.c +301 -0
- data/ext/cool.io/stat_watcher.c +269 -0
- data/ext/cool.io/timer_watcher.c +219 -0
- data/ext/cool.io/utils.c +122 -0
- data/ext/cool.io/watcher.c +264 -0
- data/ext/cool.io/watcher.h +71 -0
- data/ext/http11_client/.gitignore +5 -0
- data/ext/http11_client/LICENSE +31 -0
- data/ext/http11_client/ext_help.h +14 -0
- data/ext/http11_client/extconf.rb +6 -0
- data/ext/http11_client/http11_client.c +300 -0
- data/ext/http11_client/http11_parser.c +403 -0
- data/ext/http11_client/http11_parser.h +48 -0
- data/ext/http11_client/http11_parser.rl +173 -0
- data/ext/iobuffer/extconf.rb +9 -0
- data/ext/iobuffer/iobuffer.c +765 -0
- data/ext/libev/Changes +388 -0
- data/ext/libev/LICENSE +36 -0
- data/ext/libev/README +58 -0
- data/ext/libev/README.embed +3 -0
- data/ext/libev/ev.c +4803 -0
- data/ext/libev/ev.h +845 -0
- data/ext/libev/ev_epoll.c +279 -0
- data/ext/libev/ev_kqueue.c +214 -0
- data/ext/libev/ev_poll.c +148 -0
- data/ext/libev/ev_port.c +185 -0
- data/ext/libev/ev_select.c +314 -0
- data/ext/libev/ev_vars.h +203 -0
- data/ext/libev/ev_win32.c +163 -0
- data/ext/libev/ev_wrap.h +200 -0
- data/ext/libev/test_libev_win32.c +123 -0
- data/lib/.gitignore +2 -0
- data/lib/cool.io.rb +32 -0
- data/lib/cool.io/async_watcher.rb +43 -0
- data/lib/cool.io/custom_require.rb +9 -0
- data/lib/cool.io/dns_resolver.rb +225 -0
- data/lib/cool.io/dsl.rb +135 -0
- data/lib/cool.io/eventmachine.rb +234 -0
- data/lib/cool.io/http_client.rb +427 -0
- data/lib/cool.io/io.rb +174 -0
- data/lib/cool.io/iowatcher.rb +17 -0
- data/lib/cool.io/listener.rb +93 -0
- data/lib/cool.io/loop.rb +130 -0
- data/lib/cool.io/meta.rb +49 -0
- data/lib/cool.io/server.rb +74 -0
- data/lib/cool.io/socket.rb +230 -0
- data/lib/cool.io/timer_watcher.rb +17 -0
- data/lib/cool.io/version.rb +5 -0
- data/lib/coolio.rb +2 -0
- data/spec/async_watcher_spec.rb +57 -0
- data/spec/dns_spec.rb +39 -0
- data/spec/spec_helper.rb +12 -0
- data/spec/stat_watcher_spec.rb +77 -0
- data/spec/timer_watcher_spec.rb +55 -0
- data/spec/unix_listener_spec.rb +25 -0
- data/spec/unix_server_spec.rb +25 -0
- metadata +200 -0
data/ext/cool.io/libev.c
ADDED
data/ext/cool.io/loop.c
ADDED
@@ -0,0 +1,301 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (C) 2007-10 Tony Arcieri
|
3
|
+
* You may redistribute this under the terms of the Ruby license.
|
4
|
+
* See LICENSE for details
|
5
|
+
*/
|
6
|
+
|
7
|
+
#include <assert.h>
|
8
|
+
#include "ruby.h"
|
9
|
+
#include "rubysig.h"
|
10
|
+
#include "ev_wrap.h"
|
11
|
+
|
12
|
+
#include "cool.io.h"
|
13
|
+
|
14
|
+
#if defined(HAVE_RB_THREAD_BLOCKING_REGION)
|
15
|
+
# define Coolio_Loop_may_block_safely() (1)
|
16
|
+
#elif defined(HAVE_RB_THREAD_ALONE)
|
17
|
+
# define Coolio_Loop_may_block_safely() (rb_thread_alone())
|
18
|
+
#else /* just in case Ruby changes: */
|
19
|
+
# define Coolio_Loop_may_block_safely() (0)
|
20
|
+
#endif
|
21
|
+
|
22
|
+
static VALUE mCoolio = Qnil;
|
23
|
+
static VALUE cCoolio_Loop = Qnil;
|
24
|
+
|
25
|
+
static VALUE Coolio_Loop_allocate(VALUE klass);
|
26
|
+
static void Coolio_Loop_mark(struct Coolio_Loop *loop);
|
27
|
+
static void Coolio_Loop_free(struct Coolio_Loop *loop);
|
28
|
+
|
29
|
+
static VALUE Coolio_Loop_initialize(VALUE self);
|
30
|
+
static VALUE Coolio_Loop_ev_loop_new(VALUE self, VALUE flags);
|
31
|
+
static VALUE Coolio_Loop_run_once(VALUE self);
|
32
|
+
static VALUE Coolio_Loop_run_nonblock(VALUE self);
|
33
|
+
|
34
|
+
static void Coolio_Loop_ev_loop_oneshot(struct Coolio_Loop *loop_data);
|
35
|
+
static void Coolio_Loop_dispatch_events(struct Coolio_Loop *loop_data);
|
36
|
+
|
37
|
+
#define DEFAULT_EVENTBUF_SIZE 32
|
38
|
+
#define RUN_LOOP(loop_data, options) \
|
39
|
+
loop_data->running = 1; \
|
40
|
+
ev_loop(loop_data->ev_loop, options); \
|
41
|
+
loop_data->running = 0;
|
42
|
+
|
43
|
+
/*
|
44
|
+
* Coolio::Loop represents an event loop. Event watchers can be attached and
|
45
|
+
* unattached. When an event loop is run, all currently attached watchers
|
46
|
+
* are monitored for events, and their respective callbacks are signaled
|
47
|
+
* whenever events occur.
|
48
|
+
*/
|
49
|
+
void Init_coolio_loop()
|
50
|
+
{
|
51
|
+
mCoolio = rb_define_module("Coolio");
|
52
|
+
cCoolio_Loop = rb_define_class_under(mCoolio, "Loop", rb_cObject);
|
53
|
+
rb_define_alloc_func(cCoolio_Loop, Coolio_Loop_allocate);
|
54
|
+
|
55
|
+
rb_define_method(cCoolio_Loop, "initialize", Coolio_Loop_initialize, 0);
|
56
|
+
rb_define_private_method(cCoolio_Loop, "ev_loop_new", Coolio_Loop_ev_loop_new, 1);
|
57
|
+
rb_define_method(cCoolio_Loop, "run_once", Coolio_Loop_run_once, 0);
|
58
|
+
rb_define_method(cCoolio_Loop, "run_nonblock", Coolio_Loop_run_nonblock, 0);
|
59
|
+
}
|
60
|
+
|
61
|
+
static VALUE Coolio_Loop_allocate(VALUE klass)
|
62
|
+
{
|
63
|
+
struct Coolio_Loop *loop = (struct Coolio_Loop *)xmalloc(sizeof(struct Coolio_Loop));
|
64
|
+
|
65
|
+
loop->ev_loop = 0;
|
66
|
+
loop->running = 0;
|
67
|
+
loop->events_received = 0;
|
68
|
+
loop->eventbuf_size = DEFAULT_EVENTBUF_SIZE;
|
69
|
+
loop->eventbuf = (struct Coolio_Event *)xmalloc(sizeof(struct Coolio_Event) * DEFAULT_EVENTBUF_SIZE);
|
70
|
+
|
71
|
+
return Data_Wrap_Struct(klass, Coolio_Loop_mark, Coolio_Loop_free, loop);
|
72
|
+
}
|
73
|
+
|
74
|
+
static void Coolio_Loop_mark(struct Coolio_Loop *loop)
|
75
|
+
{
|
76
|
+
}
|
77
|
+
|
78
|
+
static void Coolio_Loop_free(struct Coolio_Loop *loop)
|
79
|
+
{
|
80
|
+
if(!loop->ev_loop)
|
81
|
+
return;
|
82
|
+
|
83
|
+
ev_loop_destroy(loop->ev_loop);
|
84
|
+
|
85
|
+
xfree(loop->eventbuf);
|
86
|
+
xfree(loop);
|
87
|
+
}
|
88
|
+
|
89
|
+
static VALUE Coolio_Loop_initialize(VALUE self)
|
90
|
+
{
|
91
|
+
Coolio_Loop_ev_loop_new(self, INT2NUM(0));
|
92
|
+
}
|
93
|
+
|
94
|
+
/* Wrapper for populating a Coolio_Loop struct with a new event loop */
|
95
|
+
static VALUE Coolio_Loop_ev_loop_new(VALUE self, VALUE flags)
|
96
|
+
{
|
97
|
+
struct Coolio_Loop *loop_data;
|
98
|
+
Data_Get_Struct(self, struct Coolio_Loop, loop_data);
|
99
|
+
|
100
|
+
if(loop_data->ev_loop)
|
101
|
+
rb_raise(rb_eRuntimeError, "loop already initialized");
|
102
|
+
|
103
|
+
loop_data->ev_loop = ev_loop_new(NUM2INT(flags));
|
104
|
+
|
105
|
+
return Qnil;
|
106
|
+
}
|
107
|
+
|
108
|
+
/* libev callback for receiving events */
|
109
|
+
void Coolio_Loop_process_event(VALUE watcher, int revents)
|
110
|
+
{
|
111
|
+
struct Coolio_Loop *loop_data;
|
112
|
+
struct Coolio_Watcher *watcher_data;
|
113
|
+
|
114
|
+
/* The Global VM lock isn't held right now, but hopefully
|
115
|
+
* we can still do this safely */
|
116
|
+
Data_Get_Struct(watcher, struct Coolio_Watcher, watcher_data);
|
117
|
+
Data_Get_Struct(watcher_data->loop, struct Coolio_Loop, loop_data);
|
118
|
+
|
119
|
+
/* Well, what better place to explain how this all works than
|
120
|
+
* where the most wonky and convoluted stuff is going on!
|
121
|
+
*
|
122
|
+
* Our call path up to here looks a little something like:
|
123
|
+
*
|
124
|
+
* -> release GVL -> event syscall -> libev callback
|
125
|
+
* (GVL = Global VM Lock) ^^^ You are here
|
126
|
+
*
|
127
|
+
* We released the GVL in the Coolio_Loop_run_once() function
|
128
|
+
* so other Ruby threads can run while we make a blocking
|
129
|
+
* system call (one of epoll, kqueue, port, poll, or select,
|
130
|
+
* depending on the platform).
|
131
|
+
*
|
132
|
+
* More specifically, this is a libev callback abstraction
|
133
|
+
* called from a real libev callback in every watcher,
|
134
|
+
* hence this function not being static. The real libev
|
135
|
+
* callbacks are event-specific and handled in a watcher.
|
136
|
+
*
|
137
|
+
* For syscalls like epoll and kqueue, the kernel tells libev
|
138
|
+
* a pointer (to a structure with a pointer) to the watcher
|
139
|
+
* object. No data structure lookups are required at all
|
140
|
+
* (beyond structs), it's smooth O(1) sailing the entire way.
|
141
|
+
* Then libev calls out to the watcher's callback, which
|
142
|
+
* calls this function.
|
143
|
+
*
|
144
|
+
* Now, you may be curious: if the watcher already knew what
|
145
|
+
* event fired, why the hell is it telling the loop? Why
|
146
|
+
* doesn't it just rb_funcall() the appropriate callback?
|
147
|
+
*
|
148
|
+
* Well, the problem is the Global VM Lock isn't held right
|
149
|
+
* now, so we can't rb_funcall() anything. In order to get
|
150
|
+
* it back we have to:
|
151
|
+
*
|
152
|
+
* stash event and return -> acquire GVL -> dispatch to Ruby
|
153
|
+
*
|
154
|
+
* Which is kinda ugly and confusing, but still gives us
|
155
|
+
* an O(1) event loop whose heart is in the kernel itself. w00t!
|
156
|
+
*
|
157
|
+
* So, stash the event in the loop's data struct. When we return
|
158
|
+
* the ev_loop() call being made in the Coolio_Loop_run_once_blocking()
|
159
|
+
* function below will also return, at which point the GVL is
|
160
|
+
* reacquired and we can call out to Ruby */
|
161
|
+
|
162
|
+
/* Grow the event buffer if it's too small */
|
163
|
+
if(loop_data->events_received >= loop_data->eventbuf_size) {
|
164
|
+
loop_data->eventbuf_size *= 2;
|
165
|
+
loop_data->eventbuf = (struct Coolio_Event *)xrealloc(
|
166
|
+
loop_data->eventbuf,
|
167
|
+
sizeof(struct Coolio_Event) * loop_data->eventbuf_size
|
168
|
+
);
|
169
|
+
}
|
170
|
+
|
171
|
+
loop_data->eventbuf[loop_data->events_received].watcher = watcher;
|
172
|
+
loop_data->eventbuf[loop_data->events_received].revents = revents;
|
173
|
+
|
174
|
+
loop_data->events_received++;
|
175
|
+
}
|
176
|
+
|
177
|
+
/**
|
178
|
+
* call-seq:
|
179
|
+
* Coolio::Loop.run_once -> nil
|
180
|
+
*
|
181
|
+
* Run the Coolio::Loop once, blocking until events are received.
|
182
|
+
*/
|
183
|
+
static VALUE Coolio_Loop_run_once(VALUE self)
|
184
|
+
{
|
185
|
+
VALUE nevents;
|
186
|
+
|
187
|
+
if (Coolio_Loop_may_block_safely()) {
|
188
|
+
struct Coolio_Loop *loop_data;
|
189
|
+
|
190
|
+
Data_Get_Struct(self, struct Coolio_Loop, loop_data);
|
191
|
+
|
192
|
+
assert(loop_data->ev_loop && !loop_data->events_received);
|
193
|
+
|
194
|
+
Coolio_Loop_ev_loop_oneshot(loop_data);
|
195
|
+
Coolio_Loop_dispatch_events(loop_data);
|
196
|
+
|
197
|
+
nevents = INT2NUM(loop_data->events_received);
|
198
|
+
loop_data->events_received = 0;
|
199
|
+
|
200
|
+
} else {
|
201
|
+
nevents = Coolio_Loop_run_nonblock(self);
|
202
|
+
rb_thread_schedule();
|
203
|
+
}
|
204
|
+
|
205
|
+
return nevents;
|
206
|
+
}
|
207
|
+
|
208
|
+
/* Ruby 1.9 supports blocking system calls through rb_thread_blocking_region() */
|
209
|
+
#ifdef HAVE_RB_THREAD_BLOCKING_REGION
|
210
|
+
#define HAVE_EV_LOOP_ONESHOT
|
211
|
+
static VALUE Coolio_Loop_ev_loop_oneshot_blocking(void *ptr)
|
212
|
+
{
|
213
|
+
/* The libev loop has now escaped through the Global VM Lock unscathed! */
|
214
|
+
struct Coolio_Loop *loop_data = (struct Coolio_Loop *)ptr;
|
215
|
+
|
216
|
+
RUN_LOOP(loop_data, EVLOOP_ONESHOT);
|
217
|
+
|
218
|
+
return Qnil;
|
219
|
+
}
|
220
|
+
|
221
|
+
static void Coolio_Loop_ev_loop_oneshot(struct Coolio_Loop *loop_data)
|
222
|
+
{
|
223
|
+
/* Use Ruby 1.9's rb_thread_blocking_region call to make a blocking system call */
|
224
|
+
rb_thread_blocking_region(Coolio_Loop_ev_loop_oneshot_blocking, loop_data, RUBY_UBF_IO, 0);
|
225
|
+
}
|
226
|
+
#endif
|
227
|
+
|
228
|
+
/* Ruby 1.8 requires us to periodically run the event loop then defer back to
|
229
|
+
* the green threads scheduler */
|
230
|
+
#ifndef HAVE_EV_LOOP_ONESHOT
|
231
|
+
#define BLOCKING_INTERVAL 0.01 /* Block for 10ms at a time */
|
232
|
+
|
233
|
+
/* Stub for scheduler's ev_timer callback */
|
234
|
+
static void timer_callback(struct ev_loop *ev_loop, struct ev_timer *timer, int revents)
|
235
|
+
{
|
236
|
+
ev_timer_again (ev_loop, timer);
|
237
|
+
}
|
238
|
+
|
239
|
+
/* Run the event loop, calling rb_thread_schedule every 10ms */
|
240
|
+
static void Coolio_Loop_ev_loop_oneshot(struct Coolio_Loop *loop_data)
|
241
|
+
{
|
242
|
+
struct ev_timer timer;
|
243
|
+
struct timeval tv;
|
244
|
+
|
245
|
+
/* Set up an ev_timer to unblock the loop every 10ms */
|
246
|
+
ev_timer_init(&timer, timer_callback, BLOCKING_INTERVAL, BLOCKING_INTERVAL);
|
247
|
+
ev_timer_start(loop_data->ev_loop, &timer);
|
248
|
+
|
249
|
+
/* Loop until we receive events */
|
250
|
+
while(!loop_data->events_received) {
|
251
|
+
TRAP_BEG;
|
252
|
+
RUN_LOOP(loop_data, EVLOOP_ONESHOT);
|
253
|
+
TRAP_END;
|
254
|
+
|
255
|
+
rb_thread_schedule();
|
256
|
+
}
|
257
|
+
|
258
|
+
ev_timer_stop(loop_data->ev_loop, &timer);
|
259
|
+
}
|
260
|
+
#endif
|
261
|
+
|
262
|
+
/**
|
263
|
+
* call-seq:
|
264
|
+
* Coolio::Loop.run_nonblock -> nil
|
265
|
+
*
|
266
|
+
* Run the Coolio::Loop once, but return immediately if there are no pending events.
|
267
|
+
*/
|
268
|
+
static VALUE Coolio_Loop_run_nonblock(VALUE self)
|
269
|
+
{
|
270
|
+
struct Coolio_Loop *loop_data;
|
271
|
+
VALUE nevents;
|
272
|
+
|
273
|
+
Data_Get_Struct(self, struct Coolio_Loop, loop_data);
|
274
|
+
|
275
|
+
assert(loop_data->ev_loop && !loop_data->events_received);
|
276
|
+
|
277
|
+
RUN_LOOP(loop_data, EVLOOP_NONBLOCK);
|
278
|
+
Coolio_Loop_dispatch_events(loop_data);
|
279
|
+
|
280
|
+
nevents = INT2NUM(loop_data->events_received);
|
281
|
+
loop_data->events_received = 0;
|
282
|
+
|
283
|
+
return nevents;
|
284
|
+
}
|
285
|
+
|
286
|
+
static void Coolio_Loop_dispatch_events(struct Coolio_Loop *loop_data)
|
287
|
+
{
|
288
|
+
int i;
|
289
|
+
struct Coolio_Watcher *watcher_data;
|
290
|
+
|
291
|
+
for(i = 0; i < loop_data->events_received; i++) {
|
292
|
+
/* A watcher with pending events may have been detached from the loop
|
293
|
+
* during the dispatch process. If so, the watcher clears the pending
|
294
|
+
* events, so skip over them */
|
295
|
+
if(loop_data->eventbuf[i].watcher == Qnil)
|
296
|
+
continue;
|
297
|
+
|
298
|
+
Data_Get_Struct(loop_data->eventbuf[i].watcher, struct Coolio_Watcher, watcher_data);
|
299
|
+
watcher_data->dispatch_callback(loop_data->eventbuf[i].watcher, loop_data->eventbuf[i].revents);
|
300
|
+
}
|
301
|
+
}
|
@@ -0,0 +1,269 @@
|
|
1
|
+
/*
|
2
|
+
* Copyright (C) 2009-10 Tony Arcieri
|
3
|
+
* You may redistribute this under the terms of the Ruby license.
|
4
|
+
* See LICENSE for details
|
5
|
+
*/
|
6
|
+
|
7
|
+
#include "ruby.h"
|
8
|
+
#include "ev_wrap.h"
|
9
|
+
|
10
|
+
#include "cool.io.h"
|
11
|
+
#include "watcher.h"
|
12
|
+
|
13
|
+
static VALUE mCoolio = Qnil;
|
14
|
+
static VALUE cCoolio_Watcher = Qnil;
|
15
|
+
static VALUE cCoolio_StatWatcher = Qnil;
|
16
|
+
static VALUE cCoolio_StatInfo = Qnil;
|
17
|
+
static VALUE cCoolio_Loop = Qnil;
|
18
|
+
|
19
|
+
static VALUE Coolio_StatWatcher_initialize(int argc, VALUE *argv, VALUE self);
|
20
|
+
static VALUE Coolio_StatWatcher_attach(VALUE self, VALUE loop);
|
21
|
+
static VALUE Coolio_StatWatcher_detach(VALUE self);
|
22
|
+
static VALUE Coolio_StatWatcher_enable(VALUE self);
|
23
|
+
static VALUE Coolio_StatWatcher_disable(VALUE self);
|
24
|
+
static VALUE Coolio_StatWatcher_on_change(VALUE self, VALUE previous, VALUE current);
|
25
|
+
static VALUE Coolio_StatWatcher_path(VALUE self);
|
26
|
+
|
27
|
+
static VALUE Coolio_StatInfo_build(ev_statdata *statdata_struct);
|
28
|
+
|
29
|
+
static void Coolio_StatWatcher_libev_callback(struct ev_loop *ev_loop, struct ev_stat *stat, int revents);
|
30
|
+
static void Coolio_StatWatcher_dispatch_callback(VALUE self, int revents);
|
31
|
+
|
32
|
+
/*
|
33
|
+
* Coolio::StatWatcher lets you create either one-shot or periodic stats which
|
34
|
+
* run within Coolio's event loop. It's useful for creating timeouts or
|
35
|
+
* events which fire periodically.
|
36
|
+
**/
|
37
|
+
void Init_coolio_stat_watcher()
|
38
|
+
{
|
39
|
+
mCoolio = rb_define_module("Coolio");
|
40
|
+
cCoolio_Watcher = rb_define_class_under(mCoolio, "Watcher", rb_cObject);
|
41
|
+
cCoolio_StatWatcher = rb_define_class_under(mCoolio, "StatWatcher", cCoolio_Watcher);
|
42
|
+
cCoolio_StatInfo = rb_struct_define("StatInfo",
|
43
|
+
"mtime",
|
44
|
+
"ctime",
|
45
|
+
"atime",
|
46
|
+
"dev",
|
47
|
+
"ino",
|
48
|
+
"mode",
|
49
|
+
"nlink",
|
50
|
+
"uid",
|
51
|
+
"guid",
|
52
|
+
"rdev",
|
53
|
+
"size",
|
54
|
+
"blksize",
|
55
|
+
"blocks",
|
56
|
+
NULL);
|
57
|
+
cCoolio_Loop = rb_define_class_under(mCoolio, "Loop", rb_cObject);
|
58
|
+
|
59
|
+
rb_define_method(cCoolio_StatWatcher, "initialize", Coolio_StatWatcher_initialize, -1);
|
60
|
+
rb_define_method(cCoolio_StatWatcher, "attach", Coolio_StatWatcher_attach, 1);
|
61
|
+
rb_define_method(cCoolio_StatWatcher, "detach", Coolio_StatWatcher_detach, 0);
|
62
|
+
rb_define_method(cCoolio_StatWatcher, "enable", Coolio_StatWatcher_enable, 0);
|
63
|
+
rb_define_method(cCoolio_StatWatcher, "disable", Coolio_StatWatcher_disable, 0);
|
64
|
+
rb_define_method(cCoolio_StatWatcher, "on_change", Coolio_StatWatcher_on_change, 2);
|
65
|
+
rb_define_method(cCoolio_StatWatcher, "path", Coolio_StatWatcher_path, 0);
|
66
|
+
}
|
67
|
+
|
68
|
+
/**
|
69
|
+
* call-seq:
|
70
|
+
* Coolio::StatWatcher.initialize(path, interval = 0) -> Coolio::StatWatcher
|
71
|
+
*
|
72
|
+
* Create a new Coolio::StatWatcher for the given path. This will monitor the
|
73
|
+
* given path for changes at the filesystem level. The interval argument
|
74
|
+
* specified how often in seconds the path should be polled for changes.
|
75
|
+
* Setting interval to zero uses an "automatic" value (typically around 5
|
76
|
+
* seconds) which optimizes performance. Otherwise, values less than
|
77
|
+
* 0.1 are not particularly meaningful. Where available (at present, on Linux)
|
78
|
+
* high performance file monitoring interfaces will be used instead of polling.
|
79
|
+
*/
|
80
|
+
static VALUE Coolio_StatWatcher_initialize(int argc, VALUE *argv, VALUE self)
|
81
|
+
{
|
82
|
+
VALUE path, interval;
|
83
|
+
struct Coolio_Watcher *watcher_data;
|
84
|
+
|
85
|
+
rb_scan_args(argc, argv, "11", &path, &interval);
|
86
|
+
if(interval != Qnil)
|
87
|
+
interval = rb_convert_type(interval, T_FLOAT, "Float", "to_f");
|
88
|
+
|
89
|
+
path = rb_String(path);
|
90
|
+
rb_iv_set(self, "@path", path);
|
91
|
+
|
92
|
+
Data_Get_Struct(self, struct Coolio_Watcher, watcher_data);
|
93
|
+
|
94
|
+
watcher_data->dispatch_callback = Coolio_StatWatcher_dispatch_callback;
|
95
|
+
ev_stat_init(
|
96
|
+
&watcher_data->event_types.ev_stat,
|
97
|
+
Coolio_StatWatcher_libev_callback,
|
98
|
+
RSTRING_PTR(path),
|
99
|
+
interval == Qnil ? 0 : NUM2DBL(interval)
|
100
|
+
);
|
101
|
+
watcher_data->event_types.ev_stat.data = (void *)self;
|
102
|
+
|
103
|
+
return Qnil;
|
104
|
+
}
|
105
|
+
|
106
|
+
/**
|
107
|
+
* call-seq:
|
108
|
+
* Coolio::StatWatcher.attach(loop) -> Coolio::StatWatcher
|
109
|
+
*
|
110
|
+
* Attach the stat watcher to the given Coolio::Loop. If the watcher is already
|
111
|
+
* attached to a loop, detach it from the old one and attach it to the new one.
|
112
|
+
*/
|
113
|
+
static VALUE Coolio_StatWatcher_attach(VALUE self, VALUE loop)
|
114
|
+
{
|
115
|
+
ev_tstamp interval, timeout;
|
116
|
+
struct Coolio_Loop *loop_data;
|
117
|
+
struct Coolio_Watcher *watcher_data;
|
118
|
+
|
119
|
+
if(!rb_obj_is_kind_of(loop, cCoolio_Loop))
|
120
|
+
rb_raise(rb_eArgError, "expected loop to be an instance of Coolio::Loop");
|
121
|
+
|
122
|
+
Data_Get_Struct(loop, struct Coolio_Loop, loop_data);
|
123
|
+
Data_Get_Struct(self, struct Coolio_Watcher, watcher_data);
|
124
|
+
|
125
|
+
if(watcher_data->loop != Qnil)
|
126
|
+
Coolio_StatWatcher_detach(self);
|
127
|
+
|
128
|
+
watcher_data->loop = loop;
|
129
|
+
|
130
|
+
ev_stat_start(loop_data->ev_loop, &watcher_data->event_types.ev_stat);
|
131
|
+
rb_call_super(1, &loop);
|
132
|
+
|
133
|
+
return self;
|
134
|
+
}
|
135
|
+
|
136
|
+
/**
|
137
|
+
* call-seq:
|
138
|
+
* Coolio::StatWatcher.detach -> Coolio::StatWatcher
|
139
|
+
*
|
140
|
+
* Detach the stat watcher from its current Coolio::Loop.
|
141
|
+
*/
|
142
|
+
static VALUE Coolio_StatWatcher_detach(VALUE self)
|
143
|
+
{
|
144
|
+
Watcher_Detach(stat, self);
|
145
|
+
|
146
|
+
return self;
|
147
|
+
}
|
148
|
+
|
149
|
+
/**
|
150
|
+
* call-seq:
|
151
|
+
* Coolio::StatWatcher.enable -> Coolio::StatWatcher
|
152
|
+
*
|
153
|
+
* Re-enable a stat watcher which has been temporarily disabled. See the
|
154
|
+
* disable method for a more thorough explanation.
|
155
|
+
*/
|
156
|
+
static VALUE Coolio_StatWatcher_enable(VALUE self)
|
157
|
+
{
|
158
|
+
Watcher_Enable(stat, self);
|
159
|
+
|
160
|
+
return self;
|
161
|
+
}
|
162
|
+
|
163
|
+
/**
|
164
|
+
* call-seq:
|
165
|
+
* Coolio::StatWatcher.disable -> Coolio::StatWatcher
|
166
|
+
*
|
167
|
+
* Temporarily disable a stat watcher which is attached to a loop.
|
168
|
+
* This is useful if you wish to toggle event monitoring on and off.
|
169
|
+
*/
|
170
|
+
static VALUE Coolio_StatWatcher_disable(VALUE self)
|
171
|
+
{
|
172
|
+
Watcher_Disable(stat, self);
|
173
|
+
|
174
|
+
return self;
|
175
|
+
}
|
176
|
+
|
177
|
+
/**
|
178
|
+
* call-seq:
|
179
|
+
* Coolio::StatWatcher#on_change -> nil
|
180
|
+
*
|
181
|
+
* Called whenever the status of the given path changes
|
182
|
+
*/
|
183
|
+
static VALUE Coolio_StatWatcher_on_change(VALUE self, VALUE previous, VALUE current)
|
184
|
+
{
|
185
|
+
return Qnil;
|
186
|
+
}
|
187
|
+
|
188
|
+
/**
|
189
|
+
* call-seq:
|
190
|
+
* Coolio::StatWatcher#path -> String
|
191
|
+
*
|
192
|
+
* Retrieve the path associated with this StatWatcher
|
193
|
+
*/
|
194
|
+
static VALUE Coolio_StatWatcher_path(VALUE self)
|
195
|
+
{
|
196
|
+
return rb_iv_get(self, "@path");
|
197
|
+
}
|
198
|
+
|
199
|
+
/* libev callback */
|
200
|
+
static void Coolio_StatWatcher_libev_callback(struct ev_loop *ev_loop, struct ev_stat *stat, int revents)
|
201
|
+
{
|
202
|
+
Coolio_Loop_process_event((VALUE)stat->data, revents);
|
203
|
+
}
|
204
|
+
|
205
|
+
/* Coolio::Loop dispatch callback */
|
206
|
+
static void Coolio_StatWatcher_dispatch_callback(VALUE self, int revents)
|
207
|
+
{
|
208
|
+
struct Coolio_Watcher *watcher_data;
|
209
|
+
Data_Get_Struct(self, struct Coolio_Watcher, watcher_data);
|
210
|
+
|
211
|
+
VALUE previous_statdata = Coolio_StatInfo_build(&watcher_data->event_types.ev_stat.prev);
|
212
|
+
VALUE current_statdata = Coolio_StatInfo_build(&watcher_data->event_types.ev_stat.attr);
|
213
|
+
rb_funcall(self, rb_intern("on_change"), 2, previous_statdata, current_statdata);
|
214
|
+
}
|
215
|
+
|
216
|
+
/**
|
217
|
+
* Convience method to build StatInfo structs given an ev_statdata
|
218
|
+
* */
|
219
|
+
static VALUE Coolio_StatInfo_build(ev_statdata *statdata_struct)
|
220
|
+
{
|
221
|
+
VALUE at_method = rb_intern("at");
|
222
|
+
VALUE cTime = rb_const_get(rb_cObject, rb_intern("Time"));
|
223
|
+
|
224
|
+
VALUE mtime = Qnil;
|
225
|
+
VALUE ctime = Qnil;
|
226
|
+
VALUE atime = Qnil;
|
227
|
+
VALUE dev = Qnil;
|
228
|
+
VALUE ino = Qnil;
|
229
|
+
VALUE mode = Qnil;
|
230
|
+
VALUE nlink = Qnil;
|
231
|
+
VALUE uid = Qnil;
|
232
|
+
VALUE gid = Qnil;
|
233
|
+
VALUE rdev = Qnil;
|
234
|
+
VALUE size = Qnil;
|
235
|
+
VALUE blksize = Qnil;
|
236
|
+
VALUE blocks = Qnil;
|
237
|
+
|
238
|
+
mtime = rb_funcall(cTime, at_method, 1, INT2NUM(statdata_struct->st_mtime));
|
239
|
+
ctime = rb_funcall(cTime, at_method, 1, INT2NUM(statdata_struct->st_ctime));
|
240
|
+
atime = rb_funcall(cTime, at_method, 1, INT2NUM(statdata_struct->st_atime));
|
241
|
+
dev = INT2NUM(statdata_struct->st_dev);
|
242
|
+
ino = INT2NUM(statdata_struct->st_ino);
|
243
|
+
mode = INT2NUM(statdata_struct->st_mode);
|
244
|
+
nlink = INT2NUM(statdata_struct->st_nlink);
|
245
|
+
uid = INT2NUM(statdata_struct->st_uid);
|
246
|
+
gid = INT2NUM(statdata_struct->st_gid);
|
247
|
+
rdev = INT2NUM(statdata_struct->st_rdev);
|
248
|
+
size = INT2NUM(statdata_struct->st_size);
|
249
|
+
#ifdef HAVE_ST_BLKSIZE
|
250
|
+
blksize = INT2NUM(statdata_struct->st_blksize);
|
251
|
+
blocks = INT2NUM(statdata_struct->st_blocks);
|
252
|
+
#endif
|
253
|
+
|
254
|
+
return rb_struct_new(cCoolio_StatInfo,
|
255
|
+
mtime,
|
256
|
+
ctime,
|
257
|
+
atime,
|
258
|
+
dev,
|
259
|
+
ino,
|
260
|
+
mode,
|
261
|
+
nlink,
|
262
|
+
uid,
|
263
|
+
gid,
|
264
|
+
rdev,
|
265
|
+
size,
|
266
|
+
blksize,
|
267
|
+
blocks,
|
268
|
+
NULL);
|
269
|
+
}
|