sidekick-client 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (162) hide show
  1. data/VERSION +1 -1
  2. data/lib/sidekick/client/sidekick_client.rb +0 -3
  3. data/lib/sidekick/shared/sidekick_queue_publisher.rb +1 -1
  4. data/sidekick-client.gemspec +1 -158
  5. metadata +1 -158
  6. data/lib/ext/eventmachine-0.12.10/.gitignore +0 -14
  7. data/lib/ext/eventmachine-0.12.10/README +0 -82
  8. data/lib/ext/eventmachine-0.12.10/Rakefile +0 -374
  9. data/lib/ext/eventmachine-0.12.10/docs/COPYING +0 -60
  10. data/lib/ext/eventmachine-0.12.10/docs/ChangeLog +0 -211
  11. data/lib/ext/eventmachine-0.12.10/docs/DEFERRABLES +0 -133
  12. data/lib/ext/eventmachine-0.12.10/docs/EPOLL +0 -141
  13. data/lib/ext/eventmachine-0.12.10/docs/GNU +0 -281
  14. data/lib/ext/eventmachine-0.12.10/docs/INSTALL +0 -13
  15. data/lib/ext/eventmachine-0.12.10/docs/KEYBOARD +0 -38
  16. data/lib/ext/eventmachine-0.12.10/docs/LEGAL +0 -25
  17. data/lib/ext/eventmachine-0.12.10/docs/LIGHTWEIGHT_CONCURRENCY +0 -70
  18. data/lib/ext/eventmachine-0.12.10/docs/PURE_RUBY +0 -75
  19. data/lib/ext/eventmachine-0.12.10/docs/RELEASE_NOTES +0 -94
  20. data/lib/ext/eventmachine-0.12.10/docs/SMTP +0 -2
  21. data/lib/ext/eventmachine-0.12.10/docs/SPAWNED_PROCESSES +0 -89
  22. data/lib/ext/eventmachine-0.12.10/docs/TODO +0 -8
  23. data/lib/ext/eventmachine-0.12.10/eventmachine.gemspec +0 -40
  24. data/lib/ext/eventmachine-0.12.10/examples/ex_channel.rb +0 -43
  25. data/lib/ext/eventmachine-0.12.10/examples/ex_queue.rb +0 -2
  26. data/lib/ext/eventmachine-0.12.10/examples/helper.rb +0 -2
  27. data/lib/ext/eventmachine-0.12.10/ext/binder.cpp +0 -125
  28. data/lib/ext/eventmachine-0.12.10/ext/binder.h +0 -46
  29. data/lib/ext/eventmachine-0.12.10/ext/cmain.cpp +0 -827
  30. data/lib/ext/eventmachine-0.12.10/ext/cplusplus.cpp +0 -202
  31. data/lib/ext/eventmachine-0.12.10/ext/ed.cpp +0 -1893
  32. data/lib/ext/eventmachine-0.12.10/ext/ed.h +0 -424
  33. data/lib/ext/eventmachine-0.12.10/ext/em.cpp +0 -2282
  34. data/lib/ext/eventmachine-0.12.10/ext/em.h +0 -232
  35. data/lib/ext/eventmachine-0.12.10/ext/emwin.cpp +0 -300
  36. data/lib/ext/eventmachine-0.12.10/ext/emwin.h +0 -94
  37. data/lib/ext/eventmachine-0.12.10/ext/epoll.cpp +0 -26
  38. data/lib/ext/eventmachine-0.12.10/ext/epoll.h +0 -25
  39. data/lib/ext/eventmachine-0.12.10/ext/eventmachine.h +0 -122
  40. data/lib/ext/eventmachine-0.12.10/ext/eventmachine_cpp.h +0 -96
  41. data/lib/ext/eventmachine-0.12.10/ext/extconf.rb +0 -148
  42. data/lib/ext/eventmachine-0.12.10/ext/fastfilereader/extconf.rb +0 -83
  43. data/lib/ext/eventmachine-0.12.10/ext/fastfilereader/mapper.cpp +0 -214
  44. data/lib/ext/eventmachine-0.12.10/ext/fastfilereader/mapper.h +0 -59
  45. data/lib/ext/eventmachine-0.12.10/ext/fastfilereader/rubymain.cpp +0 -127
  46. data/lib/ext/eventmachine-0.12.10/ext/files.cpp +0 -94
  47. data/lib/ext/eventmachine-0.12.10/ext/files.h +0 -65
  48. data/lib/ext/eventmachine-0.12.10/ext/kb.cpp +0 -81
  49. data/lib/ext/eventmachine-0.12.10/ext/page.cpp +0 -107
  50. data/lib/ext/eventmachine-0.12.10/ext/page.h +0 -51
  51. data/lib/ext/eventmachine-0.12.10/ext/pipe.cpp +0 -349
  52. data/lib/ext/eventmachine-0.12.10/ext/project.h +0 -151
  53. data/lib/ext/eventmachine-0.12.10/ext/rubymain.cpp +0 -1166
  54. data/lib/ext/eventmachine-0.12.10/ext/sigs.cpp +0 -89
  55. data/lib/ext/eventmachine-0.12.10/ext/sigs.h +0 -32
  56. data/lib/ext/eventmachine-0.12.10/ext/ssl.cpp +0 -460
  57. data/lib/ext/eventmachine-0.12.10/ext/ssl.h +0 -94
  58. data/lib/ext/eventmachine-0.12.10/java/.classpath +0 -8
  59. data/lib/ext/eventmachine-0.12.10/java/.project +0 -17
  60. data/lib/ext/eventmachine-0.12.10/java/src/com/rubyeventmachine/EmReactor.java +0 -570
  61. data/lib/ext/eventmachine-0.12.10/java/src/com/rubyeventmachine/EmReactorException.java +0 -40
  62. data/lib/ext/eventmachine-0.12.10/java/src/com/rubyeventmachine/EventableChannel.java +0 -69
  63. data/lib/ext/eventmachine-0.12.10/java/src/com/rubyeventmachine/EventableDatagramChannel.java +0 -189
  64. data/lib/ext/eventmachine-0.12.10/java/src/com/rubyeventmachine/EventableSocketChannel.java +0 -364
  65. data/lib/ext/eventmachine-0.12.10/java/src/com/rubyeventmachine/application/Application.java +0 -194
  66. data/lib/ext/eventmachine-0.12.10/java/src/com/rubyeventmachine/application/Connection.java +0 -74
  67. data/lib/ext/eventmachine-0.12.10/java/src/com/rubyeventmachine/application/ConnectionFactory.java +0 -37
  68. data/lib/ext/eventmachine-0.12.10/java/src/com/rubyeventmachine/application/DefaultConnectionFactory.java +0 -46
  69. data/lib/ext/eventmachine-0.12.10/java/src/com/rubyeventmachine/application/PeriodicTimer.java +0 -38
  70. data/lib/ext/eventmachine-0.12.10/java/src/com/rubyeventmachine/application/Timer.java +0 -54
  71. data/lib/ext/eventmachine-0.12.10/java/src/com/rubyeventmachine/tests/ApplicationTest.java +0 -109
  72. data/lib/ext/eventmachine-0.12.10/java/src/com/rubyeventmachine/tests/ConnectTest.java +0 -148
  73. data/lib/ext/eventmachine-0.12.10/java/src/com/rubyeventmachine/tests/EMTest.java +0 -80
  74. data/lib/ext/eventmachine-0.12.10/java/src/com/rubyeventmachine/tests/TestDatagrams.java +0 -53
  75. data/lib/ext/eventmachine-0.12.10/java/src/com/rubyeventmachine/tests/TestServers.java +0 -75
  76. data/lib/ext/eventmachine-0.12.10/java/src/com/rubyeventmachine/tests/TestTimers.java +0 -90
  77. data/lib/ext/eventmachine-0.12.10/lib/em/buftok.rb +0 -138
  78. data/lib/ext/eventmachine-0.12.10/lib/em/callback.rb +0 -26
  79. data/lib/ext/eventmachine-0.12.10/lib/em/channel.rb +0 -57
  80. data/lib/ext/eventmachine-0.12.10/lib/em/connection.rb +0 -564
  81. data/lib/ext/eventmachine-0.12.10/lib/em/deferrable.rb +0 -192
  82. data/lib/ext/eventmachine-0.12.10/lib/em/file_watch.rb +0 -54
  83. data/lib/ext/eventmachine-0.12.10/lib/em/future.rb +0 -61
  84. data/lib/ext/eventmachine-0.12.10/lib/em/messages.rb +0 -66
  85. data/lib/ext/eventmachine-0.12.10/lib/em/process_watch.rb +0 -44
  86. data/lib/ext/eventmachine-0.12.10/lib/em/processes.rb +0 -119
  87. data/lib/ext/eventmachine-0.12.10/lib/em/protocols/header_and_content.rb +0 -138
  88. data/lib/ext/eventmachine-0.12.10/lib/em/protocols/httpclient.rb +0 -263
  89. data/lib/ext/eventmachine-0.12.10/lib/em/protocols/httpclient2.rb +0 -590
  90. data/lib/ext/eventmachine-0.12.10/lib/em/protocols/line_and_text.rb +0 -125
  91. data/lib/ext/eventmachine-0.12.10/lib/em/protocols/linetext2.rb +0 -161
  92. data/lib/ext/eventmachine-0.12.10/lib/em/protocols/memcache.rb +0 -323
  93. data/lib/ext/eventmachine-0.12.10/lib/em/protocols/object_protocol.rb +0 -45
  94. data/lib/ext/eventmachine-0.12.10/lib/em/protocols/postgres3.rb +0 -247
  95. data/lib/ext/eventmachine-0.12.10/lib/em/protocols/saslauth.rb +0 -175
  96. data/lib/ext/eventmachine-0.12.10/lib/em/protocols/smtpclient.rb +0 -357
  97. data/lib/ext/eventmachine-0.12.10/lib/em/protocols/smtpserver.rb +0 -547
  98. data/lib/ext/eventmachine-0.12.10/lib/em/protocols/socks4.rb +0 -66
  99. data/lib/ext/eventmachine-0.12.10/lib/em/protocols/stomp.rb +0 -200
  100. data/lib/ext/eventmachine-0.12.10/lib/em/protocols/tcptest.rb +0 -53
  101. data/lib/ext/eventmachine-0.12.10/lib/em/protocols.rb +0 -36
  102. data/lib/ext/eventmachine-0.12.10/lib/em/queue.rb +0 -61
  103. data/lib/ext/eventmachine-0.12.10/lib/em/spawnable.rb +0 -85
  104. data/lib/ext/eventmachine-0.12.10/lib/em/streamer.rb +0 -130
  105. data/lib/ext/eventmachine-0.12.10/lib/em/timers.rb +0 -56
  106. data/lib/ext/eventmachine-0.12.10/lib/em/version.rb +0 -3
  107. data/lib/ext/eventmachine-0.12.10/lib/eventmachine.rb +0 -1592
  108. data/lib/ext/eventmachine-0.12.10/lib/evma/callback.rb +0 -32
  109. data/lib/ext/eventmachine-0.12.10/lib/evma/container.rb +0 -75
  110. data/lib/ext/eventmachine-0.12.10/lib/evma/factory.rb +0 -77
  111. data/lib/ext/eventmachine-0.12.10/lib/evma/protocol.rb +0 -87
  112. data/lib/ext/eventmachine-0.12.10/lib/evma/reactor.rb +0 -48
  113. data/lib/ext/eventmachine-0.12.10/lib/evma.rb +0 -32
  114. data/lib/ext/eventmachine-0.12.10/lib/jeventmachine.rb +0 -257
  115. data/lib/ext/eventmachine-0.12.10/lib/pr_eventmachine.rb +0 -1022
  116. data/lib/ext/eventmachine-0.12.10/setup.rb +0 -1585
  117. data/lib/ext/eventmachine-0.12.10/tasks/cpp.rake_example +0 -77
  118. data/lib/ext/eventmachine-0.12.10/tests/client.crt +0 -31
  119. data/lib/ext/eventmachine-0.12.10/tests/client.key +0 -51
  120. data/lib/ext/eventmachine-0.12.10/tests/test_attach.rb +0 -126
  121. data/lib/ext/eventmachine-0.12.10/tests/test_basic.rb +0 -284
  122. data/lib/ext/eventmachine-0.12.10/tests/test_channel.rb +0 -63
  123. data/lib/ext/eventmachine-0.12.10/tests/test_connection_count.rb +0 -35
  124. data/lib/ext/eventmachine-0.12.10/tests/test_defer.rb +0 -47
  125. data/lib/ext/eventmachine-0.12.10/tests/test_epoll.rb +0 -160
  126. data/lib/ext/eventmachine-0.12.10/tests/test_error_handler.rb +0 -35
  127. data/lib/ext/eventmachine-0.12.10/tests/test_errors.rb +0 -82
  128. data/lib/ext/eventmachine-0.12.10/tests/test_exc.rb +0 -55
  129. data/lib/ext/eventmachine-0.12.10/tests/test_file_watch.rb +0 -49
  130. data/lib/ext/eventmachine-0.12.10/tests/test_futures.rb +0 -198
  131. data/lib/ext/eventmachine-0.12.10/tests/test_get_sock_opt.rb +0 -30
  132. data/lib/ext/eventmachine-0.12.10/tests/test_handler_check.rb +0 -37
  133. data/lib/ext/eventmachine-0.12.10/tests/test_hc.rb +0 -218
  134. data/lib/ext/eventmachine-0.12.10/tests/test_httpclient.rb +0 -218
  135. data/lib/ext/eventmachine-0.12.10/tests/test_httpclient2.rb +0 -153
  136. data/lib/ext/eventmachine-0.12.10/tests/test_inactivity_timeout.rb +0 -50
  137. data/lib/ext/eventmachine-0.12.10/tests/test_kb.rb +0 -60
  138. data/lib/ext/eventmachine-0.12.10/tests/test_ltp.rb +0 -182
  139. data/lib/ext/eventmachine-0.12.10/tests/test_ltp2.rb +0 -317
  140. data/lib/ext/eventmachine-0.12.10/tests/test_next_tick.rb +0 -133
  141. data/lib/ext/eventmachine-0.12.10/tests/test_object_protocol.rb +0 -37
  142. data/lib/ext/eventmachine-0.12.10/tests/test_pause.rb +0 -70
  143. data/lib/ext/eventmachine-0.12.10/tests/test_pending_connect_timeout.rb +0 -48
  144. data/lib/ext/eventmachine-0.12.10/tests/test_process_watch.rb +0 -48
  145. data/lib/ext/eventmachine-0.12.10/tests/test_processes.rb +0 -128
  146. data/lib/ext/eventmachine-0.12.10/tests/test_proxy_connection.rb +0 -92
  147. data/lib/ext/eventmachine-0.12.10/tests/test_pure.rb +0 -125
  148. data/lib/ext/eventmachine-0.12.10/tests/test_queue.rb +0 -44
  149. data/lib/ext/eventmachine-0.12.10/tests/test_running.rb +0 -42
  150. data/lib/ext/eventmachine-0.12.10/tests/test_sasl.rb +0 -72
  151. data/lib/ext/eventmachine-0.12.10/tests/test_send_file.rb +0 -242
  152. data/lib/ext/eventmachine-0.12.10/tests/test_servers.rb +0 -76
  153. data/lib/ext/eventmachine-0.12.10/tests/test_smtpclient.rb +0 -83
  154. data/lib/ext/eventmachine-0.12.10/tests/test_smtpserver.rb +0 -85
  155. data/lib/ext/eventmachine-0.12.10/tests/test_spawn.rb +0 -322
  156. data/lib/ext/eventmachine-0.12.10/tests/test_ssl_args.rb +0 -79
  157. data/lib/ext/eventmachine-0.12.10/tests/test_ssl_methods.rb +0 -50
  158. data/lib/ext/eventmachine-0.12.10/tests/test_ssl_verify.rb +0 -82
  159. data/lib/ext/eventmachine-0.12.10/tests/test_timers.rb +0 -162
  160. data/lib/ext/eventmachine-0.12.10/tests/test_ud.rb +0 -36
  161. data/lib/ext/eventmachine-0.12.10/tests/testem.rb +0 -31
  162. data/lib/ext/eventmachine-0.12.10/web/whatis +0 -7
@@ -1,1022 +0,0 @@
1
- #--
2
- #
3
- # Author:: Francis Cianfrocca (gmail: blackhedd)
4
- # Homepage:: http://rubyeventmachine.com
5
- # Date:: 8 Apr 2006
6
- #
7
- # See EventMachine and EventMachine::Connection for documentation and
8
- # usage examples.
9
- #
10
- #----------------------------------------------------------------------------
11
- #
12
- # Copyright (C) 2006-07 by Francis Cianfrocca. All Rights Reserved.
13
- # Gmail: blackhedd
14
- #
15
- # This program is free software; you can redistribute it and/or modify
16
- # it under the terms of either: 1) the GNU General Public License
17
- # as published by the Free Software Foundation; either version 2 of the
18
- # License, or (at your option) any later version; or 2) Ruby's License.
19
- #
20
- # See the file COPYING for complete licensing information.
21
- #
22
- #-------------------------------------------------------------------
23
- #
24
- #
25
-
26
- # TODO List:
27
- # TCP-connects currently assume non-blocking connect is available- need to
28
- # degrade automatically on versions of Ruby prior to June 2006.
29
- #
30
-
31
- require 'singleton'
32
- require 'forwardable'
33
- require 'socket'
34
- require 'fcntl'
35
- require 'set'
36
-
37
-
38
- module EventMachine
39
-
40
-
41
- class << self
42
- # This is mostly useful for automated tests.
43
- # Return a distinctive symbol so the caller knows whether he's dealing
44
- # with an extension or with a pure-Ruby library.
45
- def library_type
46
- :pure_ruby
47
- end
48
-
49
- # #initialize_event_machine
50
- def initialize_event_machine
51
- Reactor.instance.initialize_for_run
52
- end
53
-
54
- # #add_oneshot_timer
55
- #--
56
- # Changed 04Oct06: intervals from the caller are now in milliseconds, but our native-ruby
57
- # processor still wants them in seconds.
58
- def add_oneshot_timer interval
59
- Reactor.instance.install_oneshot_timer(interval / 1000)
60
- end
61
-
62
- # run_machine
63
- def run_machine
64
- Reactor.instance.run
65
- end
66
-
67
- # release_machine. Probably a no-op.
68
- def release_machine
69
- end
70
-
71
- # #stop
72
- def stop
73
- Reactor.instance.stop
74
- end
75
-
76
- # #connect_server. Return a connection descriptor to the caller.
77
- # TODO, what do we return here if we can't connect?
78
- def connect_server host, port
79
- bind_connect_server nil, nil, host, port
80
- end
81
-
82
- def bind_connect_server bind_addr, bind_port, host, port
83
- EvmaTCPClient.connect(bind_addr, bind_port, host, port).uuid
84
- end
85
-
86
- # #send_data
87
- def send_data target, data, datalength
88
- selectable = Reactor.instance.get_selectable( target ) or raise "unknown send_data target"
89
- selectable.send_data data
90
- end
91
-
92
- # #close_connection
93
- # The extension version does NOT raise any kind of an error if an attempt is made
94
- # to close a non-existent connection. Not sure whether we should. For now, we'll
95
- # raise an error here in that case.
96
- def close_connection target, after_writing
97
- selectable = Reactor.instance.get_selectable( target ) or raise "unknown close_connection target"
98
- selectable.schedule_close after_writing
99
- end
100
-
101
- # #start_tcp_server
102
- def start_tcp_server host, port
103
- (s = EvmaTCPServer.start_server host, port) or raise "no acceptor"
104
- s.uuid
105
- end
106
-
107
- # #stop_tcp_server
108
- def stop_tcp_server sig
109
- s = Reactor.instance.get_selectable(sig)
110
- s.schedule_close
111
- end
112
-
113
- # #start_unix_server
114
- def start_unix_server chain
115
- (s = EvmaUNIXServer.start_server chain) or raise "no acceptor"
116
- s.uuid
117
- end
118
-
119
- # #connect_unix_server
120
- def connect_unix_server chain
121
- EvmaUNIXClient.connect(chain).uuid
122
- end
123
-
124
- # #signal_loopbreak
125
- def signal_loopbreak
126
- Reactor.instance.signal_loopbreak
127
- end
128
-
129
- # #get_peername
130
- def get_peername sig
131
- selectable = Reactor.instance.get_selectable( sig ) or raise "unknown get_peername target"
132
- selectable.get_peername
133
- end
134
-
135
- # #open_udp_socket
136
- def open_udp_socket host, port
137
- EvmaUDPSocket.create(host, port).uuid
138
- end
139
-
140
- # #send_datagram. This is currently only for UDP!
141
- # We need to make it work with unix-domain sockets as well.
142
- def send_datagram target, data, datalength, host, port
143
- selectable = Reactor.instance.get_selectable( target ) or raise "unknown send_data target"
144
- selectable.send_datagram data, Socket::pack_sockaddr_in(port, host)
145
- end
146
-
147
-
148
- # #set_timer_quantum in milliseconds. The underlying Reactor function wants a (possibly
149
- # fractional) number of seconds.
150
- def set_timer_quantum interval
151
- Reactor.instance.set_timer_quantum(( 1.0 * interval) / 1000.0)
152
- end
153
-
154
- # #epoll is a harmless no-op in the pure-Ruby implementation. This is intended to ensure
155
- # that user code behaves properly across different EM implementations.
156
- def epoll
157
- end
158
-
159
- # #ssl? is not implemented for pure-Ruby implementation
160
- def ssl?
161
- false
162
- end
163
-
164
- # #set_rlimit_nofile is a no-op in the pure-Ruby implementation. We simply return Ruby's built-in
165
- # per-process file-descriptor limit.
166
- def set_rlimit_nofile n
167
- 1024
168
- end
169
-
170
- # #set_max_timer_count is a harmless no-op in pure Ruby, which doesn't have a built-in limit
171
- # on the number of available timers.
172
- def set_max_timer_count n
173
- end
174
-
175
- # #send_file_data
176
- def send_file_data sig, filename
177
- sz = File.size(filename)
178
- raise "file too large" if sz > 32*1024
179
- data =
180
- begin
181
- File.read filename
182
- rescue
183
- ""
184
- end
185
- send_data sig, data, data.length
186
- end
187
-
188
- # #get_outbound_data_size
189
- #
190
- def get_outbound_data_size sig
191
- r = Reactor.instance.get_selectable( sig ) or raise "unknown get_outbound_data_size target"
192
- r.get_outbound_data_size
193
- end
194
-
195
- # #read_keyboard
196
- #
197
- def read_keyboard
198
- EvmaKeyboard.open.uuid
199
- end
200
-
201
- # #set_comm_inactivity_timeout
202
- #
203
- def set_comm_inactivity_timeout sig, tm
204
- r = Reactor.instance.get_selectable( sig ) or raise "unknown set_comm_inactivity_timeout target"
205
- r.set_inactivity_timeout tm
206
- end
207
- end
208
-
209
- end
210
-
211
-
212
- #-----------------------------------------------------------------
213
-
214
- module EventMachine
215
-
216
- class Error < Exception; end
217
-
218
- end
219
-
220
- #-----------------------------------------------------------------
221
-
222
- module EventMachine
223
- class Connection
224
- def get_outbound_data_size
225
- EventMachine::get_outbound_data_size @signature
226
- end
227
- end
228
- end
229
-
230
- #-----------------------------------------------------------------
231
-
232
- module EventMachine
233
-
234
- # Factored out so we can substitute other implementations
235
- # here if desired, such as the one in ActiveRBAC.
236
- module UuidGenerator
237
-
238
- def self.generate
239
- if @ix and @ix >= 10000
240
- @ix = nil
241
- @seed = nil
242
- end
243
-
244
- @seed ||= `uuidgen`.chomp.gsub(/-/,"")
245
- @ix ||= 0
246
-
247
- "#{@seed}#{@ix += 1}"
248
- end
249
-
250
- end
251
-
252
- end
253
-
254
- #-----------------------------------------------------------------
255
-
256
- module EventMachine
257
-
258
- TimerFired = 100
259
- ConnectionData = 101
260
- ConnectionUnbound = 102
261
- ConnectionAccepted = 103
262
- ConnectionCompleted = 104
263
- LoopbreakSignalled = 105
264
-
265
- end
266
-
267
- #-----------------------------------------------------------------
268
-
269
- module EventMachine
270
- class Reactor
271
- include Singleton
272
-
273
- HeartbeatInterval = 2
274
-
275
- attr_reader :current_loop_time
276
-
277
- def initialize
278
- initialize_for_run
279
- end
280
-
281
- #--
282
- # Replaced original implementation 05Dec07, was way too slow because of the sort.
283
- def install_oneshot_timer interval
284
- uuid = UuidGenerator::generate
285
- #@timers << [Time.now + interval, uuid]
286
- #@timers.sort! {|a,b| a.first <=> b.first}
287
- @timers.add([Time.now + interval, uuid])
288
- uuid
289
- end
290
-
291
- # Called before run, this is a good place to clear out arrays
292
- # with cruft that may be left over from a previous run.
293
- def initialize_for_run
294
- @running = false
295
- @stop_scheduled = false
296
- @selectables ||= {}; @selectables.clear
297
- @timers = SortedSet.new # []
298
- set_timer_quantum(0.1)
299
- @current_loop_time = Time.now
300
- @next_heartbeat = @current_loop_time + HeartbeatInterval
301
- end
302
-
303
- def add_selectable io
304
- @selectables[io.uuid] = io
305
- end
306
-
307
- def get_selectable uuid
308
- @selectables[uuid]
309
- end
310
-
311
- def run
312
- raise Error.new( "already running" ) if @running
313
- @running = true
314
-
315
- begin
316
- open_loopbreaker
317
-
318
- loop {
319
- @current_loop_time = Time.now
320
-
321
- break if @stop_scheduled
322
- run_timers
323
- break if @stop_scheduled
324
- crank_selectables
325
- break if @stop_scheduled
326
- run_heartbeats
327
- }
328
- ensure
329
- close_loopbreaker
330
- @selectables.each {|k, io| io.close}
331
- @selectables.clear
332
-
333
- @running = false
334
- end
335
-
336
- end
337
-
338
- def run_timers
339
- @timers.each {|t|
340
- if t.first <= @current_loop_time
341
- @timers.delete t
342
- EventMachine::event_callback "", TimerFired, t.last
343
- else
344
- break
345
- end
346
- }
347
- #while @timers.length > 0 and @timers.first.first <= now
348
- # t = @timers.shift
349
- # EventMachine::event_callback "", TimerFired, t.last
350
- #end
351
- end
352
-
353
- def run_heartbeats
354
- if @next_heartbeat <= @current_loop_time
355
- @next_heartbeat = @current_loop_time + HeartbeatInterval
356
- @selectables.each {|k,io| io.heartbeat}
357
- end
358
- end
359
-
360
- def crank_selectables
361
- #$stderr.write 'R'
362
-
363
- readers = @selectables.values.select {|io| io.select_for_reading?}
364
- writers = @selectables.values.select {|io| io.select_for_writing?}
365
-
366
- s = select( readers, writers, nil, @timer_quantum)
367
-
368
- s and s[1] and s[1].each {|w| w.eventable_write }
369
- s and s[0] and s[0].each {|r| r.eventable_read }
370
-
371
- @selectables.delete_if {|k,io|
372
- if io.close_scheduled?
373
- io.close
374
- true
375
- end
376
- }
377
- end
378
-
379
- # #stop
380
- def stop
381
- raise Error.new( "not running") unless @running
382
- @stop_scheduled = true
383
- end
384
-
385
- def open_loopbreaker
386
- # Can't use an IO.pipe because they can't be set nonselectable in Windows.
387
- # Pick a random localhost UDP port.
388
- #@loopbreak_writer.close if @loopbreak_writer
389
- #rd,@loopbreak_writer = IO.pipe
390
- @loopbreak_reader = UDPSocket.new
391
- @loopbreak_writer = UDPSocket.new
392
- bound = false
393
- 100.times {
394
- @loopbreak_port = rand(10000) + 40000
395
- begin
396
- @loopbreak_reader.bind "localhost", @loopbreak_port
397
- bound = true
398
- break
399
- rescue
400
- end
401
- }
402
- raise "Unable to bind Loopbreaker" unless bound
403
- LoopbreakReader.new(@loopbreak_reader)
404
- end
405
-
406
- def close_loopbreaker
407
- @loopbreak_writer.close
408
- @loopbreak_writer = nil
409
- end
410
-
411
- def signal_loopbreak
412
- #@loopbreak_writer.write '+' if @loopbreak_writer
413
- @loopbreak_writer.send('+',0,"localhost",@loopbreak_port) if @loopbreak_writer
414
- end
415
-
416
- def set_timer_quantum interval_in_seconds
417
- @timer_quantum = interval_in_seconds
418
- end
419
-
420
- end
421
-
422
- end
423
-
424
-
425
- #--------------------------------------------------------------
426
-
427
- class IO
428
- extend Forwardable
429
- def_delegator :@my_selectable, :close_scheduled?
430
- def_delegator :@my_selectable, :select_for_reading?
431
- def_delegator :@my_selectable, :select_for_writing?
432
- def_delegator :@my_selectable, :eventable_read
433
- def_delegator :@my_selectable, :eventable_write
434
- def_delegator :@my_selectable, :uuid
435
- def_delegator :@my_selectable, :send_data
436
- def_delegator :@my_selectable, :schedule_close
437
- def_delegator :@my_selectable, :get_peername
438
- def_delegator :@my_selectable, :send_datagram
439
- def_delegator :@my_selectable, :get_outbound_data_size
440
- def_delegator :@my_selectable, :set_inactivity_timeout
441
- def_delegator :@my_selectable, :heartbeat
442
- end
443
-
444
- #--------------------------------------------------------------
445
-
446
- module EventMachine
447
- class Selectable
448
-
449
- attr_reader :io, :uuid
450
-
451
- def initialize io
452
- @uuid = UuidGenerator.generate
453
- @io = io
454
- @last_activity = Reactor.instance.current_loop_time
455
-
456
- if defined?(Fcntl::F_GETFL)
457
- m = @io.fcntl(Fcntl::F_GETFL, 0)
458
- @io.fcntl(Fcntl::F_SETFL, Fcntl::O_NONBLOCK | m)
459
- else
460
- # Windows doesn't define F_GETFL.
461
- # It's not very reliable about setting descriptors nonblocking either.
462
- begin
463
- s = Socket.for_fd(@io.fileno)
464
- s.fcntl( Fcntl::F_SETFL, Fcntl::O_NONBLOCK )
465
- rescue Errno::EINVAL, Errno::EBADF
466
- STDERR.puts "Serious error: unable to set descriptor non-blocking"
467
- end
468
- end
469
- # TODO, should set CLOEXEC on Unix?
470
-
471
- @close_scheduled = false
472
- @close_requested = false
473
-
474
- se = self; @io.instance_eval { @my_selectable = se }
475
- Reactor.instance.add_selectable @io
476
- end
477
-
478
- def close_scheduled?
479
- @close_scheduled
480
- end
481
-
482
- def select_for_reading?
483
- false
484
- end
485
-
486
- def select_for_writing?
487
- false
488
- end
489
-
490
- def get_peername
491
- nil
492
- end
493
-
494
- def set_inactivity_timeout tm
495
- @inactivity_timeout = tm
496
- end
497
-
498
- def heartbeat
499
- end
500
- end
501
-
502
- end
503
-
504
- #--------------------------------------------------------------
505
-
506
-
507
- module EventMachine
508
-
509
- class StreamObject < Selectable
510
- def initialize io
511
- super io
512
- @outbound_q = []
513
- end
514
-
515
- # If we have to close, or a close-after-writing has been requested,
516
- # then don't read any more data.
517
- def select_for_reading?
518
- true unless (@close_scheduled || @close_requested)
519
- end
520
-
521
- # If we have to close, don't select for writing.
522
- # Otherwise, see if the protocol is ready to close.
523
- # If not, see if he has data to send.
524
- # If a close-after-writing has been requested and the outbound queue
525
- # is empty, convert the status to close_scheduled.
526
- def select_for_writing?
527
- unless @close_scheduled
528
- if @outbound_q.empty?
529
- @close_scheduled = true if @close_requested
530
- false
531
- else
532
- true
533
- end
534
- end
535
- end
536
-
537
- # Proper nonblocking I/O was added to Ruby 1.8.4 in May 2006.
538
- # If we have it, then we can read multiple times safely to improve
539
- # performance.
540
- # The last-activity clock ASSUMES that we only come here when we
541
- # have selected readable.
542
- # TODO, coalesce multiple reads into a single event.
543
- # TODO, do the function check somewhere else and cache it.
544
- def eventable_read
545
- @last_activity = Reactor.instance.current_loop_time
546
- begin
547
- if io.respond_to?(:read_nonblock)
548
- 10.times {
549
- data = io.read_nonblock(4096)
550
- EventMachine::event_callback uuid, ConnectionData, data
551
- }
552
- else
553
- data = io.sysread(4096)
554
- EventMachine::event_callback uuid, ConnectionData, data
555
- end
556
- rescue Errno::EAGAIN, Errno::EWOULDBLOCK
557
- # no-op
558
- rescue Errno::ECONNRESET, Errno::ECONNREFUSED, EOFError
559
- @close_scheduled = true
560
- EventMachine::event_callback uuid, ConnectionUnbound, nil
561
- end
562
-
563
- end
564
-
565
- # Provisional implementation. Will be re-implemented in subclasses.
566
- # TODO: Complete this implementation. As it stands, this only writes
567
- # a single packet per cycle. Highly inefficient, but required unless
568
- # we're running on a Ruby with proper nonblocking I/O (Ruby 1.8.4
569
- # built from sources from May 25, 2006 or newer).
570
- # We need to improve the loop so it writes multiple times, however
571
- # not more than a certain number of bytes per cycle, otherwise
572
- # one busy connection could hog output buffers and slow down other
573
- # connections. Also we should coalesce small writes.
574
- # URGENT TODO: Coalesce small writes. They are a performance killer.
575
- # The last-activity recorder ASSUMES we'll only come here if we've
576
- # selected writable.
577
- def eventable_write
578
- # coalesce the outbound array here, perhaps
579
- @last_activity = Reactor.instance.current_loop_time
580
- while data = @outbound_q.shift do
581
- begin
582
- data = data.to_s
583
- w = if io.respond_to?(:write_nonblock)
584
- io.write_nonblock data
585
- else
586
- io.syswrite data
587
- end
588
-
589
- if w < data.length
590
- @outbound_q.unshift data[w..-1]
591
- break
592
- end
593
- rescue Errno::EAGAIN
594
- @outbound_q.unshift data
595
- rescue EOFError, Errno::ECONNRESET, Errno::ECONNREFUSED
596
- @close_scheduled = true
597
- @outbound_q.clear
598
- end
599
- end
600
-
601
- end
602
-
603
- # #send_data
604
- def send_data data
605
- # TODO, coalesce here perhaps by being smarter about appending to @outbound_q.last?
606
- unless @close_scheduled or @close_requested or !data or data.length <= 0
607
- @outbound_q << data.to_s
608
- end
609
- end
610
-
611
- # #schedule_close
612
- # The application wants to close the connection.
613
- def schedule_close after_writing
614
- if after_writing
615
- @close_requested = true
616
- else
617
- @close_scheduled = true
618
- end
619
- end
620
-
621
- # #get_peername
622
- # This is defined in the normal way on connected stream objects.
623
- # Return an object that is suitable for passing to Socket#unpack_sockaddr_in or variants.
624
- # We could also use a convenience method that did the unpacking automatically.
625
- def get_peername
626
- io.getpeername
627
- end
628
-
629
- # #get_outbound_data_size
630
- def get_outbound_data_size
631
- @outbound_q.inject(0) {|memo,obj| memo += (obj || "").length}
632
- end
633
-
634
- def heartbeat
635
- if @inactivity_timeout and @inactivity_timeout > 0 and (@last_activity + @inactivity_timeout) < Reactor.instance.current_loop_time
636
- schedule_close true
637
- end
638
- end
639
- end
640
-
641
-
642
- end
643
-
644
-
645
- #--------------------------------------------------------------
646
-
647
-
648
-
649
- module EventMachine
650
- class EvmaTCPClient < StreamObject
651
-
652
- def self.connect bind_addr, bind_port, host, port
653
- sd = Socket.new( Socket::AF_INET, Socket::SOCK_STREAM, 0 )
654
- sd.bind( Socket.pack_sockaddr_in( bind_port, bind_addr )) if bind_addr
655
-
656
- begin
657
- # TODO, this assumes a current Ruby snapshot.
658
- # We need to degrade to a nonblocking connect otherwise.
659
- sd.connect_nonblock( Socket.pack_sockaddr_in( port, host ))
660
- rescue Errno::EINPROGRESS
661
- end
662
- EvmaTCPClient.new sd
663
- end
664
-
665
-
666
- def initialize io
667
- super
668
- @pending = true
669
- end
670
-
671
-
672
- def select_for_writing?
673
- @pending ? true : super
674
- end
675
-
676
- def select_for_reading?
677
- @pending ? false : super
678
- end
679
-
680
- def eventable_write
681
- if @pending
682
- @pending = false
683
- if 0 == io.getsockopt(Socket::SOL_SOCKET, Socket::SO_ERROR).unpack("i").first
684
- EventMachine::event_callback uuid, ConnectionCompleted, ""
685
- end
686
- else
687
- super
688
- end
689
- end
690
-
691
-
692
-
693
- end
694
- end
695
-
696
- #--------------------------------------------------------------
697
-
698
-
699
-
700
- module EventMachine
701
- class EvmaKeyboard < StreamObject
702
-
703
- def self.open
704
- EvmaKeyboard.new STDIN
705
- end
706
-
707
-
708
- def initialize io
709
- super
710
- end
711
-
712
-
713
- def select_for_writing?
714
- false
715
- end
716
-
717
- def select_for_reading?
718
- true
719
- end
720
-
721
-
722
- end
723
- end
724
-
725
-
726
- #--------------------------------------------------------------
727
-
728
-
729
-
730
- module EventMachine
731
- class EvmaUNIXClient < StreamObject
732
-
733
- def self.connect chain
734
- sd = Socket.new( Socket::AF_LOCAL, Socket::SOCK_STREAM, 0 )
735
- begin
736
- # TODO, this assumes a current Ruby snapshot.
737
- # We need to degrade to a nonblocking connect otherwise.
738
- sd.connect_nonblock( Socket.pack_sockaddr_un( chain ))
739
- rescue Errno::EINPROGRESS
740
- end
741
- EvmaUNIXClient.new sd
742
- end
743
-
744
-
745
- def initialize io
746
- super
747
- @pending = true
748
- end
749
-
750
-
751
- def select_for_writing?
752
- @pending ? true : super
753
- end
754
-
755
- def select_for_reading?
756
- @pending ? false : super
757
- end
758
-
759
- def eventable_write
760
- if @pending
761
- @pending = false
762
- if 0 == io.getsockopt(Socket::SOL_SOCKET, Socket::SO_ERROR).unpack("i").first
763
- EventMachine::event_callback uuid, ConnectionCompleted, ""
764
- end
765
- else
766
- super
767
- end
768
- end
769
-
770
-
771
-
772
- end
773
- end
774
-
775
-
776
- #--------------------------------------------------------------
777
-
778
- module EventMachine
779
- class EvmaTCPServer < Selectable
780
-
781
- # TODO, refactor and unify with EvmaUNIXServer.
782
-
783
- class << self
784
- # Versions of ruby 1.8.4 later than May 26 2006 will work properly
785
- # with an object of type TCPServer. Prior versions won't so we
786
- # play it safe and just build a socket.
787
- #
788
- def start_server host, port
789
- sd = Socket.new( Socket::AF_INET, Socket::SOCK_STREAM, 0 )
790
- sd.setsockopt( Socket::SOL_SOCKET, Socket::SO_REUSEADDR, true )
791
- sd.bind( Socket.pack_sockaddr_in( port, host ))
792
- sd.listen( 50 ) # 5 is what you see in all the books. Ain't enough.
793
- EvmaTCPServer.new sd
794
- end
795
- end
796
-
797
- def initialize io
798
- super io
799
- end
800
-
801
-
802
- def select_for_reading?
803
- true
804
- end
805
-
806
- #--
807
- # accept_nonblock returns an array consisting of the accepted
808
- # socket and a sockaddr_in which names the peer.
809
- # Don't accept more than 10 at a time.
810
- def eventable_read
811
- begin
812
- 10.times {
813
- descriptor,peername = io.accept_nonblock
814
- sd = StreamObject.new descriptor
815
- EventMachine::event_callback uuid, ConnectionAccepted, sd.uuid
816
- }
817
- rescue Errno::EWOULDBLOCK, Errno::EAGAIN
818
- end
819
- end
820
-
821
- #--
822
- #
823
- def schedule_close
824
- @close_scheduled = true
825
- end
826
-
827
- end
828
- end
829
-
830
-
831
- #--------------------------------------------------------------
832
-
833
- module EventMachine
834
- class EvmaUNIXServer < Selectable
835
-
836
- # TODO, refactor and unify with EvmaTCPServer.
837
-
838
- class << self
839
- # Versions of ruby 1.8.4 later than May 26 2006 will work properly
840
- # with an object of type TCPServer. Prior versions won't so we
841
- # play it safe and just build a socket.
842
- #
843
- def start_server chain
844
- sd = Socket.new( Socket::AF_LOCAL, Socket::SOCK_STREAM, 0 )
845
- sd.setsockopt( Socket::SOL_SOCKET, Socket::SO_REUSEADDR, true )
846
- sd.bind( Socket.pack_sockaddr_un( chain ))
847
- sd.listen( 50 ) # 5 is what you see in all the books. Ain't enough.
848
- EvmaUNIXServer.new sd
849
- end
850
- end
851
-
852
- def initialize io
853
- super io
854
- end
855
-
856
-
857
- def select_for_reading?
858
- true
859
- end
860
-
861
- #--
862
- # accept_nonblock returns an array consisting of the accepted
863
- # socket and a sockaddr_in which names the peer.
864
- # Don't accept more than 10 at a time.
865
- def eventable_read
866
- begin
867
- 10.times {
868
- descriptor,peername = io.accept_nonblock
869
- sd = StreamObject.new descriptor
870
- EventMachine::event_callback uuid, ConnectionAccepted, sd.uuid
871
- }
872
- rescue Errno::EWOULDBLOCK, Errno::EAGAIN
873
- end
874
- end
875
-
876
- #--
877
- #
878
- def schedule_close
879
- @close_scheduled = true
880
- end
881
-
882
- end
883
- end
884
-
885
-
886
-
887
- #--------------------------------------------------------------
888
-
889
- module EventMachine
890
- class LoopbreakReader < Selectable
891
-
892
- def select_for_reading?
893
- true
894
- end
895
-
896
- def eventable_read
897
- io.sysread(128)
898
- EventMachine::event_callback "", LoopbreakSignalled, ""
899
- end
900
-
901
- end
902
- end
903
-
904
- #--------------------------------------------------------------
905
-
906
-
907
- module EventMachine
908
-
909
- class DatagramObject < Selectable
910
- def initialize io
911
- super io
912
- @outbound_q = []
913
- end
914
-
915
- # #send_datagram
916
- def send_datagram data, target
917
- # TODO, coalesce here perhaps by being smarter about appending to @outbound_q.last?
918
- unless @close_scheduled or @close_requested
919
- @outbound_q << [data.to_s, target]
920
- end
921
- end
922
-
923
- # #select_for_writing?
924
- def select_for_writing?
925
- unless @close_scheduled
926
- if @outbound_q.empty?
927
- @close_scheduled = true if @close_requested
928
- false
929
- else
930
- true
931
- end
932
- end
933
- end
934
-
935
- # #select_for_reading?
936
- def select_for_reading?
937
- true
938
- end
939
-
940
- # #get_outbound_data_size
941
- def get_outbound_data_size
942
- @outbound_q.inject(0) {|memo,obj| memo += (obj || "").length}
943
- end
944
-
945
-
946
- end
947
-
948
-
949
- end
950
-
951
-
952
- #--------------------------------------------------------------
953
-
954
- module EventMachine
955
- class EvmaUDPSocket < DatagramObject
956
-
957
- class << self
958
- def create host, port
959
- sd = Socket.new( Socket::AF_INET, Socket::SOCK_DGRAM, 0 )
960
- sd.bind Socket::pack_sockaddr_in( port, host )
961
- EvmaUDPSocket.new sd
962
- end
963
- end
964
-
965
- # #eventable_write
966
- # This really belongs in DatagramObject, but there is some UDP-specific stuff.
967
- def eventable_write
968
- 40.times {
969
- break if @outbound_q.empty?
970
- begin
971
- data,target = @outbound_q.first
972
-
973
- # This damn better be nonblocking.
974
- io.send data.to_s, 0, target
975
-
976
- @outbound_q.shift
977
- rescue Errno::EAGAIN
978
- # It's not been observed in testing that we ever get here.
979
- # True to the definition, packets will be accepted and quietly dropped
980
- # if the system is under pressure.
981
- break
982
- rescue EOFError, Errno::ECONNRESET
983
- @close_scheduled = true
984
- @outbound_q.clear
985
- end
986
- }
987
- end
988
-
989
- # Proper nonblocking I/O was added to Ruby 1.8.4 in May 2006.
990
- # If we have it, then we can read multiple times safely to improve
991
- # performance.
992
- def eventable_read
993
- begin
994
- if io.respond_to?(:recvfrom_nonblock)
995
- 40.times {
996
- data,@return_address = io.recvfrom_nonblock(16384)
997
- EventMachine::event_callback uuid, ConnectionData, data
998
- @return_address = nil
999
- }
1000
- else
1001
- raise "unimplemented datagram-read operation on this Ruby"
1002
- end
1003
- rescue Errno::EAGAIN
1004
- # no-op
1005
- rescue Errno::ECONNRESET, EOFError
1006
- @close_scheduled = true
1007
- EventMachine::event_callback uuid, ConnectionUnbound, nil
1008
- end
1009
-
1010
- end
1011
-
1012
-
1013
- def send_data data
1014
- send_datagram data, @return_address
1015
- end
1016
-
1017
- end
1018
- end
1019
-
1020
- #--------------------------------------------------------------
1021
-
1022
-