libc-eventmachine 0.12.5.42

Sign up to get free protection for your applications and to get access to all the features.
Files changed (131) hide show
  1. data/Rakefile +195 -0
  2. data/docs/COPYING +60 -0
  3. data/docs/ChangeLog +211 -0
  4. data/docs/DEFERRABLES +138 -0
  5. data/docs/EPOLL +141 -0
  6. data/docs/GNU +281 -0
  7. data/docs/INSTALL +15 -0
  8. data/docs/KEYBOARD +38 -0
  9. data/docs/LEGAL +25 -0
  10. data/docs/LIGHTWEIGHT_CONCURRENCY +72 -0
  11. data/docs/PURE_RUBY +77 -0
  12. data/docs/README +74 -0
  13. data/docs/RELEASE_NOTES +96 -0
  14. data/docs/SMTP +9 -0
  15. data/docs/SPAWNED_PROCESSES +93 -0
  16. data/docs/TODO +10 -0
  17. data/ext/binder.cpp +126 -0
  18. data/ext/binder.h +48 -0
  19. data/ext/cmain.cpp +582 -0
  20. data/ext/cplusplus.cpp +177 -0
  21. data/ext/ed.cpp +1522 -0
  22. data/ext/ed.h +380 -0
  23. data/ext/em.cpp +1947 -0
  24. data/ext/em.h +186 -0
  25. data/ext/emwin.cpp +300 -0
  26. data/ext/emwin.h +94 -0
  27. data/ext/epoll.cpp +26 -0
  28. data/ext/epoll.h +25 -0
  29. data/ext/eventmachine.h +98 -0
  30. data/ext/eventmachine_cpp.h +96 -0
  31. data/ext/extconf.rb +129 -0
  32. data/ext/fastfilereader/extconf.rb +77 -0
  33. data/ext/fastfilereader/mapper.cpp +214 -0
  34. data/ext/fastfilereader/mapper.h +59 -0
  35. data/ext/fastfilereader/rubymain.cpp +127 -0
  36. data/ext/files.cpp +94 -0
  37. data/ext/files.h +65 -0
  38. data/ext/kb.cpp +82 -0
  39. data/ext/page.cpp +107 -0
  40. data/ext/page.h +51 -0
  41. data/ext/pipe.cpp +351 -0
  42. data/ext/project.h +119 -0
  43. data/ext/rubymain.cpp +858 -0
  44. data/ext/sigs.cpp +89 -0
  45. data/ext/sigs.h +32 -0
  46. data/ext/ssl.cpp +423 -0
  47. data/ext/ssl.h +90 -0
  48. data/java/src/com/rubyeventmachine/Application.java +196 -0
  49. data/java/src/com/rubyeventmachine/Connection.java +74 -0
  50. data/java/src/com/rubyeventmachine/ConnectionFactory.java +37 -0
  51. data/java/src/com/rubyeventmachine/DefaultConnectionFactory.java +46 -0
  52. data/java/src/com/rubyeventmachine/EmReactor.java +408 -0
  53. data/java/src/com/rubyeventmachine/EmReactorException.java +40 -0
  54. data/java/src/com/rubyeventmachine/EventableChannel.java +57 -0
  55. data/java/src/com/rubyeventmachine/EventableDatagramChannel.java +171 -0
  56. data/java/src/com/rubyeventmachine/EventableSocketChannel.java +244 -0
  57. data/java/src/com/rubyeventmachine/PeriodicTimer.java +38 -0
  58. data/java/src/com/rubyeventmachine/Timer.java +54 -0
  59. data/java/src/com/rubyeventmachine/tests/ApplicationTest.java +108 -0
  60. data/java/src/com/rubyeventmachine/tests/ConnectTest.java +124 -0
  61. data/java/src/com/rubyeventmachine/tests/EMTest.java +80 -0
  62. data/java/src/com/rubyeventmachine/tests/TestDatagrams.java +53 -0
  63. data/java/src/com/rubyeventmachine/tests/TestServers.java +74 -0
  64. data/java/src/com/rubyeventmachine/tests/TestTimers.java +89 -0
  65. data/lib/em/deferrable.rb +208 -0
  66. data/lib/em/eventable.rb +39 -0
  67. data/lib/em/future.rb +62 -0
  68. data/lib/em/messages.rb +66 -0
  69. data/lib/em/processes.rb +68 -0
  70. data/lib/em/spawnable.rb +88 -0
  71. data/lib/em/streamer.rb +112 -0
  72. data/lib/eventmachine.rb +1920 -0
  73. data/lib/eventmachine_version.rb +31 -0
  74. data/lib/evma/callback.rb +32 -0
  75. data/lib/evma/container.rb +75 -0
  76. data/lib/evma/factory.rb +77 -0
  77. data/lib/evma/protocol.rb +87 -0
  78. data/lib/evma/reactor.rb +48 -0
  79. data/lib/evma.rb +32 -0
  80. data/lib/jeventmachine.rb +140 -0
  81. data/lib/pr_eventmachine.rb +1017 -0
  82. data/lib/protocols/buftok.rb +127 -0
  83. data/lib/protocols/header_and_content.rb +129 -0
  84. data/lib/protocols/httpcli2.rb +803 -0
  85. data/lib/protocols/httpclient.rb +270 -0
  86. data/lib/protocols/line_and_text.rb +126 -0
  87. data/lib/protocols/linetext2.rb +161 -0
  88. data/lib/protocols/memcache.rb +293 -0
  89. data/lib/protocols/postgres.rb +261 -0
  90. data/lib/protocols/saslauth.rb +179 -0
  91. data/lib/protocols/smtpclient.rb +308 -0
  92. data/lib/protocols/smtpserver.rb +556 -0
  93. data/lib/protocols/stomp.rb +153 -0
  94. data/lib/protocols/tcptest.rb +57 -0
  95. data/tasks/cpp.rake +77 -0
  96. data/tasks/project.rake +78 -0
  97. data/tasks/tests.rake +193 -0
  98. data/tests/test_attach.rb +83 -0
  99. data/tests/test_basic.rb +231 -0
  100. data/tests/test_bind.rb +73 -0
  101. data/tests/test_connection_count.rb +35 -0
  102. data/tests/test_defer.rb +47 -0
  103. data/tests/test_epoll.rb +163 -0
  104. data/tests/test_error_handler.rb +32 -0
  105. data/tests/test_errors.rb +82 -0
  106. data/tests/test_eventables.rb +77 -0
  107. data/tests/test_exc.rb +58 -0
  108. data/tests/test_futures.rb +214 -0
  109. data/tests/test_handler_check.rb +37 -0
  110. data/tests/test_hc.rb +218 -0
  111. data/tests/test_httpclient.rb +215 -0
  112. data/tests/test_httpclient2.rb +155 -0
  113. data/tests/test_kb.rb +61 -0
  114. data/tests/test_ltp.rb +188 -0
  115. data/tests/test_ltp2.rb +320 -0
  116. data/tests/test_next_tick.rb +109 -0
  117. data/tests/test_processes.rb +56 -0
  118. data/tests/test_pure.rb +129 -0
  119. data/tests/test_running.rb +47 -0
  120. data/tests/test_sasl.rb +74 -0
  121. data/tests/test_send_file.rb +243 -0
  122. data/tests/test_servers.rb +80 -0
  123. data/tests/test_smtpclient.rb +83 -0
  124. data/tests/test_smtpserver.rb +93 -0
  125. data/tests/test_spawn.rb +329 -0
  126. data/tests/test_ssl_args.rb +68 -0
  127. data/tests/test_ssl_methods.rb +50 -0
  128. data/tests/test_timers.rb +148 -0
  129. data/tests/test_ud.rb +43 -0
  130. data/tests/testem.rb +31 -0
  131. metadata +230 -0
data/ext/rubymain.cpp ADDED
@@ -0,0 +1,858 @@
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
+
25
+
26
+ /*******
27
+ Statics
28
+ *******/
29
+
30
+ static VALUE EmModule;
31
+ static VALUE EmConnection;
32
+
33
+ static VALUE Intern_at_signature;
34
+ static VALUE Intern_at_timers;
35
+ static VALUE Intern_at_conns;
36
+ static VALUE Intern_at_error_handler;
37
+ static VALUE Intern_event_callback;
38
+ static VALUE Intern_run_deferred_callbacks;
39
+ static VALUE Intern_delete;
40
+ static VALUE Intern_call;
41
+ static VALUE Intern_receive_data;
42
+ static VALUE Intern_ssl_handshake_completed;
43
+ static VALUE Intern_notify_readable;
44
+ static VALUE Intern_notify_writable;
45
+
46
+ static VALUE rb_cProcStatus;
47
+
48
+ struct em_event {
49
+ const char *a1;
50
+ int a2;
51
+ const char *a3;
52
+ int a4;
53
+ };
54
+
55
+ /****************
56
+ t_event_callback
57
+ ****************/
58
+
59
+ static void event_callback (struct em_event* e)
60
+ {
61
+ const char *a1 = e->a1;
62
+ int a2 = e->a2;
63
+ const char *a3 = e->a3;
64
+ int a4 = e->a4;
65
+
66
+ if (a2 == EM_CONNECTION_READ) {
67
+ VALUE t = rb_ivar_get (EmModule, Intern_at_conns);
68
+ VALUE q = rb_hash_aref (t, rb_str_new2(a1));
69
+ if (q == Qnil)
70
+ rb_raise (rb_eRuntimeError, "no connection");
71
+ rb_funcall (q, Intern_receive_data, 1, rb_str_new (a3, a4));
72
+ }
73
+ else if (a2 == EM_CONNECTION_NOTIFY_READABLE) {
74
+ VALUE t = rb_ivar_get (EmModule, Intern_at_conns);
75
+ VALUE q = rb_hash_aref (t, rb_str_new2(a1));
76
+ if (q == Qnil)
77
+ rb_raise (rb_eRuntimeError, "no connection");
78
+ rb_funcall (q, Intern_notify_readable, 0);
79
+ }
80
+ else if (a2 == EM_CONNECTION_NOTIFY_WRITABLE) {
81
+ VALUE t = rb_ivar_get (EmModule, Intern_at_conns);
82
+ VALUE q = rb_hash_aref (t, rb_str_new2(a1));
83
+ if (q == Qnil)
84
+ rb_raise (rb_eRuntimeError, "no connection");
85
+ rb_funcall (q, Intern_notify_writable, 0);
86
+ }
87
+ else if (a2 == EM_LOOPBREAK_SIGNAL) {
88
+ rb_funcall (EmModule, Intern_run_deferred_callbacks, 0);
89
+ }
90
+ else if (a2 == EM_TIMER_FIRED) {
91
+ VALUE t = rb_ivar_get (EmModule, Intern_at_timers);
92
+ VALUE q = rb_funcall (t, Intern_delete, 1, rb_str_new(a3, a4));
93
+ if (q == Qnil)
94
+ rb_raise (rb_eRuntimeError, "no timer");
95
+ rb_funcall (q, Intern_call, 0);
96
+ }
97
+ else if (a2 == EM_SSL_HANDSHAKE_COMPLETED) {
98
+ VALUE t = rb_ivar_get (EmModule, Intern_at_conns);
99
+ VALUE q = rb_hash_aref (t, rb_str_new2(a1));
100
+ if (q == Qnil)
101
+ rb_raise (rb_eRuntimeError, "no connection");
102
+ rb_funcall (q, Intern_ssl_handshake_completed, 0);
103
+ }
104
+ else
105
+ rb_funcall (EmModule, Intern_event_callback, 3, rb_str_new2(a1), (a2 << 1) | 1, rb_str_new(a3,a4));
106
+ }
107
+
108
+ /*******************
109
+ event_error_handler
110
+ *******************/
111
+
112
+ static void event_error_handler(void *, VALUE err)
113
+ {
114
+ VALUE error_handler = rb_ivar_get(EmModule, Intern_at_error_handler);
115
+ rb_funcall (error_handler, Intern_call, 1, err);
116
+ }
117
+
118
+ /**********************
119
+ event_callback_wrapper
120
+ **********************/
121
+
122
+ static void event_callback_wrapper (const char *a1, int a2, const char *a3, int a4)
123
+ {
124
+ struct em_event e;
125
+ e.a1 = a1;
126
+ e.a2 = a2;
127
+ e.a3 = a3;
128
+ e.a4 = a4;
129
+
130
+ if (!rb_ivar_defined(EmModule, Intern_at_error_handler))
131
+ event_callback(&e);
132
+ else
133
+ rb_rescue((VALUE (*)(ANYARGS))event_callback, (VALUE)&e, (VALUE (*)(ANYARGS))event_error_handler, NULL);
134
+ }
135
+
136
+ /**************************
137
+ t_initialize_event_machine
138
+ **************************/
139
+
140
+ static VALUE t_initialize_event_machine (VALUE self)
141
+ {
142
+ evma_initialize_library (event_callback_wrapper);
143
+ return Qnil;
144
+ }
145
+
146
+
147
+
148
+ /*****************************
149
+ t_run_machine_without_threads
150
+ *****************************/
151
+
152
+ static VALUE t_run_machine_without_threads (VALUE self)
153
+ {
154
+ evma_run_machine();
155
+ return Qnil;
156
+ }
157
+
158
+
159
+ /*******************
160
+ t_add_oneshot_timer
161
+ *******************/
162
+
163
+ static VALUE t_add_oneshot_timer (VALUE self, VALUE interval)
164
+ {
165
+ const char *f = evma_install_oneshot_timer (FIX2INT (interval));
166
+ if (!f || !*f)
167
+ rb_raise (rb_eRuntimeError, "no timer");
168
+ return rb_str_new2 (f);
169
+ }
170
+
171
+
172
+ /**************
173
+ t_start_server
174
+ **************/
175
+
176
+ static VALUE t_start_server (VALUE self, VALUE server, VALUE port)
177
+ {
178
+ const char *f = evma_create_tcp_server (StringValuePtr(server), FIX2INT(port));
179
+ if (!f || !*f)
180
+ rb_raise (rb_eRuntimeError, "no acceptor");
181
+ return rb_str_new2 (f);
182
+ }
183
+
184
+ /*************
185
+ t_stop_server
186
+ *************/
187
+
188
+ static VALUE t_stop_server (VALUE self, VALUE signature)
189
+ {
190
+ evma_stop_tcp_server (StringValuePtr (signature));
191
+ return Qnil;
192
+ }
193
+
194
+
195
+ /*******************
196
+ t_start_unix_server
197
+ *******************/
198
+
199
+ static VALUE t_start_unix_server (VALUE self, VALUE filename)
200
+ {
201
+ const char *f = evma_create_unix_domain_server (StringValuePtr(filename));
202
+ if (!f || !*f)
203
+ rb_raise (rb_eRuntimeError, "no unix-domain acceptor");
204
+ return rb_str_new2 (f);
205
+ }
206
+
207
+
208
+
209
+ /***********
210
+ t_send_data
211
+ ***********/
212
+
213
+ static VALUE t_send_data (VALUE self, VALUE signature, VALUE data, VALUE data_length)
214
+ {
215
+ int b = evma_send_data_to_connection (StringValuePtr (signature), StringValuePtr (data), FIX2INT (data_length));
216
+ return INT2NUM (b);
217
+ }
218
+
219
+
220
+ /***********
221
+ t_start_tls
222
+ ***********/
223
+
224
+ static VALUE t_start_tls (VALUE self, VALUE signature)
225
+ {
226
+ evma_start_tls (StringValuePtr (signature));
227
+ return Qnil;
228
+ }
229
+
230
+ /***************
231
+ t_set_tls_parms
232
+ ***************/
233
+
234
+ static VALUE t_set_tls_parms (VALUE self, VALUE signature, VALUE privkeyfile, VALUE certchainfile)
235
+ {
236
+ /* set_tls_parms takes a series of positional arguments for specifying such things
237
+ * as private keys and certificate chains.
238
+ * It's expected that the parameter list will grow as we add more supported features.
239
+ * ALL of these parameters are optional, and can be specified as empty or NULL strings.
240
+ */
241
+ evma_set_tls_parms (StringValuePtr (signature), StringValuePtr (privkeyfile), StringValuePtr (certchainfile) );
242
+ return Qnil;
243
+ }
244
+
245
+ /***********
246
+ t_get_peer_cert
247
+ ***********/
248
+
249
+ static VALUE t_get_peer_cert (VALUE self, VALUE signature)
250
+ {
251
+ VALUE ret = Qnil;
252
+
253
+ #ifdef WITH_SSL
254
+ X509 *cert = NULL;
255
+ BUF_MEM *buf;
256
+ BIO *out;
257
+
258
+ cert = evma_get_peer_cert (StringValuePtr (signature));
259
+
260
+ if (cert != NULL) {
261
+ out = BIO_new(BIO_s_mem());
262
+ PEM_write_bio_X509(out, cert);
263
+ BIO_get_mem_ptr(out, &buf);
264
+ ret = rb_str_new(buf->data, buf->length);
265
+ X509_free(cert);
266
+ BUF_MEM_free(buf);
267
+ }
268
+ #endif
269
+
270
+ return ret;
271
+ }
272
+
273
+ /**************
274
+ t_get_peername
275
+ **************/
276
+
277
+ static VALUE t_get_peername (VALUE self, VALUE signature)
278
+ {
279
+ struct sockaddr s;
280
+ if (evma_get_peername (StringValuePtr (signature), &s)) {
281
+ return rb_str_new ((const char*)&s, sizeof(s));
282
+ }
283
+
284
+ return Qnil;
285
+ }
286
+
287
+ /**************
288
+ t_get_sockname
289
+ **************/
290
+
291
+ static VALUE t_get_sockname (VALUE self, VALUE signature)
292
+ {
293
+ struct sockaddr s;
294
+ if (evma_get_sockname (StringValuePtr (signature), &s)) {
295
+ return rb_str_new ((const char*)&s, sizeof(s));
296
+ }
297
+
298
+ return Qnil;
299
+ }
300
+
301
+ /********************
302
+ t_get_subprocess_pid
303
+ ********************/
304
+
305
+ static VALUE t_get_subprocess_pid (VALUE self, VALUE signature)
306
+ {
307
+ pid_t pid;
308
+ if (evma_get_subprocess_pid (StringValuePtr (signature), &pid)) {
309
+ return INT2NUM (pid);
310
+ }
311
+
312
+ return Qnil;
313
+ }
314
+
315
+ /***********************
316
+ t_get_subprocess_status
317
+ ***********************/
318
+
319
+ static VALUE t_get_subprocess_status (VALUE self, VALUE signature)
320
+ {
321
+ VALUE proc_status = Qnil;
322
+
323
+ int status;
324
+ pid_t pid;
325
+
326
+ if (evma_get_subprocess_status (StringValuePtr (signature), &status)) {
327
+ if (evma_get_subprocess_pid (StringValuePtr (signature), &pid)) {
328
+ proc_status = rb_obj_alloc(rb_cProcStatus);
329
+ rb_iv_set(proc_status, "status", INT2FIX(status));
330
+ rb_iv_set(proc_status, "pid", INT2FIX(pid));
331
+ }
332
+ }
333
+
334
+ return proc_status;
335
+ }
336
+
337
+ /**********************
338
+ t_get_connection_count
339
+ **********************/
340
+
341
+ static VALUE t_get_connection_count (VALUE self)
342
+ {
343
+ return INT2NUM(evma_get_connection_count());
344
+ }
345
+
346
+ /*****************************
347
+ t_get_comm_inactivity_timeout
348
+ *****************************/
349
+
350
+ static VALUE t_get_comm_inactivity_timeout (VALUE self, VALUE signature)
351
+ {
352
+ int timeout;
353
+ if (evma_get_comm_inactivity_timeout (StringValuePtr (signature), &timeout))
354
+ return INT2FIX (timeout);
355
+ return Qnil;
356
+ }
357
+
358
+ /*****************************
359
+ t_set_comm_inactivity_timeout
360
+ *****************************/
361
+
362
+ static VALUE t_set_comm_inactivity_timeout (VALUE self, VALUE signature, VALUE timeout)
363
+ {
364
+ int ti = FIX2INT (timeout);
365
+ if (evma_set_comm_inactivity_timeout (StringValuePtr (signature), &ti));
366
+ return Qtrue;
367
+ return Qnil;
368
+ }
369
+
370
+
371
+ /***************
372
+ t_send_datagram
373
+ ***************/
374
+
375
+ static VALUE t_send_datagram (VALUE self, VALUE signature, VALUE data, VALUE data_length, VALUE address, VALUE port)
376
+ {
377
+ int b = evma_send_datagram (StringValuePtr (signature), StringValuePtr (data), FIX2INT (data_length), StringValuePtr(address), FIX2INT(port));
378
+ return INT2NUM (b);
379
+ }
380
+
381
+
382
+ /******************
383
+ t_close_connection
384
+ ******************/
385
+
386
+ static VALUE t_close_connection (VALUE self, VALUE signature, VALUE after_writing)
387
+ {
388
+ evma_close_connection (StringValuePtr (signature), ((after_writing == Qtrue) ? 1 : 0));
389
+ return Qnil;
390
+ }
391
+
392
+ /********************************
393
+ t_report_connection_error_status
394
+ ********************************/
395
+
396
+ static VALUE t_report_connection_error_status (VALUE self, VALUE signature)
397
+ {
398
+ int b = evma_report_connection_error_status (StringValuePtr (signature));
399
+ return INT2NUM (b);
400
+ }
401
+
402
+
403
+
404
+ /****************
405
+ t_connect_server
406
+ ****************/
407
+
408
+ static VALUE t_connect_server (VALUE self, VALUE server, VALUE port, VALUE bind_host)
409
+ {
410
+ // Avoid FIX2INT in this case, because it doesn't deal with type errors properly.
411
+ // Specifically, if the value of port comes in as a string rather than an integer,
412
+ // NUM2INT will throw a type error, but FIX2INT will generate garbage.
413
+
414
+ const char *f = evma_connect_to_server (StringValuePtr(server), NUM2INT(port), bind_host == Qnil ? 0 : StringValuePtr(bind_host));
415
+ if (!f || !*f)
416
+ rb_raise (rb_eRuntimeError, "no connection");
417
+ return rb_str_new2 (f);
418
+ }
419
+
420
+ /*********************
421
+ t_connect_unix_server
422
+ *********************/
423
+
424
+ static VALUE t_connect_unix_server (VALUE self, VALUE serversocket)
425
+ {
426
+ const char *f = evma_connect_to_unix_server (StringValuePtr(serversocket));
427
+ if (!f || !*f)
428
+ rb_raise (rb_eRuntimeError, "no connection");
429
+ return rb_str_new2 (f);
430
+ }
431
+
432
+ /***********
433
+ t_attach_fd
434
+ ***********/
435
+
436
+ static VALUE t_attach_fd (VALUE self, VALUE file_descriptor, VALUE read_mode, VALUE write_mode)
437
+ {
438
+ const char *f = evma_attach_fd (NUM2INT(file_descriptor), (read_mode == Qtrue) ? 1 : 0, (write_mode == Qtrue) ? 1 : 0);
439
+ if (!f || !*f)
440
+ rb_raise (rb_eRuntimeError, "no connection");
441
+ return rb_str_new2 (f);
442
+ }
443
+
444
+ /***********
445
+ t_detach_fd
446
+ ***********/
447
+
448
+ static VALUE t_detach_fd (VALUE self, VALUE signature)
449
+ {
450
+ return INT2NUM(evma_detach_fd (StringValuePtr(signature)));
451
+ }
452
+
453
+ /*****************
454
+ t_open_udp_socket
455
+ *****************/
456
+
457
+ static VALUE t_open_udp_socket (VALUE self, VALUE server, VALUE port)
458
+ {
459
+ const char *f = evma_open_datagram_socket (StringValuePtr(server), FIX2INT(port));
460
+ if (!f || !*f)
461
+ rb_raise (rb_eRuntimeError, "no datagram socket");
462
+ return rb_str_new2 (f);
463
+ }
464
+
465
+
466
+
467
+ /*****************
468
+ t_release_machine
469
+ *****************/
470
+
471
+ static VALUE t_release_machine (VALUE self)
472
+ {
473
+ evma_release_library();
474
+ return Qnil;
475
+ }
476
+
477
+
478
+ /******
479
+ t_stop
480
+ ******/
481
+
482
+ static VALUE t_stop (VALUE self)
483
+ {
484
+ evma_stop_machine();
485
+ return Qnil;
486
+ }
487
+
488
+ /******************
489
+ t_signal_loopbreak
490
+ ******************/
491
+
492
+ static VALUE t_signal_loopbreak (VALUE self)
493
+ {
494
+ evma_signal_loopbreak();
495
+ return Qnil;
496
+ }
497
+
498
+ /**************
499
+ t_library_type
500
+ **************/
501
+
502
+ static VALUE t_library_type (VALUE self)
503
+ {
504
+ return rb_eval_string (":extension");
505
+ }
506
+
507
+
508
+
509
+ /*******************
510
+ t_set_timer_quantum
511
+ *******************/
512
+
513
+ static VALUE t_set_timer_quantum (VALUE self, VALUE interval)
514
+ {
515
+ evma_set_timer_quantum (FIX2INT (interval));
516
+ return Qnil;
517
+ }
518
+
519
+ /********************
520
+ t_get_max_timer_count
521
+ ********************/
522
+
523
+ static VALUE t_get_max_timer_count (VALUE self)
524
+ {
525
+ return INT2FIX (evma_get_max_timer_count());
526
+ }
527
+
528
+ /********************
529
+ t_set_max_timer_count
530
+ ********************/
531
+
532
+ static VALUE t_set_max_timer_count (VALUE self, VALUE ct)
533
+ {
534
+ evma_set_max_timer_count (FIX2INT (ct));
535
+ return Qnil;
536
+ }
537
+
538
+ /***************
539
+ t_setuid_string
540
+ ***************/
541
+
542
+ static VALUE t_setuid_string (VALUE self, VALUE username)
543
+ {
544
+ evma_setuid_string (StringValuePtr (username));
545
+ return Qnil;
546
+ }
547
+
548
+
549
+
550
+ /*************
551
+ t__write_file
552
+ *************/
553
+
554
+ static VALUE t__write_file (VALUE self, VALUE filename)
555
+ {
556
+ const char *f = evma__write_file (StringValuePtr (filename));
557
+ if (!f || !*f)
558
+ rb_raise (rb_eRuntimeError, "file not opened");
559
+ return rb_str_new2 (f);
560
+ }
561
+
562
+ /**************
563
+ t_invoke_popen
564
+ **************/
565
+
566
+ static VALUE t_invoke_popen (VALUE self, VALUE cmd)
567
+ {
568
+ // 1.8.7+
569
+ #ifdef RARRAY_LEN
570
+ int len = RARRAY_LEN(cmd);
571
+ #else
572
+ int len = RARRAY (cmd)->len;
573
+ #endif
574
+ if (len > 98)
575
+ rb_raise (rb_eRuntimeError, "too many arguments to popen");
576
+ char *strings [100];
577
+ for (int i=0; i < len; i++) {
578
+ VALUE ix = INT2FIX (i);
579
+ VALUE s = rb_ary_aref (1, &ix, cmd);
580
+ strings[i] = StringValuePtr (s);
581
+ }
582
+ strings[len] = NULL;
583
+
584
+ const char *f = evma_popen (strings);
585
+ if (!f || !*f) {
586
+ char *err = strerror (errno);
587
+ char buf[100];
588
+ memset (buf, 0, sizeof(buf));
589
+ snprintf (buf, sizeof(buf)-1, "no popen: %s", (err?err:"???"));
590
+ rb_raise (rb_eRuntimeError, buf);
591
+ }
592
+ return rb_str_new2 (f);
593
+ }
594
+
595
+
596
+ /***************
597
+ t_read_keyboard
598
+ ***************/
599
+
600
+ static VALUE t_read_keyboard (VALUE self)
601
+ {
602
+ const char *f = evma_open_keyboard();
603
+ if (!f || !*f)
604
+ rb_raise (rb_eRuntimeError, "no keyboard reader");
605
+ return rb_str_new2 (f);
606
+ }
607
+
608
+
609
+ /********
610
+ t__epoll
611
+ ********/
612
+
613
+ static VALUE t__epoll (VALUE self)
614
+ {
615
+ // Temporary.
616
+ evma__epoll();
617
+ return Qnil;
618
+ }
619
+
620
+ /**********
621
+ t__epoll_p
622
+ **********/
623
+
624
+ static VALUE t__epoll_p (VALUE self)
625
+ {
626
+ #ifdef HAVE_EPOLL
627
+ return Qtrue;
628
+ #else
629
+ return Qfalse;
630
+ #endif
631
+ }
632
+
633
+
634
+ /*********
635
+ t__kqueue
636
+ *********/
637
+
638
+ static VALUE t__kqueue (VALUE self)
639
+ {
640
+ // Temporary.
641
+ evma__kqueue();
642
+ return Qnil;
643
+ }
644
+
645
+ /***********
646
+ t__kqueue_p
647
+ ***********/
648
+
649
+ static VALUE t__kqueue_p (VALUE self)
650
+ {
651
+ #ifdef HAVE_KQUEUE
652
+ return Qtrue;
653
+ #else
654
+ return Qfalse;
655
+ #endif
656
+ }
657
+
658
+ /********
659
+ t__ssl_p
660
+ ********/
661
+
662
+ static VALUE t__ssl_p (VALUE self)
663
+ {
664
+ #ifdef WITH_SSL
665
+ return Qtrue;
666
+ #else
667
+ return Qfalse;
668
+ #endif
669
+ }
670
+
671
+ /****************
672
+ t_send_file_data
673
+ ****************/
674
+
675
+ static VALUE t_send_file_data (VALUE self, VALUE signature, VALUE filename)
676
+ {
677
+
678
+ /* The current implementation of evma_send_file_data_to_connection enforces a strict
679
+ * upper limit on the file size it will transmit (currently 32K). The function returns
680
+ * zero on success, -1 if the requested file exceeds its size limit, and a positive
681
+ * number for other errors.
682
+ * TODO: Positive return values are actually errno's, which is probably the wrong way to
683
+ * do this. For one thing it's ugly. For another, we can't be sure zero is never a real errno.
684
+ */
685
+
686
+ int b = evma_send_file_data_to_connection (StringValuePtr(signature), StringValuePtr(filename));
687
+ if (b == -1)
688
+ rb_raise(rb_eRuntimeError, "File too large. send_file_data() supports files under 32k.");
689
+ if (b > 0) {
690
+ char *err = strerror (b);
691
+ char buf[1024];
692
+ memset (buf, 0, sizeof(buf));
693
+ snprintf (buf, sizeof(buf)-1, ": %s %s", StringValuePtr(filename),(err?err:"???"));
694
+
695
+ rb_raise (rb_eIOError, buf);
696
+ }
697
+
698
+ return INT2NUM (0);
699
+ }
700
+
701
+
702
+ /*******************
703
+ t_set_rlimit_nofile
704
+ *******************/
705
+
706
+ static VALUE t_set_rlimit_nofile (VALUE self, VALUE arg)
707
+ {
708
+ arg = (NIL_P(arg)) ? -1 : NUM2INT (arg);
709
+ return INT2NUM (evma_set_rlimit_nofile (arg));
710
+ }
711
+
712
+ /***************************
713
+ conn_get_outbound_data_size
714
+ ***************************/
715
+
716
+ static VALUE conn_get_outbound_data_size (VALUE self)
717
+ {
718
+ VALUE sig = rb_ivar_get (self, Intern_at_signature);
719
+ return INT2NUM (evma_get_outbound_data_size (StringValuePtr(sig)));
720
+ }
721
+
722
+
723
+ /******************************
724
+ conn_associate_callback_target
725
+ ******************************/
726
+
727
+ static VALUE conn_associate_callback_target (VALUE self, VALUE sig)
728
+ {
729
+ // No-op for the time being.
730
+ return Qnil;
731
+ }
732
+
733
+
734
+ /***************
735
+ t_get_loop_time
736
+ ****************/
737
+
738
+ static VALUE t_get_loop_time (VALUE self)
739
+ {
740
+ VALUE cTime = rb_path2class("Time");
741
+ if (gCurrentLoopTime != 0) {
742
+ return rb_funcall(cTime,
743
+ rb_intern("at"),
744
+ 1,
745
+ INT2NUM(gCurrentLoopTime));
746
+ }
747
+ return Qnil;
748
+ }
749
+
750
+
751
+ /*********************
752
+ Init_rubyeventmachine
753
+ *********************/
754
+
755
+ extern "C" void Init_rubyeventmachine()
756
+ {
757
+ // Lookup Process::Status for get_subprocess_status
758
+ VALUE rb_mProcess = rb_const_get(rb_cObject, rb_intern("Process"));
759
+ rb_cProcStatus = rb_const_get(rb_mProcess, rb_intern("Status"));
760
+
761
+ // Tuck away some symbol values so we don't have to look 'em up every time we need 'em.
762
+ Intern_at_signature = rb_intern ("@signature");
763
+ Intern_at_timers = rb_intern ("@timers");
764
+ Intern_at_conns = rb_intern ("@conns");
765
+ Intern_at_error_handler = rb_intern("@error_handler");
766
+
767
+ Intern_event_callback = rb_intern ("event_callback");
768
+ Intern_run_deferred_callbacks = rb_intern ("run_deferred_callbacks");
769
+ Intern_delete = rb_intern ("delete");
770
+ Intern_call = rb_intern ("call");
771
+ Intern_receive_data = rb_intern ("receive_data");
772
+ Intern_ssl_handshake_completed = rb_intern ("ssl_handshake_completed");
773
+ Intern_notify_readable = rb_intern ("notify_readable");
774
+ Intern_notify_writable = rb_intern ("notify_writable");
775
+
776
+ // INCOMPLETE, we need to define class Connections inside module EventMachine
777
+ // run_machine and run_machine_without_threads are now identical.
778
+ // Must deprecate the without_threads variant.
779
+ EmModule = rb_define_module ("EventMachine");
780
+ EmConnection = rb_define_class_under (EmModule, "Connection", rb_cObject);
781
+
782
+ rb_define_class_under (EmModule, "ConnectionNotBound", rb_eException);
783
+ rb_define_class_under (EmModule, "NoHandlerForAcceptedConnection", rb_eException);
784
+ rb_define_class_under (EmModule, "UnknownTimerFired", rb_eException);
785
+
786
+ rb_define_module_function (EmModule, "initialize_event_machine", (VALUE(*)(...))t_initialize_event_machine, 0);
787
+ rb_define_module_function (EmModule, "run_machine", (VALUE(*)(...))t_run_machine_without_threads, 0);
788
+ rb_define_module_function (EmModule, "run_machine_without_threads", (VALUE(*)(...))t_run_machine_without_threads, 0);
789
+ rb_define_module_function (EmModule, "add_oneshot_timer", (VALUE(*)(...))t_add_oneshot_timer, 1);
790
+ rb_define_module_function (EmModule, "start_tcp_server", (VALUE(*)(...))t_start_server, 2);
791
+ rb_define_module_function (EmModule, "stop_tcp_server", (VALUE(*)(...))t_stop_server, 1);
792
+ rb_define_module_function (EmModule, "start_unix_server", (VALUE(*)(...))t_start_unix_server, 1);
793
+ rb_define_module_function (EmModule, "set_tls_parms", (VALUE(*)(...))t_set_tls_parms, 3);
794
+ rb_define_module_function (EmModule, "start_tls", (VALUE(*)(...))t_start_tls, 1);
795
+ rb_define_module_function (EmModule, "get_peer_cert", (VALUE(*)(...))t_get_peer_cert, 1);
796
+ rb_define_module_function (EmModule, "send_data", (VALUE(*)(...))t_send_data, 3);
797
+ rb_define_module_function (EmModule, "send_datagram", (VALUE(*)(...))t_send_datagram, 5);
798
+ rb_define_module_function (EmModule, "close_connection", (VALUE(*)(...))t_close_connection, 2);
799
+ rb_define_module_function (EmModule, "report_connection_error_status", (VALUE(*)(...))t_report_connection_error_status, 1);
800
+ rb_define_module_function (EmModule, "connect_server", (VALUE(*)(...))t_connect_server, 3);
801
+ rb_define_module_function (EmModule, "connect_unix_server", (VALUE(*)(...))t_connect_unix_server, 1);
802
+
803
+ rb_define_module_function (EmModule, "attach_fd", (VALUE (*)(...))t_attach_fd, 3);
804
+ rb_define_module_function (EmModule, "detach_fd", (VALUE (*)(...))t_detach_fd, 1);
805
+
806
+ rb_define_module_function (EmModule, "current_time", (VALUE(*)(...))t_get_loop_time, 0);
807
+
808
+ rb_define_module_function (EmModule, "open_udp_socket", (VALUE(*)(...))t_open_udp_socket, 2);
809
+ rb_define_module_function (EmModule, "read_keyboard", (VALUE(*)(...))t_read_keyboard, 0);
810
+ rb_define_module_function (EmModule, "release_machine", (VALUE(*)(...))t_release_machine, 0);
811
+ rb_define_module_function (EmModule, "stop", (VALUE(*)(...))t_stop, 0);
812
+ rb_define_module_function (EmModule, "signal_loopbreak", (VALUE(*)(...))t_signal_loopbreak, 0);
813
+ rb_define_module_function (EmModule, "library_type", (VALUE(*)(...))t_library_type, 0);
814
+ rb_define_module_function (EmModule, "set_timer_quantum", (VALUE(*)(...))t_set_timer_quantum, 1);
815
+ rb_define_module_function (EmModule, "get_max_timer_count", (VALUE(*)(...))t_get_max_timer_count, 0);
816
+ rb_define_module_function (EmModule, "set_max_timer_count", (VALUE(*)(...))t_set_max_timer_count, 1);
817
+ rb_define_module_function (EmModule, "setuid_string", (VALUE(*)(...))t_setuid_string, 1);
818
+ rb_define_module_function (EmModule, "invoke_popen", (VALUE(*)(...))t_invoke_popen, 1);
819
+ rb_define_module_function (EmModule, "send_file_data", (VALUE(*)(...))t_send_file_data, 2);
820
+
821
+ // Provisional:
822
+ rb_define_module_function (EmModule, "_write_file", (VALUE(*)(...))t__write_file, 1);
823
+
824
+ rb_define_module_function (EmModule, "get_peername", (VALUE(*)(...))t_get_peername, 1);
825
+ rb_define_module_function (EmModule, "get_sockname", (VALUE(*)(...))t_get_sockname, 1);
826
+ rb_define_module_function (EmModule, "get_subprocess_pid", (VALUE(*)(...))t_get_subprocess_pid, 1);
827
+ rb_define_module_function (EmModule, "get_subprocess_status", (VALUE(*)(...))t_get_subprocess_status, 1);
828
+ rb_define_module_function (EmModule, "get_comm_inactivity_timeout", (VALUE(*)(...))t_get_comm_inactivity_timeout, 1);
829
+ rb_define_module_function (EmModule, "set_comm_inactivity_timeout", (VALUE(*)(...))t_set_comm_inactivity_timeout, 2);
830
+ rb_define_module_function (EmModule, "set_rlimit_nofile", (VALUE(*)(...))t_set_rlimit_nofile, 1);
831
+ rb_define_module_function (EmModule, "get_connection_count", (VALUE(*)(...))t_get_connection_count, 0);
832
+
833
+ // Temporary:
834
+ rb_define_module_function (EmModule, "epoll", (VALUE(*)(...))t__epoll, 0);
835
+ rb_define_module_function (EmModule, "kqueue", (VALUE(*)(...))t__kqueue, 0);
836
+
837
+ rb_define_module_function (EmModule, "epoll?", (VALUE(*)(...))t__epoll_p, 0);
838
+ rb_define_module_function (EmModule, "kqueue?", (VALUE(*)(...))t__kqueue_p, 0);
839
+
840
+ rb_define_module_function (EmModule, "ssl?", (VALUE(*)(...))t__ssl_p, 0);
841
+
842
+ rb_define_method (EmConnection, "get_outbound_data_size", (VALUE(*)(...))conn_get_outbound_data_size, 0);
843
+ rb_define_method (EmConnection, "associate_callback_target", (VALUE(*)(...))conn_associate_callback_target, 1);
844
+
845
+ rb_define_const (EmModule, "TimerFired", INT2NUM(100));
846
+ rb_define_const (EmModule, "ConnectionData", INT2NUM(101));
847
+ rb_define_const (EmModule, "ConnectionUnbound", INT2NUM(102));
848
+ rb_define_const (EmModule, "ConnectionAccepted", INT2NUM(103));
849
+ rb_define_const (EmModule, "ConnectionCompleted", INT2NUM(104));
850
+ rb_define_const (EmModule, "LoopbreakSignalled", INT2NUM(105));
851
+
852
+ rb_define_const (EmModule, "ConnectionNotifyReadable", INT2NUM(106));
853
+ rb_define_const (EmModule, "ConnectionNotifyWritable", INT2NUM(107));
854
+
855
+ rb_define_const (EmModule, "SslHandshakeCompleted", INT2NUM(108));
856
+
857
+ }
858
+