eventmachine 1.0.0.beta.2-x86-mingw32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (141) hide show
  1. data/.gitignore +16 -0
  2. data/Gemfile +1 -0
  3. data/README +81 -0
  4. data/Rakefile +11 -0
  5. data/docs/COPYING +60 -0
  6. data/docs/ChangeLog +211 -0
  7. data/docs/DEFERRABLES +246 -0
  8. data/docs/EPOLL +141 -0
  9. data/docs/GNU +281 -0
  10. data/docs/INSTALL +13 -0
  11. data/docs/KEYBOARD +42 -0
  12. data/docs/LEGAL +25 -0
  13. data/docs/LIGHTWEIGHT_CONCURRENCY +130 -0
  14. data/docs/PURE_RUBY +75 -0
  15. data/docs/RELEASE_NOTES +94 -0
  16. data/docs/SMTP +4 -0
  17. data/docs/SPAWNED_PROCESSES +148 -0
  18. data/docs/TODO +8 -0
  19. data/eventmachine.gemspec +33 -0
  20. data/examples/ex_channel.rb +43 -0
  21. data/examples/ex_queue.rb +2 -0
  22. data/examples/ex_tick_loop_array.rb +15 -0
  23. data/examples/ex_tick_loop_counter.rb +32 -0
  24. data/examples/helper.rb +2 -0
  25. data/ext/binder.cpp +124 -0
  26. data/ext/binder.h +46 -0
  27. data/ext/cmain.cpp +838 -0
  28. data/ext/ed.cpp +1884 -0
  29. data/ext/ed.h +418 -0
  30. data/ext/em.cpp +2348 -0
  31. data/ext/em.h +228 -0
  32. data/ext/eventmachine.h +123 -0
  33. data/ext/extconf.rb +157 -0
  34. data/ext/fastfilereader/extconf.rb +85 -0
  35. data/ext/fastfilereader/mapper.cpp +214 -0
  36. data/ext/fastfilereader/mapper.h +59 -0
  37. data/ext/fastfilereader/rubymain.cpp +127 -0
  38. data/ext/kb.cpp +79 -0
  39. data/ext/page.cpp +107 -0
  40. data/ext/page.h +51 -0
  41. data/ext/pipe.cpp +347 -0
  42. data/ext/project.h +155 -0
  43. data/ext/rubymain.cpp +1200 -0
  44. data/ext/ssl.cpp +460 -0
  45. data/ext/ssl.h +94 -0
  46. data/java/.classpath +8 -0
  47. data/java/.project +17 -0
  48. data/java/src/com/rubyeventmachine/EmReactor.java +571 -0
  49. data/java/src/com/rubyeventmachine/EmReactorException.java +40 -0
  50. data/java/src/com/rubyeventmachine/EventableChannel.java +69 -0
  51. data/java/src/com/rubyeventmachine/EventableDatagramChannel.java +189 -0
  52. data/java/src/com/rubyeventmachine/EventableSocketChannel.java +364 -0
  53. data/lib/em/buftok.rb +138 -0
  54. data/lib/em/callback.rb +26 -0
  55. data/lib/em/channel.rb +57 -0
  56. data/lib/em/connection.rb +569 -0
  57. data/lib/em/deferrable.rb +206 -0
  58. data/lib/em/file_watch.rb +54 -0
  59. data/lib/em/future.rb +61 -0
  60. data/lib/em/iterator.rb +270 -0
  61. data/lib/em/messages.rb +66 -0
  62. data/lib/em/process_watch.rb +44 -0
  63. data/lib/em/processes.rb +119 -0
  64. data/lib/em/protocols.rb +36 -0
  65. data/lib/em/protocols/header_and_content.rb +138 -0
  66. data/lib/em/protocols/httpclient.rb +268 -0
  67. data/lib/em/protocols/httpclient2.rb +590 -0
  68. data/lib/em/protocols/line_and_text.rb +125 -0
  69. data/lib/em/protocols/line_protocol.rb +28 -0
  70. data/lib/em/protocols/linetext2.rb +161 -0
  71. data/lib/em/protocols/memcache.rb +323 -0
  72. data/lib/em/protocols/object_protocol.rb +45 -0
  73. data/lib/em/protocols/postgres3.rb +247 -0
  74. data/lib/em/protocols/saslauth.rb +175 -0
  75. data/lib/em/protocols/smtpclient.rb +357 -0
  76. data/lib/em/protocols/smtpserver.rb +640 -0
  77. data/lib/em/protocols/socks4.rb +66 -0
  78. data/lib/em/protocols/stomp.rb +200 -0
  79. data/lib/em/protocols/tcptest.rb +53 -0
  80. data/lib/em/pure_ruby.rb +1013 -0
  81. data/lib/em/queue.rb +62 -0
  82. data/lib/em/spawnable.rb +85 -0
  83. data/lib/em/streamer.rb +130 -0
  84. data/lib/em/tick_loop.rb +85 -0
  85. data/lib/em/timers.rb +57 -0
  86. data/lib/em/version.rb +3 -0
  87. data/lib/eventmachine.rb +1548 -0
  88. data/lib/jeventmachine.rb +258 -0
  89. data/lib/rubyeventmachine.rb +2 -0
  90. data/setup.rb +1585 -0
  91. data/tasks/cpp.rake_example +77 -0
  92. data/tasks/doc.rake +30 -0
  93. data/tasks/package.rake +85 -0
  94. data/tasks/test.rake +6 -0
  95. data/tests/client.crt +31 -0
  96. data/tests/client.key +51 -0
  97. data/tests/test_attach.rb +136 -0
  98. data/tests/test_basic.rb +249 -0
  99. data/tests/test_channel.rb +64 -0
  100. data/tests/test_connection_count.rb +35 -0
  101. data/tests/test_defer.rb +49 -0
  102. data/tests/test_deferrable.rb +35 -0
  103. data/tests/test_epoll.rb +160 -0
  104. data/tests/test_error_handler.rb +35 -0
  105. data/tests/test_errors.rb +82 -0
  106. data/tests/test_exc.rb +55 -0
  107. data/tests/test_file_watch.rb +49 -0
  108. data/tests/test_futures.rb +198 -0
  109. data/tests/test_get_sock_opt.rb +30 -0
  110. data/tests/test_handler_check.rb +37 -0
  111. data/tests/test_hc.rb +190 -0
  112. data/tests/test_httpclient.rb +227 -0
  113. data/tests/test_httpclient2.rb +154 -0
  114. data/tests/test_inactivity_timeout.rb +50 -0
  115. data/tests/test_kb.rb +60 -0
  116. data/tests/test_ltp.rb +190 -0
  117. data/tests/test_ltp2.rb +317 -0
  118. data/tests/test_next_tick.rb +133 -0
  119. data/tests/test_object_protocol.rb +37 -0
  120. data/tests/test_pause.rb +70 -0
  121. data/tests/test_pending_connect_timeout.rb +48 -0
  122. data/tests/test_process_watch.rb +50 -0
  123. data/tests/test_processes.rb +128 -0
  124. data/tests/test_proxy_connection.rb +144 -0
  125. data/tests/test_pure.rb +134 -0
  126. data/tests/test_queue.rb +44 -0
  127. data/tests/test_running.rb +42 -0
  128. data/tests/test_sasl.rb +72 -0
  129. data/tests/test_send_file.rb +251 -0
  130. data/tests/test_servers.rb +76 -0
  131. data/tests/test_smtpclient.rb +83 -0
  132. data/tests/test_smtpserver.rb +85 -0
  133. data/tests/test_spawn.rb +322 -0
  134. data/tests/test_ssl_args.rb +79 -0
  135. data/tests/test_ssl_methods.rb +50 -0
  136. data/tests/test_ssl_verify.rb +82 -0
  137. data/tests/test_tick_loop.rb +59 -0
  138. data/tests/test_timers.rb +160 -0
  139. data/tests/test_ud.rb +36 -0
  140. data/tests/testem.rb +31 -0
  141. metadata +240 -0
@@ -0,0 +1,45 @@
1
+ module EventMachine
2
+ module Protocols
3
+ # ObjectProtocol allows for easy communication using marshaled ruby objects
4
+ #
5
+ # module RubyServer
6
+ # include EM::P::ObjectProtocol
7
+ #
8
+ # def receive_object obj
9
+ # send_object({'you said' => obj})
10
+ # end
11
+ # end
12
+ #
13
+ module ObjectProtocol
14
+ # By default returns Marshal, override to return JSON or YAML, or any
15
+ # other serializer/deserializer responding to #dump and #load.
16
+ def serializer
17
+ Marshal
18
+ end
19
+
20
+ def receive_data data # :nodoc:
21
+ (@buf ||= '') << data
22
+
23
+ while @buf.size >= 4
24
+ if @buf.size >= 4+(size=@buf.unpack('N').first)
25
+ @buf.slice!(0,4)
26
+ receive_object serializer.load(@buf.slice!(0,size))
27
+ else
28
+ break
29
+ end
30
+ end
31
+ end
32
+
33
+ # Invoked with ruby objects received over the network
34
+ def receive_object obj
35
+ # stub
36
+ end
37
+
38
+ # Sends a ruby object over the network
39
+ def send_object obj
40
+ data = serializer.dump(obj)
41
+ send_data [data.respond_to?(:bytesize) ? data.bytesize : data.size, data].pack('Na*')
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,247 @@
1
+ #--
2
+ #
3
+ # Author:: Francis Cianfrocca (gmail: blackhedd)
4
+ # Homepage:: http://rubyeventmachine.com
5
+ # Date:: 15 November 2006
6
+ #
7
+ # See EventMachine and EventMachine::Connection for documentation and
8
+ # usage examples.
9
+ #
10
+ #----------------------------------------------------------------------------
11
+ #
12
+ # Copyright (C) 2006-08 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
+
27
+ require 'readbytes'
28
+ require 'postgres-pr/message'
29
+ require 'postgres-pr/connection'
30
+ require 'stringio'
31
+
32
+ class StringIO # :nodoc:
33
+ # Reads exactly +n+ bytes.
34
+ #
35
+ # If the data read is nil an EOFError is raised.
36
+ #
37
+ # If the data read is too short a TruncatedDataError is raised and the read
38
+ # data is obtainable via its #data method.
39
+ def readbytes(n)
40
+ str = read(n)
41
+ if str == nil
42
+ raise EOFError, "End of file reached"
43
+ end
44
+ if str.size < n
45
+ raise TruncatedDataError.new("data truncated", str)
46
+ end
47
+ str
48
+ end
49
+ alias read_exactly_n_bytes readbytes
50
+ end
51
+
52
+
53
+ module EventMachine
54
+ module Protocols
55
+ # PROVISIONAL IMPLEMENTATION of an evented Postgres client.
56
+ # This implements version 3 of the Postgres wire protocol, which will work
57
+ # with any Postgres version from roughly 7.4 onward.
58
+ #
59
+ # Objective: we want to access Postgres databases without requiring threads.
60
+ # Until now this has been a problem because the Postgres client implementations
61
+ # have all made use of blocking I/O calls, which is incompatible with a
62
+ # thread-free evented model.
63
+ #
64
+ # But rather than re-implement the Postgres Wire3 protocol, we're taking advantage
65
+ # of the existing postgres-pr library, which was originally written by Michael
66
+ # Neumann but (at this writing) appears to be no longer maintained. Still, it's
67
+ # in basically a production-ready state, and the wire protocol isn't that complicated
68
+ # anyway.
69
+ #
70
+ # We need to monkeypatch StringIO because it lacks the #readbytes method needed
71
+ # by postgres-pr.
72
+ #
73
+ # We're tucking in a bunch of require statements that may not be present in garden-variety
74
+ # EM installations. Until we find a good way to only require these if a program
75
+ # requires postgres, this file will need to be required explicitly.
76
+ #
77
+ # The StringIO monkeypatch is lifted verbatim from the standard library readbytes.rb,
78
+ # which adds method #readbytes directly to class IO. But StringIO is not a subclass of IO.
79
+ #
80
+ # We cloned the handling of postgres messages from lib/postgres-pr/connection.rb
81
+ # in the postgres-pr library, and modified it for event-handling.
82
+ #
83
+ # TODO: The password handling in dispatch_conn_message is totally incomplete.
84
+ #
85
+ #
86
+ # We return Deferrables from the user-level operations surfaced by this interface.
87
+ # Experimentally, we're using the pattern of always returning a boolean value as the
88
+ # first argument of a deferrable callback to indicate success or failure. This is
89
+ # instead of the traditional pattern of calling Deferrable#succeed or #fail, and
90
+ # requiring the user to define both a callback and an errback function.
91
+ #
92
+ # === Usage
93
+ # EM.run {
94
+ # db = EM.connect_unix_domain( "/tmp/.s.PGSQL.5432", EM::P::Postgres3 )
95
+ # db.connect( dbname, username, psw ).callback do |status|
96
+ # if status
97
+ # db.query( "select * from some_table" ).callback do |status, result, errors|
98
+ # if status
99
+ # result.rows.each do |row|
100
+ # p row
101
+ # end
102
+ # end
103
+ # end
104
+ # end
105
+ # end
106
+ # }
107
+ class Postgres3 < EventMachine::Connection
108
+ include PostgresPR
109
+
110
+ def initialize
111
+ @data = ""
112
+ @params = {}
113
+ end
114
+
115
+ def connect db, user, psw=nil
116
+ d = EM::DefaultDeferrable.new
117
+ d.timeout 15
118
+
119
+ if @pending_query || @pending_conn
120
+ d.succeed false, "Operation already in progress"
121
+ else
122
+ @pending_conn = d
123
+ prms = {"user"=>user, "database"=>db}
124
+ @user = user
125
+ if psw
126
+ @password = psw
127
+ #prms["password"] = psw
128
+ end
129
+ send_data PostgresPR::StartupMessage.new( 3 << 16, prms ).dump
130
+ end
131
+
132
+ d
133
+ end
134
+
135
+ def query sql
136
+ d = EM::DefaultDeferrable.new
137
+ d.timeout 15
138
+
139
+ if @pending_query || @pending_conn
140
+ d.succeed false, "Operation already in progress"
141
+ else
142
+ @r = PostgresPR::Connection::Result.new
143
+ @e = []
144
+ @pending_query = d
145
+ send_data PostgresPR::Query.dump(sql)
146
+ end
147
+
148
+ d
149
+ end
150
+
151
+
152
+ def receive_data data
153
+ @data << data
154
+ while @data.length >= 5
155
+ pktlen = @data[1...5].unpack("N").first
156
+ if @data.length >= (1 + pktlen)
157
+ pkt = @data.slice!(0...(1+pktlen))
158
+ m = StringIO.open( pkt, "r" ) {|io| PostgresPR::Message.read( io ) }
159
+ if @pending_conn
160
+ dispatch_conn_message m
161
+ elsif @pending_query
162
+ dispatch_query_message m
163
+ else
164
+ raise "Unexpected message from database"
165
+ end
166
+ else
167
+ break # very important, break out of the while
168
+ end
169
+ end
170
+ end
171
+
172
+
173
+ def unbind
174
+ if o = (@pending_query || @pending_conn)
175
+ o.succeed false, "lost connection"
176
+ end
177
+ end
178
+
179
+ # Cloned and modified from the postgres-pr.
180
+ def dispatch_conn_message msg
181
+ case msg
182
+ when AuthentificationClearTextPassword
183
+ raise ArgumentError, "no password specified" if @password.nil?
184
+ send_data PasswordMessage.new(@password).dump
185
+
186
+ when AuthentificationCryptPassword
187
+ raise ArgumentError, "no password specified" if @password.nil?
188
+ send_data PasswordMessage.new(@password.crypt(msg.salt)).dump
189
+
190
+ when AuthentificationMD5Password
191
+ raise ArgumentError, "no password specified" if @password.nil?
192
+ require 'digest/md5'
193
+
194
+ m = Digest::MD5.hexdigest(@password + @user)
195
+ m = Digest::MD5.hexdigest(m + msg.salt)
196
+ m = 'md5' + m
197
+ send_data PasswordMessage.new(m).dump
198
+
199
+ when AuthentificationKerberosV4, AuthentificationKerberosV5, AuthentificationSCMCredential
200
+ raise "unsupported authentification"
201
+
202
+ when AuthentificationOk
203
+ when ErrorResponse
204
+ raise msg.field_values.join("\t")
205
+ when NoticeResponse
206
+ @notice_processor.call(msg) if @notice_processor
207
+ when ParameterStatus
208
+ @params[msg.key] = msg.value
209
+ when BackendKeyData
210
+ # TODO
211
+ #p msg
212
+ when ReadyForQuery
213
+ # TODO: use transaction status
214
+ pc,@pending_conn = @pending_conn,nil
215
+ pc.succeed true
216
+ else
217
+ raise "unhandled message type"
218
+ end
219
+ end
220
+
221
+ # Cloned and modified from the postgres-pr.
222
+ def dispatch_query_message msg
223
+ case msg
224
+ when DataRow
225
+ @r.rows << msg.columns
226
+ when CommandComplete
227
+ @r.cmd_tag = msg.cmd_tag
228
+ when ReadyForQuery
229
+ pq,@pending_query = @pending_query,nil
230
+ pq.succeed true, @r, @e
231
+ when RowDescription
232
+ @r.fields = msg.fields
233
+ when CopyInResponse
234
+ when CopyOutResponse
235
+ when EmptyQueryResponse
236
+ when ErrorResponse
237
+ # TODO
238
+ @e << msg
239
+ when NoticeResponse
240
+ @notice_processor.call(msg) if @notice_processor
241
+ else
242
+ # TODO
243
+ end
244
+ end
245
+ end
246
+ end
247
+ end
@@ -0,0 +1,175 @@
1
+ #--
2
+ #
3
+ # Author:: Francis Cianfrocca (gmail: blackhedd)
4
+ # Homepage:: http://rubyeventmachine.com
5
+ # Date:: 15 November 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
+
27
+ module EventMachine
28
+ module Protocols
29
+
30
+ # Implements SASL authd.
31
+ # This is a very, very simple protocol that mimics the one used
32
+ # by saslauthd and pwcheck, two outboard daemons included in the
33
+ # standard SASL library distro.
34
+ # The only thing this is really suitable for is SASL PLAIN
35
+ # (user+password) authentication, but the SASL libs that are
36
+ # linked into standard servers (like imapd and sendmail) implement
37
+ # the other ones.
38
+ #
39
+ # SASL-auth is intended for reasonably fast operation inside a
40
+ # single machine, so it has no transport-security (although there
41
+ # have been multi-machine extensions incorporating transport-layer
42
+ # encryption).
43
+ #
44
+ # The standard saslauthd module generally runs privileged and does
45
+ # its work by referring to the system-account files.
46
+ #
47
+ # This feature was added to EventMachine to enable the development
48
+ # of custom authentication/authorization engines for standard servers.
49
+ #
50
+ # To use SASLauth, include it in a class that subclasses EM::Connection,
51
+ # and reimplement the validate method.
52
+ #
53
+ # The typical way to incorporate this module into an authentication
54
+ # daemon would be to set it as the handler for a UNIX-domain socket.
55
+ # The code might look like this:
56
+ #
57
+ # EM.start_unix_domain_server( "/var/run/saslauthd/mux", MyHandler )
58
+ # File.chmod( 0777, "/var/run/saslauthd/mux")
59
+ #
60
+ # The chmod is probably needed to ensure that unprivileged clients can
61
+ # access the UNIX-domain socket.
62
+ #
63
+ # It's also a very good idea to drop superuser privileges (if any), after
64
+ # the UNIX-domain socket has been opened.
65
+ #--
66
+ # Implementation details: assume the client can send us pipelined requests,
67
+ # and that the client will close the connection.
68
+ #
69
+ # The client sends us four values, each encoded as a two-byte length field in
70
+ # network order followed by the specified number of octets.
71
+ # The fields specify the username, password, service name (such as imap),
72
+ # and the "realm" name. We send back the barest minimum reply, a single
73
+ # field also encoded as a two-octet length in network order, followed by
74
+ # either "NO" or "OK" - simplicity itself.
75
+ #
76
+ # We enforce a maximum field size just as a sanity check.
77
+ # We do NOT automatically time out the connection.
78
+ #
79
+ # The code we use to parse out the values is ugly and probably slow.
80
+ # Improvements welcome.
81
+ #
82
+ module SASLauth
83
+
84
+ MaxFieldSize = 128*1024
85
+ def post_init
86
+ super
87
+ @sasl_data = ""
88
+ @sasl_values = []
89
+ end
90
+
91
+ def receive_data data
92
+ @sasl_data << data
93
+ while @sasl_data.length >= 2
94
+ len = (@sasl_data[0,2].unpack("n")).first
95
+ raise "SASL Max Field Length exceeded" if len > MaxFieldSize
96
+ if @sasl_data.length >= (len + 2)
97
+ @sasl_values << @sasl_data[2,len]
98
+ @sasl_data.slice!(0...(2+len))
99
+ if @sasl_values.length == 4
100
+ send_data( validate(*@sasl_values) ? "\0\002OK" : "\0\002NO" )
101
+ @sasl_values.clear
102
+ end
103
+ else
104
+ break
105
+ end
106
+ end
107
+ end
108
+
109
+ def validate username, psw, sysname, realm
110
+ p username
111
+ p psw
112
+ p sysname
113
+ p realm
114
+ true
115
+ end
116
+ end
117
+
118
+ # Implements the SASL authd client protocol.
119
+ # This is a very, very simple protocol that mimics the one used
120
+ # by saslauthd and pwcheck, two outboard daemons included in the
121
+ # standard SASL library distro.
122
+ # The only thing this is really suitable for is SASL PLAIN
123
+ # (user+password) authentication, but the SASL libs that are
124
+ # linked into standard servers (like imapd and sendmail) implement
125
+ # the other ones.
126
+ #
127
+ # You can use this module directly as a handler for EM Connections,
128
+ # or include it in a module or handler class of your own.
129
+ #
130
+ # First connect to a SASL server (it's probably a TCP server, or more
131
+ # likely a Unix-domain socket). Then call the #validate? method,
132
+ # passing at least a username and a password. #validate? returns
133
+ # a Deferrable which will either succeed or fail, depending
134
+ # on the status of the authentication operation.
135
+ #
136
+ module SASLauthclient
137
+ MaxFieldSize = 128*1024
138
+
139
+ def validate? username, psw, sysname=nil, realm=nil
140
+
141
+ str = [username, psw, sysname, realm].map {|m|
142
+ [(m || "").length, (m || "")]
143
+ }.flatten.pack( "nA*" * 4 )
144
+ send_data str
145
+
146
+ d = EM::DefaultDeferrable.new
147
+ @queries.unshift d
148
+ d
149
+ end
150
+
151
+ def post_init
152
+ @sasl_data = ""
153
+ @queries = []
154
+ end
155
+
156
+ def receive_data data
157
+ @sasl_data << data
158
+
159
+ while @sasl_data.length > 2
160
+ len = (@sasl_data[0,2].unpack("n")).first
161
+ raise "SASL Max Field Length exceeded" if len > MaxFieldSize
162
+ if @sasl_data.length >= (len + 2)
163
+ val = @sasl_data[2,len]
164
+ @sasl_data.slice!(0...(2+len))
165
+ q = @queries.pop
166
+ (val == "NO") ? q.fail : q.succeed
167
+ else
168
+ break
169
+ end
170
+ end
171
+ end
172
+ end
173
+
174
+ end
175
+ end