smparkes-eventmachine 0.12.10

Sign up to get free protection for your applications and to get access to all the features.
Files changed (158) hide show
  1. data/.gitignore +15 -0
  2. data/README +81 -0
  3. data/Rakefile +374 -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 +40 -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 +827 -0
  25. data/ext/cplusplus.cpp +202 -0
  26. data/ext/ed.cpp +1901 -0
  27. data/ext/ed.h +424 -0
  28. data/ext/em.cpp +2288 -0
  29. data/ext/em.h +229 -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 +150 -0
  37. data/ext/fastfilereader/extconf.rb +85 -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 +156 -0
  48. data/ext/rubymain.cpp +1194 -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/EmReactor.java +570 -0
  56. data/java/src/com/rubyeventmachine/EmReactorException.java +40 -0
  57. data/java/src/com/rubyeventmachine/EventableChannel.java +69 -0
  58. data/java/src/com/rubyeventmachine/EventableDatagramChannel.java +189 -0
  59. data/java/src/com/rubyeventmachine/EventableSocketChannel.java +364 -0
  60. data/java/src/com/rubyeventmachine/application/Application.java +194 -0
  61. data/java/src/com/rubyeventmachine/application/Connection.java +74 -0
  62. data/java/src/com/rubyeventmachine/application/ConnectionFactory.java +37 -0
  63. data/java/src/com/rubyeventmachine/application/DefaultConnectionFactory.java +46 -0
  64. data/java/src/com/rubyeventmachine/application/PeriodicTimer.java +38 -0
  65. data/java/src/com/rubyeventmachine/application/Timer.java +54 -0
  66. data/java/src/com/rubyeventmachine/tests/ApplicationTest.java +109 -0
  67. data/java/src/com/rubyeventmachine/tests/ConnectTest.java +148 -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 +75 -0
  71. data/java/src/com/rubyeventmachine/tests/TestTimers.java +90 -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 +192 -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/header_and_content.rb +138 -0
  83. data/lib/em/protocols/httpclient.rb +263 -0
  84. data/lib/em/protocols/httpclient2.rb +590 -0
  85. data/lib/em/protocols/line_and_text.rb +125 -0
  86. data/lib/em/protocols/linetext2.rb +161 -0
  87. data/lib/em/protocols/memcache.rb +323 -0
  88. data/lib/em/protocols/object_protocol.rb +45 -0
  89. data/lib/em/protocols/postgres3.rb +247 -0
  90. data/lib/em/protocols/saslauth.rb +175 -0
  91. data/lib/em/protocols/smtpclient.rb +357 -0
  92. data/lib/em/protocols/smtpserver.rb +547 -0
  93. data/lib/em/protocols/socks4.rb +66 -0
  94. data/lib/em/protocols/stomp.rb +200 -0
  95. data/lib/em/protocols/tcptest.rb +53 -0
  96. data/lib/em/protocols.rb +36 -0
  97. data/lib/em/queue.rb +61 -0
  98. data/lib/em/spawnable.rb +85 -0
  99. data/lib/em/streamer.rb +130 -0
  100. data/lib/em/timers.rb +56 -0
  101. data/lib/em/version.rb +3 -0
  102. data/lib/eventmachine.rb +1592 -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/evma.rb +32 -0
  109. data/lib/jeventmachine.rb +257 -0
  110. data/lib/pr_eventmachine.rb +1022 -0
  111. data/setup.rb +1585 -0
  112. data/tasks/cpp.rake_example +77 -0
  113. data/tests/client.crt +31 -0
  114. data/tests/client.key +51 -0
  115. data/tests/test_attach.rb +126 -0
  116. data/tests/test_basic.rb +284 -0
  117. data/tests/test_channel.rb +63 -0
  118. data/tests/test_connection_count.rb +35 -0
  119. data/tests/test_defer.rb +47 -0
  120. data/tests/test_epoll.rb +160 -0
  121. data/tests/test_error_handler.rb +35 -0
  122. data/tests/test_errors.rb +82 -0
  123. data/tests/test_exc.rb +55 -0
  124. data/tests/test_file_watch.rb +49 -0
  125. data/tests/test_futures.rb +198 -0
  126. data/tests/test_get_sock_opt.rb +30 -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_pause.rb +70 -0
  138. data/tests/test_pending_connect_timeout.rb +48 -0
  139. data/tests/test_process_watch.rb +48 -0
  140. data/tests/test_processes.rb +128 -0
  141. data/tests/test_proxy_connection.rb +92 -0
  142. data/tests/test_pure.rb +125 -0
  143. data/tests/test_queue.rb +44 -0
  144. data/tests/test_running.rb +42 -0
  145. data/tests/test_sasl.rb +72 -0
  146. data/tests/test_send_file.rb +242 -0
  147. data/tests/test_servers.rb +76 -0
  148. data/tests/test_smtpclient.rb +83 -0
  149. data/tests/test_smtpserver.rb +85 -0
  150. data/tests/test_spawn.rb +322 -0
  151. data/tests/test_ssl_args.rb +79 -0
  152. data/tests/test_ssl_methods.rb +50 -0
  153. data/tests/test_ssl_verify.rb +82 -0
  154. data/tests/test_timers.rb +162 -0
  155. data/tests/test_ud.rb +36 -0
  156. data/tests/testem.rb +31 -0
  157. data/web/whatis +7 -0
  158. metadata +237 -0
@@ -0,0 +1,564 @@
1
+ module EventMachine
2
+ class FileNotFoundException < Exception # :nodoc:
3
+ end
4
+
5
+ # EventMachine::Connection is a class that is instantiated
6
+ # by EventMachine's processing loop whenever a new connection
7
+ # is created. (New connections can be either initiated locally
8
+ # to a remote server or accepted locally from a remote client.)
9
+ # When a Connection object is instantiated, it <i>mixes in</i>
10
+ # the functionality contained in the user-defined module
11
+ # specified in calls to EventMachine#connect or EventMachine#start_server.
12
+ # User-defined handler modules may redefine any or all of the standard
13
+ # methods defined here, as well as add arbitrary additional code
14
+ # that will also be mixed in.
15
+ #
16
+ # EventMachine manages one object inherited from EventMachine::Connection
17
+ # (and containing the mixed-in user code) for every network connection
18
+ # that is active at any given time.
19
+ # The event loop will automatically call methods on EventMachine::Connection
20
+ # objects whenever specific events occur on the corresponding connections,
21
+ # as described below.
22
+ #
23
+ # This class is never instantiated by user code, and does not publish an
24
+ # initialize method. The instance methods of EventMachine::Connection
25
+ # which may be called by the event loop are: post_init, receive_data,
26
+ # and unbind. All of the other instance methods defined here are called
27
+ # only by user code.
28
+ #
29
+ class Connection
30
+ attr_accessor :signature # :nodoc:
31
+
32
+ # Override .new so subclasses don't have to call super and can ignore
33
+ # connection-specific arguments
34
+ #
35
+ def self.new(sig, *args) #:nodoc:
36
+ allocate.instance_eval do
37
+ # Store signature
38
+ @signature = sig
39
+ associate_callback_target sig
40
+
41
+ # Call a superclass's #initialize if it has one
42
+ initialize(*args)
43
+
44
+ # post initialize callback
45
+ post_init
46
+
47
+ self
48
+ end
49
+ end
50
+
51
+ # Stubbed initialize so legacy superclasses can safely call super
52
+ #
53
+ def initialize(*args) #:nodoc:
54
+ end
55
+
56
+ # def associate_callback_target(sig) #:nodoc:
57
+ # # no-op for the time being, to match similar no-op in rubymain.cpp
58
+ # end
59
+
60
+ # EventMachine::Connection#post_init is called by the event loop
61
+ # immediately after the network connection has been established,
62
+ # and before resumption of the network loop.
63
+ # This method is generally not called by user code, but is called automatically
64
+ # by the event loop. The base-class implementation is a no-op.
65
+ # This is a very good place to initialize instance variables that will
66
+ # be used throughout the lifetime of the network connection.
67
+ #
68
+ def post_init
69
+ end
70
+
71
+ # EventMachine::Connection#receive_data is called by the event loop
72
+ # whenever data has been received by the network connection.
73
+ # It is never called by user code.
74
+ # receive_data is called with a single parameter, a String containing
75
+ # the network protocol data, which may of course be binary. You will
76
+ # generally redefine this method to perform your own processing of the incoming data.
77
+ #
78
+ # Here's a key point which is essential to understanding the event-driven
79
+ # programming model: <i>EventMachine knows absolutely nothing about the protocol
80
+ # which your code implements.</i> You must not make any assumptions about
81
+ # the size of the incoming data packets, or about their alignment on any
82
+ # particular intra-message or PDU boundaries (such as line breaks).
83
+ # receive_data can and will send you arbitrary chunks of data, with the
84
+ # only guarantee being that the data is presented to your code in the order
85
+ # it was collected from the network. Don't even assume that the chunks of
86
+ # data will correspond to network packets, as EventMachine can and will coalesce
87
+ # several incoming packets into one, to improve performance. The implication for your
88
+ # code is that you generally will need to implement some kind of a state machine
89
+ # in your redefined implementation of receive_data. For a better understanding
90
+ # of this, read through the examples of specific protocol handlers in EventMachine::Protocols
91
+ #
92
+ # The base-class implementation of receive_data (which will be invoked if
93
+ # you don't redefine it) simply prints the size of each incoming data packet
94
+ # to stdout.
95
+ #
96
+ def receive_data data
97
+ puts "............>>>#{data.length}"
98
+ end
99
+
100
+ # #ssl_handshake_completed is called by EventMachine when the SSL/TLS handshake has
101
+ # been completed, as a result of calling #start_tls to initiate SSL/TLS on the connection.
102
+ #
103
+ # This callback exists because #post_init and #connection_completed are <b>not</b> reliable
104
+ # for indicating when an SSL/TLS connection is ready to have it's certificate queried for.
105
+ #
106
+ # See #get_peer_cert for application and example.
107
+ def ssl_handshake_completed
108
+ end
109
+
110
+ # #ssl_verify_peer is called by EventMachine when :verify_peer => true has been passed to #start_tls.
111
+ # It will be called with each certificate in the certificate chain provided by the remote peer.
112
+ # The cert will be passed as a String in PEM format, the same as in #get_peer_cert. It is up to user defined
113
+ # code to perform a check on the certificates. The return value from this callback is used to accept or deny the peer.
114
+ # A return value that is not nil or false triggers acceptance. If the peer is not accepted, the connection
115
+ # will be subsequently closed. See 'tests/test_ssl_verify.rb' for a simple example.
116
+ def ssl_verify_peer(cert)
117
+ end
118
+
119
+ # EventMachine::Connection#unbind is called by the framework whenever a connection
120
+ # (either a server or client connection) is closed. The close can occur because
121
+ # your code intentionally closes it (see close_connection and close_connection_after_writing),
122
+ # because the remote peer closed the connection, or because of a network error.
123
+ # You may not assume that the network connection is still open and able to send or
124
+ # receive data when the callback to unbind is made. This is intended only to give
125
+ # you a chance to clean up associations your code may have made to the connection
126
+ # object while it was open.
127
+ #
128
+ def unbind
129
+ end
130
+
131
+ # EventMachine::Connection#proxy_target_unbound is called by the reactor after attempting
132
+ # to relay incoming data to a descriptor (set as a proxy target descriptor with
133
+ # EventMachine::enable_proxy) that has already been closed.
134
+ def proxy_target_unbound
135
+ end
136
+
137
+ # EventMachine::Connection#proxy_incoming_to is called only by user code. It sets up
138
+ # a low-level proxy relay for all data inbound for this connection, to the connection given
139
+ # as the argument. This is essentially just a helper method for enable_proxy.
140
+ # See EventMachine::enable_proxy documentation for details.
141
+ def proxy_incoming_to(conn,bufsize=0)
142
+ EventMachine::enable_proxy(self, conn, bufsize)
143
+ end
144
+
145
+ # Helper method for EventMachine::disable_proxy(self)
146
+ def stop_proxying
147
+ EventMachine::disable_proxy(self)
148
+ end
149
+
150
+ # EventMachine::Connection#close_connection is called only by user code, and never
151
+ # by the event loop. You may call this method against a connection object in any
152
+ # callback handler, whether or not the callback was made against the connection
153
+ # you want to close. close_connection <i>schedules</i> the connection to be closed
154
+ # at the next available opportunity within the event loop. You may not assume that
155
+ # the connection is closed when close_connection returns. In particular, the framework
156
+ # will callback the unbind method for the particular connection at a point shortly
157
+ # after you call close_connection. You may assume that the unbind callback will
158
+ # take place sometime after your call to close_connection completes. In other words,
159
+ # the unbind callback will not re-enter your code "inside" of your call to close_connection.
160
+ # However, it's not guaranteed that a future version of EventMachine will not change
161
+ # this behavior.
162
+ #
163
+ # close_connection will <i>silently discard</i> any outbound data which you have
164
+ # sent to the connection using EventMachine::Connection#send_data but which has not
165
+ # yet been sent across the network. If you want to avoid this behavior, use
166
+ # EventMachine::Connection#close_connection_after_writing.
167
+ #
168
+ def close_connection after_writing = false
169
+ EventMachine::close_connection @signature, after_writing
170
+ end
171
+
172
+ # EventMachine::Connection#detach will remove the given connection from the event loop.
173
+ # The connection's socket remains open and its file descriptor number is returned
174
+ def detach
175
+ EventMachine::detach_fd @signature
176
+ end
177
+
178
+ def get_sock_opt level, option
179
+ EventMachine::get_sock_opt @signature, level, option
180
+ end
181
+
182
+ # EventMachine::Connection#close_connection_after_writing is a variant of close_connection.
183
+ # All of the descriptive comments given for close_connection also apply to
184
+ # close_connection_after_writing, <i>with one exception:</i> If the connection has
185
+ # outbound data sent using send_dat but which has not yet been sent across the network,
186
+ # close_connection_after_writing will schedule the connection to be closed <i>after</i>
187
+ # all of the outbound data has been safely written to the remote peer.
188
+ #
189
+ # Depending on the amount of outgoing data and the speed of the network,
190
+ # considerable time may elapse between your call to close_connection_after_writing
191
+ # and the actual closing of the socket (at which time the unbind callback will be called
192
+ # by the event loop). During this time, you <i>may not</i> call send_data to transmit
193
+ # additional data (that is, the connection is closed for further writes). In very
194
+ # rare cases, you may experience a receive_data callback after your call to close_connection_after_writing,
195
+ # depending on whether incoming data was in the process of being received on the connection
196
+ # at the moment when you called close_connection_after_writing. Your protocol handler must
197
+ # be prepared to properly deal with such data (probably by ignoring it).
198
+ #
199
+ def close_connection_after_writing
200
+ close_connection true
201
+ end
202
+
203
+ # EventMachine::Connection#send_data is only called by user code, never by
204
+ # the event loop. You call this method to send data to the remote end of the
205
+ # network connection. send_data is called with a single String argument, which
206
+ # may of course contain binary data. You can call send_data any number of times.
207
+ # send_data is an instance method of an object derived from EventMachine::Connection
208
+ # and containing your mixed-in handler code), so if you call it without qualification
209
+ # within a callback function, the data will be sent to the same network connection
210
+ # that generated the callback. Calling self.send_data is exactly equivalent.
211
+ #
212
+ # You can also call send_data to write to a connection <i>other than the one
213
+ # whose callback you are calling send_data from.</i> This is done by recording
214
+ # the value of the connection in any callback function (the value self), in any
215
+ # variable visible to other callback invocations on the same or different
216
+ # connection objects. (Need an example to make that clear.)
217
+ #
218
+ def send_data data
219
+ data = data.to_s
220
+ size = data.bytesize if data.respond_to?(:bytesize)
221
+ size ||= data.size
222
+ EventMachine::send_data @signature, data, size
223
+ end
224
+
225
+ # Returns true if the connection is in an error state, false otherwise.
226
+ # In general, you can detect the occurrence of communication errors or unexpected
227
+ # disconnection by the remote peer by handing the #unbind method. In some cases, however,
228
+ # it's useful to check the status of the connection using #error? before attempting to send data.
229
+ # This function is synchronous: it will return immediately without blocking.
230
+ #
231
+ #
232
+ def error?
233
+ EventMachine::report_connection_error_status(@signature) != 0
234
+ end
235
+
236
+ # #connection_completed is called by the event loop when a remote TCP connection
237
+ # attempt completes successfully. You can expect to get this notification after calls
238
+ # to EventMachine#connect. Remember that EventMachine makes remote connections
239
+ # asynchronously, just as with any other kind of network event. #connection_completed
240
+ # is intended primarily to assist with network diagnostics. For normal protocol
241
+ # handling, use #post_init to perform initial work on a new connection (such as
242
+ # send an initial set of data).
243
+ # #post_init will always be called. #connection_completed will only be called in case
244
+ # of a successful completion. A connection-attempt which fails will receive a call
245
+ # to #unbind after the failure.
246
+ def connection_completed
247
+ end
248
+
249
+ # Call #start_tls at any point to initiate TLS encryption on connected streams.
250
+ # The method is smart enough to know whether it should perform a server-side
251
+ # or a client-side handshake. An appropriate place to call #start_tls is in
252
+ # your redefined #post_init method, or in the #connection_completed handler for
253
+ # an outbound connection.
254
+ #
255
+ # #start_tls takes an optional parameter hash that allows you to specify certificate
256
+ # and other options to be used with this Connection object. Here are the currently-supported
257
+ # options:
258
+ #
259
+ # * :cert_chain_file :
260
+ # takes a String, which is interpreted as the name of a readable file in the
261
+ # local filesystem. The file is expected to contain a chain of X509 certificates in
262
+ # PEM format, with the most-resolved certificate at the top of the file, successive
263
+ # intermediate certs in the middle, and the root (or CA) cert at the bottom.
264
+ #
265
+ # * :private_key_file :
266
+ # takes a String, which is interpreted as the name of a readable file in the
267
+ # local filesystem. The file must contain a private key in PEM format.
268
+ #
269
+ # * :verify_peer :
270
+ # takes either true or false. Default is false. This indicates whether a server should request a
271
+ # certificate from a peer, to be verified by user code. If true, the #ssl_verify_peer callback
272
+ # on the Connection object is called with each certificate in the certificate chain provided by
273
+ # the peer. See documentation on #ssl_verify_peer for how to use this.
274
+ #
275
+ # === Usage example:
276
+ #
277
+ # require 'rubygems'
278
+ # require 'eventmachine'
279
+ #
280
+ # module Handler
281
+ # def post_init
282
+ # start_tls(:private_key_file => '/tmp/server.key', :cert_chain_file => '/tmp/server.crt', :verify_peer => false)
283
+ # end
284
+ # end
285
+ #
286
+ # EM.run {
287
+ # EM.start_server("127.0.0.1", 9999, Handler)
288
+ # }
289
+ #
290
+ #--
291
+ # TODO: support passing an encryption parameter, which can be string or Proc, to get a passphrase
292
+ # for encrypted private keys.
293
+ # TODO: support passing key material via raw strings or Procs that return strings instead of
294
+ # just filenames.
295
+ # What will get nasty is whether we have to define a location for storing this stuff as files.
296
+ # In general, the OpenSSL interfaces for dealing with certs and keys in files are much better
297
+ # behaved than the ones for raw chunks of memory.
298
+ #
299
+ def start_tls args={}
300
+ priv_key, cert_chain, verify_peer = args.values_at(:private_key_file, :cert_chain_file, :verify_peer)
301
+
302
+ [priv_key, cert_chain].each do |file|
303
+ next if file.nil? or file.empty?
304
+ raise FileNotFoundException,
305
+ "Could not find #{file} for start_tls" unless File.exists? file
306
+ end
307
+
308
+ EventMachine::set_tls_parms(@signature, priv_key || '', cert_chain || '', verify_peer)
309
+ EventMachine::start_tls @signature
310
+ end
311
+
312
+ # If SSL/TLS is active on the connection, #get_peer_cert returns the remote X509 certificate
313
+ # as a String, in the popular PEM format. This can then be used for arbitrary validation
314
+ # of a peer's certificate in your code.
315
+ #
316
+ # This should be called in/after the #ssl_handshake_completed callback, which indicates
317
+ # that SSL/TLS is active. Using this callback is important, because the certificate may not
318
+ # be available until the time it is executed. Using #post_init or #connection_completed is
319
+ # not adequate, because the SSL handshake may still be taking place.
320
+ #
321
+ # #get_peer_cert will return <b>nil</b> if:
322
+ #
323
+ # * EventMachine is not built with OpenSSL support
324
+ # * SSL/TLS is not active on the connection
325
+ # * SSL/TLS handshake is not yet complete
326
+ # * Remote peer for any other reason has not presented a certificate
327
+ #
328
+ # === Example:
329
+ #
330
+ # module Handler
331
+ #
332
+ # def post_init
333
+ # puts "Starting TLS"
334
+ # start_tls
335
+ # end
336
+ #
337
+ # def ssl_handshake_completed
338
+ # puts get_peer_cert
339
+ # close_connection
340
+ # end
341
+ #
342
+ # def unbind
343
+ # EventMachine::stop_event_loop
344
+ # end
345
+ #
346
+ # end
347
+ #
348
+ # EM.run {
349
+ # EventMachine::connect "mail.google.com", 443, Handler
350
+ # }
351
+ #
352
+ # Output:
353
+ # -----BEGIN CERTIFICATE-----
354
+ # MIIDIjCCAougAwIBAgIQbldpChBPqv+BdPg4iwgN8TANBgkqhkiG9w0BAQUFADBM
355
+ # MQswCQYDVQQGEwJaQTElMCMGA1UEChMcVGhhd3RlIENvbnN1bHRpbmcgKFB0eSkg
356
+ # THRkLjEWMBQGA1UEAxMNVGhhd3RlIFNHQyBDQTAeFw0wODA1MDIxNjMyNTRaFw0w
357
+ # OTA1MDIxNjMyNTRaMGkxCzAJBgNVBAYTAlVTMRMwEQYDVQQIEwpDYWxpZm9ybmlh
358
+ # MRYwFAYDVQQHEw1Nb3VudGFpbiBWaWV3MRMwEQYDVQQKEwpHb29nbGUgSW5jMRgw
359
+ # FgYDVQQDEw9tYWlsLmdvb2dsZS5jb20wgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJ
360
+ # AoGBALlkxdh2QXegdElukCSOV2+8PKiONIS+8Tu9K7MQsYpqtLNC860zwOPQ2NLI
361
+ # 3Zp4jwuXVTrtzGuiqf5Jioh35Ig3CqDXtLyZoypjZUQcq4mlLzHlhIQ4EhSjDmA7
362
+ # Ffw9y3ckSOQgdBQWNLbquHh9AbEUjmhkrYxIqKXeCnRKhv6nAgMBAAGjgecwgeQw
363
+ # KAYDVR0lBCEwHwYIKwYBBQUHAwEGCCsGAQUFBwMCBglghkgBhvhCBAEwNgYDVR0f
364
+ # BC8wLTAroCmgJ4YlaHR0cDovL2NybC50aGF3dGUuY29tL1RoYXd0ZVNHQ0NBLmNy
365
+ # bDByBggrBgEFBQcBAQRmMGQwIgYIKwYBBQUHMAGGFmh0dHA6Ly9vY3NwLnRoYXd0
366
+ # ZS5jb20wPgYIKwYBBQUHMAKGMmh0dHA6Ly93d3cudGhhd3RlLmNvbS9yZXBvc2l0
367
+ # b3J5L1RoYXd0ZV9TR0NfQ0EuY3J0MAwGA1UdEwEB/wQCMAAwDQYJKoZIhvcNAQEF
368
+ # BQADgYEAsRwpLg1dgCR1gYDK185MFGukXMeQFUvhGqF8eT/CjpdvezyKVuz84gSu
369
+ # 6ccMXgcPQZGQN/F4Xug+Q01eccJjRSVfdvR5qwpqCj+6BFl5oiKDBsveSkrmL5dz
370
+ # s2bn7TdTSYKcLeBkjXxDLHGBqLJ6TNCJ3c4/cbbG5JhGvoema94=
371
+ # -----END CERTIFICATE-----
372
+ #
373
+ # You can do whatever you want with the certificate String, such as load it
374
+ # as a certificate object using the OpenSSL library, and check it's fields.
375
+ def get_peer_cert
376
+ EventMachine::get_peer_cert @signature
377
+ end
378
+
379
+
380
+ # send_datagram is for sending UDP messages.
381
+ # This method may be called from any Connection object that refers
382
+ # to an open datagram socket (see EventMachine#open_datagram_socket).
383
+ # The method sends a UDP (datagram) packet containing the data you specify,
384
+ # to a remote peer specified by the IP address and port that you give
385
+ # as parameters to the method.
386
+ # Observe that you may send a zero-length packet (empty string).
387
+ # However, you may not send an arbitrarily-large data packet because
388
+ # your operating system will enforce a platform-specific limit on
389
+ # the size of the outbound packet. (Your kernel
390
+ # will respond in a platform-specific way if you send an overlarge
391
+ # packet: some will send a truncated packet, some will complain, and
392
+ # some will silently drop your request).
393
+ # On LANs, it's usually OK to send datagrams up to about 4000 bytes in length,
394
+ # but to be really safe, send messages smaller than the Ethernet-packet
395
+ # size (typically about 1400 bytes). Some very restrictive WANs
396
+ # will either drop or truncate packets larger than about 500 bytes.
397
+ #--
398
+ # Added the Integer wrapper around the port parameter per suggestion by
399
+ # Matthieu Riou, after he passed a String and spent hours tearing his hair out.
400
+ #
401
+ def send_datagram data, recipient_address, recipient_port
402
+ data = data.to_s
403
+ EventMachine::send_datagram @signature, data, data.length, recipient_address, Integer(recipient_port)
404
+ end
405
+
406
+
407
+ # #get_peername is used with stream-connections to obtain the identity
408
+ # of the remotely-connected peer. If a peername is available, this method
409
+ # returns a sockaddr structure. The method returns nil if no peername is available.
410
+ # You can use Socket.unpack_sockaddr_in and its variants to obtain the
411
+ # values contained in the peername structure returned from #get_peername.
412
+ #
413
+ # require 'socket'
414
+ # module Handler
415
+ # def receive_data data
416
+ # port, ip = Socket.unpack_sockaddr_in(get_peername)
417
+ # puts "got #{data.inspect} from #{ip}:#{port}"
418
+ # end
419
+ # end
420
+ def get_peername
421
+ EventMachine::get_peername @signature
422
+ end
423
+
424
+ # #get_sockname is used with stream-connections to obtain the identity
425
+ # of the local side of the connection. If a local name is available, this method
426
+ # returns a sockaddr structure. The method returns nil if no local name is available.
427
+ # You can use Socket#unpack_sockaddr_in and its variants to obtain the
428
+ # values contained in the local-name structure returned from #get_sockname.
429
+ def get_sockname
430
+ EventMachine::get_sockname @signature
431
+ end
432
+
433
+ # Returns the PID (kernel process identifier) of a subprocess
434
+ # associated with this Connection object. For use with EventMachine#popen
435
+ # and similar methods. Returns nil when there is no meaningful subprocess.
436
+ #--
437
+ #
438
+ def get_pid
439
+ EventMachine::get_subprocess_pid @signature
440
+ end
441
+
442
+ # Returns a subprocess exit status. Only useful for #popen. Call it in your
443
+ # #unbind handler.
444
+ #
445
+ def get_status
446
+ EventMachine::get_subprocess_status @signature
447
+ end
448
+
449
+ # comm_inactivity_timeout returns the current value (float in seconds) of the inactivity-timeout
450
+ # property of network-connection and datagram-socket objects. A nonzero value
451
+ # indicates that the connection or socket will automatically be closed if no read or write
452
+ # activity takes place for at least that number of seconds.
453
+ # A zero value (the default) specifies that no automatic timeout will take place.
454
+ def comm_inactivity_timeout
455
+ EventMachine::get_comm_inactivity_timeout @signature
456
+ end
457
+
458
+ # Alias for #set_comm_inactivity_timeout.
459
+ def comm_inactivity_timeout= value
460
+ self.set_comm_inactivity_timeout value
461
+ end
462
+
463
+ # comm_inactivity_timeout= allows you to set the inactivity-timeout property for
464
+ # a network connection or datagram socket. Specify a non-negative float value in seconds.
465
+ # If the value is greater than zero, the connection or socket will automatically be closed
466
+ # if no read or write activity takes place for at least that number of seconds.
467
+ # Specify a value of zero to indicate that no automatic timeout should take place.
468
+ # Zero is the default value.
469
+ def set_comm_inactivity_timeout value
470
+ EventMachine::set_comm_inactivity_timeout @signature, value.to_f
471
+ end
472
+
473
+ # pending_connect_timeout is the duration after which a TCP connection in the connecting
474
+ # state will fail. It is important to distinguish this value from comm_inactivity_timeout,
475
+ # which looks at how long since data was passed on an already established connection.
476
+ # The value is a float in seconds.
477
+ def pending_connect_timeout
478
+ EventMachine::get_pending_connect_timeout @signature
479
+ end
480
+
481
+ # Alias for #set_pending_connect_timeout.
482
+ def pending_connect_timeout= value
483
+ self.set_pending_connect_timeout value
484
+ end
485
+
486
+ # set_pending_connect_timeout sets the duration after which a TCP connection in a
487
+ # connecting state will fail. Takes a float in seconds.
488
+ def set_pending_connect_timeout value
489
+ EventMachine::set_pending_connect_timeout @signature, value.to_f
490
+ end
491
+
492
+ # Reconnect to a given host/port with the current EventMachine::Connection instance
493
+ def reconnect server, port
494
+ EventMachine::reconnect server, port, self
495
+ end
496
+
497
+
498
+ # Like EventMachine::Connection#send_data, this sends data to the remote end of
499
+ # the network connection. EventMachine::Connection@send_file_data takes a
500
+ # filename as an argument, though, and sends the contents of the file, in one
501
+ # chunk. Contributed by Kirk Haines.
502
+ #
503
+ def send_file_data filename
504
+ EventMachine::send_file_data @signature, filename
505
+ end
506
+
507
+ # Open a file on the filesystem and send it to the remote peer. This returns an
508
+ # object of type EventMachine::Deferrable. The object's callbacks will be executed
509
+ # on the reactor main thread when the file has been completely scheduled for
510
+ # transmission to the remote peer. Its errbacks will be called in case of an error
511
+ # (such as file-not-found). #stream_file_data employs various strategems to achieve
512
+ # the fastest possible performance, balanced against minimum consumption of memory.
513
+ #
514
+ # You can control the behavior of #stream_file_data with the optional arguments parameter.
515
+ # Currently-supported arguments are:
516
+ # :http_chunks, a boolean flag which defaults false. If true, this flag streams the
517
+ # file data in a format compatible with the HTTP chunked-transfer encoding.
518
+ #
519
+ # Warning: this feature has an implicit dependency on an outboard extension,
520
+ # evma_fastfilereader. You must install this extension in order to use #stream_file_data
521
+ # with files larger than a certain size (currently 8192 bytes).
522
+ #
523
+ def stream_file_data filename, args={}
524
+ EventMachine::FileStreamer.new( self, filename, args )
525
+ end
526
+
527
+ # Enable notify_readable callbacks on this connection. Only possible if the connection was created
528
+ # using EM.attach and had notify_readable/notify_writable defined on the handler.
529
+ def notify_readable= mode
530
+ EventMachine::set_notify_readable @signature, mode
531
+ end
532
+
533
+ # Returns true if the connection is being watched for readability.
534
+ def notify_readable?
535
+ EventMachine::is_notify_readable @signature
536
+ end
537
+
538
+ # Enable notify_writable callbacks on this connection. Only possible if the connection was created
539
+ # using EM.attach and had notify_readable/notify_writable defined on the handler.
540
+ def notify_writable= mode
541
+ EventMachine::set_notify_writable @signature, mode
542
+ end
543
+
544
+ # Returns true if the connection is being watched for writability.
545
+ def notify_writable?
546
+ EventMachine::is_notify_writable @signature
547
+ end
548
+
549
+ # Pause a connection so that #send_data and #receive_data events are not fired until #resume is called.
550
+ def pause
551
+ EventMachine::pause_connection @signature
552
+ end
553
+
554
+ # Resume a connection's #send_data and #receive_data events.
555
+ def resume
556
+ EventMachine::resume_connection @signature
557
+ end
558
+
559
+ # True if the connect was paused using #pause.
560
+ def paused?
561
+ EventMachine::connection_paused? @signature
562
+ end
563
+ end
564
+ end