sensu-em 2.4.0-x86-mingw32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (178) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +21 -0
  3. data/.travis.yml +12 -0
  4. data/.yardopts +7 -0
  5. data/CHANGELOG.md +33 -0
  6. data/GNU +281 -0
  7. data/Gemfile +2 -0
  8. data/LICENSE +60 -0
  9. data/README.md +109 -0
  10. data/Rakefile +20 -0
  11. data/docs/DocumentationGuidesIndex.md +27 -0
  12. data/docs/GettingStarted.md +521 -0
  13. data/docs/old/ChangeLog +211 -0
  14. data/docs/old/DEFERRABLES +246 -0
  15. data/docs/old/EPOLL +141 -0
  16. data/docs/old/INSTALL +13 -0
  17. data/docs/old/KEYBOARD +42 -0
  18. data/docs/old/LEGAL +25 -0
  19. data/docs/old/LIGHTWEIGHT_CONCURRENCY +130 -0
  20. data/docs/old/PURE_RUBY +75 -0
  21. data/docs/old/RELEASE_NOTES +94 -0
  22. data/docs/old/SMTP +4 -0
  23. data/docs/old/SPAWNED_PROCESSES +148 -0
  24. data/docs/old/TODO +8 -0
  25. data/eventmachine.gemspec +37 -0
  26. data/examples/guides/getting_started/01_eventmachine_echo_server.rb +18 -0
  27. data/examples/guides/getting_started/02_eventmachine_echo_server_that_recognizes_exit_command.rb +22 -0
  28. data/examples/guides/getting_started/03_simple_chat_server.rb +149 -0
  29. data/examples/guides/getting_started/04_simple_chat_server_step_one.rb +27 -0
  30. data/examples/guides/getting_started/05_simple_chat_server_step_two.rb +43 -0
  31. data/examples/guides/getting_started/06_simple_chat_server_step_three.rb +98 -0
  32. data/examples/guides/getting_started/07_simple_chat_server_step_four.rb +121 -0
  33. data/examples/guides/getting_started/08_simple_chat_server_step_five.rb +141 -0
  34. data/examples/old/ex_channel.rb +43 -0
  35. data/examples/old/ex_queue.rb +2 -0
  36. data/examples/old/ex_tick_loop_array.rb +15 -0
  37. data/examples/old/ex_tick_loop_counter.rb +32 -0
  38. data/examples/old/helper.rb +2 -0
  39. data/ext/binder.cpp +124 -0
  40. data/ext/binder.h +46 -0
  41. data/ext/cmain.cpp +887 -0
  42. data/ext/ed.cpp +1992 -0
  43. data/ext/ed.h +424 -0
  44. data/ext/em.cpp +2352 -0
  45. data/ext/em.h +253 -0
  46. data/ext/eventmachine.h +128 -0
  47. data/ext/extconf.rb +179 -0
  48. data/ext/fastfilereader/extconf.rb +103 -0
  49. data/ext/fastfilereader/mapper.cpp +214 -0
  50. data/ext/fastfilereader/mapper.h +59 -0
  51. data/ext/fastfilereader/rubymain.cpp +127 -0
  52. data/ext/kb.cpp +79 -0
  53. data/ext/page.cpp +107 -0
  54. data/ext/page.h +51 -0
  55. data/ext/pipe.cpp +347 -0
  56. data/ext/project.h +161 -0
  57. data/ext/rubymain.cpp +1318 -0
  58. data/ext/ssl.cpp +476 -0
  59. data/ext/ssl.h +95 -0
  60. data/java/.classpath +6 -0
  61. data/java/.gitignore +1 -0
  62. data/java/.project +17 -0
  63. data/java/src/com/rubyeventmachine/DatagramPacket.java +13 -0
  64. data/java/src/com/rubyeventmachine/EmReactor.java +531 -0
  65. data/java/src/com/rubyeventmachine/EmReactorException.java +40 -0
  66. data/java/src/com/rubyeventmachine/EventCallback.java +7 -0
  67. data/java/src/com/rubyeventmachine/EventCode.java +26 -0
  68. data/java/src/com/rubyeventmachine/EventableChannel.java +130 -0
  69. data/java/src/com/rubyeventmachine/EventableDatagramChannel.java +179 -0
  70. data/java/src/com/rubyeventmachine/EventableSocketChannel.java +405 -0
  71. data/java/src/com/rubyeventmachine/SslBox.java +311 -0
  72. data/lib/em/buftok.rb +110 -0
  73. data/lib/em/callback.rb +58 -0
  74. data/lib/em/channel.rb +64 -0
  75. data/lib/em/completion.rb +304 -0
  76. data/lib/em/connection.rb +716 -0
  77. data/lib/em/deferrable.rb +210 -0
  78. data/lib/em/deferrable/pool.rb +2 -0
  79. data/lib/em/file_watch.rb +73 -0
  80. data/lib/em/future.rb +61 -0
  81. data/lib/em/iterator.rb +231 -0
  82. data/lib/em/messages.rb +66 -0
  83. data/lib/em/pool.rb +151 -0
  84. data/lib/em/process_watch.rb +45 -0
  85. data/lib/em/processes.rb +123 -0
  86. data/lib/em/protocols.rb +37 -0
  87. data/lib/em/protocols/header_and_content.rb +138 -0
  88. data/lib/em/protocols/httpclient.rb +279 -0
  89. data/lib/em/protocols/httpclient2.rb +600 -0
  90. data/lib/em/protocols/line_and_text.rb +125 -0
  91. data/lib/em/protocols/line_protocol.rb +29 -0
  92. data/lib/em/protocols/linetext2.rb +161 -0
  93. data/lib/em/protocols/memcache.rb +331 -0
  94. data/lib/em/protocols/object_protocol.rb +46 -0
  95. data/lib/em/protocols/postgres3.rb +246 -0
  96. data/lib/em/protocols/saslauth.rb +175 -0
  97. data/lib/em/protocols/smtpclient.rb +365 -0
  98. data/lib/em/protocols/smtpserver.rb +643 -0
  99. data/lib/em/protocols/socks4.rb +66 -0
  100. data/lib/em/protocols/stomp.rb +205 -0
  101. data/lib/em/protocols/tcptest.rb +54 -0
  102. data/lib/em/pure_ruby.rb +1017 -0
  103. data/lib/em/queue.rb +71 -0
  104. data/lib/em/resolver.rb +209 -0
  105. data/lib/em/spawnable.rb +84 -0
  106. data/lib/em/streamer.rb +118 -0
  107. data/lib/em/threaded_resource.rb +90 -0
  108. data/lib/em/tick_loop.rb +85 -0
  109. data/lib/em/timers.rb +61 -0
  110. data/lib/em/version.rb +3 -0
  111. data/lib/eventmachine.rb +1553 -0
  112. data/lib/fastfilereaderext.rb +2 -0
  113. data/lib/jeventmachine.rb +321 -0
  114. data/lib/rubyeventmachine.rb +2 -0
  115. data/rakelib/cpp.rake_example +77 -0
  116. data/rakelib/package.rake +98 -0
  117. data/rakelib/test.rake +8 -0
  118. data/tests/client.crt +31 -0
  119. data/tests/client.key +51 -0
  120. data/tests/em_test_helper.rb +64 -0
  121. data/tests/server.crt +36 -0
  122. data/tests/server.key +51 -0
  123. data/tests/test_attach.rb +150 -0
  124. data/tests/test_basic.rb +294 -0
  125. data/tests/test_channel.rb +62 -0
  126. data/tests/test_completion.rb +177 -0
  127. data/tests/test_connection_count.rb +53 -0
  128. data/tests/test_defer.rb +18 -0
  129. data/tests/test_deferrable.rb +35 -0
  130. data/tests/test_epoll.rb +145 -0
  131. data/tests/test_error_handler.rb +38 -0
  132. data/tests/test_exc.rb +28 -0
  133. data/tests/test_file_watch.rb +65 -0
  134. data/tests/test_futures.rb +170 -0
  135. data/tests/test_get_sock_opt.rb +37 -0
  136. data/tests/test_handler_check.rb +35 -0
  137. data/tests/test_hc.rb +155 -0
  138. data/tests/test_httpclient.rb +190 -0
  139. data/tests/test_httpclient2.rb +133 -0
  140. data/tests/test_idle_connection.rb +25 -0
  141. data/tests/test_inactivity_timeout.rb +54 -0
  142. data/tests/test_iterator.rb +97 -0
  143. data/tests/test_kb.rb +34 -0
  144. data/tests/test_line_protocol.rb +33 -0
  145. data/tests/test_ltp.rb +138 -0
  146. data/tests/test_ltp2.rb +288 -0
  147. data/tests/test_next_tick.rb +104 -0
  148. data/tests/test_object_protocol.rb +36 -0
  149. data/tests/test_pause.rb +102 -0
  150. data/tests/test_pending_connect_timeout.rb +52 -0
  151. data/tests/test_pool.rb +194 -0
  152. data/tests/test_process_watch.rb +48 -0
  153. data/tests/test_processes.rb +128 -0
  154. data/tests/test_proxy_connection.rb +180 -0
  155. data/tests/test_pure.rb +88 -0
  156. data/tests/test_queue.rb +50 -0
  157. data/tests/test_resolver.rb +55 -0
  158. data/tests/test_running.rb +14 -0
  159. data/tests/test_sasl.rb +47 -0
  160. data/tests/test_send_file.rb +217 -0
  161. data/tests/test_servers.rb +33 -0
  162. data/tests/test_set_sock_opt.rb +37 -0
  163. data/tests/test_shutdown_hooks.rb +23 -0
  164. data/tests/test_smtpclient.rb +55 -0
  165. data/tests/test_smtpserver.rb +57 -0
  166. data/tests/test_spawn.rb +293 -0
  167. data/tests/test_ssl_args.rb +78 -0
  168. data/tests/test_ssl_echo_data.rb +60 -0
  169. data/tests/test_ssl_methods.rb +56 -0
  170. data/tests/test_ssl_verify.rb +82 -0
  171. data/tests/test_stomp.rb +37 -0
  172. data/tests/test_system.rb +42 -0
  173. data/tests/test_threaded_resource.rb +53 -0
  174. data/tests/test_tick_loop.rb +59 -0
  175. data/tests/test_timers.rb +123 -0
  176. data/tests/test_ud.rb +8 -0
  177. data/tests/test_unbind_reason.rb +48 -0
  178. metadata +300 -0
data/lib/em/channel.rb ADDED
@@ -0,0 +1,64 @@
1
+ module EventMachine
2
+ # Provides a simple thread-safe way to transfer data between (typically) long running
3
+ # tasks in {EventMachine.defer} and event loop thread.
4
+ #
5
+ # @example
6
+ #
7
+ # channel = EventMachine::Channel.new
8
+ # sid = channel.subscribe { |msg| p [:got, msg] }
9
+ #
10
+ # channel.push('hello world')
11
+ # channel.unsubscribe(sid)
12
+ #
13
+ #
14
+ class Channel
15
+ def initialize
16
+ @subs = {}
17
+ @uid = 0
18
+ end
19
+
20
+ # Takes any arguments suitable for EM::Callback() and returns a subscriber
21
+ # id for use when unsubscribing.
22
+ #
23
+ # @return [Integer] Subscribe identifier
24
+ # @see #unsubscribe
25
+ def subscribe(*a, &b)
26
+ name = gen_id
27
+ EM.schedule { @subs[name] = EM::Callback(*a, &b) }
28
+
29
+ name
30
+ end
31
+
32
+ # Removes subscriber from the list.
33
+ #
34
+ # @param [Integer] Subscriber identifier
35
+ # @see #subscribe
36
+ def unsubscribe(name)
37
+ EM.schedule { @subs.delete name }
38
+ end
39
+
40
+ # Add items to the channel, which are pushed out to all subscribers.
41
+ def push(*items)
42
+ items = items.dup
43
+ EM.schedule { items.each { |i| @subs.values.each { |s| s.call i } } }
44
+ end
45
+ alias << push
46
+
47
+ # Fetches one message from the channel.
48
+ def pop(*a, &b)
49
+ EM.schedule {
50
+ name = subscribe do |*args|
51
+ unsubscribe(name)
52
+ EM::Callback(*a, &b).call(*args)
53
+ end
54
+ }
55
+ end
56
+
57
+ private
58
+
59
+ # @private
60
+ def gen_id
61
+ @uid += 1
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,304 @@
1
+ # = EM::Completion
2
+ #
3
+ # A completion is a callback container for various states of completion. In
4
+ # it's most basic form it has a start state and a finish state.
5
+ #
6
+ # This implementation includes some hold-back from the EM::Deferrable
7
+ # interface in order to be compatible - but it has a much cleaner
8
+ # implementation.
9
+ #
10
+ # In general it is preferred that this implementation be used as a state
11
+ # callback container than EM::DefaultDeferrable or other classes including
12
+ # EM::Deferrable. This is because it is generally more sane to keep this level
13
+ # of state in a dedicated state-back container. This generally leads to more
14
+ # malleable interfaces and software designs, as well as eradicating nasty bugs
15
+ # that result from abstraction leakage.
16
+ #
17
+ # == Basic Usage
18
+ #
19
+ # As already mentioned, the basic usage of a Completion is simply for its two
20
+ # final states, :succeeded and :failed.
21
+ #
22
+ # An asynchronous operation will complete at some future point in time, and
23
+ # users often want to react to this event. API authors will want to expose
24
+ # some common interface to react to these events.
25
+ #
26
+ # In the following example, the user wants to know when a short lived
27
+ # connection has completed its exchange with the remote server. The simple
28
+ # protocol just waits for an ack to its message.
29
+ #
30
+ # class Protocol < EM::Connection
31
+ # include EM::P::LineText2
32
+ #
33
+ # def initialize(message, completion)
34
+ # @message, @completion = message, completion
35
+ # @completion.completion { close_connection }
36
+ # @completion.timeout(1, :timeout)
37
+ # end
38
+ #
39
+ # def post_init
40
+ # send_data(@message)
41
+ # end
42
+ #
43
+ # def receive_line(line)
44
+ # case line
45
+ # when /ACK/i
46
+ # @completion.succeed line
47
+ # when /ERR/i
48
+ # @completion.fail :error, line
49
+ # else
50
+ # @completion.fail :unknown, line
51
+ # end
52
+ # end
53
+ #
54
+ # def unbind
55
+ # @completion.fail :disconnected unless @completion.completed?
56
+ # end
57
+ # end
58
+ #
59
+ # class API
60
+ # attr_reader :host, :port
61
+ #
62
+ # def initialize(host = 'example.org', port = 8000)
63
+ # @host, @port = host, port
64
+ # end
65
+ #
66
+ # def request(message)
67
+ # completion = EM::Deferrable::Completion.new
68
+ # EM.connect(host, port, Protocol, message, completion)
69
+ # completion
70
+ # end
71
+ # end
72
+ #
73
+ # api = API.new
74
+ # completion = api.request('stuff')
75
+ # completion.callback do |line|
76
+ # puts "API responded with: #{line}"
77
+ # end
78
+ # completion.errback do |type, line|
79
+ # case type
80
+ # when :error
81
+ # puts "API error: #{line}"
82
+ # when :unknown
83
+ # puts "API returned unknown response: #{line}"
84
+ # when :disconnected
85
+ # puts "API server disconnected prematurely"
86
+ # when :timeout
87
+ # puts "API server did not respond in a timely fashion"
88
+ # end
89
+ # end
90
+ #
91
+ # == Advanced Usage
92
+ #
93
+ # This completion implementation also supports more state callbacks and
94
+ # arbitrary states (unlike the original Deferrable API). This allows for basic
95
+ # stateful process encapsulation. One might use this to setup state callbacks
96
+ # for various states in an exchange like in the basic usage example, except
97
+ # where the applicaiton could be made to react to "connected" and
98
+ # "disconnected" states additionally.
99
+ #
100
+ # class Protocol < EM::Connection
101
+ # def initialize(completion)
102
+ # @response = []
103
+ # @completion = completion
104
+ # @completion.stateback(:disconnected) do
105
+ # @completion.succeed @response.join
106
+ # end
107
+ # end
108
+ #
109
+ # def connection_completed
110
+ # @host, @port = Socket.unpack_sockaddr_in get_peername
111
+ # @completion.change_state(:connected, @host, @port)
112
+ # send_data("GET http://example.org/ HTTP/1.0\r\n\r\n")
113
+ # end
114
+ #
115
+ # def receive_data(data)
116
+ # @response << data
117
+ # end
118
+ #
119
+ # def unbind
120
+ # @completion.change_state(:disconnected, @host, @port)
121
+ # end
122
+ # end
123
+ #
124
+ # completion = EM::Deferrable::Completion.new
125
+ # completion.stateback(:connected) do |host, port|
126
+ # puts "Connected to #{host}:#{port}"
127
+ # end
128
+ # completion.stateback(:disconnected) do |host, port|
129
+ # puts "Disconnected from #{host}:#{port}"
130
+ # end
131
+ # completion.callback do |response|
132
+ # puts response
133
+ # end
134
+ #
135
+ # EM.connect('example.org', 80, Protocol, completion)
136
+ #
137
+ # == Timeout
138
+ #
139
+ # The Completion also has a timeout. The timeout is global and is not aware of
140
+ # states apart from completion states. The timeout is only engaged if #timeout
141
+ # is called, and it will call fail if it is reached.
142
+ #
143
+ # == Completion states
144
+ #
145
+ # By default there are two completion states, :succeeded and :failed. These
146
+ # states can be modified by subclassing and overrding the #completion_states
147
+ # method. Completion states are special, in that callbacks for all completion
148
+ # states are explcitly cleared when a completion state is entered. This
149
+ # prevents errors that could arise from accidental unterminated timeouts, and
150
+ # other such user errors.
151
+ #
152
+ # == Other notes
153
+ #
154
+ # Several APIs have been carried over from EM::Deferrable for compatibility
155
+ # reasons during a transitionary period. Specifically cancel_errback and
156
+ # cancel_callback are implemented, but their usage is to be strongly
157
+ # discouraged. Due to the already complex nature of reaction systems, dynamic
158
+ # callback deletion only makes the problem much worse. It is always better to
159
+ # add correct conditionals to the callback code, or use more states, than to
160
+ # address such implementaiton issues with conditional callbacks.
161
+
162
+ module EventMachine
163
+
164
+ class Completion
165
+ # This is totally not used (re-implemented), it's here in case people check
166
+ # for kind_of?
167
+ include EventMachine::Deferrable
168
+
169
+ attr_reader :state, :value
170
+
171
+ def initialize
172
+ @state = :unknown
173
+ @callbacks = Hash.new { |h,k| h[k] = [] }
174
+ @value = []
175
+ @timeout_timer = nil
176
+ end
177
+
178
+ # Enter the :succeeded state, setting the result value if given.
179
+ def succeed(*args)
180
+ change_state(:succeeded, *args)
181
+ end
182
+ # The old EM method:
183
+ alias set_deferred_success succeed
184
+
185
+ # Enter the :failed state, setting the result value if given.
186
+ def fail(*args)
187
+ change_state(:failed, *args)
188
+ end
189
+ # The old EM method:
190
+ alias set_deferred_failure fail
191
+
192
+ # Statebacks are called when you enter (or are in) the named state.
193
+ def stateback(state, *a, &b)
194
+ # The following is quite unfortunate special casing for :completed
195
+ # statebacks, but it's a necessary evil for latent completion
196
+ # definitions.
197
+
198
+ if :completed == state || !completed? || @state == state
199
+ @callbacks[state] << EM::Callback(*a, &b)
200
+ end
201
+ execute_callbacks
202
+ self
203
+ end
204
+
205
+ # Callbacks are called when you enter (or are in) a :succeeded state.
206
+ def callback(*a, &b)
207
+ stateback(:succeeded, *a, &b)
208
+ end
209
+
210
+ # Errbacks are called when you enter (or are in) a :failed state.
211
+ def errback(*a, &b)
212
+ stateback(:failed, *a, &b)
213
+ end
214
+
215
+ # Completions are called when you enter (or are in) either a :failed or a
216
+ # :succeeded state. They are stored as a special (reserved) state called
217
+ # :completed.
218
+ def completion(*a, &b)
219
+ stateback(:completed, *a, &b)
220
+ end
221
+
222
+ # Enter a new state, setting the result value if given. If the state is one
223
+ # of :succeeded or :failed, then :completed callbacks will also be called.
224
+ def change_state(state, *args)
225
+ @value = args
226
+ @state = state
227
+
228
+ EM.schedule { execute_callbacks }
229
+ end
230
+
231
+ # The old EM method:
232
+ alias set_deferred_status change_state
233
+
234
+ # Indicates that we've reached some kind of completion state, by default
235
+ # this is :succeeded or :failed. Due to these semantics, the :completed
236
+ # state is reserved for internal use.
237
+ def completed?
238
+ completion_states.any? { |s| state == s }
239
+ end
240
+
241
+ # Completion states simply returns a list of completion states, by default
242
+ # this is :succeeded and :failed.
243
+ def completion_states
244
+ [:succeeded, :failed]
245
+ end
246
+
247
+ # Schedule a time which if passes before we enter a completion state, this
248
+ # deferrable will be failed with the given arguments.
249
+ def timeout(time, *args)
250
+ cancel_timeout
251
+ @timeout_timer = EM::Timer.new(time) do
252
+ fail(*args) unless completed?
253
+ end
254
+ end
255
+
256
+ # Disable the timeout
257
+ def cancel_timeout
258
+ if @timeout_timer
259
+ @timeout_timer.cancel
260
+ @timeout_timer = nil
261
+ end
262
+ end
263
+
264
+ # Remove an errback. N.B. Some errbacks cannot be deleted. Usage is NOT
265
+ # recommended, this is an anti-pattern.
266
+ def cancel_errback(*a, &b)
267
+ @callbacks[:failed].delete(EM::Callback(*a, &b))
268
+ end
269
+
270
+ # Remove a callback. N.B. Some callbacks cannot be deleted. Usage is NOT
271
+ # recommended, this is an anti-pattern.
272
+ def cancel_callback(*a, &b)
273
+ @callbacks[:succeeded].delete(EM::Callback(*a, &b))
274
+ end
275
+
276
+ private
277
+ # Execute all callbacks for the current state. If in a completed state, then
278
+ # call any statebacks associated with the completed state.
279
+ def execute_callbacks
280
+ execute_state_callbacks(state)
281
+ if completed?
282
+ execute_state_callbacks(:completed)
283
+ clear_dead_callbacks
284
+ cancel_timeout
285
+ end
286
+ end
287
+
288
+ # Iterate all callbacks for a given state, and remove then call them.
289
+ def execute_state_callbacks(state)
290
+ while callback = @callbacks[state].shift
291
+ callback.call(*value)
292
+ end
293
+ end
294
+
295
+ # If we enter a completion state, clear other completion states after all
296
+ # callback chains are completed. This means that operation specific
297
+ # callbacks can't be dual-called, which is most common user error.
298
+ def clear_dead_callbacks
299
+ completion_states.each do |state|
300
+ @callbacks[state].clear
301
+ end
302
+ end
303
+ end
304
+ end
@@ -0,0 +1,716 @@
1
+ module EventMachine
2
+ class FileNotFoundException < Exception
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:
26
+ #
27
+ # * {#post_init}
28
+ # * {#connection_completed}
29
+ # * {#receive_data}
30
+ # * {#unbind}
31
+ # * {#ssl_verify_peer} (if TLS is used)
32
+ # * {#ssl_handshake_completed}
33
+ #
34
+ # All of the other instance methods defined here are called only by user code.
35
+ #
36
+ # @see file:docs/GettingStarted.md EventMachine tutorial
37
+ class Connection
38
+ # @private
39
+ attr_accessor :signature
40
+
41
+ # @private
42
+ alias original_method method
43
+
44
+ # Override .new so subclasses don't have to call super and can ignore
45
+ # connection-specific arguments
46
+ #
47
+ # @private
48
+ def self.new(sig, *args)
49
+ allocate.instance_eval do
50
+ # Store signature
51
+ @signature = sig
52
+ # associate_callback_target sig
53
+
54
+ # Call a superclass's #initialize if it has one
55
+ initialize(*args)
56
+
57
+ # post initialize callback
58
+ post_init
59
+
60
+ self
61
+ end
62
+ end
63
+
64
+ # Stubbed initialize so legacy superclasses can safely call super
65
+ #
66
+ # @private
67
+ def initialize(*args)
68
+ end
69
+
70
+ # Called by the event loop immediately after the network connection has been established,
71
+ # and before resumption of the network loop.
72
+ # This method is generally not called by user code, but is called automatically
73
+ # by the event loop. The base-class implementation is a no-op.
74
+ # This is a very good place to initialize instance variables that will
75
+ # be used throughout the lifetime of the network connection.
76
+ #
77
+ # @see #connection_completed
78
+ # @see #unbind
79
+ # @see #send_data
80
+ # @see #receive_data
81
+ def post_init
82
+ end
83
+
84
+ # Called by the event loop whenever data has been received by the network connection.
85
+ # It is never called by user code. {#receive_data} is called with a single parameter, a String containing
86
+ # the network protocol data, which may of course be binary. You will
87
+ # generally redefine this method to perform your own processing of the incoming data.
88
+ #
89
+ # Here's a key point which is essential to understanding the event-driven
90
+ # programming model: <i>EventMachine knows absolutely nothing about the protocol
91
+ # which your code implements.</i> You must not make any assumptions about
92
+ # the size of the incoming data packets, or about their alignment on any
93
+ # particular intra-message or PDU boundaries (such as line breaks).
94
+ # receive_data can and will send you arbitrary chunks of data, with the
95
+ # only guarantee being that the data is presented to your code in the order
96
+ # it was collected from the network. Don't even assume that the chunks of
97
+ # data will correspond to network packets, as EventMachine can and will coalesce
98
+ # several incoming packets into one, to improve performance. The implication for your
99
+ # code is that you generally will need to implement some kind of a state machine
100
+ # in your redefined implementation of receive_data. For a better understanding
101
+ # of this, read through the examples of specific protocol handlers in EventMachine::Protocols
102
+ #
103
+ # The base-class implementation (which will be invoked only if you didn't override it in your protocol handler)
104
+ # simply prints incoming data packet size to stdout.
105
+ #
106
+ # @param [String] data Opaque incoming data.
107
+ # @note Depending on the protocol, buffer sizes and OS networking stack configuration, incoming data may or may not be "a complete message".
108
+ # It is up to this handler to detect content boundaries to determine whether all the content (for example, full HTTP request)
109
+ # has been received and can be processed.
110
+ #
111
+ # @see #post_init
112
+ # @see #connection_completed
113
+ # @see #unbind
114
+ # @see #send_data
115
+ # @see file:docs/GettingStarted.md EventMachine tutorial
116
+ def receive_data data
117
+ puts "............>>>#{data.length}"
118
+ end
119
+
120
+ # Called by EventMachine when the SSL/TLS handshake has
121
+ # been completed, as a result of calling #start_tls to initiate SSL/TLS on the connection.
122
+ #
123
+ # This callback exists because {#post_init} and {#connection_completed} are **not** reliable
124
+ # for indicating when an SSL/TLS connection is ready to have its certificate queried for.
125
+ #
126
+ # @see #get_peer_cert
127
+ def ssl_handshake_completed
128
+ end
129
+
130
+ # Called by EventMachine when :verify_peer => true has been passed to {#start_tls}.
131
+ # It will be called with each certificate in the certificate chain provided by the remote peer.
132
+ #
133
+ # The cert will be passed as a String in PEM format, the same as in {#get_peer_cert}. It is up to user defined
134
+ # code to perform a check on the certificates. The return value from this callback is used to accept or deny the peer.
135
+ # A return value that is not nil or false triggers acceptance. If the peer is not accepted, the connection
136
+ # will be subsequently closed.
137
+ #
138
+ # @example This server always accepts all peers
139
+ #
140
+ # module AcceptServer
141
+ # def post_init
142
+ # start_tls(:verify_peer => true)
143
+ # end
144
+ #
145
+ # def ssl_verify_peer(cert)
146
+ # true
147
+ # end
148
+ #
149
+ # def ssl_handshake_completed
150
+ # $server_handshake_completed = true
151
+ # end
152
+ # end
153
+ #
154
+ #
155
+ # @example This server never accepts any peers
156
+ #
157
+ # module DenyServer
158
+ # def post_init
159
+ # start_tls(:verify_peer => true)
160
+ # end
161
+ #
162
+ # def ssl_verify_peer(cert)
163
+ # # Do not accept the peer. This should now cause the connection to shut down
164
+ # # without the SSL handshake being completed.
165
+ # false
166
+ # end
167
+ #
168
+ # def ssl_handshake_completed
169
+ # $server_handshake_completed = true
170
+ # end
171
+ # end
172
+ #
173
+ # @see #start_tls
174
+ def ssl_verify_peer(cert)
175
+ end
176
+
177
+ # called by the framework whenever a connection (either a server or client connection) is closed.
178
+ # The close can occur because your code intentionally closes it (using {#close_connection} and {#close_connection_after_writing}),
179
+ # because the remote peer closed the connection, or because of a network error.
180
+ # You may not assume that the network connection is still open and able to send or
181
+ # receive data when the callback to unbind is made. This is intended only to give
182
+ # you a chance to clean up associations your code may have made to the connection
183
+ # object while it was open.
184
+ #
185
+ # If you want to detect which peer has closed the connection, you can override {#close_connection} in your protocol handler
186
+ # and set an @ivar.
187
+ #
188
+ # @example Overriding Connection#close_connection to distinguish connections closed on our side
189
+ #
190
+ # class MyProtocolHandler < EventMachine::Connection
191
+ #
192
+ # # ...
193
+ #
194
+ # def close_connection(*args)
195
+ # @intentionally_closed_connection = true
196
+ # super(*args)
197
+ # end
198
+ #
199
+ # def unbind
200
+ # if @intentionally_closed_connection
201
+ # # ...
202
+ # end
203
+ # end
204
+ #
205
+ # # ...
206
+ #
207
+ # end
208
+ #
209
+ # @see #post_init
210
+ # @see #connection_completed
211
+ # @see file:docs/GettingStarted.md EventMachine tutorial
212
+ def unbind
213
+ end
214
+
215
+ # Called by the reactor after attempting to relay incoming data to a descriptor (set as a proxy target descriptor with
216
+ # {EventMachine.enable_proxy}) that has already been closed.
217
+ #
218
+ # @see EventMachine.enable_proxy
219
+ def proxy_target_unbound
220
+ end
221
+
222
+ # called when the reactor finished proxying all
223
+ # of the requested bytes.
224
+ def proxy_completed
225
+ end
226
+
227
+ # EventMachine::Connection#proxy_incoming_to is called only by user code. It sets up
228
+ # a low-level proxy relay for all data inbound for this connection, to the connection given
229
+ # as the argument. This is essentially just a helper method for enable_proxy.
230
+ #
231
+ # @see EventMachine.enable_proxy
232
+ def proxy_incoming_to(conn,bufsize=0)
233
+ EventMachine::enable_proxy(self, conn, bufsize)
234
+ end
235
+
236
+ # A helper method for {EventMachine.disable_proxy}
237
+ def stop_proxying
238
+ EventMachine::disable_proxy(self)
239
+ end
240
+
241
+ # The number of bytes proxied to another connection. Reset to zero when
242
+ # EventMachine::Connection#proxy_incoming_to is called, and incremented whenever data is proxied.
243
+ def get_proxied_bytes
244
+ EventMachine::get_proxied_bytes(@signature)
245
+ end
246
+
247
+ # EventMachine::Connection#close_connection is called only by user code, and never
248
+ # by the event loop. You may call this method against a connection object in any
249
+ # callback handler, whether or not the callback was made against the connection
250
+ # you want to close. close_connection <i>schedules</i> the connection to be closed
251
+ # at the next available opportunity within the event loop. You may not assume that
252
+ # the connection is closed when close_connection returns. In particular, the framework
253
+ # will callback the unbind method for the particular connection at a point shortly
254
+ # after you call close_connection. You may assume that the unbind callback will
255
+ # take place sometime after your call to close_connection completes. In other words,
256
+ # the unbind callback will not re-enter your code "inside" of your call to close_connection.
257
+ # However, it's not guaranteed that a future version of EventMachine will not change
258
+ # this behavior.
259
+ #
260
+ # {#close_connection} will *silently discard* any outbound data which you have
261
+ # sent to the connection using {EventMachine::Connection#send_data} but which has not
262
+ # yet been sent across the network. If you want to avoid this behavior, use
263
+ # {EventMachine::Connection#close_connection_after_writing}.
264
+ #
265
+ def close_connection after_writing = false
266
+ EventMachine::close_connection @signature, after_writing
267
+ end
268
+
269
+ # Removes given connection from the event loop.
270
+ # The connection's socket remains open and its file descriptor number is returned.
271
+ def detach
272
+ EventMachine::detach_fd @signature
273
+ end
274
+
275
+ def get_sock_opt level, option
276
+ EventMachine::get_sock_opt @signature, level, option
277
+ end
278
+
279
+ def set_sock_opt level, optname, optval
280
+ EventMachine::set_sock_opt @signature, level, optname, optval
281
+ end
282
+
283
+ # A variant of {#close_connection}.
284
+ # All of the descriptive comments given for close_connection also apply to
285
+ # close_connection_after_writing, *with one exception*: if the connection has
286
+ # outbound data sent using send_dat but which has not yet been sent across the network,
287
+ # close_connection_after_writing will schedule the connection to be closed *after*
288
+ # all of the outbound data has been safely written to the remote peer.
289
+ #
290
+ # Depending on the amount of outgoing data and the speed of the network,
291
+ # considerable time may elapse between your call to close_connection_after_writing
292
+ # and the actual closing of the socket (at which time the unbind callback will be called
293
+ # by the event loop). During this time, you *may not* call send_data to transmit
294
+ # additional data (that is, the connection is closed for further writes). In very
295
+ # rare cases, you may experience a receive_data callback after your call to {#close_connection_after_writing},
296
+ # depending on whether incoming data was in the process of being received on the connection
297
+ # at the moment when you called {#close_connection_after_writing}. Your protocol handler must
298
+ # be prepared to properly deal with such data (probably by ignoring it).
299
+ #
300
+ # @see #close_connection
301
+ # @see #send_data
302
+ def close_connection_after_writing
303
+ close_connection true
304
+ end
305
+
306
+ # Call this method to send data to the remote end of the network connection. It takes a single String argument,
307
+ # which may contain binary data. Data is buffered to be sent at the end of this event loop tick (cycle).
308
+ #
309
+ # When used in a method that is event handler (for example, {#post_init} or {#connection_completed}, it will send
310
+ # data to the other end of the connection that generated the event.
311
+ # You can also call {#send_data} to write to other connections. For more information see The Chat Server Example in the
312
+ # {file:docs/GettingStarted.md EventMachine tutorial}.
313
+ #
314
+ # If you want to send some data and then immediately close the connection, make sure to use {#close_connection_after_writing}
315
+ # instead of {#close_connection}.
316
+ #
317
+ #
318
+ # @param [String] data Data to send asynchronously
319
+ #
320
+ # @see file:docs/GettingStarted.md EventMachine tutorial
321
+ # @see Connection#receive_data
322
+ # @see Connection#post_init
323
+ # @see Connection#unbind
324
+ def send_data data
325
+ data = data.to_s
326
+ size = data.bytesize if data.respond_to?(:bytesize)
327
+ size ||= data.size
328
+ EventMachine::send_data @signature, data, size
329
+ end
330
+
331
+ # Returns true if the connection is in an error state, false otherwise.
332
+ #
333
+ # In general, you can detect the occurrence of communication errors or unexpected
334
+ # disconnection by the remote peer by handing the {#unbind} method. In some cases, however,
335
+ # it's useful to check the status of the connection using {#error?} before attempting to send data.
336
+ # This function is synchronous but it will return immediately without blocking.
337
+ #
338
+ # @return [Boolean] true if the connection is in an error state, false otherwise
339
+ def error?
340
+ errno = EventMachine::report_connection_error_status(@signature)
341
+ case errno
342
+ when 0
343
+ false
344
+ when -1
345
+ true
346
+ else
347
+ EventMachine::ERRNOS[errno]
348
+ end
349
+ end
350
+
351
+ # Called by the event loop when a remote TCP connection attempt completes successfully.
352
+ # You can expect to get this notification after calls to {EventMachine.connect}. Remember that EventMachine makes remote connections
353
+ # asynchronously, just as with any other kind of network event. This method
354
+ # is intended primarily to assist with network diagnostics. For normal protocol
355
+ # handling, use #post_init to perform initial work on a new connection (such as sending initial set of data).
356
+ # {Connection#post_init} will always be called. This method will only be called in case of a successful completion.
357
+ # A connection attempt which fails will result a call to {Connection#unbind} after the failure.
358
+ #
359
+ # @see Connection#post_init
360
+ # @see Connection#unbind
361
+ # @see file:docs/GettingStarted.md EventMachine tutorial
362
+ def connection_completed
363
+ end
364
+
365
+ # Call {#start_tls} at any point to initiate TLS encryption on connected streams.
366
+ # The method is smart enough to know whether it should perform a server-side
367
+ # or a client-side handshake. An appropriate place to call {#start_tls} is in
368
+ # your redefined {#post_init} method, or in the {#connection_completed} handler for
369
+ # an outbound connection.
370
+ #
371
+ #
372
+ # @option args [String] :cert_chain_file (nil) local path of a readable file that contants a chain of X509 certificates in
373
+ # the [PEM format](http://en.wikipedia.org/wiki/Privacy_Enhanced_Mail),
374
+ # with the most-resolved certificate at the top of the file, successive intermediate
375
+ # certs in the middle, and the root (or CA) cert at the bottom.
376
+ #
377
+ # @option args [String] :private_key_file (nil) local path of a readable file that must contain a private key in the [PEM format](http://en.wikipedia.org/wiki/Privacy_Enhanced_Mail).
378
+ #
379
+ # @option args [Boolean] :verify_peer (false) indicates whether a server should request a certificate from a peer, to be verified by user code.
380
+ # If true, the {#ssl_verify_peer} callback on the {EventMachine::Connection} object is called with each certificate
381
+ # in the certificate chain provided by the peer. See documentation on {#ssl_verify_peer} for how to use this.
382
+ #
383
+ # @option args [Boolean] :use_tls (false) indicates whether TLS or SSL must be offered to the peer. If true TLS is used, SSL otherwise.
384
+ #
385
+ # @option args [String] :cipher_list ("ALL:!ADH:!LOW:!EXP:!DES-CBC3-SHA:@STRENGTH") indicates the available SSL cipher values.
386
+ #
387
+ # @example Using TLS with EventMachine
388
+ #
389
+ # require 'rubygems'
390
+ # require 'eventmachine'
391
+ #
392
+ # module Handler
393
+ # def post_init
394
+ # start_tls(:private_key_file => '/tmp/server.key', :cert_chain_file => '/tmp/server.crt', :verify_peer => false)
395
+ # end
396
+ # end
397
+ #
398
+ # EventMachine.run do
399
+ # EventMachine.start_server("127.0.0.1", 9999, Handler)
400
+ # end
401
+ #
402
+ # @param [Hash] args
403
+ #
404
+ # @todo support passing an encryption parameter, which can be string or Proc, to get a passphrase
405
+ # for encrypted private keys.
406
+ # @todo support passing key material via raw strings or Procs that return strings instead of
407
+ # just filenames.
408
+ #
409
+ # @see #ssl_verify_peer
410
+ def start_tls args={}
411
+ priv_key, cert_chain, verify_peer, use_tls, cipher_list = args.values_at(:private_key_file, :cert_chain_file, :verify_peer, :use_tls, :cipher_list)
412
+
413
+ [priv_key, cert_chain].each do |file|
414
+ next if file.nil? or file.empty?
415
+ raise FileNotFoundException,
416
+ "Could not find #{file} for start_tls" unless File.exists? file
417
+ end
418
+
419
+ EventMachine::set_tls_parms(@signature, priv_key || '', cert_chain || '', verify_peer, (use_tls ? true : false), cipher_list || '')
420
+ EventMachine::start_tls @signature
421
+ end
422
+
423
+ # If [TLS](http://en.wikipedia.org/wiki/Transport_Layer_Security) is active on the connection, returns the remote [X509 certificate](http://en.wikipedia.org/wiki/X.509)
424
+ # as a string, in the popular [PEM format](http://en.wikipedia.org/wiki/Privacy_Enhanced_Mail). This can then be used for arbitrary validation
425
+ # of a peer's certificate in your code.
426
+ #
427
+ # This should be called in/after the {#ssl_handshake_completed} callback, which indicates
428
+ # that SSL/TLS is active. Using this callback is important, because the certificate may not
429
+ # be available until the time it is executed. Using #post_init or #connection_completed is
430
+ # not adequate, because the SSL handshake may still be taking place.
431
+ #
432
+ # This method will return `nil` if:
433
+ #
434
+ # * EventMachine is not built with [OpenSSL](http://www.openssl.org) support
435
+ # * [TLS](http://en.wikipedia.org/wiki/Transport_Layer_Security) is not active on the connection
436
+ # * TLS handshake is not yet complete
437
+ # * Remote peer for any other reason has not presented a certificate
438
+ #
439
+ #
440
+ # @example Getting peer TLS certificate information in EventMachine
441
+ #
442
+ # module Handler
443
+ # def post_init
444
+ # puts "Starting TLS"
445
+ # start_tls
446
+ # end
447
+ #
448
+ # def ssl_handshake_completed
449
+ # puts get_peer_cert
450
+ # close_connection
451
+ # end
452
+ #
453
+ # def unbind
454
+ # EventMachine::stop_event_loop
455
+ # end
456
+ # end
457
+ #
458
+ # EventMachine.run do
459
+ # EventMachine.connect "mail.google.com", 443, Handler
460
+ # end
461
+ #
462
+ # # Will output:
463
+ # # -----BEGIN CERTIFICATE-----
464
+ # # MIIDIjCCAougAwIBAgIQbldpChBPqv+BdPg4iwgN8TANBgkqhkiG9w0BAQUFADBM
465
+ # # MQswCQYDVQQGEwJaQTElMCMGA1UEChMcVGhhd3RlIENvbnN1bHRpbmcgKFB0eSkg
466
+ # # THRkLjEWMBQGA1UEAxMNVGhhd3RlIFNHQyBDQTAeFw0wODA1MDIxNjMyNTRaFw0w
467
+ # # OTA1MDIxNjMyNTRaMGkxCzAJBgNVBAYTAlVTMRMwEQYDVQQIEwpDYWxpZm9ybmlh
468
+ # # MRYwFAYDVQQHEw1Nb3VudGFpbiBWaWV3MRMwEQYDVQQKEwpHb29nbGUgSW5jMRgw
469
+ # # FgYDVQQDEw9tYWlsLmdvb2dsZS5jb20wgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJ
470
+ # # AoGBALlkxdh2QXegdElukCSOV2+8PKiONIS+8Tu9K7MQsYpqtLNC860zwOPQ2NLI
471
+ # # 3Zp4jwuXVTrtzGuiqf5Jioh35Ig3CqDXtLyZoypjZUQcq4mlLzHlhIQ4EhSjDmA7
472
+ # # Ffw9y3ckSOQgdBQWNLbquHh9AbEUjmhkrYxIqKXeCnRKhv6nAgMBAAGjgecwgeQw
473
+ # # KAYDVR0lBCEwHwYIKwYBBQUHAwEGCCsGAQUFBwMCBglghkgBhvhCBAEwNgYDVR0f
474
+ # # BC8wLTAroCmgJ4YlaHR0cDovL2NybC50aGF3dGUuY29tL1RoYXd0ZVNHQ0NBLmNy
475
+ # # bDByBggrBgEFBQcBAQRmMGQwIgYIKwYBBQUHMAGGFmh0dHA6Ly9vY3NwLnRoYXd0
476
+ # # ZS5jb20wPgYIKwYBBQUHMAKGMmh0dHA6Ly93d3cudGhhd3RlLmNvbS9yZXBvc2l0
477
+ # # b3J5L1RoYXd0ZV9TR0NfQ0EuY3J0MAwGA1UdEwEB/wQCMAAwDQYJKoZIhvcNAQEF
478
+ # # BQADgYEAsRwpLg1dgCR1gYDK185MFGukXMeQFUvhGqF8eT/CjpdvezyKVuz84gSu
479
+ # # 6ccMXgcPQZGQN/F4Xug+Q01eccJjRSVfdvR5qwpqCj+6BFl5oiKDBsveSkrmL5dz
480
+ # # s2bn7TdTSYKcLeBkjXxDLHGBqLJ6TNCJ3c4/cbbG5JhGvoema94=
481
+ # # -----END CERTIFICATE-----
482
+ #
483
+ # You can do whatever you want with the certificate String, such as load it
484
+ # as a certificate object using the OpenSSL library, and check its fields.
485
+ #
486
+ # @return [String] the remote [X509 certificate](http://en.wikipedia.org/wiki/X.509), in the popular [PEM format](http://en.wikipedia.org/wiki/Privacy_Enhanced_Mail),
487
+ # if TLS is active on the connection
488
+ #
489
+ # @see Connection#start_tls
490
+ # @see Connection#ssl_handshake_completed
491
+ def get_peer_cert
492
+ EventMachine::get_peer_cert @signature
493
+ end
494
+
495
+
496
+ # Sends UDP messages.
497
+ #
498
+ # This method may be called from any Connection object that refers
499
+ # to an open datagram socket (see EventMachine#open_datagram_socket).
500
+ # The method sends a UDP (datagram) packet containing the data you specify,
501
+ # to a remote peer specified by the IP address and port that you give
502
+ # as parameters to the method.
503
+ # Observe that you may send a zero-length packet (empty string).
504
+ # However, you may not send an arbitrarily-large data packet because
505
+ # your operating system will enforce a platform-specific limit on
506
+ # the size of the outbound packet. (Your kernel
507
+ # will respond in a platform-specific way if you send an overlarge
508
+ # packet: some will send a truncated packet, some will complain, and
509
+ # some will silently drop your request).
510
+ # On LANs, it's usually OK to send datagrams up to about 4000 bytes in length,
511
+ # but to be really safe, send messages smaller than the Ethernet-packet
512
+ # size (typically about 1400 bytes). Some very restrictive WANs
513
+ # will either drop or truncate packets larger than about 500 bytes.
514
+ #
515
+ # @param [String] data Data to send asynchronously
516
+ # @param [String] recipient_address IP address of the recipient
517
+ # @param [String] recipient_port Port of the recipient
518
+ def send_datagram data, recipient_address, recipient_port
519
+ data = data.to_s
520
+ size = data.bytesize if data.respond_to?(:bytesize)
521
+ size ||= data.size
522
+ EventMachine::send_datagram @signature, data, size, recipient_address, Integer(recipient_port)
523
+ end
524
+
525
+
526
+ # This method is used with stream-connections to obtain the identity
527
+ # of the remotely-connected peer. If a peername is available, this method
528
+ # returns a sockaddr structure. The method returns nil if no peername is available.
529
+ # You can use Socket.unpack_sockaddr_in and its variants to obtain the
530
+ # values contained in the peername structure returned from #get_peername.
531
+ #
532
+ # @example How to get peer IP address and port with EventMachine
533
+ #
534
+ # require 'socket'
535
+ #
536
+ # module Handler
537
+ # def receive_data data
538
+ # port, ip = Socket.unpack_sockaddr_in(get_peername)
539
+ # puts "got #{data.inspect} from #{ip}:#{port}"
540
+ # end
541
+ # end
542
+ def get_peername
543
+ EventMachine::get_peername @signature
544
+ end
545
+
546
+ # Used with stream-connections to obtain the identity
547
+ # of the local side of the connection. If a local name is available, this method
548
+ # returns a sockaddr structure. The method returns nil if no local name is available.
549
+ # You can use {Socket.unpack_sockaddr_in} and its variants to obtain the
550
+ # values contained in the local-name structure returned from this method.
551
+ #
552
+ # @example
553
+ #
554
+ # require 'socket'
555
+ #
556
+ # module Handler
557
+ # def receive_data data
558
+ # port, ip = Socket.unpack_sockaddr_in(get_sockname)
559
+ # puts "got #{data.inspect}"
560
+ # end
561
+ # end
562
+ def get_sockname
563
+ EventMachine::get_sockname @signature
564
+ end
565
+
566
+ # Returns the PID (kernel process identifier) of a subprocess
567
+ # associated with this Connection object. For use with {EventMachine.popen}
568
+ # and similar methods. Returns nil when there is no meaningful subprocess.
569
+ #
570
+ # @return [Integer]
571
+ def get_pid
572
+ EventMachine::get_subprocess_pid @signature
573
+ end
574
+
575
+ # Returns a subprocess exit status. Only useful for {EventMachine.popen}. Call it in your
576
+ # {#unbind} handler.
577
+ #
578
+ # @return [Integer]
579
+ def get_status
580
+ EventMachine::get_subprocess_status @signature
581
+ end
582
+
583
+ # The number of seconds since the last send/receive activity on this connection.
584
+ def get_idle_time
585
+ EventMachine::get_idle_time @signature
586
+ end
587
+
588
+ # comm_inactivity_timeout returns the current value (float in seconds) of the inactivity-timeout
589
+ # property of network-connection and datagram-socket objects. A nonzero value
590
+ # indicates that the connection or socket will automatically be closed if no read or write
591
+ # activity takes place for at least that number of seconds.
592
+ # A zero value (the default) specifies that no automatic timeout will take place.
593
+ def comm_inactivity_timeout
594
+ EventMachine::get_comm_inactivity_timeout @signature
595
+ end
596
+
597
+ # Allows you to set the inactivity-timeout property for
598
+ # a network connection or datagram socket. Specify a non-negative float value in seconds.
599
+ # If the value is greater than zero, the connection or socket will automatically be closed
600
+ # if no read or write activity takes place for at least that number of seconds.
601
+ # Specify a value of zero to indicate that no automatic timeout should take place.
602
+ # Zero is the default value.
603
+ def comm_inactivity_timeout= value
604
+ EventMachine::set_comm_inactivity_timeout @signature, value.to_f
605
+ end
606
+ alias set_comm_inactivity_timeout comm_inactivity_timeout=
607
+
608
+ # The duration after which a TCP connection in the connecting state will fail.
609
+ # It is important to distinguish this value from {EventMachine::Connection#comm_inactivity_timeout},
610
+ # which looks at how long since data was passed on an already established connection.
611
+ # The value is a float in seconds.
612
+ #
613
+ # @return [Float] The duration after which a TCP connection in the connecting state will fail, in seconds.
614
+ def pending_connect_timeout
615
+ EventMachine::get_pending_connect_timeout @signature
616
+ end
617
+
618
+ # Sets the duration after which a TCP connection in a
619
+ # connecting state will fail.
620
+ #
621
+ # @param [Float, #to_f] value Connection timeout in seconds
622
+ def pending_connect_timeout= value
623
+ EventMachine::set_pending_connect_timeout @signature, value.to_f
624
+ end
625
+ alias set_pending_connect_timeout pending_connect_timeout=
626
+
627
+ # Reconnect to a given host/port with the current instance
628
+ #
629
+ # @param [String] server Hostname or IP address
630
+ # @param [Integer] port Port to reconnect to
631
+ def reconnect server, port
632
+ EventMachine::reconnect server, port, self
633
+ end
634
+
635
+
636
+ # Like {EventMachine::Connection#send_data}, this sends data to the remote end of
637
+ # the network connection. {EventMachine::Connection#send_file_data} takes a
638
+ # filename as an argument, though, and sends the contents of the file, in one
639
+ # chunk.
640
+ #
641
+ # @param [String] filename Local path of the file to send
642
+ #
643
+ # @see #send_data
644
+ # @author Kirk Haines
645
+ def send_file_data filename
646
+ EventMachine::send_file_data @signature, filename
647
+ end
648
+
649
+ # Open a file on the filesystem and send it to the remote peer. This returns an
650
+ # object of type {EventMachine::Deferrable}. The object's callbacks will be executed
651
+ # on the reactor main thread when the file has been completely scheduled for
652
+ # transmission to the remote peer. Its errbacks will be called in case of an error (such as file-not-found).
653
+ # This method employs various strategies to achieve the fastest possible performance,
654
+ # balanced against minimum consumption of memory.
655
+ #
656
+ # Warning: this feature has an implicit dependency on an outboard extension,
657
+ # evma_fastfilereader. You must install this extension in order to use {#stream_file_data}
658
+ # with files larger than a certain size (currently 8192 bytes).
659
+ #
660
+ # @option args [Boolean] :http_chunks (false) If true, this method will stream the file data in a format
661
+ # compatible with the HTTP chunked-transfer encoding
662
+ #
663
+ # @param [String] filename Local path of the file to stream
664
+ # @param [Hash] args Options
665
+ #
666
+ # @return [EventMachine::Deferrable]
667
+ def stream_file_data filename, args={}
668
+ EventMachine::FileStreamer.new( self, filename, args )
669
+ end
670
+
671
+ # Watches connection for readability. Only possible if the connection was created
672
+ # using {EventMachine.attach} and had {EventMachine.notify_readable}/{EventMachine.notify_writable} defined on the handler.
673
+ #
674
+ # @see #notify_readable?
675
+ def notify_readable= mode
676
+ EventMachine::set_notify_readable @signature, mode
677
+ end
678
+
679
+ # @return [Boolean] true if the connection is being watched for readability.
680
+ def notify_readable?
681
+ EventMachine::is_notify_readable @signature
682
+ end
683
+
684
+ # Watches connection for writeability. Only possible if the connection was created
685
+ # using {EventMachine.attach} and had {EventMachine.notify_readable}/{EventMachine.notify_writable} defined on the handler.
686
+ #
687
+ # @see #notify_writable?
688
+ def notify_writable= mode
689
+ EventMachine::set_notify_writable @signature, mode
690
+ end
691
+
692
+ # Returns true if the connection is being watched for writability.
693
+ def notify_writable?
694
+ EventMachine::is_notify_writable @signature
695
+ end
696
+
697
+ # Pause a connection so that {#send_data} and {#receive_data} events are not fired until {#resume} is called.
698
+ # @see #resume
699
+ def pause
700
+ EventMachine::pause_connection @signature
701
+ end
702
+
703
+ # Resume a connection's {#send_data} and {#receive_data} events.
704
+ # @see #pause
705
+ def resume
706
+ EventMachine::resume_connection @signature
707
+ end
708
+
709
+ # @return [Boolean] true if the connect was paused using {EventMachine::Connection#pause}.
710
+ # @see #pause
711
+ # @see #resume
712
+ def paused?
713
+ EventMachine::connection_paused? @signature
714
+ end
715
+ end
716
+ end