MattHulse-eventmachine 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (156) hide show
  1. data/.gitignore +14 -0
  2. data/README +82 -0
  3. data/Rakefile +279 -0
  4. data/docs/COPYING +60 -0
  5. data/docs/ChangeLog +211 -0
  6. data/docs/DEFERRABLES +133 -0
  7. data/docs/EPOLL +141 -0
  8. data/docs/GNU +281 -0
  9. data/docs/INSTALL +13 -0
  10. data/docs/KEYBOARD +38 -0
  11. data/docs/LEGAL +25 -0
  12. data/docs/LIGHTWEIGHT_CONCURRENCY +70 -0
  13. data/docs/PURE_RUBY +75 -0
  14. data/docs/RELEASE_NOTES +94 -0
  15. data/docs/SMTP +2 -0
  16. data/docs/SPAWNED_PROCESSES +89 -0
  17. data/docs/TODO +8 -0
  18. data/eventmachine.gemspec +41 -0
  19. data/examples/ex_channel.rb +43 -0
  20. data/examples/ex_queue.rb +2 -0
  21. data/examples/helper.rb +2 -0
  22. data/ext/binder.cpp +125 -0
  23. data/ext/binder.h +46 -0
  24. data/ext/cmain.cpp +821 -0
  25. data/ext/cplusplus.cpp +202 -0
  26. data/ext/ed.cpp +1868 -0
  27. data/ext/ed.h +416 -0
  28. data/ext/em.cpp +2270 -0
  29. data/ext/em.h +228 -0
  30. data/ext/emwin.cpp +300 -0
  31. data/ext/emwin.h +94 -0
  32. data/ext/epoll.cpp +26 -0
  33. data/ext/epoll.h +25 -0
  34. data/ext/eventmachine.h +122 -0
  35. data/ext/eventmachine_cpp.h +96 -0
  36. data/ext/extconf.rb +138 -0
  37. data/ext/fastfilereader/extconf.rb +84 -0
  38. data/ext/fastfilereader/mapper.cpp +214 -0
  39. data/ext/fastfilereader/mapper.h +59 -0
  40. data/ext/fastfilereader/rubymain.cpp +127 -0
  41. data/ext/files.cpp +94 -0
  42. data/ext/files.h +65 -0
  43. data/ext/kb.cpp +81 -0
  44. data/ext/page.cpp +107 -0
  45. data/ext/page.h +51 -0
  46. data/ext/pipe.cpp +349 -0
  47. data/ext/project.h +147 -0
  48. data/ext/rubymain.cpp +1152 -0
  49. data/ext/sigs.cpp +89 -0
  50. data/ext/sigs.h +32 -0
  51. data/ext/ssl.cpp +460 -0
  52. data/ext/ssl.h +94 -0
  53. data/java/.classpath +8 -0
  54. data/java/.project +17 -0
  55. data/java/src/com/rubyeventmachine/Application.java +192 -0
  56. data/java/src/com/rubyeventmachine/Connection.java +74 -0
  57. data/java/src/com/rubyeventmachine/ConnectionFactory.java +37 -0
  58. data/java/src/com/rubyeventmachine/DefaultConnectionFactory.java +46 -0
  59. data/java/src/com/rubyeventmachine/EmReactor.java +557 -0
  60. data/java/src/com/rubyeventmachine/EmReactorException.java +40 -0
  61. data/java/src/com/rubyeventmachine/EventableChannel.java +69 -0
  62. data/java/src/com/rubyeventmachine/EventableDatagramChannel.java +189 -0
  63. data/java/src/com/rubyeventmachine/EventableSocketChannel.java +364 -0
  64. data/java/src/com/rubyeventmachine/PeriodicTimer.java +38 -0
  65. data/java/src/com/rubyeventmachine/Timer.java +54 -0
  66. data/java/src/com/rubyeventmachine/tests/ApplicationTest.java +108 -0
  67. data/java/src/com/rubyeventmachine/tests/ConnectTest.java +146 -0
  68. data/java/src/com/rubyeventmachine/tests/EMTest.java +80 -0
  69. data/java/src/com/rubyeventmachine/tests/TestDatagrams.java +53 -0
  70. data/java/src/com/rubyeventmachine/tests/TestServers.java +74 -0
  71. data/java/src/com/rubyeventmachine/tests/TestTimers.java +89 -0
  72. data/lib/em/buftok.rb +138 -0
  73. data/lib/em/callback.rb +26 -0
  74. data/lib/em/channel.rb +57 -0
  75. data/lib/em/connection.rb +564 -0
  76. data/lib/em/deferrable.rb +187 -0
  77. data/lib/em/file_watch.rb +54 -0
  78. data/lib/em/future.rb +61 -0
  79. data/lib/em/messages.rb +66 -0
  80. data/lib/em/process_watch.rb +44 -0
  81. data/lib/em/processes.rb +119 -0
  82. data/lib/em/protocols.rb +35 -0
  83. data/lib/em/protocols/header_and_content.rb +138 -0
  84. data/lib/em/protocols/httpclient.rb +263 -0
  85. data/lib/em/protocols/httpclient2.rb +582 -0
  86. data/lib/em/protocols/line_and_text.rb +126 -0
  87. data/lib/em/protocols/linetext2.rb +160 -0
  88. data/lib/em/protocols/memcache.rb +323 -0
  89. data/lib/em/protocols/object_protocol.rb +45 -0
  90. data/lib/em/protocols/postgres3.rb +247 -0
  91. data/lib/em/protocols/saslauth.rb +175 -0
  92. data/lib/em/protocols/smtpclient.rb +350 -0
  93. data/lib/em/protocols/smtpserver.rb +547 -0
  94. data/lib/em/protocols/stomp.rb +200 -0
  95. data/lib/em/protocols/tcptest.rb +53 -0
  96. data/lib/em/queue.rb +61 -0
  97. data/lib/em/spawnable.rb +85 -0
  98. data/lib/em/streamer.rb +130 -0
  99. data/lib/em/timers.rb +55 -0
  100. data/lib/em/version.rb +3 -0
  101. data/lib/eventmachine.rb +1698 -0
  102. data/lib/evma.rb +32 -0
  103. data/lib/evma/callback.rb +32 -0
  104. data/lib/evma/container.rb +75 -0
  105. data/lib/evma/factory.rb +77 -0
  106. data/lib/evma/protocol.rb +87 -0
  107. data/lib/evma/reactor.rb +48 -0
  108. data/lib/jeventmachine.rb +246 -0
  109. data/lib/pr_eventmachine.rb +1022 -0
  110. data/setup.rb +1585 -0
  111. data/tasks/cpp.rake +77 -0
  112. data/tasks/project.rake +79 -0
  113. data/tasks/tests.rake +193 -0
  114. data/tests/client.crt +31 -0
  115. data/tests/client.key +51 -0
  116. data/tests/test_attach.rb +126 -0
  117. data/tests/test_basic.rb +284 -0
  118. data/tests/test_channel.rb +63 -0
  119. data/tests/test_connection_count.rb +35 -0
  120. data/tests/test_defer.rb +47 -0
  121. data/tests/test_epoll.rb +160 -0
  122. data/tests/test_error_handler.rb +35 -0
  123. data/tests/test_errors.rb +82 -0
  124. data/tests/test_exc.rb +55 -0
  125. data/tests/test_file_watch.rb +49 -0
  126. data/tests/test_futures.rb +198 -0
  127. data/tests/test_handler_check.rb +37 -0
  128. data/tests/test_hc.rb +218 -0
  129. data/tests/test_httpclient.rb +218 -0
  130. data/tests/test_httpclient2.rb +153 -0
  131. data/tests/test_inactivity_timeout.rb +50 -0
  132. data/tests/test_kb.rb +60 -0
  133. data/tests/test_ltp.rb +182 -0
  134. data/tests/test_ltp2.rb +317 -0
  135. data/tests/test_next_tick.rb +133 -0
  136. data/tests/test_object_protocol.rb +37 -0
  137. data/tests/test_process_watch.rb +48 -0
  138. data/tests/test_processes.rb +128 -0
  139. data/tests/test_proxy_connection.rb +92 -0
  140. data/tests/test_pure.rb +125 -0
  141. data/tests/test_queue.rb +44 -0
  142. data/tests/test_running.rb +42 -0
  143. data/tests/test_sasl.rb +72 -0
  144. data/tests/test_send_file.rb +242 -0
  145. data/tests/test_servers.rb +76 -0
  146. data/tests/test_smtpclient.rb +83 -0
  147. data/tests/test_smtpserver.rb +85 -0
  148. data/tests/test_spawn.rb +322 -0
  149. data/tests/test_ssl_args.rb +68 -0
  150. data/tests/test_ssl_methods.rb +50 -0
  151. data/tests/test_ssl_verify.rb +82 -0
  152. data/tests/test_timers.rb +162 -0
  153. data/tests/test_ud.rb +36 -0
  154. data/tests/testem.rb +31 -0
  155. data/web/whatis +7 -0
  156. metadata +223 -0
@@ -0,0 +1,43 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
3
+ EM.run do
4
+
5
+ # Create a channel to push data to, this could be stocks...
6
+ RandChannel = EM::Channel.new
7
+
8
+ # The server simply subscribes client connections to the channel on connect,
9
+ # and unsubscribes them on disconnect.
10
+ class Server < EM::Connection
11
+ def self.start(host = '127.0.0.1', port = 8000)
12
+ EM.start_server(host, port, self)
13
+ end
14
+
15
+ def post_init
16
+ @sid = RandChannel.subscribe { |m| send_data "#{m.inspect}\n" }
17
+ end
18
+
19
+ def unbind
20
+ RandChannel.unsubscribe @sid
21
+ end
22
+ end
23
+ Server.start
24
+
25
+ # Two client connections, that just print what they receive.
26
+ 2.times do
27
+ EM.connect('127.0.0.1', 8000) do |c|
28
+ c.extend EM::P::LineText2
29
+ def c.receive_line(line)
30
+ puts "Subscriber: #{signature} got #{line}"
31
+ end
32
+ EM.add_timer(2) { c.close_connection }
33
+ end
34
+ end
35
+
36
+ # This part of the example is more fake, but imagine sleep was in fact a
37
+ # long running calculation to achieve the value.
38
+ 40.times do
39
+ EM.defer lambda { v = sleep(rand * 2); RandChannel << [Time.now, v] }
40
+ end
41
+
42
+ EM.add_timer(5) { EM.stop }
43
+ end
@@ -0,0 +1,2 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+
@@ -0,0 +1,2 @@
1
+ $:.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')
2
+ require 'eventmachine'
data/ext/binder.cpp ADDED
@@ -0,0 +1,125 @@
1
+ /*****************************************************************************
2
+
3
+ $Id$
4
+
5
+ File: binder.cpp
6
+ Date: 07Apr06
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
+
22
+ #define DEV_URANDOM "/dev/urandom"
23
+
24
+
25
+ map<unsigned long, Bindable_t*> Bindable_t::BindingBag;
26
+
27
+
28
+ /********************************
29
+ STATIC Bindable_t::CreateBinding
30
+ ********************************/
31
+
32
+ unsigned long Bindable_t::CreateBinding()
33
+ {
34
+ // XXX use atomic_t to prevent thread-safety issues
35
+ static unsigned long num = 0;
36
+ while(BindingBag[++num]);
37
+ return num;
38
+ }
39
+
40
+ #if 0
41
+ string Bindable_t::CreateBinding()
42
+ {
43
+ static int index = 0;
44
+ static string seed;
45
+
46
+ if ((index >= 1000000) || (seed.length() == 0)) {
47
+ #ifdef OS_UNIX
48
+ int fd = open (DEV_URANDOM, O_RDONLY);
49
+ if (fd < 0)
50
+ throw std::runtime_error ("No entropy device");
51
+
52
+ unsigned char u[16];
53
+ size_t r = read (fd, u, sizeof(u));
54
+ if (r < sizeof(u))
55
+ throw std::runtime_error ("Unable to read entropy device");
56
+
57
+ unsigned char *u1 = (unsigned char*)u;
58
+ char u2 [sizeof(u) * 2 + 1];
59
+
60
+ for (size_t i=0; i < sizeof(u); i++)
61
+ sprintf (u2 + (i * 2), "%02x", u1[i]);
62
+
63
+ seed = string (u2);
64
+ #endif
65
+
66
+
67
+ #ifdef OS_WIN32
68
+ UUID uuid;
69
+ UuidCreate (&uuid);
70
+ unsigned char *uuidstring = NULL;
71
+ UuidToString (&uuid, &uuidstring);
72
+ if (!uuidstring)
73
+ throw std::runtime_error ("Unable to read uuid");
74
+ seed = string ((const char*)uuidstring);
75
+
76
+ RpcStringFree (&uuidstring);
77
+ #endif
78
+
79
+ index = 0;
80
+
81
+
82
+ }
83
+
84
+ stringstream ss;
85
+ ss << seed << (++index);
86
+ return ss.str();
87
+ }
88
+ #endif
89
+
90
+ /*****************************
91
+ STATIC: Bindable_t::GetObject
92
+ *****************************/
93
+
94
+ Bindable_t *Bindable_t::GetObject (const unsigned long binding)
95
+ {
96
+ map<unsigned long, Bindable_t*>::const_iterator i = BindingBag.find (binding);
97
+ if (i != BindingBag.end())
98
+ return i->second;
99
+ else
100
+ return NULL;
101
+ }
102
+
103
+
104
+ /**********************
105
+ Bindable_t::Bindable_t
106
+ **********************/
107
+
108
+ Bindable_t::Bindable_t()
109
+ {
110
+ Binding = Bindable_t::CreateBinding();
111
+ BindingBag [Binding] = this;
112
+ }
113
+
114
+
115
+
116
+ /***********************
117
+ Bindable_t::~Bindable_t
118
+ ***********************/
119
+
120
+ Bindable_t::~Bindable_t()
121
+ {
122
+ BindingBag.erase (Binding);
123
+ }
124
+
125
+
data/ext/binder.h ADDED
@@ -0,0 +1,46 @@
1
+ /*****************************************************************************
2
+
3
+ $Id$
4
+
5
+ File: binder.h
6
+ Date: 07Apr06
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
+ #ifndef __ObjectBindings__H_
21
+ #define __ObjectBindings__H_
22
+
23
+
24
+ class Bindable_t
25
+ {
26
+ public:
27
+ static unsigned long CreateBinding();
28
+ static Bindable_t *GetObject (const unsigned long);
29
+ static map<unsigned long, Bindable_t*> BindingBag;
30
+
31
+ public:
32
+ Bindable_t();
33
+ virtual ~Bindable_t();
34
+
35
+ const unsigned long GetBinding() {return Binding;}
36
+
37
+ private:
38
+ unsigned long Binding;
39
+ };
40
+
41
+
42
+
43
+
44
+
45
+ #endif // __ObjectBindings__H_
46
+
data/ext/cmain.cpp ADDED
@@ -0,0 +1,821 @@
1
+ /*****************************************************************************
2
+
3
+ $Id$
4
+
5
+ File: cmain.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
+
22
+
23
+ static EventMachine_t *EventMachine;
24
+ static int bUseEpoll = 0;
25
+ static int bUseKqueue = 0;
26
+
27
+ extern "C" void ensure_eventmachine (const char *caller = "unknown caller")
28
+ {
29
+ if (!EventMachine) {
30
+ const int err_size = 128;
31
+ char err_string[err_size];
32
+ snprintf (err_string, err_size, "eventmachine not initialized: %s", caller);
33
+ #ifdef BUILD_FOR_RUBY
34
+ rb_raise(rb_eRuntimeError, "%s", err_string);
35
+ #else
36
+ throw std::runtime_error (err_string);
37
+ #endif
38
+ }
39
+ }
40
+
41
+ /***********************
42
+ evma_initialize_library
43
+ ***********************/
44
+
45
+ extern "C" void evma_initialize_library (void(*cb)(const unsigned long, int, const char*, const unsigned long))
46
+ {
47
+ // Probably a bad idea to mess with the signal mask of a process
48
+ // we're just being linked into.
49
+ //InstallSignalHandlers();
50
+ if (EventMachine)
51
+ #ifdef BUILD_FOR_RUBY
52
+ rb_raise(rb_eRuntimeError, "eventmachine already initialized: evma_initialize_library");
53
+ #else
54
+ throw std::runtime_error ("eventmachine already initialized: evma_initialize_library");
55
+ #endif
56
+ EventMachine = new EventMachine_t (cb);
57
+ if (bUseEpoll)
58
+ EventMachine->_UseEpoll();
59
+ if (bUseKqueue)
60
+ EventMachine->_UseKqueue();
61
+ }
62
+
63
+
64
+ /********************
65
+ evma_release_library
66
+ ********************/
67
+
68
+ extern "C" void evma_release_library()
69
+ {
70
+ ensure_eventmachine("evma_release_library");
71
+ delete EventMachine;
72
+ EventMachine = NULL;
73
+ }
74
+
75
+
76
+ /****************
77
+ evma_run_machine
78
+ ****************/
79
+
80
+ extern "C" void evma_run_machine()
81
+ {
82
+ ensure_eventmachine("evma_run_machine");
83
+ EventMachine->Run();
84
+ }
85
+
86
+
87
+ /**************************
88
+ evma_install_oneshot_timer
89
+ **************************/
90
+
91
+ extern "C" const unsigned long evma_install_oneshot_timer (int seconds)
92
+ {
93
+ ensure_eventmachine("evma_install_oneshot_timer");
94
+ return EventMachine->InstallOneshotTimer (seconds);
95
+ }
96
+
97
+
98
+ /**********************
99
+ evma_connect_to_server
100
+ **********************/
101
+
102
+ extern "C" const unsigned long evma_connect_to_server (const char *bind_addr, int bind_port, const char *server, int port)
103
+ {
104
+ ensure_eventmachine("evma_connect_to_server");
105
+ return EventMachine->ConnectToServer (bind_addr, bind_port, server, port);
106
+ }
107
+
108
+ /***************************
109
+ evma_connect_to_unix_server
110
+ ***************************/
111
+
112
+ extern "C" const unsigned long evma_connect_to_unix_server (const char *server)
113
+ {
114
+ ensure_eventmachine("evma_connect_to_unix_server");
115
+ return EventMachine->ConnectToUnixServer (server);
116
+ }
117
+
118
+ /**************
119
+ evma_attach_fd
120
+ **************/
121
+
122
+ extern "C" const unsigned long evma_attach_fd (int file_descriptor, int watch_mode)
123
+ {
124
+ ensure_eventmachine("evma_attach_fd");
125
+ return EventMachine->AttachFD (file_descriptor, watch_mode ? true : false);
126
+ }
127
+
128
+ /**************
129
+ evma_detach_fd
130
+ **************/
131
+
132
+ extern "C" int evma_detach_fd (const unsigned long binding)
133
+ {
134
+ ensure_eventmachine("evma_detach_fd");
135
+ EventableDescriptor *ed = dynamic_cast <EventableDescriptor*> (Bindable_t::GetObject (binding));
136
+ if (ed)
137
+ return EventMachine->DetachFD (ed);
138
+ else
139
+ #ifdef BUILD_FOR_RUBY
140
+ rb_raise(rb_eRuntimeError, "invalid binding to detach");
141
+ #else
142
+ throw std::runtime_error ("invalid binding to detach");
143
+ #endif
144
+ }
145
+
146
+ /************************
147
+ evma_get_file_descriptor
148
+ ************************/
149
+
150
+ extern "C" int evma_get_file_descriptor (const unsigned long binding)
151
+ {
152
+ ensure_eventmachine("evma_get_file_descriptor");
153
+ EventableDescriptor *ed = dynamic_cast <EventableDescriptor*> (Bindable_t::GetObject (binding));
154
+ if (ed)
155
+ return ed->GetSocket();
156
+ else
157
+ #ifdef BUILD_FOR_RUBY
158
+ rb_raise(rb_eRuntimeError, "invalid binding to get_fd");
159
+ #else
160
+ throw std::runtime_error ("invalid binding to get_fd");
161
+ #endif
162
+ }
163
+
164
+ /***********************
165
+ evma_is_notify_readable
166
+ ***********************/
167
+
168
+ extern "C" int evma_is_notify_readable (const unsigned long binding)
169
+ {
170
+ ConnectionDescriptor *cd = dynamic_cast <ConnectionDescriptor*> (Bindable_t::GetObject (binding));
171
+ if (cd)
172
+ return cd->IsNotifyReadable() ? 1 : 0;
173
+ return -1;
174
+ }
175
+
176
+ /************************
177
+ evma_set_notify_readable
178
+ ************************/
179
+
180
+ extern "C" void evma_set_notify_readable (const unsigned long binding, int mode)
181
+ {
182
+ ConnectionDescriptor *cd = dynamic_cast <ConnectionDescriptor*> (Bindable_t::GetObject (binding));
183
+ if (cd)
184
+ cd->SetNotifyReadable (mode ? true : false);
185
+ }
186
+
187
+ /***********************
188
+ evma_is_notify_writable
189
+ ***********************/
190
+
191
+ extern "C" int evma_is_notify_writable (const unsigned long binding)
192
+ {
193
+ ConnectionDescriptor *cd = dynamic_cast <ConnectionDescriptor*> (Bindable_t::GetObject (binding));
194
+ if (cd)
195
+ return cd->IsNotifyWritable() ? 1 : 0;
196
+ return -1;
197
+ }
198
+
199
+ /************************
200
+ evma_set_notify_writable
201
+ ************************/
202
+
203
+ extern "C" void evma_set_notify_writable (const unsigned long binding, int mode)
204
+ {
205
+ ConnectionDescriptor *cd = dynamic_cast <ConnectionDescriptor*> (Bindable_t::GetObject (binding));
206
+ if (cd)
207
+ cd->SetNotifyWritable (mode ? true : false);
208
+ }
209
+
210
+ /**********
211
+ evma_pause
212
+ **********/
213
+
214
+ extern "C" int evma_pause (const unsigned long binding)
215
+ {
216
+ ConnectionDescriptor *cd = dynamic_cast <ConnectionDescriptor*> (Bindable_t::GetObject (binding));
217
+ if (cd)
218
+ return cd->Pause() ? 1 : 0;
219
+
220
+ return 0;
221
+ }
222
+
223
+ /***********
224
+ evma_resume
225
+ ***********/
226
+
227
+ extern "C" int evma_resume (const unsigned long binding)
228
+ {
229
+ ConnectionDescriptor *cd = dynamic_cast <ConnectionDescriptor*> (Bindable_t::GetObject (binding));
230
+ if (cd)
231
+ return cd->Resume() ? 1 : 0;
232
+
233
+ return 0;
234
+ }
235
+
236
+ /**************
237
+ evma_is_paused
238
+ **************/
239
+
240
+ extern "C" int evma_is_paused (const unsigned long binding)
241
+ {
242
+ ConnectionDescriptor *cd = dynamic_cast <ConnectionDescriptor*> (Bindable_t::GetObject (binding));
243
+ if (cd)
244
+ return cd->IsPaused() ? 1 : 0;
245
+
246
+ return 0;
247
+ }
248
+
249
+ /**********************
250
+ evma_create_tcp_server
251
+ **********************/
252
+
253
+ extern "C" const unsigned long evma_create_tcp_server (const char *address, int port)
254
+ {
255
+ ensure_eventmachine("evma_create_tcp_server");
256
+ return EventMachine->CreateTcpServer (address, port);
257
+ }
258
+
259
+ /******************************
260
+ evma_create_unix_domain_server
261
+ ******************************/
262
+
263
+ extern "C" const unsigned long evma_create_unix_domain_server (const char *filename)
264
+ {
265
+ ensure_eventmachine("evma_create_unix_domain_server");
266
+ return EventMachine->CreateUnixDomainServer (filename);
267
+ }
268
+
269
+ /*************************
270
+ evma_open_datagram_socket
271
+ *************************/
272
+
273
+ extern "C" const unsigned long evma_open_datagram_socket (const char *address, int port)
274
+ {
275
+ ensure_eventmachine("evma_open_datagram_socket");
276
+ return EventMachine->OpenDatagramSocket (address, port);
277
+ }
278
+
279
+ /******************
280
+ evma_open_keyboard
281
+ ******************/
282
+
283
+ extern "C" const unsigned long evma_open_keyboard()
284
+ {
285
+ ensure_eventmachine("evma_open_keyboard");
286
+ return EventMachine->OpenKeyboard();
287
+ }
288
+
289
+ /*******************
290
+ evma_watch_filename
291
+ *******************/
292
+
293
+ extern "C" const unsigned long evma_watch_filename (const char *fname)
294
+ {
295
+ ensure_eventmachine("evma_watch_filename");
296
+ return EventMachine->WatchFile(fname);
297
+ }
298
+
299
+ /*********************
300
+ evma_unwatch_filename
301
+ *********************/
302
+
303
+ extern "C" void evma_unwatch_filename (const unsigned long sig)
304
+ {
305
+ ensure_eventmachine("evma_unwatch_file");
306
+ EventMachine->UnwatchFile(sig);
307
+ }
308
+
309
+ /**************
310
+ evma_watch_pid
311
+ **************/
312
+
313
+ extern "C" const unsigned long evma_watch_pid (int pid)
314
+ {
315
+ ensure_eventmachine("evma_watch_pid");
316
+ return EventMachine->WatchPid(pid);
317
+ }
318
+
319
+ /****************
320
+ evma_unwatch_pid
321
+ ****************/
322
+
323
+ extern "C" void evma_unwatch_pid (const unsigned long sig)
324
+ {
325
+ ensure_eventmachine("evma_unwatch_pid");
326
+ EventMachine->UnwatchPid(sig);
327
+ }
328
+
329
+ /****************************
330
+ evma_send_data_to_connection
331
+ ****************************/
332
+
333
+ extern "C" int evma_send_data_to_connection (const unsigned long binding, const char *data, int data_length)
334
+ {
335
+ ensure_eventmachine("evma_send_data_to_connection");
336
+ return ConnectionDescriptor::SendDataToConnection (binding, data, data_length);
337
+ }
338
+
339
+ /******************
340
+ evma_send_datagram
341
+ ******************/
342
+
343
+ extern "C" int evma_send_datagram (const unsigned long binding, const char *data, int data_length, const char *address, int port)
344
+ {
345
+ ensure_eventmachine("evma_send_datagram");
346
+ return DatagramDescriptor::SendDatagram (binding, data, data_length, address, port);
347
+ }
348
+
349
+
350
+ /*********************
351
+ evma_close_connection
352
+ *********************/
353
+
354
+ extern "C" void evma_close_connection (const unsigned long binding, int after_writing)
355
+ {
356
+ ensure_eventmachine("evma_close_connection");
357
+ ConnectionDescriptor::CloseConnection (binding, (after_writing ? true : false));
358
+ }
359
+
360
+ /***********************************
361
+ evma_report_connection_error_status
362
+ ***********************************/
363
+
364
+ extern "C" int evma_report_connection_error_status (const unsigned long binding)
365
+ {
366
+ ensure_eventmachine("evma_report_connection_error_status");
367
+ return ConnectionDescriptor::ReportErrorStatus (binding);
368
+ }
369
+
370
+ /********************
371
+ evma_stop_tcp_server
372
+ ********************/
373
+
374
+ extern "C" void evma_stop_tcp_server (const unsigned long binding)
375
+ {
376
+ ensure_eventmachine("evma_stop_tcp_server");
377
+ AcceptorDescriptor::StopAcceptor (binding);
378
+ }
379
+
380
+
381
+ /*****************
382
+ evma_stop_machine
383
+ *****************/
384
+
385
+ extern "C" void evma_stop_machine()
386
+ {
387
+ ensure_eventmachine("evma_stop_machine");
388
+ EventMachine->ScheduleHalt();
389
+ }
390
+
391
+
392
+ /**************
393
+ evma_start_tls
394
+ **************/
395
+
396
+ extern "C" void evma_start_tls (const unsigned long binding)
397
+ {
398
+ ensure_eventmachine("evma_start_tls");
399
+ EventableDescriptor *ed = dynamic_cast <EventableDescriptor*> (Bindable_t::GetObject (binding));
400
+ if (ed)
401
+ ed->StartTls();
402
+ }
403
+
404
+ /******************
405
+ evma_set_tls_parms
406
+ ******************/
407
+
408
+ extern "C" void evma_set_tls_parms (const unsigned long binding, const char *privatekey_filename, const char *certchain_filename, int verify_peer)
409
+ {
410
+ ensure_eventmachine("evma_set_tls_parms");
411
+ EventableDescriptor *ed = dynamic_cast <EventableDescriptor*> (Bindable_t::GetObject (binding));
412
+ if (ed)
413
+ ed->SetTlsParms (privatekey_filename, certchain_filename, (verify_peer == 1 ? true : false));
414
+ }
415
+
416
+ /******************
417
+ evma_get_peer_cert
418
+ ******************/
419
+
420
+ #ifdef WITH_SSL
421
+ extern "C" X509 *evma_get_peer_cert (const unsigned long binding)
422
+ {
423
+ ensure_eventmachine("evma_get_peer_cert");
424
+ EventableDescriptor *ed = dynamic_cast <EventableDescriptor*> (Bindable_t::GetObject (binding));
425
+ if (ed)
426
+ return ed->GetPeerCert();
427
+ return NULL;
428
+ }
429
+ #endif
430
+
431
+ /********************
432
+ evma_accept_ssl_peer
433
+ ********************/
434
+
435
+ #ifdef WITH_SSL
436
+ extern "C" void evma_accept_ssl_peer (const unsigned long binding)
437
+ {
438
+ ensure_eventmachine("evma_accept_ssl_peer");
439
+ ConnectionDescriptor *cd = dynamic_cast <ConnectionDescriptor*> (Bindable_t::GetObject (binding));
440
+ if (cd)
441
+ cd->AcceptSslPeer();
442
+ }
443
+ #endif
444
+
445
+ /*****************
446
+ evma_get_peername
447
+ *****************/
448
+
449
+ extern "C" int evma_get_peername (const unsigned long binding, struct sockaddr *sa)
450
+ {
451
+ ensure_eventmachine("evma_get_peername");
452
+ EventableDescriptor *ed = dynamic_cast <EventableDescriptor*> (Bindable_t::GetObject (binding));
453
+ if (ed) {
454
+ return ed->GetPeername (sa) ? 1 : 0;
455
+ }
456
+ else
457
+ return 0;
458
+ }
459
+
460
+ /*****************
461
+ evma_get_sockname
462
+ *****************/
463
+
464
+ extern "C" int evma_get_sockname (const unsigned long binding, struct sockaddr *sa)
465
+ {
466
+ ensure_eventmachine("evma_get_sockname");
467
+ EventableDescriptor *ed = dynamic_cast <EventableDescriptor*> (Bindable_t::GetObject (binding));
468
+ if (ed) {
469
+ return ed->GetSockname (sa) ? 1 : 0;
470
+ }
471
+ else
472
+ return 0;
473
+ }
474
+
475
+ /***********************
476
+ evma_get_subprocess_pid
477
+ ***********************/
478
+
479
+ extern "C" int evma_get_subprocess_pid (const unsigned long binding, pid_t *pid)
480
+ {
481
+ ensure_eventmachine("evma_get_subprocess_pid");
482
+ #ifdef OS_UNIX
483
+ PipeDescriptor *pd = dynamic_cast <PipeDescriptor*> (Bindable_t::GetObject (binding));
484
+ if (pd) {
485
+ return pd->GetSubprocessPid (pid) ? 1 : 0;
486
+ }
487
+ else if (pid && EventMachine->SubprocessPid) {
488
+ *pid = EventMachine->SubprocessPid;
489
+ return 1;
490
+ }
491
+ else
492
+ return 0;
493
+ #else
494
+ return 0;
495
+ #endif
496
+ }
497
+
498
+ /**************************
499
+ evma_get_subprocess_status
500
+ **************************/
501
+
502
+ extern "C" int evma_get_subprocess_status (const unsigned long binding, int *status)
503
+ {
504
+ ensure_eventmachine("evma_get_subprocess_status");
505
+ if (status) {
506
+ *status = EventMachine->SubprocessExitStatus;
507
+ return 1;
508
+ }
509
+ else
510
+ return 0;
511
+ }
512
+
513
+ /*************************
514
+ evma_get_connection_count
515
+ *************************/
516
+
517
+ extern "C" int evma_get_connection_count()
518
+ {
519
+ ensure_eventmachine("evma_get_connection_count");
520
+ return EventMachine->GetConnectionCount();
521
+ }
522
+
523
+ /*********************
524
+ evma_signal_loopbreak
525
+ *********************/
526
+
527
+ extern "C" void evma_signal_loopbreak()
528
+ {
529
+ ensure_eventmachine("evma_signal_loopbreak");
530
+ EventMachine->SignalLoopBreaker();
531
+ }
532
+
533
+
534
+
535
+ /****************
536
+ evma__write_file
537
+ ****************/
538
+
539
+ extern "C" const unsigned long evma__write_file (const char *filename)
540
+ {
541
+ ensure_eventmachine("evma__write_file");
542
+ return EventMachine->_OpenFileForWriting (filename);
543
+ }
544
+
545
+
546
+ /********************************
547
+ evma_get_comm_inactivity_timeout
548
+ ********************************/
549
+
550
+ extern "C" float evma_get_comm_inactivity_timeout (const unsigned long binding)
551
+ {
552
+ ensure_eventmachine("evma_get_comm_inactivity_timeout");
553
+ EventableDescriptor *ed = dynamic_cast <EventableDescriptor*> (Bindable_t::GetObject (binding));
554
+ if (ed) {
555
+ return ed->GetCommInactivityTimeout();
556
+ }
557
+ else
558
+ return 0.0; //Perhaps this should be an exception. Access to an unknown binding.
559
+ }
560
+
561
+ /********************************
562
+ evma_set_comm_inactivity_timeout
563
+ ********************************/
564
+
565
+ extern "C" int evma_set_comm_inactivity_timeout (const unsigned long binding, float value)
566
+ {
567
+ ensure_eventmachine("evma_set_comm_inactivity_timeout");
568
+ EventableDescriptor *ed = dynamic_cast <EventableDescriptor*> (Bindable_t::GetObject (binding));
569
+ if (ed) {
570
+ return ed->SetCommInactivityTimeout (value);
571
+ }
572
+ else
573
+ return 0; //Perhaps this should be an exception. Access to an unknown binding.
574
+ }
575
+
576
+
577
+ /********************************
578
+ evma_get_pending_connect_timeout
579
+ ********************************/
580
+
581
+ extern "C" float evma_get_pending_connect_timeout (const unsigned long binding)
582
+ {
583
+ ensure_eventmachine("evma_get_pending_connect_timeout");
584
+ EventableDescriptor *ed = dynamic_cast <EventableDescriptor*> (Bindable_t::GetObject (binding));
585
+ if (ed) {
586
+ return ed->GetPendingConnectTimeout();
587
+ }
588
+ else
589
+ return 0.0;
590
+ }
591
+
592
+
593
+ /********************************
594
+ evma_set_pending_connect_timeout
595
+ ********************************/
596
+
597
+ extern "C" int evma_set_pending_connect_timeout (const unsigned long binding, float value)
598
+ {
599
+ ensure_eventmachine("evma_set_pending_connect_timeout");
600
+ EventableDescriptor *ed = dynamic_cast <EventableDescriptor*> (Bindable_t::GetObject (binding));
601
+ if (ed) {
602
+ return ed->SetPendingConnectTimeout (value);
603
+ }
604
+ else
605
+ return 0;
606
+ }
607
+
608
+
609
+ /**********************
610
+ evma_set_timer_quantum
611
+ **********************/
612
+
613
+ extern "C" void evma_set_timer_quantum (int interval)
614
+ {
615
+ ensure_eventmachine("evma_set_timer_quantum");
616
+ EventMachine->SetTimerQuantum (interval);
617
+ }
618
+
619
+
620
+ /************************
621
+ evma_get_max_timer_count
622
+ ************************/
623
+
624
+ extern "C" int evma_get_max_timer_count()
625
+ {
626
+ return EventMachine_t::GetMaxTimerCount();
627
+ }
628
+
629
+
630
+ /************************
631
+ evma_set_max_timer_count
632
+ ************************/
633
+
634
+ extern "C" void evma_set_max_timer_count (int ct)
635
+ {
636
+ // This may only be called if the reactor is not running.
637
+
638
+ if (EventMachine)
639
+ #ifdef BUILD_FOR_RUBY
640
+ rb_raise(rb_eRuntimeError, "eventmachine already initialized: evma_set_max_timer_count");
641
+ #else
642
+ throw std::runtime_error ("eventmachine already initialized: evma_set_max_timer_count");
643
+ #endif
644
+ EventMachine_t::SetMaxTimerCount (ct);
645
+ }
646
+
647
+ /******************
648
+ evma_setuid_string
649
+ ******************/
650
+
651
+ extern "C" void evma_setuid_string (const char *username)
652
+ {
653
+ // We do NOT need to be running an EM instance because this method is static.
654
+ EventMachine_t::SetuidString (username);
655
+ }
656
+
657
+
658
+ /**********
659
+ evma_popen
660
+ **********/
661
+
662
+ extern "C" const unsigned long evma_popen (char * const*cmd_strings)
663
+ {
664
+ ensure_eventmachine("evma_popen");
665
+ return EventMachine->Socketpair (cmd_strings);
666
+ }
667
+
668
+
669
+ /***************************
670
+ evma_get_outbound_data_size
671
+ ***************************/
672
+
673
+ extern "C" int evma_get_outbound_data_size (const unsigned long binding)
674
+ {
675
+ ensure_eventmachine("evma_get_outbound_data_size");
676
+ EventableDescriptor *ed = dynamic_cast <EventableDescriptor*> (Bindable_t::GetObject (binding));
677
+ return ed ? ed->GetOutboundDataSize() : 0;
678
+ }
679
+
680
+
681
+ /**************
682
+ evma_set_epoll
683
+ **************/
684
+
685
+ extern "C" void evma_set_epoll (int use)
686
+ {
687
+ bUseEpoll = !!use;
688
+ }
689
+
690
+ /***************
691
+ evma_set_kqueue
692
+ ***************/
693
+
694
+ extern "C" void evma_set_kqueue (int use)
695
+ {
696
+ bUseKqueue = !!use;
697
+ }
698
+
699
+
700
+ /**********************
701
+ evma_set_rlimit_nofile
702
+ **********************/
703
+
704
+ extern "C" int evma_set_rlimit_nofile (int nofiles)
705
+ {
706
+ return EventMachine_t::SetRlimitNofile (nofiles);
707
+ }
708
+
709
+
710
+ /*********************************
711
+ evma_send_file_data_to_connection
712
+ *********************************/
713
+
714
+ extern "C" int evma_send_file_data_to_connection (const unsigned long binding, const char *filename)
715
+ {
716
+ /* This is a sugaring over send_data_to_connection that reads a file into a
717
+ * locally-allocated buffer, and sends the file data to the remote peer.
718
+ * Return the number of bytes written to the caller.
719
+ * TODO, needs to impose a limit on the file size. This is intended only for
720
+ * small files. (I don't know, maybe 8K or less.) For larger files, use interleaved
721
+ * I/O to avoid slowing the rest of the system down.
722
+ * TODO: we should return a code rather than barf, in case of file-not-found.
723
+ * TODO, does this compile on Windows?
724
+ * TODO, given that we want this to work only with small files, how about allocating
725
+ * the buffer on the stack rather than the heap?
726
+ *
727
+ * Modified 25Jul07. This now returns -1 on file-too-large; 0 for success, and a positive
728
+ * errno in case of other errors.
729
+ *
730
+ * Contributed by Kirk Haines.
731
+ */
732
+
733
+ char data[32*1024];
734
+ int r;
735
+
736
+ ensure_eventmachine("evma_send_file_data_to_connection");
737
+
738
+ int Fd = open (filename, O_RDONLY);
739
+
740
+ if (Fd < 0)
741
+ return errno;
742
+ // From here on, all early returns MUST close Fd.
743
+
744
+ #undef stat //for Ruby 1.9
745
+ struct stat st;
746
+ if (fstat (Fd, &st)) {
747
+ int e = errno;
748
+ close (Fd);
749
+ return e;
750
+ }
751
+
752
+ off_t filesize = st.st_size;
753
+ if (filesize <= 0) {
754
+ close (Fd);
755
+ return 0;
756
+ }
757
+ else if (filesize > (off_t) sizeof(data)) {
758
+ close (Fd);
759
+ return -1;
760
+ }
761
+
762
+
763
+ r = read (Fd, data, filesize);
764
+ if (r != filesize) {
765
+ int e = errno;
766
+ close (Fd);
767
+ return e;
768
+ }
769
+ evma_send_data_to_connection (binding, data, r);
770
+ close (Fd);
771
+
772
+ return 0;
773
+ }
774
+
775
+
776
+ /****************
777
+ evma_start_proxy
778
+ *****************/
779
+
780
+ extern "C" void evma_start_proxy (const unsigned long from, const unsigned long to)
781
+ {
782
+ ensure_eventmachine("evma_start_proxy");
783
+ EventableDescriptor *ed = dynamic_cast <EventableDescriptor*> (Bindable_t::GetObject (from));
784
+ if (ed)
785
+ ed->StartProxy(to);
786
+ }
787
+
788
+
789
+ /***************
790
+ evma_stop_proxy
791
+ ****************/
792
+
793
+ extern "C" void evma_stop_proxy (const unsigned long from)
794
+ {
795
+ ensure_eventmachine("evma_stop_proxy");
796
+ EventableDescriptor *ed = dynamic_cast <EventableDescriptor*> (Bindable_t::GetObject (from));
797
+ if (ed)
798
+ ed->StopProxy();
799
+ }
800
+
801
+
802
+ /***************************
803
+ evma_get_heartbeat_interval
804
+ ****************************/
805
+
806
+ extern "C" float evma_get_heartbeat_interval()
807
+ {
808
+ ensure_eventmachine("evma_get_heartbeat_interval");
809
+ return EventMachine->GetHeartbeatInterval();
810
+ }
811
+
812
+
813
+ /***************************
814
+ evma_set_heartbeat_interval
815
+ ****************************/
816
+
817
+ extern "C" int evma_set_heartbeat_interval(float interval)
818
+ {
819
+ ensure_eventmachine("evma_set_heartbeat_interval");
820
+ return EventMachine->SetHeartbeatInterval(interval);
821
+ }