eventmachine 0.12.6-java

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