sensu-em 2.0.0-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (177) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +21 -0
  3. data/.travis.yml +12 -0
  4. data/.yardopts +7 -0
  5. data/CHANGELOG.md +33 -0
  6. data/GNU +281 -0
  7. data/Gemfile +2 -0
  8. data/LICENSE +60 -0
  9. data/README.md +109 -0
  10. data/Rakefile +20 -0
  11. data/docs/DocumentationGuidesIndex.md +27 -0
  12. data/docs/GettingStarted.md +521 -0
  13. data/docs/old/ChangeLog +211 -0
  14. data/docs/old/DEFERRABLES +246 -0
  15. data/docs/old/EPOLL +141 -0
  16. data/docs/old/INSTALL +13 -0
  17. data/docs/old/KEYBOARD +42 -0
  18. data/docs/old/LEGAL +25 -0
  19. data/docs/old/LIGHTWEIGHT_CONCURRENCY +130 -0
  20. data/docs/old/PURE_RUBY +75 -0
  21. data/docs/old/RELEASE_NOTES +94 -0
  22. data/docs/old/SMTP +4 -0
  23. data/docs/old/SPAWNED_PROCESSES +148 -0
  24. data/docs/old/TODO +8 -0
  25. data/eventmachine.gemspec +38 -0
  26. data/examples/guides/getting_started/01_eventmachine_echo_server.rb +18 -0
  27. data/examples/guides/getting_started/02_eventmachine_echo_server_that_recognizes_exit_command.rb +22 -0
  28. data/examples/guides/getting_started/03_simple_chat_server.rb +149 -0
  29. data/examples/guides/getting_started/04_simple_chat_server_step_one.rb +27 -0
  30. data/examples/guides/getting_started/05_simple_chat_server_step_two.rb +43 -0
  31. data/examples/guides/getting_started/06_simple_chat_server_step_three.rb +98 -0
  32. data/examples/guides/getting_started/07_simple_chat_server_step_four.rb +121 -0
  33. data/examples/guides/getting_started/08_simple_chat_server_step_five.rb +141 -0
  34. data/examples/old/ex_channel.rb +43 -0
  35. data/examples/old/ex_queue.rb +2 -0
  36. data/examples/old/ex_tick_loop_array.rb +15 -0
  37. data/examples/old/ex_tick_loop_counter.rb +32 -0
  38. data/examples/old/helper.rb +2 -0
  39. data/ext/binder.cpp +124 -0
  40. data/ext/binder.h +46 -0
  41. data/ext/cmain.cpp +887 -0
  42. data/ext/ed.cpp +1988 -0
  43. data/ext/ed.h +422 -0
  44. data/ext/em.cpp +2352 -0
  45. data/ext/em.h +253 -0
  46. data/ext/eventmachine.h +128 -0
  47. data/ext/extconf.rb +179 -0
  48. data/ext/fastfilereader/extconf.rb +103 -0
  49. data/ext/fastfilereader/mapper.cpp +214 -0
  50. data/ext/fastfilereader/mapper.h +59 -0
  51. data/ext/fastfilereader/rubymain.cpp +127 -0
  52. data/ext/kb.cpp +79 -0
  53. data/ext/page.cpp +107 -0
  54. data/ext/page.h +51 -0
  55. data/ext/pipe.cpp +347 -0
  56. data/ext/project.h +161 -0
  57. data/ext/rubymain.cpp +1318 -0
  58. data/ext/ssl.cpp +468 -0
  59. data/ext/ssl.h +94 -0
  60. data/java/.classpath +6 -0
  61. data/java/.gitignore +1 -0
  62. data/java/.project +17 -0
  63. data/java/src/com/rubyeventmachine/DatagramPacket.java +13 -0
  64. data/java/src/com/rubyeventmachine/EmReactor.java +529 -0
  65. data/java/src/com/rubyeventmachine/EmReactorException.java +40 -0
  66. data/java/src/com/rubyeventmachine/EventCallback.java +7 -0
  67. data/java/src/com/rubyeventmachine/EventCode.java +26 -0
  68. data/java/src/com/rubyeventmachine/EventableChannel.java +130 -0
  69. data/java/src/com/rubyeventmachine/EventableDatagramChannel.java +180 -0
  70. data/java/src/com/rubyeventmachine/EventableSocketChannel.java +405 -0
  71. data/java/src/com/rubyeventmachine/SslBox.java +310 -0
  72. data/lib/em/buftok.rb +110 -0
  73. data/lib/em/callback.rb +58 -0
  74. data/lib/em/channel.rb +64 -0
  75. data/lib/em/completion.rb +304 -0
  76. data/lib/em/connection.rb +712 -0
  77. data/lib/em/deferrable/pool.rb +2 -0
  78. data/lib/em/deferrable.rb +210 -0
  79. data/lib/em/file_watch.rb +73 -0
  80. data/lib/em/future.rb +61 -0
  81. data/lib/em/iterator.rb +231 -0
  82. data/lib/em/messages.rb +66 -0
  83. data/lib/em/pool.rb +151 -0
  84. data/lib/em/process_watch.rb +45 -0
  85. data/lib/em/processes.rb +123 -0
  86. data/lib/em/protocols/header_and_content.rb +138 -0
  87. data/lib/em/protocols/httpclient.rb +279 -0
  88. data/lib/em/protocols/httpclient2.rb +600 -0
  89. data/lib/em/protocols/line_and_text.rb +125 -0
  90. data/lib/em/protocols/line_protocol.rb +29 -0
  91. data/lib/em/protocols/linetext2.rb +161 -0
  92. data/lib/em/protocols/memcache.rb +331 -0
  93. data/lib/em/protocols/object_protocol.rb +46 -0
  94. data/lib/em/protocols/postgres3.rb +246 -0
  95. data/lib/em/protocols/saslauth.rb +175 -0
  96. data/lib/em/protocols/smtpclient.rb +365 -0
  97. data/lib/em/protocols/smtpserver.rb +643 -0
  98. data/lib/em/protocols/socks4.rb +66 -0
  99. data/lib/em/protocols/stomp.rb +205 -0
  100. data/lib/em/protocols/tcptest.rb +54 -0
  101. data/lib/em/protocols.rb +37 -0
  102. data/lib/em/pure_ruby.rb +1017 -0
  103. data/lib/em/queue.rb +71 -0
  104. data/lib/em/resolver.rb +209 -0
  105. data/lib/em/spawnable.rb +84 -0
  106. data/lib/em/streamer.rb +118 -0
  107. data/lib/em/threaded_resource.rb +90 -0
  108. data/lib/em/tick_loop.rb +85 -0
  109. data/lib/em/timers.rb +61 -0
  110. data/lib/em/version.rb +3 -0
  111. data/lib/eventmachine.rb +1553 -0
  112. data/lib/jeventmachine.rb +321 -0
  113. data/lib/rubyeventmachine.jar +0 -0
  114. data/rakelib/cpp.rake_example +77 -0
  115. data/rakelib/package.rake +98 -0
  116. data/rakelib/test.rake +8 -0
  117. data/tests/client.crt +31 -0
  118. data/tests/client.key +51 -0
  119. data/tests/em_test_helper.rb +64 -0
  120. data/tests/server.crt +36 -0
  121. data/tests/server.key +51 -0
  122. data/tests/test_attach.rb +150 -0
  123. data/tests/test_basic.rb +294 -0
  124. data/tests/test_channel.rb +62 -0
  125. data/tests/test_completion.rb +177 -0
  126. data/tests/test_connection_count.rb +53 -0
  127. data/tests/test_defer.rb +18 -0
  128. data/tests/test_deferrable.rb +35 -0
  129. data/tests/test_epoll.rb +145 -0
  130. data/tests/test_error_handler.rb +38 -0
  131. data/tests/test_exc.rb +28 -0
  132. data/tests/test_file_watch.rb +65 -0
  133. data/tests/test_futures.rb +170 -0
  134. data/tests/test_get_sock_opt.rb +37 -0
  135. data/tests/test_handler_check.rb +35 -0
  136. data/tests/test_hc.rb +155 -0
  137. data/tests/test_httpclient.rb +190 -0
  138. data/tests/test_httpclient2.rb +133 -0
  139. data/tests/test_idle_connection.rb +25 -0
  140. data/tests/test_inactivity_timeout.rb +54 -0
  141. data/tests/test_iterator.rb +97 -0
  142. data/tests/test_kb.rb +34 -0
  143. data/tests/test_line_protocol.rb +33 -0
  144. data/tests/test_ltp.rb +138 -0
  145. data/tests/test_ltp2.rb +288 -0
  146. data/tests/test_next_tick.rb +104 -0
  147. data/tests/test_object_protocol.rb +36 -0
  148. data/tests/test_pause.rb +102 -0
  149. data/tests/test_pending_connect_timeout.rb +52 -0
  150. data/tests/test_pool.rb +194 -0
  151. data/tests/test_process_watch.rb +48 -0
  152. data/tests/test_processes.rb +128 -0
  153. data/tests/test_proxy_connection.rb +180 -0
  154. data/tests/test_pure.rb +88 -0
  155. data/tests/test_queue.rb +50 -0
  156. data/tests/test_resolver.rb +55 -0
  157. data/tests/test_running.rb +14 -0
  158. data/tests/test_sasl.rb +47 -0
  159. data/tests/test_send_file.rb +217 -0
  160. data/tests/test_servers.rb +33 -0
  161. data/tests/test_set_sock_opt.rb +37 -0
  162. data/tests/test_shutdown_hooks.rb +23 -0
  163. data/tests/test_smtpclient.rb +55 -0
  164. data/tests/test_smtpserver.rb +57 -0
  165. data/tests/test_spawn.rb +293 -0
  166. data/tests/test_ssl_args.rb +78 -0
  167. data/tests/test_ssl_echo_data.rb +60 -0
  168. data/tests/test_ssl_methods.rb +56 -0
  169. data/tests/test_ssl_verify.rb +82 -0
  170. data/tests/test_stomp.rb +37 -0
  171. data/tests/test_system.rb +42 -0
  172. data/tests/test_threaded_resource.rb +53 -0
  173. data/tests/test_tick_loop.rb +59 -0
  174. data/tests/test_timers.rb +123 -0
  175. data/tests/test_ud.rb +8 -0
  176. data/tests/test_unbind_reason.rb +48 -0
  177. metadata +297 -0
data/ext/rubymain.cpp ADDED
@@ -0,0 +1,1318 @@
1
+ /*****************************************************************************
2
+
3
+ $Id$
4
+
5
+ File: rubymain.cpp
6
+ Date: 06Apr06
7
+
8
+ Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
9
+ Gmail: blackhedd
10
+
11
+ This program is free software; you can redistribute it and/or modify
12
+ it under the terms of either: 1) the GNU General Public License
13
+ as published by the Free Software Foundation; either version 2 of the
14
+ License, or (at your option) any later version; or 2) Ruby's License.
15
+
16
+ See the file COPYING for complete licensing information.
17
+
18
+ *****************************************************************************/
19
+
20
+ #include "project.h"
21
+ #include "eventmachine.h"
22
+ #include <ruby.h>
23
+
24
+ #ifndef RFLOAT_VALUE
25
+ #define RFLOAT_VALUE(arg) RFLOAT(arg)->value
26
+ #endif
27
+
28
+ /*******
29
+ Statics
30
+ *******/
31
+
32
+ static VALUE EmModule;
33
+ static VALUE EmConnection;
34
+ static VALUE EmConnsHash;
35
+ static VALUE EmTimersHash;
36
+
37
+ static VALUE EM_eConnectionError;
38
+ static VALUE EM_eUnknownTimerFired;
39
+ static VALUE EM_eConnectionNotBound;
40
+ static VALUE EM_eUnsupported;
41
+
42
+ static VALUE Intern_at_signature;
43
+ static VALUE Intern_at_timers;
44
+ static VALUE Intern_at_conns;
45
+ static VALUE Intern_at_error_handler;
46
+ static VALUE Intern_event_callback;
47
+ static VALUE Intern_run_deferred_callbacks;
48
+ static VALUE Intern_delete;
49
+ static VALUE Intern_call;
50
+ static VALUE Intern_receive_data;
51
+ static VALUE Intern_ssl_handshake_completed;
52
+ static VALUE Intern_ssl_verify_peer;
53
+ static VALUE Intern_notify_readable;
54
+ static VALUE Intern_notify_writable;
55
+ static VALUE Intern_proxy_target_unbound;
56
+ static VALUE Intern_proxy_completed;
57
+ static VALUE Intern_connection_completed;
58
+
59
+ static VALUE rb_cProcStatus;
60
+
61
+ struct em_event {
62
+ unsigned long signature;
63
+ int event;
64
+ const char *data_str;
65
+ unsigned long data_num;
66
+ };
67
+
68
+ static inline VALUE ensure_conn(const unsigned long signature)
69
+ {
70
+ VALUE conn = rb_hash_aref (EmConnsHash, ULONG2NUM (signature));
71
+ if (conn == Qnil)
72
+ rb_raise (EM_eConnectionNotBound, "unknown connection: %lu", signature);
73
+ return conn;
74
+ }
75
+
76
+
77
+ /****************
78
+ t_event_callback
79
+ ****************/
80
+
81
+ static inline void event_callback (struct em_event* e)
82
+ {
83
+ const unsigned long signature = e->signature;
84
+ int event = e->event;
85
+ const char *data_str = e->data_str;
86
+ const unsigned long data_num = e->data_num;
87
+
88
+ switch (event) {
89
+ case EM_CONNECTION_READ:
90
+ {
91
+ VALUE conn = rb_hash_aref (EmConnsHash, ULONG2NUM (signature));
92
+ if (conn == Qnil)
93
+ rb_raise (EM_eConnectionNotBound, "received %lu bytes of data for unknown signature: %lu", data_num, signature);
94
+ rb_funcall (conn, Intern_receive_data, 1, rb_str_new (data_str, data_num));
95
+ return;
96
+ }
97
+ case EM_CONNECTION_ACCEPTED:
98
+ {
99
+ rb_funcall (EmModule, Intern_event_callback, 3, ULONG2NUM(signature), INT2FIX(event), ULONG2NUM(data_num));
100
+ return;
101
+ }
102
+ case EM_CONNECTION_UNBOUND:
103
+ {
104
+ rb_funcall (EmModule, Intern_event_callback, 3, ULONG2NUM(signature), INT2FIX(event), ULONG2NUM(data_num));
105
+ return;
106
+ }
107
+ case EM_CONNECTION_COMPLETED:
108
+ {
109
+ VALUE conn = ensure_conn(signature);
110
+ rb_funcall (conn, Intern_connection_completed, 0);
111
+ return;
112
+ }
113
+ case EM_CONNECTION_NOTIFY_READABLE:
114
+ {
115
+ VALUE conn = ensure_conn(signature);
116
+ rb_funcall (conn, Intern_notify_readable, 0);
117
+ return;
118
+ }
119
+ case EM_CONNECTION_NOTIFY_WRITABLE:
120
+ {
121
+ VALUE conn = ensure_conn(signature);
122
+ rb_funcall (conn, Intern_notify_writable, 0);
123
+ return;
124
+ }
125
+ case EM_LOOPBREAK_SIGNAL:
126
+ {
127
+ rb_funcall (EmModule, Intern_run_deferred_callbacks, 0);
128
+ return;
129
+ }
130
+ case EM_TIMER_FIRED:
131
+ {
132
+ VALUE timer = rb_funcall (EmTimersHash, Intern_delete, 1, ULONG2NUM (data_num));
133
+ if (timer == Qnil) {
134
+ rb_raise (EM_eUnknownTimerFired, "no such timer: %lu", data_num);
135
+ } else if (timer == Qfalse) {
136
+ /* Timer Canceled */
137
+ } else {
138
+ rb_funcall (timer, Intern_call, 0);
139
+ }
140
+ return;
141
+ }
142
+ #ifdef WITH_SSL
143
+ case EM_SSL_HANDSHAKE_COMPLETED:
144
+ {
145
+ VALUE conn = ensure_conn(signature);
146
+ rb_funcall (conn, Intern_ssl_handshake_completed, 0);
147
+ return;
148
+ }
149
+ case EM_SSL_VERIFY:
150
+ {
151
+ VALUE conn = ensure_conn(signature);
152
+ VALUE should_accept = rb_funcall (conn, Intern_ssl_verify_peer, 1, rb_str_new(data_str, data_num));
153
+ if (RTEST(should_accept))
154
+ evma_accept_ssl_peer (signature);
155
+ return;
156
+ }
157
+ #endif
158
+ case EM_PROXY_TARGET_UNBOUND:
159
+ {
160
+ VALUE conn = ensure_conn(signature);
161
+ rb_funcall (conn, Intern_proxy_target_unbound, 0);
162
+ return;
163
+ }
164
+ case EM_PROXY_COMPLETED:
165
+ {
166
+ VALUE conn = ensure_conn(signature);
167
+ rb_funcall (conn, Intern_proxy_completed, 0);
168
+ return;
169
+ }
170
+ }
171
+ }
172
+
173
+ /*******************
174
+ event_error_handler
175
+ *******************/
176
+
177
+ static void event_error_handler(VALUE unused, VALUE err)
178
+ {
179
+ VALUE error_handler = rb_ivar_get(EmModule, Intern_at_error_handler);
180
+ rb_funcall (error_handler, Intern_call, 1, err);
181
+ }
182
+
183
+ /**********************
184
+ event_callback_wrapper
185
+ **********************/
186
+
187
+ static void event_callback_wrapper (const unsigned long signature, int event, const char *data_str, const unsigned long data_num)
188
+ {
189
+ struct em_event e;
190
+ e.signature = signature;
191
+ e.event = event;
192
+ e.data_str = data_str;
193
+ e.data_num = data_num;
194
+
195
+ if (!rb_ivar_defined(EmModule, Intern_at_error_handler))
196
+ event_callback(&e);
197
+ else
198
+ rb_rescue((VALUE (*)(ANYARGS))event_callback, (VALUE)&e, (VALUE (*)(ANYARGS))event_error_handler, Qnil);
199
+ }
200
+
201
+ /**************************
202
+ t_initialize_event_machine
203
+ **************************/
204
+
205
+ static VALUE t_initialize_event_machine (VALUE self)
206
+ {
207
+ EmConnsHash = rb_ivar_get (EmModule, Intern_at_conns);
208
+ EmTimersHash = rb_ivar_get (EmModule, Intern_at_timers);
209
+ assert(EmConnsHash != Qnil);
210
+ assert(EmTimersHash != Qnil);
211
+ evma_initialize_library ((EMCallback)event_callback_wrapper);
212
+ return Qnil;
213
+ }
214
+
215
+
216
+
217
+ /*****************************
218
+ t_run_machine_without_threads
219
+ *****************************/
220
+
221
+ static VALUE t_run_machine_without_threads (VALUE self)
222
+ {
223
+ evma_run_machine();
224
+ return Qnil;
225
+ }
226
+
227
+
228
+ /*******************
229
+ t_add_oneshot_timer
230
+ *******************/
231
+
232
+ static VALUE t_add_oneshot_timer (VALUE self, VALUE interval)
233
+ {
234
+ const unsigned long f = evma_install_oneshot_timer (FIX2INT (interval));
235
+ if (!f)
236
+ rb_raise (rb_eRuntimeError, "%s", "ran out of timers; use #set_max_timers to increase limit");
237
+ return ULONG2NUM (f);
238
+ }
239
+
240
+
241
+ /**************
242
+ t_start_server
243
+ **************/
244
+
245
+ static VALUE t_start_server (VALUE self, VALUE server, VALUE port)
246
+ {
247
+ const unsigned long f = evma_create_tcp_server (StringValuePtr(server), FIX2INT(port));
248
+ if (!f)
249
+ rb_raise (rb_eRuntimeError, "%s", "no acceptor (port is in use or requires root privileges)");
250
+ return ULONG2NUM (f);
251
+ }
252
+
253
+ /*************
254
+ t_stop_server
255
+ *************/
256
+
257
+ static VALUE t_stop_server (VALUE self, VALUE signature)
258
+ {
259
+ evma_stop_tcp_server (NUM2ULONG (signature));
260
+ return Qnil;
261
+ }
262
+
263
+
264
+ /*******************
265
+ t_start_unix_server
266
+ *******************/
267
+
268
+ static VALUE t_start_unix_server (VALUE self, VALUE filename)
269
+ {
270
+ const unsigned long f = evma_create_unix_domain_server (StringValuePtr(filename));
271
+ if (!f)
272
+ rb_raise (rb_eRuntimeError, "%s", "no unix-domain acceptor");
273
+ return ULONG2NUM (f);
274
+ }
275
+
276
+ /********************
277
+ t_attach_sd
278
+ ********************/
279
+
280
+ static VALUE t_attach_sd(VALUE self, VALUE sd)
281
+ {
282
+ const unsigned long f = evma_attach_sd(FIX2INT(sd));
283
+ if (!f)
284
+ rb_raise (rb_eRuntimeError, "%s", "no socket descriptor acceptor");
285
+ return ULONG2NUM (f);
286
+ }
287
+
288
+
289
+ /***********
290
+ t_send_data
291
+ ***********/
292
+
293
+ static VALUE t_send_data (VALUE self, VALUE signature, VALUE data, VALUE data_length)
294
+ {
295
+ int b = evma_send_data_to_connection (NUM2ULONG (signature), StringValuePtr (data), FIX2INT (data_length));
296
+ return INT2NUM (b);
297
+ }
298
+
299
+
300
+ /***********
301
+ t_start_tls
302
+ ***********/
303
+
304
+ static VALUE t_start_tls (VALUE self, VALUE signature)
305
+ {
306
+ evma_start_tls (NUM2ULONG (signature));
307
+ return Qnil;
308
+ }
309
+
310
+ /***************
311
+ t_set_tls_parms
312
+ ***************/
313
+
314
+ static VALUE t_set_tls_parms (VALUE self, VALUE signature, VALUE privkeyfile, VALUE certchainfile, VALUE verify_peer)
315
+ {
316
+ /* set_tls_parms takes a series of positional arguments for specifying such things
317
+ * as private keys and certificate chains.
318
+ * It's expected that the parameter list will grow as we add more supported features.
319
+ * ALL of these parameters are optional, and can be specified as empty or NULL strings.
320
+ */
321
+ evma_set_tls_parms (NUM2ULONG (signature), StringValuePtr (privkeyfile), StringValuePtr (certchainfile), (verify_peer == Qtrue ? 1 : 0));
322
+ return Qnil;
323
+ }
324
+
325
+ /***************
326
+ t_get_peer_cert
327
+ ***************/
328
+
329
+ static VALUE t_get_peer_cert (VALUE self, VALUE signature)
330
+ {
331
+ VALUE ret = Qnil;
332
+
333
+ #ifdef WITH_SSL
334
+ X509 *cert = NULL;
335
+ BUF_MEM *buf;
336
+ BIO *out;
337
+
338
+ cert = evma_get_peer_cert (NUM2ULONG (signature));
339
+
340
+ if (cert != NULL) {
341
+ out = BIO_new(BIO_s_mem());
342
+ PEM_write_bio_X509(out, cert);
343
+ BIO_get_mem_ptr(out, &buf);
344
+ ret = rb_str_new(buf->data, buf->length);
345
+ X509_free(cert);
346
+ BIO_free(out);
347
+ }
348
+ #endif
349
+
350
+ return ret;
351
+ }
352
+
353
+ /**************
354
+ t_get_peername
355
+ **************/
356
+
357
+ static VALUE t_get_peername (VALUE self, VALUE signature)
358
+ {
359
+ char buf[1024];
360
+ socklen_t len = sizeof buf;
361
+ if (evma_get_peername (NUM2ULONG (signature), (struct sockaddr*)buf, &len)) {
362
+ return rb_str_new (buf, len);
363
+ }
364
+
365
+ return Qnil;
366
+ }
367
+
368
+ /**************
369
+ t_get_sockname
370
+ **************/
371
+
372
+ static VALUE t_get_sockname (VALUE self, VALUE signature)
373
+ {
374
+ char buf[1024];
375
+ socklen_t len = sizeof buf;
376
+ if (evma_get_sockname (NUM2ULONG (signature), (struct sockaddr*)buf, &len)) {
377
+ return rb_str_new (buf, len);
378
+ }
379
+
380
+ return Qnil;
381
+ }
382
+
383
+ /********************
384
+ t_get_subprocess_pid
385
+ ********************/
386
+
387
+ static VALUE t_get_subprocess_pid (VALUE self, VALUE signature)
388
+ {
389
+ pid_t pid;
390
+ if (evma_get_subprocess_pid (NUM2ULONG (signature), &pid)) {
391
+ return INT2NUM (pid);
392
+ }
393
+
394
+ return Qnil;
395
+ }
396
+
397
+ /***********************
398
+ t_get_subprocess_status
399
+ ***********************/
400
+
401
+ static VALUE t_get_subprocess_status (VALUE self, VALUE signature)
402
+ {
403
+ VALUE proc_status = Qnil;
404
+
405
+ int status;
406
+ pid_t pid;
407
+
408
+ if (evma_get_subprocess_status (NUM2ULONG (signature), &status)) {
409
+ if (evma_get_subprocess_pid (NUM2ULONG (signature), &pid)) {
410
+ proc_status = rb_obj_alloc(rb_cProcStatus);
411
+ rb_iv_set(proc_status, "status", INT2FIX(status));
412
+ rb_iv_set(proc_status, "pid", INT2FIX(pid));
413
+ }
414
+ }
415
+
416
+ return proc_status;
417
+ }
418
+
419
+ /**********************
420
+ t_get_connection_count
421
+ **********************/
422
+
423
+ static VALUE t_get_connection_count (VALUE self)
424
+ {
425
+ return INT2NUM(evma_get_connection_count());
426
+ }
427
+
428
+ /*****************************
429
+ t_get_comm_inactivity_timeout
430
+ *****************************/
431
+
432
+ static VALUE t_get_comm_inactivity_timeout (VALUE self, VALUE signature)
433
+ {
434
+ return rb_float_new(evma_get_comm_inactivity_timeout(NUM2ULONG (signature)));
435
+ }
436
+
437
+ /*****************************
438
+ t_set_comm_inactivity_timeout
439
+ *****************************/
440
+
441
+ static VALUE t_set_comm_inactivity_timeout (VALUE self, VALUE signature, VALUE timeout)
442
+ {
443
+ float ti = RFLOAT_VALUE(timeout);
444
+ if (evma_set_comm_inactivity_timeout(NUM2ULONG(signature), ti)) {
445
+ return Qtrue;
446
+ }
447
+ return Qfalse;
448
+ }
449
+
450
+ /*****************************
451
+ t_get_pending_connect_timeout
452
+ *****************************/
453
+
454
+ static VALUE t_get_pending_connect_timeout (VALUE self, VALUE signature)
455
+ {
456
+ return rb_float_new(evma_get_pending_connect_timeout(NUM2ULONG (signature)));
457
+ }
458
+
459
+ /*****************************
460
+ t_set_pending_connect_timeout
461
+ *****************************/
462
+
463
+ static VALUE t_set_pending_connect_timeout (VALUE self, VALUE signature, VALUE timeout)
464
+ {
465
+ float ti = RFLOAT_VALUE(timeout);
466
+ if (evma_set_pending_connect_timeout(NUM2ULONG(signature), ti)) {
467
+ return Qtrue;
468
+ }
469
+ return Qfalse;
470
+ }
471
+
472
+ /***************
473
+ t_send_datagram
474
+ ***************/
475
+
476
+ static VALUE t_send_datagram (VALUE self, VALUE signature, VALUE data, VALUE data_length, VALUE address, VALUE port)
477
+ {
478
+ int b = evma_send_datagram (NUM2ULONG (signature), StringValuePtr (data), FIX2INT (data_length), StringValuePtr(address), FIX2INT(port));
479
+ return INT2NUM (b);
480
+ }
481
+
482
+
483
+ /******************
484
+ t_close_connection
485
+ ******************/
486
+
487
+ static VALUE t_close_connection (VALUE self, VALUE signature, VALUE after_writing)
488
+ {
489
+ evma_close_connection (NUM2ULONG (signature), ((after_writing == Qtrue) ? 1 : 0));
490
+ return Qnil;
491
+ }
492
+
493
+ /********************************
494
+ t_report_connection_error_status
495
+ ********************************/
496
+
497
+ static VALUE t_report_connection_error_status (VALUE self, VALUE signature)
498
+ {
499
+ int b = evma_report_connection_error_status (NUM2ULONG (signature));
500
+ return INT2NUM (b);
501
+ }
502
+
503
+
504
+
505
+ /****************
506
+ t_connect_server
507
+ ****************/
508
+
509
+ static VALUE t_connect_server (VALUE self, VALUE server, VALUE port)
510
+ {
511
+ // Avoid FIX2INT in this case, because it doesn't deal with type errors properly.
512
+ // Specifically, if the value of port comes in as a string rather than an integer,
513
+ // NUM2INT will throw a type error, but FIX2INT will generate garbage.
514
+
515
+ try {
516
+ const unsigned long f = evma_connect_to_server (NULL, 0, StringValuePtr(server), NUM2INT(port));
517
+ if (!f)
518
+ rb_raise (EM_eConnectionError, "%s", "no connection");
519
+ return ULONG2NUM (f);
520
+ } catch (std::runtime_error e) {
521
+ rb_raise (EM_eConnectionError, "%s", e.what());
522
+ }
523
+ return Qnil;
524
+ }
525
+
526
+ /*********************
527
+ t_bind_connect_server
528
+ *********************/
529
+
530
+ static VALUE t_bind_connect_server (VALUE self, VALUE bind_addr, VALUE bind_port, VALUE server, VALUE port)
531
+ {
532
+ // Avoid FIX2INT in this case, because it doesn't deal with type errors properly.
533
+ // Specifically, if the value of port comes in as a string rather than an integer,
534
+ // NUM2INT will throw a type error, but FIX2INT will generate garbage.
535
+
536
+ try {
537
+ const unsigned long f = evma_connect_to_server (StringValuePtr(bind_addr), NUM2INT(bind_port), StringValuePtr(server), NUM2INT(port));
538
+ if (!f)
539
+ rb_raise (EM_eConnectionError, "%s", "no connection");
540
+ return ULONG2NUM (f);
541
+ } catch (std::runtime_error e) {
542
+ rb_raise (EM_eConnectionError, "%s", e.what());
543
+ }
544
+ return Qnil;
545
+ }
546
+
547
+ /*********************
548
+ t_connect_unix_server
549
+ *********************/
550
+
551
+ static VALUE t_connect_unix_server (VALUE self, VALUE serversocket)
552
+ {
553
+ const unsigned long f = evma_connect_to_unix_server (StringValuePtr(serversocket));
554
+ if (!f)
555
+ rb_raise (rb_eRuntimeError, "%s", "no connection");
556
+ return ULONG2NUM (f);
557
+ }
558
+
559
+ /***********
560
+ t_attach_fd
561
+ ***********/
562
+
563
+ static VALUE t_attach_fd (VALUE self, VALUE file_descriptor, VALUE watch_mode)
564
+ {
565
+ const unsigned long f = evma_attach_fd (NUM2INT(file_descriptor), watch_mode == Qtrue);
566
+ if (!f)
567
+ rb_raise (rb_eRuntimeError, "%s", "no connection");
568
+ return ULONG2NUM (f);
569
+ }
570
+
571
+ /***********
572
+ t_detach_fd
573
+ ***********/
574
+
575
+ static VALUE t_detach_fd (VALUE self, VALUE signature)
576
+ {
577
+ return INT2NUM(evma_detach_fd (NUM2ULONG (signature)));
578
+ }
579
+
580
+ /*********************
581
+ t_get_file_descriptor
582
+ *********************/
583
+ static VALUE t_get_file_descriptor (VALUE self, VALUE signature)
584
+ {
585
+ return INT2NUM(evma_get_file_descriptor (NUM2ULONG (signature)));
586
+ }
587
+
588
+ /**************
589
+ t_get_sock_opt
590
+ **************/
591
+
592
+ static VALUE t_get_sock_opt (VALUE self, VALUE signature, VALUE lev, VALUE optname)
593
+ {
594
+ int fd = evma_get_file_descriptor (NUM2ULONG (signature));
595
+ int level = NUM2INT(lev), option = NUM2INT(optname);
596
+ socklen_t len = 128;
597
+ char buf[128];
598
+
599
+ if (getsockopt(fd, level, option, buf, &len) < 0)
600
+ rb_sys_fail("getsockopt");
601
+
602
+ return rb_str_new(buf, len);
603
+ }
604
+
605
+ /**************
606
+ t_set_sock_opt
607
+ **************/
608
+
609
+ static VALUE t_set_sock_opt (VALUE self, VALUE signature, VALUE lev, VALUE optname, VALUE optval)
610
+ {
611
+ int fd = evma_get_file_descriptor (NUM2ULONG (signature));
612
+ int level = NUM2INT(lev), option = NUM2INT(optname);
613
+ int i;
614
+ void *v;
615
+ socklen_t len;
616
+
617
+ switch (TYPE(optval)) {
618
+ case T_FIXNUM:
619
+ i = FIX2INT(optval);
620
+ goto numval;
621
+ case T_FALSE:
622
+ i = 0;
623
+ goto numval;
624
+ case T_TRUE:
625
+ i = 1;
626
+ numval:
627
+ v = (void*)&i; len = sizeof(i);
628
+ break;
629
+ default:
630
+ StringValue(optval);
631
+ v = RSTRING_PTR(optval);
632
+ len = RSTRING_LENINT(optval);
633
+ break;
634
+ }
635
+
636
+
637
+ if (setsockopt(fd, level, option, (char *)v, len) < 0)
638
+ rb_sys_fail("setsockopt");
639
+
640
+ return INT2FIX(0);
641
+ }
642
+
643
+ /********************
644
+ t_is_notify_readable
645
+ ********************/
646
+
647
+ static VALUE t_is_notify_readable (VALUE self, VALUE signature)
648
+ {
649
+ return evma_is_notify_readable(NUM2ULONG (signature)) ? Qtrue : Qfalse;
650
+ }
651
+
652
+ /*********************
653
+ t_set_notify_readable
654
+ *********************/
655
+
656
+ static VALUE t_set_notify_readable (VALUE self, VALUE signature, VALUE mode)
657
+ {
658
+ evma_set_notify_readable(NUM2ULONG (signature), mode == Qtrue);
659
+ return Qnil;
660
+ }
661
+
662
+ /********************
663
+ t_is_notify_readable
664
+ ********************/
665
+
666
+ static VALUE t_is_notify_writable (VALUE self, VALUE signature)
667
+ {
668
+ return evma_is_notify_writable(NUM2ULONG (signature)) ? Qtrue : Qfalse;
669
+ }
670
+
671
+ /*********************
672
+ t_set_notify_writable
673
+ *********************/
674
+
675
+ static VALUE t_set_notify_writable (VALUE self, VALUE signature, VALUE mode)
676
+ {
677
+ evma_set_notify_writable(NUM2ULONG (signature), mode == Qtrue);
678
+ return Qnil;
679
+ }
680
+
681
+ /*******
682
+ t_pause
683
+ *******/
684
+
685
+ static VALUE t_pause (VALUE self, VALUE signature)
686
+ {
687
+ return evma_pause(NUM2ULONG (signature)) ? Qtrue : Qfalse;
688
+ }
689
+
690
+ /********
691
+ t_resume
692
+ ********/
693
+
694
+ static VALUE t_resume (VALUE self, VALUE signature)
695
+ {
696
+ return evma_resume(NUM2ULONG (signature)) ? Qtrue : Qfalse;
697
+ }
698
+
699
+ /**********
700
+ t_paused_p
701
+ **********/
702
+
703
+ static VALUE t_paused_p (VALUE self, VALUE signature)
704
+ {
705
+ return evma_is_paused(NUM2ULONG (signature)) ? Qtrue : Qfalse;
706
+ }
707
+
708
+ /*********************
709
+ t_num_close_scheduled
710
+ *********************/
711
+
712
+ static VALUE t_num_close_scheduled (VALUE self)
713
+ {
714
+ return INT2FIX(evma_num_close_scheduled());
715
+ }
716
+
717
+ /*****************
718
+ t_open_udp_socket
719
+ *****************/
720
+
721
+ static VALUE t_open_udp_socket (VALUE self, VALUE server, VALUE port)
722
+ {
723
+ const unsigned long f = evma_open_datagram_socket (StringValuePtr(server), FIX2INT(port));
724
+ if (!f)
725
+ rb_raise (rb_eRuntimeError, "%s", "no datagram socket");
726
+ return ULONG2NUM (f);
727
+ }
728
+
729
+
730
+
731
+ /*****************
732
+ t_release_machine
733
+ *****************/
734
+
735
+ static VALUE t_release_machine (VALUE self)
736
+ {
737
+ evma_release_library();
738
+ return Qnil;
739
+ }
740
+
741
+
742
+ /******
743
+ t_stop
744
+ ******/
745
+
746
+ static VALUE t_stop (VALUE self)
747
+ {
748
+ evma_stop_machine();
749
+ return Qnil;
750
+ }
751
+
752
+ /******************
753
+ t_signal_loopbreak
754
+ ******************/
755
+
756
+ static VALUE t_signal_loopbreak (VALUE self)
757
+ {
758
+ evma_signal_loopbreak();
759
+ return Qnil;
760
+ }
761
+
762
+ /**************
763
+ t_library_type
764
+ **************/
765
+
766
+ static VALUE t_library_type (VALUE self)
767
+ {
768
+ return rb_eval_string (":extension");
769
+ }
770
+
771
+
772
+
773
+ /*******************
774
+ t_set_timer_quantum
775
+ *******************/
776
+
777
+ static VALUE t_set_timer_quantum (VALUE self, VALUE interval)
778
+ {
779
+ evma_set_timer_quantum (FIX2INT (interval));
780
+ return Qnil;
781
+ }
782
+
783
+ /********************
784
+ t_get_max_timer_count
785
+ ********************/
786
+
787
+ static VALUE t_get_max_timer_count (VALUE self)
788
+ {
789
+ return INT2FIX (evma_get_max_timer_count());
790
+ }
791
+
792
+ /********************
793
+ t_set_max_timer_count
794
+ ********************/
795
+
796
+ static VALUE t_set_max_timer_count (VALUE self, VALUE ct)
797
+ {
798
+ evma_set_max_timer_count (FIX2INT (ct));
799
+ return Qnil;
800
+ }
801
+
802
+ /***************
803
+ t_setuid_string
804
+ ***************/
805
+
806
+ static VALUE t_setuid_string (VALUE self, VALUE username)
807
+ {
808
+ evma_setuid_string (StringValuePtr (username));
809
+ return Qnil;
810
+ }
811
+
812
+
813
+
814
+ /**************
815
+ t_invoke_popen
816
+ **************/
817
+
818
+ static VALUE t_invoke_popen (VALUE self, VALUE cmd)
819
+ {
820
+ // 1.8.7+
821
+ #ifdef RARRAY_LEN
822
+ int len = RARRAY_LEN(cmd);
823
+ #else
824
+ int len = RARRAY (cmd)->len;
825
+ #endif
826
+ if (len >= 2048)
827
+ rb_raise (rb_eRuntimeError, "%s", "too many arguments to popen");
828
+ char *strings [2048];
829
+ for (int i=0; i < len; i++) {
830
+ VALUE ix = INT2FIX (i);
831
+ VALUE s = rb_ary_aref (1, &ix, cmd);
832
+ strings[i] = StringValuePtr (s);
833
+ }
834
+ strings[len] = NULL;
835
+
836
+ const unsigned long f = evma_popen (strings);
837
+ if (!f) {
838
+ char *err = strerror (errno);
839
+ char buf[100];
840
+ memset (buf, 0, sizeof(buf));
841
+ snprintf (buf, sizeof(buf)-1, "no popen: %s", (err?err:"???"));
842
+ rb_raise (rb_eRuntimeError, "%s", buf);
843
+ }
844
+ return ULONG2NUM (f);
845
+ }
846
+
847
+
848
+ /***************
849
+ t_read_keyboard
850
+ ***************/
851
+
852
+ static VALUE t_read_keyboard (VALUE self)
853
+ {
854
+ const unsigned long f = evma_open_keyboard();
855
+ if (!f)
856
+ rb_raise (rb_eRuntimeError, "%s", "no keyboard reader");
857
+ return ULONG2NUM (f);
858
+ }
859
+
860
+
861
+ /****************
862
+ t_watch_filename
863
+ ****************/
864
+
865
+ static VALUE t_watch_filename (VALUE self, VALUE fname)
866
+ {
867
+ try {
868
+ return ULONG2NUM(evma_watch_filename(StringValuePtr(fname)));
869
+ } catch (std::runtime_error e) {
870
+ rb_raise (EM_eUnsupported, "%s", e.what());
871
+ }
872
+ return Qnil;
873
+ }
874
+
875
+
876
+ /******************
877
+ t_unwatch_filename
878
+ ******************/
879
+
880
+ static VALUE t_unwatch_filename (VALUE self, VALUE sig)
881
+ {
882
+ evma_unwatch_filename(NUM2ULONG (sig));
883
+ return Qnil;
884
+ }
885
+
886
+
887
+ /***********
888
+ t_watch_pid
889
+ ***********/
890
+
891
+ static VALUE t_watch_pid (VALUE self, VALUE pid)
892
+ {
893
+ try {
894
+ return ULONG2NUM(evma_watch_pid(NUM2INT(pid)));
895
+ } catch (std::runtime_error e) {
896
+ rb_raise (EM_eUnsupported, "%s", e.what());
897
+ }
898
+ return Qnil;
899
+ }
900
+
901
+
902
+ /*************
903
+ t_unwatch_pid
904
+ *************/
905
+
906
+ static VALUE t_unwatch_pid (VALUE self, VALUE sig)
907
+ {
908
+ evma_unwatch_pid(NUM2ULONG (sig));
909
+ return Qnil;
910
+ }
911
+
912
+
913
+ /**********
914
+ t__epoll_p
915
+ **********/
916
+
917
+ static VALUE t__epoll_p (VALUE self)
918
+ {
919
+ #ifdef HAVE_EPOLL
920
+ return Qtrue;
921
+ #else
922
+ return Qfalse;
923
+ #endif
924
+ }
925
+
926
+ /********
927
+ t__epoll
928
+ ********/
929
+
930
+ static VALUE t__epoll (VALUE self)
931
+ {
932
+ evma_set_epoll (1);
933
+ return Qtrue;
934
+ }
935
+
936
+ /***********
937
+ t__epoll_set
938
+ ***********/
939
+
940
+ static VALUE t__epoll_set (VALUE self, VALUE val)
941
+ {
942
+ if (t__epoll_p(self) == Qfalse)
943
+ rb_raise (EM_eUnsupported, "%s", "epoll is not supported on this platform");
944
+
945
+ evma_set_epoll (val == Qtrue ? 1 : 0);
946
+ return val;
947
+ }
948
+
949
+
950
+ /***********
951
+ t__kqueue_p
952
+ ***********/
953
+
954
+ static VALUE t__kqueue_p (VALUE self)
955
+ {
956
+ #ifdef HAVE_KQUEUE
957
+ return Qtrue;
958
+ #else
959
+ return Qfalse;
960
+ #endif
961
+ }
962
+
963
+ /*********
964
+ t__kqueue
965
+ *********/
966
+
967
+ static VALUE t__kqueue (VALUE self)
968
+ {
969
+ evma_set_kqueue (1);
970
+ return Qtrue;
971
+ }
972
+
973
+ /*************
974
+ t__kqueue_set
975
+ *************/
976
+
977
+ static VALUE t__kqueue_set (VALUE self, VALUE val)
978
+ {
979
+ if (t__kqueue_p(self) == Qfalse)
980
+ rb_raise (EM_eUnsupported, "%s", "kqueue is not supported on this platform");
981
+
982
+ evma_set_kqueue (val == Qtrue ? 1 : 0);
983
+ return val;
984
+ }
985
+
986
+
987
+ /********
988
+ t__ssl_p
989
+ ********/
990
+
991
+ static VALUE t__ssl_p (VALUE self)
992
+ {
993
+ #ifdef WITH_SSL
994
+ return Qtrue;
995
+ #else
996
+ return Qfalse;
997
+ #endif
998
+ }
999
+
1000
+
1001
+ /****************
1002
+ t_send_file_data
1003
+ ****************/
1004
+
1005
+ static VALUE t_send_file_data (VALUE self, VALUE signature, VALUE filename)
1006
+ {
1007
+
1008
+ /* The current implementation of evma_send_file_data_to_connection enforces a strict
1009
+ * upper limit on the file size it will transmit (currently 32K). The function returns
1010
+ * zero on success, -1 if the requested file exceeds its size limit, and a positive
1011
+ * number for other errors.
1012
+ * TODO: Positive return values are actually errno's, which is probably the wrong way to
1013
+ * do this. For one thing it's ugly. For another, we can't be sure zero is never a real errno.
1014
+ */
1015
+
1016
+ int b = evma_send_file_data_to_connection (NUM2ULONG (signature), StringValuePtr(filename));
1017
+ if (b == -1)
1018
+ rb_raise(rb_eRuntimeError, "%s", "File too large. send_file_data() supports files under 32k.");
1019
+ if (b > 0) {
1020
+ char *err = strerror (b);
1021
+ char buf[1024];
1022
+ memset (buf, 0, sizeof(buf));
1023
+ snprintf (buf, sizeof(buf)-1, ": %s %s", StringValuePtr(filename),(err?err:"???"));
1024
+
1025
+ rb_raise (rb_eIOError, "%s", buf);
1026
+ }
1027
+
1028
+ return INT2NUM (0);
1029
+ }
1030
+
1031
+
1032
+ /*******************
1033
+ t_set_rlimit_nofile
1034
+ *******************/
1035
+
1036
+ static VALUE t_set_rlimit_nofile (VALUE self, VALUE arg)
1037
+ {
1038
+ arg = (NIL_P(arg)) ? -1 : NUM2INT (arg);
1039
+ return INT2NUM (evma_set_rlimit_nofile (arg));
1040
+ }
1041
+
1042
+ /***************************
1043
+ conn_get_outbound_data_size
1044
+ ***************************/
1045
+
1046
+ static VALUE conn_get_outbound_data_size (VALUE self)
1047
+ {
1048
+ VALUE sig = rb_ivar_get (self, Intern_at_signature);
1049
+ return INT2NUM (evma_get_outbound_data_size (NUM2ULONG (sig)));
1050
+ }
1051
+
1052
+
1053
+ /******************************
1054
+ conn_associate_callback_target
1055
+ ******************************/
1056
+
1057
+ static VALUE conn_associate_callback_target (VALUE self, VALUE sig)
1058
+ {
1059
+ // No-op for the time being.
1060
+ return Qnil;
1061
+ }
1062
+
1063
+
1064
+ /***************
1065
+ t_get_loop_time
1066
+ ****************/
1067
+
1068
+ static VALUE t_get_loop_time (VALUE self)
1069
+ {
1070
+ #ifndef HAVE_RB_TIME_NEW
1071
+ static VALUE cTime = rb_path2class("Time");
1072
+ static ID at = rb_intern("at");
1073
+ #endif
1074
+
1075
+ uint64_t current_time = evma_get_current_loop_time();
1076
+ if (current_time != 0) {
1077
+ #ifndef HAVE_RB_TIME_NEW
1078
+ return rb_funcall(cTime, at, 2, INT2NUM(current_time / 1000000), INT2NUM(current_time % 1000000));
1079
+ #else
1080
+ return rb_time_new(current_time / 1000000, current_time % 1000000);
1081
+ #endif
1082
+ }
1083
+ return Qnil;
1084
+ }
1085
+
1086
+
1087
+ /*************
1088
+ t_start_proxy
1089
+ **************/
1090
+
1091
+ static VALUE t_start_proxy (VALUE self, VALUE from, VALUE to, VALUE bufsize, VALUE length)
1092
+ {
1093
+ try {
1094
+ evma_start_proxy(NUM2ULONG (from), NUM2ULONG (to), NUM2ULONG(bufsize), NUM2ULONG(length));
1095
+ } catch (std::runtime_error e) {
1096
+ rb_raise (EM_eConnectionError, "%s", e.what());
1097
+ }
1098
+ return Qnil;
1099
+ }
1100
+
1101
+
1102
+ /************
1103
+ t_stop_proxy
1104
+ *************/
1105
+
1106
+ static VALUE t_stop_proxy (VALUE self, VALUE from)
1107
+ {
1108
+ try{
1109
+ evma_stop_proxy(NUM2ULONG (from));
1110
+ } catch (std::runtime_error e) {
1111
+ rb_raise (EM_eConnectionError, "%s", e.what());
1112
+ }
1113
+ return Qnil;
1114
+ }
1115
+
1116
+ /***************
1117
+ t_proxied_bytes
1118
+ ****************/
1119
+
1120
+ static VALUE t_proxied_bytes (VALUE self, VALUE from)
1121
+ {
1122
+ try{
1123
+ return ULONG2NUM(evma_proxied_bytes(NUM2ULONG (from)));
1124
+ } catch (std::runtime_error e) {
1125
+ rb_raise (EM_eConnectionError, "%s", e.what());
1126
+ }
1127
+ return Qnil;
1128
+ }
1129
+
1130
+ /***************
1131
+ t_get_idle_time
1132
+ ****************/
1133
+
1134
+ static VALUE t_get_idle_time (VALUE self, VALUE from)
1135
+ {
1136
+ try{
1137
+ uint64_t current_time = evma_get_current_loop_time();
1138
+ uint64_t time = evma_get_last_activity_time(NUM2ULONG (from));
1139
+ if (current_time != 0 && time != 0) {
1140
+ if (time >= current_time)
1141
+ return ULONG2NUM(0);
1142
+ else {
1143
+ uint64_t diff = current_time - time;
1144
+ float seconds = diff / (1000.0*1000.0);
1145
+ return rb_float_new(seconds);
1146
+ }
1147
+ return Qnil;
1148
+ }
1149
+ } catch (std::runtime_error e) {
1150
+ rb_raise (EM_eConnectionError, "%s", e.what());
1151
+ }
1152
+ return Qnil;
1153
+ }
1154
+
1155
+ /************************
1156
+ t_get_heartbeat_interval
1157
+ *************************/
1158
+
1159
+ static VALUE t_get_heartbeat_interval (VALUE self)
1160
+ {
1161
+ return rb_float_new(evma_get_heartbeat_interval());
1162
+ }
1163
+
1164
+
1165
+ /************************
1166
+ t_set_heartbeat_interval
1167
+ *************************/
1168
+
1169
+ static VALUE t_set_heartbeat_interval (VALUE self, VALUE interval)
1170
+ {
1171
+ float iv = RFLOAT_VALUE(interval);
1172
+ if (evma_set_heartbeat_interval(iv))
1173
+ return Qtrue;
1174
+ return Qfalse;
1175
+ }
1176
+
1177
+
1178
+ /*********************
1179
+ Init_rubyeventmachine
1180
+ *********************/
1181
+
1182
+ extern "C" void Init_rubyeventmachine()
1183
+ {
1184
+ // Lookup Process::Status for get_subprocess_status
1185
+ VALUE rb_mProcess = rb_const_get(rb_cObject, rb_intern("Process"));
1186
+ rb_cProcStatus = rb_const_get(rb_mProcess, rb_intern("Status"));
1187
+
1188
+ // Tuck away some symbol values so we don't have to look 'em up every time we need 'em.
1189
+ Intern_at_signature = rb_intern ("@signature");
1190
+ Intern_at_timers = rb_intern ("@timers");
1191
+ Intern_at_conns = rb_intern ("@conns");
1192
+ Intern_at_error_handler = rb_intern("@error_handler");
1193
+
1194
+ Intern_event_callback = rb_intern ("event_callback");
1195
+ Intern_run_deferred_callbacks = rb_intern ("run_deferred_callbacks");
1196
+ Intern_delete = rb_intern ("delete");
1197
+ Intern_call = rb_intern ("call");
1198
+ Intern_receive_data = rb_intern ("receive_data");
1199
+ Intern_ssl_handshake_completed = rb_intern ("ssl_handshake_completed");
1200
+ Intern_ssl_verify_peer = rb_intern ("ssl_verify_peer");
1201
+ Intern_notify_readable = rb_intern ("notify_readable");
1202
+ Intern_notify_writable = rb_intern ("notify_writable");
1203
+ Intern_proxy_target_unbound = rb_intern ("proxy_target_unbound");
1204
+ Intern_proxy_completed = rb_intern ("proxy_completed");
1205
+ Intern_connection_completed = rb_intern ("connection_completed");
1206
+
1207
+ // INCOMPLETE, we need to define class Connections inside module EventMachine
1208
+ // run_machine and run_machine_without_threads are now identical.
1209
+ // Must deprecate the without_threads variant.
1210
+ EmModule = rb_define_module ("EventMachine");
1211
+ EmConnection = rb_define_class_under (EmModule, "Connection", rb_cObject);
1212
+
1213
+ rb_define_class_under (EmModule, "NoHandlerForAcceptedConnection", rb_eRuntimeError);
1214
+ EM_eConnectionError = rb_define_class_under (EmModule, "ConnectionError", rb_eRuntimeError);
1215
+ EM_eConnectionNotBound = rb_define_class_under (EmModule, "ConnectionNotBound", rb_eRuntimeError);
1216
+ EM_eUnknownTimerFired = rb_define_class_under (EmModule, "UnknownTimerFired", rb_eRuntimeError);
1217
+ EM_eUnsupported = rb_define_class_under (EmModule, "Unsupported", rb_eRuntimeError);
1218
+
1219
+ rb_define_module_function (EmModule, "initialize_event_machine", (VALUE(*)(...))t_initialize_event_machine, 0);
1220
+ rb_define_module_function (EmModule, "run_machine", (VALUE(*)(...))t_run_machine_without_threads, 0);
1221
+ rb_define_module_function (EmModule, "run_machine_without_threads", (VALUE(*)(...))t_run_machine_without_threads, 0);
1222
+ rb_define_module_function (EmModule, "add_oneshot_timer", (VALUE(*)(...))t_add_oneshot_timer, 1);
1223
+ rb_define_module_function (EmModule, "start_tcp_server", (VALUE(*)(...))t_start_server, 2);
1224
+ rb_define_module_function (EmModule, "stop_tcp_server", (VALUE(*)(...))t_stop_server, 1);
1225
+ rb_define_module_function (EmModule, "start_unix_server", (VALUE(*)(...))t_start_unix_server, 1);
1226
+ rb_define_module_function (EmModule, "attach_sd", (VALUE(*)(...))t_attach_sd, 1);
1227
+ rb_define_module_function (EmModule, "set_tls_parms", (VALUE(*)(...))t_set_tls_parms, 4);
1228
+ rb_define_module_function (EmModule, "start_tls", (VALUE(*)(...))t_start_tls, 1);
1229
+ rb_define_module_function (EmModule, "get_peer_cert", (VALUE(*)(...))t_get_peer_cert, 1);
1230
+ rb_define_module_function (EmModule, "send_data", (VALUE(*)(...))t_send_data, 3);
1231
+ rb_define_module_function (EmModule, "send_datagram", (VALUE(*)(...))t_send_datagram, 5);
1232
+ rb_define_module_function (EmModule, "close_connection", (VALUE(*)(...))t_close_connection, 2);
1233
+ rb_define_module_function (EmModule, "report_connection_error_status", (VALUE(*)(...))t_report_connection_error_status, 1);
1234
+ rb_define_module_function (EmModule, "connect_server", (VALUE(*)(...))t_connect_server, 2);
1235
+ rb_define_module_function (EmModule, "bind_connect_server", (VALUE(*)(...))t_bind_connect_server, 4);
1236
+ rb_define_module_function (EmModule, "connect_unix_server", (VALUE(*)(...))t_connect_unix_server, 1);
1237
+
1238
+ rb_define_module_function (EmModule, "attach_fd", (VALUE (*)(...))t_attach_fd, 2);
1239
+ rb_define_module_function (EmModule, "detach_fd", (VALUE (*)(...))t_detach_fd, 1);
1240
+ rb_define_module_function (EmModule, "get_file_descriptor", (VALUE (*)(...))t_get_file_descriptor, 1);
1241
+ rb_define_module_function (EmModule, "get_sock_opt", (VALUE (*)(...))t_get_sock_opt, 3);
1242
+ rb_define_module_function (EmModule, "set_sock_opt", (VALUE (*)(...))t_set_sock_opt, 4);
1243
+ rb_define_module_function (EmModule, "set_notify_readable", (VALUE (*)(...))t_set_notify_readable, 2);
1244
+ rb_define_module_function (EmModule, "set_notify_writable", (VALUE (*)(...))t_set_notify_writable, 2);
1245
+ rb_define_module_function (EmModule, "is_notify_readable", (VALUE (*)(...))t_is_notify_readable, 1);
1246
+ rb_define_module_function (EmModule, "is_notify_writable", (VALUE (*)(...))t_is_notify_writable, 1);
1247
+
1248
+ rb_define_module_function (EmModule, "pause_connection", (VALUE (*)(...))t_pause, 1);
1249
+ rb_define_module_function (EmModule, "resume_connection", (VALUE (*)(...))t_resume, 1);
1250
+ rb_define_module_function (EmModule, "connection_paused?", (VALUE (*)(...))t_paused_p, 1);
1251
+ rb_define_module_function (EmModule, "num_close_scheduled", (VALUE (*)(...))t_num_close_scheduled, 0);
1252
+
1253
+ rb_define_module_function (EmModule, "start_proxy", (VALUE (*)(...))t_start_proxy, 4);
1254
+ rb_define_module_function (EmModule, "stop_proxy", (VALUE (*)(...))t_stop_proxy, 1);
1255
+ rb_define_module_function (EmModule, "get_proxied_bytes", (VALUE (*)(...))t_proxied_bytes, 1);
1256
+
1257
+ rb_define_module_function (EmModule, "watch_filename", (VALUE (*)(...))t_watch_filename, 1);
1258
+ rb_define_module_function (EmModule, "unwatch_filename", (VALUE (*)(...))t_unwatch_filename, 1);
1259
+
1260
+ rb_define_module_function (EmModule, "watch_pid", (VALUE (*)(...))t_watch_pid, 1);
1261
+ rb_define_module_function (EmModule, "unwatch_pid", (VALUE (*)(...))t_unwatch_pid, 1);
1262
+
1263
+ rb_define_module_function (EmModule, "current_time", (VALUE(*)(...))t_get_loop_time, 0);
1264
+
1265
+ rb_define_module_function (EmModule, "open_udp_socket", (VALUE(*)(...))t_open_udp_socket, 2);
1266
+ rb_define_module_function (EmModule, "read_keyboard", (VALUE(*)(...))t_read_keyboard, 0);
1267
+ rb_define_module_function (EmModule, "release_machine", (VALUE(*)(...))t_release_machine, 0);
1268
+ rb_define_module_function (EmModule, "stop", (VALUE(*)(...))t_stop, 0);
1269
+ rb_define_module_function (EmModule, "signal_loopbreak", (VALUE(*)(...))t_signal_loopbreak, 0);
1270
+ rb_define_module_function (EmModule, "library_type", (VALUE(*)(...))t_library_type, 0);
1271
+ rb_define_module_function (EmModule, "set_timer_quantum", (VALUE(*)(...))t_set_timer_quantum, 1);
1272
+ rb_define_module_function (EmModule, "get_max_timer_count", (VALUE(*)(...))t_get_max_timer_count, 0);
1273
+ rb_define_module_function (EmModule, "set_max_timer_count", (VALUE(*)(...))t_set_max_timer_count, 1);
1274
+ rb_define_module_function (EmModule, "setuid_string", (VALUE(*)(...))t_setuid_string, 1);
1275
+ rb_define_module_function (EmModule, "invoke_popen", (VALUE(*)(...))t_invoke_popen, 1);
1276
+ rb_define_module_function (EmModule, "send_file_data", (VALUE(*)(...))t_send_file_data, 2);
1277
+ rb_define_module_function (EmModule, "get_heartbeat_interval", (VALUE(*)(...))t_get_heartbeat_interval, 0);
1278
+ rb_define_module_function (EmModule, "set_heartbeat_interval", (VALUE(*)(...))t_set_heartbeat_interval, 1);
1279
+ rb_define_module_function (EmModule, "get_idle_time", (VALUE(*)(...))t_get_idle_time, 1);
1280
+
1281
+ rb_define_module_function (EmModule, "get_peername", (VALUE(*)(...))t_get_peername, 1);
1282
+ rb_define_module_function (EmModule, "get_sockname", (VALUE(*)(...))t_get_sockname, 1);
1283
+ rb_define_module_function (EmModule, "get_subprocess_pid", (VALUE(*)(...))t_get_subprocess_pid, 1);
1284
+ rb_define_module_function (EmModule, "get_subprocess_status", (VALUE(*)(...))t_get_subprocess_status, 1);
1285
+ rb_define_module_function (EmModule, "get_comm_inactivity_timeout", (VALUE(*)(...))t_get_comm_inactivity_timeout, 1);
1286
+ rb_define_module_function (EmModule, "set_comm_inactivity_timeout", (VALUE(*)(...))t_set_comm_inactivity_timeout, 2);
1287
+ rb_define_module_function (EmModule, "get_pending_connect_timeout", (VALUE(*)(...))t_get_pending_connect_timeout, 1);
1288
+ rb_define_module_function (EmModule, "set_pending_connect_timeout", (VALUE(*)(...))t_set_pending_connect_timeout, 2);
1289
+ rb_define_module_function (EmModule, "set_rlimit_nofile", (VALUE(*)(...))t_set_rlimit_nofile, 1);
1290
+ rb_define_module_function (EmModule, "get_connection_count", (VALUE(*)(...))t_get_connection_count, 0);
1291
+
1292
+ rb_define_module_function (EmModule, "epoll", (VALUE(*)(...))t__epoll, 0);
1293
+ rb_define_module_function (EmModule, "epoll=", (VALUE(*)(...))t__epoll_set, 1);
1294
+ rb_define_module_function (EmModule, "epoll?", (VALUE(*)(...))t__epoll_p, 0);
1295
+
1296
+ rb_define_module_function (EmModule, "kqueue", (VALUE(*)(...))t__kqueue, 0);
1297
+ rb_define_module_function (EmModule, "kqueue=", (VALUE(*)(...))t__kqueue_set, 1);
1298
+ rb_define_module_function (EmModule, "kqueue?", (VALUE(*)(...))t__kqueue_p, 0);
1299
+
1300
+ rb_define_module_function (EmModule, "ssl?", (VALUE(*)(...))t__ssl_p, 0);
1301
+
1302
+ rb_define_method (EmConnection, "get_outbound_data_size", (VALUE(*)(...))conn_get_outbound_data_size, 0);
1303
+ rb_define_method (EmConnection, "associate_callback_target", (VALUE(*)(...))conn_associate_callback_target, 1);
1304
+
1305
+ rb_define_const (EmModule, "TimerFired", INT2NUM(100));
1306
+ rb_define_const (EmModule, "ConnectionData", INT2NUM(101));
1307
+ rb_define_const (EmModule, "ConnectionUnbound", INT2NUM(102));
1308
+ rb_define_const (EmModule, "ConnectionAccepted", INT2NUM(103));
1309
+ rb_define_const (EmModule, "ConnectionCompleted", INT2NUM(104));
1310
+ rb_define_const (EmModule, "LoopbreakSignalled", INT2NUM(105));
1311
+
1312
+ rb_define_const (EmModule, "ConnectionNotifyReadable", INT2NUM(106));
1313
+ rb_define_const (EmModule, "ConnectionNotifyWritable", INT2NUM(107));
1314
+
1315
+ rb_define_const (EmModule, "SslHandshakeCompleted", INT2NUM(108));
1316
+
1317
+ }
1318
+