cool.io 1.4.1-x64-mingw32

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +29 -0
  3. data/.rspec +3 -0
  4. data/.travis.yml +13 -0
  5. data/CHANGES.md +229 -0
  6. data/Gemfile +4 -0
  7. data/LICENSE +20 -0
  8. data/README.md +166 -0
  9. data/Rakefile +79 -0
  10. data/cool.io.gemspec +29 -0
  11. data/examples/callbacked_echo_server.rb +24 -0
  12. data/examples/dslified_echo_client.rb +34 -0
  13. data/examples/dslified_echo_server.rb +24 -0
  14. data/examples/echo_client.rb +38 -0
  15. data/examples/echo_server.rb +27 -0
  16. data/examples/google.rb +9 -0
  17. data/ext/cool.io/.gitignore +5 -0
  18. data/ext/cool.io/cool.io.h +59 -0
  19. data/ext/cool.io/cool.io_ext.c +25 -0
  20. data/ext/cool.io/ev_wrap.h +10 -0
  21. data/ext/cool.io/extconf.rb +61 -0
  22. data/ext/cool.io/iowatcher.c +189 -0
  23. data/ext/cool.io/libev.c +8 -0
  24. data/ext/cool.io/loop.c +261 -0
  25. data/ext/cool.io/stat_watcher.c +269 -0
  26. data/ext/cool.io/timer_watcher.c +219 -0
  27. data/ext/cool.io/utils.c +122 -0
  28. data/ext/cool.io/watcher.c +264 -0
  29. data/ext/cool.io/watcher.h +71 -0
  30. data/ext/iobuffer/extconf.rb +9 -0
  31. data/ext/iobuffer/iobuffer.c +767 -0
  32. data/ext/libev/Changes +507 -0
  33. data/ext/libev/LICENSE +37 -0
  34. data/ext/libev/README +58 -0
  35. data/ext/libev/README.embed +3 -0
  36. data/ext/libev/ev.c +5054 -0
  37. data/ext/libev/ev.h +853 -0
  38. data/ext/libev/ev_epoll.c +282 -0
  39. data/ext/libev/ev_kqueue.c +214 -0
  40. data/ext/libev/ev_poll.c +148 -0
  41. data/ext/libev/ev_port.c +185 -0
  42. data/ext/libev/ev_select.c +362 -0
  43. data/ext/libev/ev_vars.h +204 -0
  44. data/ext/libev/ev_win32.c +163 -0
  45. data/ext/libev/ev_wrap.h +200 -0
  46. data/ext/libev/ruby_gil.patch +97 -0
  47. data/ext/libev/test_libev_win32.c +123 -0
  48. data/ext/libev/win_select.patch +115 -0
  49. data/lib/.gitignore +2 -0
  50. data/lib/cool.io.rb +34 -0
  51. data/lib/cool.io/async_watcher.rb +43 -0
  52. data/lib/cool.io/custom_require.rb +9 -0
  53. data/lib/cool.io/dns_resolver.rb +219 -0
  54. data/lib/cool.io/dsl.rb +139 -0
  55. data/lib/cool.io/io.rb +194 -0
  56. data/lib/cool.io/iowatcher.rb +17 -0
  57. data/lib/cool.io/listener.rb +99 -0
  58. data/lib/cool.io/loop.rb +122 -0
  59. data/lib/cool.io/meta.rb +49 -0
  60. data/lib/cool.io/server.rb +75 -0
  61. data/lib/cool.io/socket.rb +230 -0
  62. data/lib/cool.io/timer_watcher.rb +17 -0
  63. data/lib/cool.io/version.rb +7 -0
  64. data/lib/coolio.rb +2 -0
  65. data/spec/async_watcher_spec.rb +57 -0
  66. data/spec/dns_spec.rb +43 -0
  67. data/spec/iobuffer_spec.rb +147 -0
  68. data/spec/spec_helper.rb +19 -0
  69. data/spec/stat_watcher_spec.rb +77 -0
  70. data/spec/tcp_server_spec.rb +225 -0
  71. data/spec/tcp_socket_spec.rb +185 -0
  72. data/spec/timer_watcher_spec.rb +59 -0
  73. data/spec/udp_socket_spec.rb +58 -0
  74. data/spec/unix_listener_spec.rb +25 -0
  75. data/spec/unix_server_spec.rb +27 -0
  76. metadata +182 -0
@@ -0,0 +1,8 @@
1
+ /*
2
+ * Copyright (C) 2007 Tony Arcieri
3
+ * You may redistribute this under the terms of the Ruby license.
4
+ * See LICENSE for details
5
+ */
6
+
7
+ #include "ev_wrap.h"
8
+ #include "../libev/ev.c"
@@ -0,0 +1,261 @@
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
+ static VALUE mCoolio = Qnil;
15
+ static VALUE cCoolio_Loop = Qnil;
16
+
17
+ static VALUE Coolio_Loop_allocate(VALUE klass);
18
+ static void Coolio_Loop_mark(struct Coolio_Loop *loop);
19
+ static void Coolio_Loop_free(struct Coolio_Loop *loop);
20
+
21
+ static VALUE Coolio_Loop_initialize(VALUE self);
22
+ static VALUE Coolio_Loop_ev_loop_new(VALUE self, VALUE flags);
23
+ static VALUE Coolio_Loop_run_once(int argc, VALUE *argv, VALUE self);
24
+ static VALUE Coolio_Loop_run_nonblock(VALUE self);
25
+
26
+ static void Coolio_Loop_timeout_callback(struct ev_loop *ev_loop, struct ev_timer *timer, int revents);
27
+ static void Coolio_Loop_dispatch_events(struct Coolio_Loop *loop_data);
28
+
29
+ #define DEFAULT_EVENTBUF_SIZE 32
30
+ #define RUN_LOOP(loop_data, options) \
31
+ loop_data->running = 1; \
32
+ ev_loop(loop_data->ev_loop, options); \
33
+ loop_data->running = 0;
34
+
35
+ /*
36
+ * Coolio::Loop represents an event loop. Event watchers can be attached and
37
+ * unattached. When an event loop is run, all currently attached watchers
38
+ * are monitored for events, and their respective callbacks are signaled
39
+ * whenever events occur.
40
+ */
41
+ void Init_coolio_loop()
42
+ {
43
+ mCoolio = rb_define_module("Coolio");
44
+ cCoolio_Loop = rb_define_class_under(mCoolio, "Loop", rb_cObject);
45
+ rb_define_alloc_func(cCoolio_Loop, Coolio_Loop_allocate);
46
+
47
+ rb_define_method(cCoolio_Loop, "initialize", Coolio_Loop_initialize, 0);
48
+ rb_define_private_method(cCoolio_Loop, "ev_loop_new", Coolio_Loop_ev_loop_new, 1);
49
+ rb_define_method(cCoolio_Loop, "run_once", Coolio_Loop_run_once, -1);
50
+ rb_define_method(cCoolio_Loop, "run_nonblock", Coolio_Loop_run_nonblock, 0);
51
+ }
52
+
53
+ static VALUE Coolio_Loop_allocate(VALUE klass)
54
+ {
55
+ struct Coolio_Loop *loop = (struct Coolio_Loop *)xmalloc(sizeof(struct Coolio_Loop));
56
+
57
+ loop->ev_loop = 0;
58
+ ev_init(&loop->timer, Coolio_Loop_timeout_callback);
59
+ loop->running = 0;
60
+ loop->events_received = 0;
61
+ loop->eventbuf_size = DEFAULT_EVENTBUF_SIZE;
62
+ loop->eventbuf = (struct Coolio_Event *)xmalloc(sizeof(struct Coolio_Event) * DEFAULT_EVENTBUF_SIZE);
63
+
64
+ return Data_Wrap_Struct(klass, Coolio_Loop_mark, Coolio_Loop_free, loop);
65
+ }
66
+
67
+ static void Coolio_Loop_mark(struct Coolio_Loop *loop)
68
+ {
69
+ }
70
+
71
+ static void Coolio_Loop_free(struct Coolio_Loop *loop)
72
+ {
73
+ if(!loop->ev_loop)
74
+ return;
75
+
76
+ ev_loop_destroy(loop->ev_loop);
77
+
78
+ xfree(loop->eventbuf);
79
+ xfree(loop);
80
+ }
81
+
82
+ static VALUE Coolio_Loop_initialize(VALUE self)
83
+ {
84
+ return Coolio_Loop_ev_loop_new(self, INT2NUM(0));
85
+ }
86
+
87
+ /* Wrapper for populating a Coolio_Loop struct with a new event loop */
88
+ static VALUE Coolio_Loop_ev_loop_new(VALUE self, VALUE flags)
89
+ {
90
+ struct Coolio_Loop *loop_data;
91
+ Data_Get_Struct(self, struct Coolio_Loop, loop_data);
92
+
93
+ if(loop_data->ev_loop)
94
+ rb_raise(rb_eRuntimeError, "loop already initialized");
95
+
96
+ loop_data->ev_loop = ev_loop_new(NUM2INT(flags));
97
+
98
+ return Qnil;
99
+ }
100
+
101
+ /* libev callback for receiving events */
102
+ void Coolio_Loop_process_event(VALUE watcher, int revents)
103
+ {
104
+ struct Coolio_Loop *loop_data;
105
+ struct Coolio_Watcher *watcher_data;
106
+
107
+ /* The Global VM lock isn't held right now, but hopefully
108
+ * we can still do this safely */
109
+ Data_Get_Struct(watcher, struct Coolio_Watcher, watcher_data);
110
+ Data_Get_Struct(watcher_data->loop, struct Coolio_Loop, loop_data);
111
+
112
+ /* Well, what better place to explain how this all works than
113
+ * where the most wonky and convoluted stuff is going on!
114
+ *
115
+ * Our call path up to here looks a little something like:
116
+ *
117
+ * -> release GVL -> event syscall -> libev callback
118
+ * (GVL = Global VM Lock) ^^^ You are here
119
+ *
120
+ * We released the GVL in the Coolio_Loop_run_once() function
121
+ * so other Ruby threads can run while we make a blocking
122
+ * system call (one of epoll, kqueue, port, poll, or select,
123
+ * depending on the platform).
124
+ *
125
+ * More specifically, this is a libev callback abstraction
126
+ * called from a real libev callback in every watcher,
127
+ * hence this function not being static. The real libev
128
+ * callbacks are event-specific and handled in a watcher.
129
+ *
130
+ * For syscalls like epoll and kqueue, the kernel tells libev
131
+ * a pointer (to a structure with a pointer) to the watcher
132
+ * object. No data structure lookups are required at all
133
+ * (beyond structs), it's smooth O(1) sailing the entire way.
134
+ * Then libev calls out to the watcher's callback, which
135
+ * calls this function.
136
+ *
137
+ * Now, you may be curious: if the watcher already knew what
138
+ * event fired, why the hell is it telling the loop? Why
139
+ * doesn't it just rb_funcall() the appropriate callback?
140
+ *
141
+ * Well, the problem is the Global VM Lock isn't held right
142
+ * now, so we can't rb_funcall() anything. In order to get
143
+ * it back we have to:
144
+ *
145
+ * stash event and return -> acquire GVL -> dispatch to Ruby
146
+ *
147
+ * Which is kinda ugly and confusing, but still gives us
148
+ * an O(1) event loop whose heart is in the kernel itself. w00t!
149
+ *
150
+ * So, stash the event in the loop's data struct. When we return
151
+ * the ev_loop() call being made in the Coolio_Loop_run_once_blocking()
152
+ * function below will also return, at which point the GVL is
153
+ * reacquired and we can call out to Ruby */
154
+
155
+ /* Grow the event buffer if it's too small */
156
+ if(loop_data->events_received >= loop_data->eventbuf_size) {
157
+ loop_data->eventbuf_size *= 2;
158
+ loop_data->eventbuf = (struct Coolio_Event *)xrealloc(
159
+ loop_data->eventbuf,
160
+ sizeof(struct Coolio_Event) * loop_data->eventbuf_size
161
+ );
162
+ }
163
+
164
+ loop_data->eventbuf[loop_data->events_received].watcher = watcher;
165
+ loop_data->eventbuf[loop_data->events_received].revents = revents;
166
+
167
+ loop_data->events_received++;
168
+ }
169
+
170
+ /* Called whenever a timeout fires on the event loop */
171
+ static void Coolio_Loop_timeout_callback(struct ev_loop *ev_loop, struct ev_timer *timer, int revents)
172
+ {
173
+ /* We don't actually need to do anything here, the mere firing of the
174
+ timer is sufficient to interrupt the selector. However, libev still wants a callback */
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(int argc, VALUE *argv, VALUE self)
184
+ {
185
+ VALUE timeout;
186
+ VALUE nevents;
187
+ struct Coolio_Loop *loop_data;
188
+
189
+ rb_scan_args(argc, argv, "01", &timeout);
190
+
191
+ if (timeout != Qnil && NUM2DBL(timeout) < 0) {
192
+ rb_raise(rb_eArgError, "time interval must be positive");
193
+ }
194
+
195
+ Data_Get_Struct(self, struct Coolio_Loop, loop_data);
196
+
197
+ assert(loop_data->ev_loop && !loop_data->events_received);
198
+
199
+ /* Implement the optional timeout (if any) as a ev_timer */
200
+ /* Using the technique written at
201
+ http://pod.tst.eu/http://cvs.schmorp.de/libev/ev.pod#code_ev_timer_code_relative_and_opti,
202
+ the timer is not stopped/started everytime when timeout is specified, instead,
203
+ the timer is stopped when timeout is not specified. */
204
+ if (timeout != Qnil) {
205
+ /* It seems libev is not a fan of timers being zero, so fudge a little */
206
+ loop_data->timer.repeat = NUM2DBL(timeout) + 0.0001;
207
+ ev_timer_again(loop_data->ev_loop, &loop_data->timer);
208
+ } else {
209
+ ev_timer_stop(loop_data->ev_loop, &loop_data->timer);
210
+ }
211
+
212
+ /* libev is patched to release the GIL when it makes its system call */
213
+ RUN_LOOP(loop_data, EVLOOP_ONESHOT);
214
+
215
+ Coolio_Loop_dispatch_events(loop_data);
216
+ nevents = INT2NUM(loop_data->events_received);
217
+ loop_data->events_received = 0;
218
+
219
+ return nevents;
220
+ }
221
+
222
+ /**
223
+ * call-seq:
224
+ * Coolio::Loop.run_nonblock -> nil
225
+ *
226
+ * Run the Coolio::Loop once, but return immediately if there are no pending events.
227
+ */
228
+ static VALUE Coolio_Loop_run_nonblock(VALUE self)
229
+ {
230
+ struct Coolio_Loop *loop_data;
231
+ VALUE nevents;
232
+
233
+ Data_Get_Struct(self, struct Coolio_Loop, loop_data);
234
+
235
+ assert(loop_data->ev_loop && !loop_data->events_received);
236
+
237
+ RUN_LOOP(loop_data, EVLOOP_NONBLOCK);
238
+ Coolio_Loop_dispatch_events(loop_data);
239
+
240
+ nevents = INT2NUM(loop_data->events_received);
241
+ loop_data->events_received = 0;
242
+
243
+ return nevents;
244
+ }
245
+
246
+ static void Coolio_Loop_dispatch_events(struct Coolio_Loop *loop_data)
247
+ {
248
+ int i;
249
+ struct Coolio_Watcher *watcher_data;
250
+
251
+ for(i = 0; i < loop_data->events_received; i++) {
252
+ /* A watcher with pending events may have been detached from the loop
253
+ * during the dispatch process. If so, the watcher clears the pending
254
+ * events, so skip over them */
255
+ if(loop_data->eventbuf[i].watcher == Qnil)
256
+ continue;
257
+
258
+ Data_Get_Struct(loop_data->eventbuf[i].watcher, struct Coolio_Watcher, watcher_data);
259
+ watcher_data->dispatch_callback(loop_data->eventbuf[i].watcher, loop_data->eventbuf[i].revents);
260
+ }
261
+ }
@@ -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
+ }