eventmachine-le 1.1.0.beta.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (129) hide show
  1. data/.gitignore +21 -0
  2. data/.yardopts +7 -0
  3. data/GNU +281 -0
  4. data/LICENSE +60 -0
  5. data/README.md +80 -0
  6. data/Rakefile +19 -0
  7. data/eventmachine-le.gemspec +42 -0
  8. data/ext/binder.cpp +124 -0
  9. data/ext/binder.h +46 -0
  10. data/ext/cmain.cpp +841 -0
  11. data/ext/ed.cpp +1995 -0
  12. data/ext/ed.h +424 -0
  13. data/ext/em.cpp +2377 -0
  14. data/ext/em.h +243 -0
  15. data/ext/eventmachine.h +126 -0
  16. data/ext/extconf.rb +166 -0
  17. data/ext/fastfilereader/extconf.rb +94 -0
  18. data/ext/fastfilereader/mapper.cpp +214 -0
  19. data/ext/fastfilereader/mapper.h +59 -0
  20. data/ext/fastfilereader/rubymain.cpp +127 -0
  21. data/ext/kb.cpp +79 -0
  22. data/ext/page.cpp +107 -0
  23. data/ext/page.h +51 -0
  24. data/ext/pipe.cpp +347 -0
  25. data/ext/project.h +155 -0
  26. data/ext/rubymain.cpp +1269 -0
  27. data/ext/ssl.cpp +468 -0
  28. data/ext/ssl.h +94 -0
  29. data/lib/em/buftok.rb +110 -0
  30. data/lib/em/callback.rb +58 -0
  31. data/lib/em/channel.rb +64 -0
  32. data/lib/em/completion.rb +304 -0
  33. data/lib/em/connection.rb +728 -0
  34. data/lib/em/deferrable.rb +210 -0
  35. data/lib/em/deferrable/pool.rb +2 -0
  36. data/lib/em/file_watch.rb +73 -0
  37. data/lib/em/future.rb +61 -0
  38. data/lib/em/iterator.rb +313 -0
  39. data/lib/em/messages.rb +66 -0
  40. data/lib/em/pool.rb +151 -0
  41. data/lib/em/process_watch.rb +45 -0
  42. data/lib/em/processes.rb +123 -0
  43. data/lib/em/protocols.rb +37 -0
  44. data/lib/em/protocols/header_and_content.rb +138 -0
  45. data/lib/em/protocols/httpclient.rb +279 -0
  46. data/lib/em/protocols/httpclient2.rb +600 -0
  47. data/lib/em/protocols/line_and_text.rb +125 -0
  48. data/lib/em/protocols/line_protocol.rb +29 -0
  49. data/lib/em/protocols/linetext2.rb +161 -0
  50. data/lib/em/protocols/memcache.rb +331 -0
  51. data/lib/em/protocols/object_protocol.rb +46 -0
  52. data/lib/em/protocols/postgres3.rb +246 -0
  53. data/lib/em/protocols/saslauth.rb +175 -0
  54. data/lib/em/protocols/smtpclient.rb +365 -0
  55. data/lib/em/protocols/smtpserver.rb +663 -0
  56. data/lib/em/protocols/socks4.rb +66 -0
  57. data/lib/em/protocols/stomp.rb +202 -0
  58. data/lib/em/protocols/tcptest.rb +54 -0
  59. data/lib/em/queue.rb +71 -0
  60. data/lib/em/resolver.rb +195 -0
  61. data/lib/em/spawnable.rb +84 -0
  62. data/lib/em/streamer.rb +118 -0
  63. data/lib/em/threaded_resource.rb +90 -0
  64. data/lib/em/tick_loop.rb +85 -0
  65. data/lib/em/timers.rb +106 -0
  66. data/lib/em/version.rb +3 -0
  67. data/lib/eventmachine-le.rb +10 -0
  68. data/lib/eventmachine.rb +1548 -0
  69. data/rakelib/cpp.rake_example +77 -0
  70. data/rakelib/package.rake +98 -0
  71. data/rakelib/test.rake +8 -0
  72. data/tests/client.crt +31 -0
  73. data/tests/client.key +51 -0
  74. data/tests/em_test_helper.rb +143 -0
  75. data/tests/test_attach.rb +148 -0
  76. data/tests/test_basic.rb +294 -0
  77. data/tests/test_channel.rb +62 -0
  78. data/tests/test_completion.rb +177 -0
  79. data/tests/test_connection_count.rb +33 -0
  80. data/tests/test_defer.rb +18 -0
  81. data/tests/test_deferrable.rb +35 -0
  82. data/tests/test_epoll.rb +134 -0
  83. data/tests/test_error_handler.rb +38 -0
  84. data/tests/test_exc.rb +28 -0
  85. data/tests/test_file_watch.rb +65 -0
  86. data/tests/test_futures.rb +170 -0
  87. data/tests/test_get_sock_opt.rb +37 -0
  88. data/tests/test_handler_check.rb +35 -0
  89. data/tests/test_hc.rb +155 -0
  90. data/tests/test_httpclient.rb +190 -0
  91. data/tests/test_httpclient2.rb +128 -0
  92. data/tests/test_inactivity_timeout.rb +54 -0
  93. data/tests/test_ipv4.rb +125 -0
  94. data/tests/test_ipv6.rb +131 -0
  95. data/tests/test_iterator.rb +110 -0
  96. data/tests/test_kb.rb +34 -0
  97. data/tests/test_line_protocol.rb +33 -0
  98. data/tests/test_ltp.rb +138 -0
  99. data/tests/test_ltp2.rb +288 -0
  100. data/tests/test_next_tick.rb +104 -0
  101. data/tests/test_object_protocol.rb +36 -0
  102. data/tests/test_pause.rb +78 -0
  103. data/tests/test_pending_connect_timeout.rb +52 -0
  104. data/tests/test_pool.rb +196 -0
  105. data/tests/test_process_watch.rb +48 -0
  106. data/tests/test_processes.rb +133 -0
  107. data/tests/test_proxy_connection.rb +168 -0
  108. data/tests/test_pure.rb +88 -0
  109. data/tests/test_queue.rb +50 -0
  110. data/tests/test_resolver.rb +55 -0
  111. data/tests/test_running.rb +14 -0
  112. data/tests/test_sasl.rb +47 -0
  113. data/tests/test_send_file.rb +217 -0
  114. data/tests/test_servers.rb +33 -0
  115. data/tests/test_set_sock_opt.rb +41 -0
  116. data/tests/test_shutdown_hooks.rb +23 -0
  117. data/tests/test_smtpclient.rb +55 -0
  118. data/tests/test_smtpserver.rb +120 -0
  119. data/tests/test_spawn.rb +293 -0
  120. data/tests/test_ssl_args.rb +78 -0
  121. data/tests/test_ssl_methods.rb +48 -0
  122. data/tests/test_ssl_verify.rb +82 -0
  123. data/tests/test_threaded_resource.rb +55 -0
  124. data/tests/test_tick_loop.rb +59 -0
  125. data/tests/test_timers.rb +180 -0
  126. data/tests/test_ud.rb +8 -0
  127. data/tests/test_udp46.rb +53 -0
  128. data/tests/test_unbind_reason.rb +48 -0
  129. metadata +390 -0
@@ -0,0 +1,46 @@
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
+ # @private
21
+ def receive_data data
22
+ (@buf ||= '') << data
23
+
24
+ while @buf.size >= 4
25
+ if @buf.size >= 4+(size=@buf.unpack('N').first)
26
+ @buf.slice!(0,4)
27
+ receive_object serializer.load(@buf.slice!(0,size))
28
+ else
29
+ break
30
+ end
31
+ end
32
+ end
33
+
34
+ # Invoked with ruby objects received over the network
35
+ def receive_object obj
36
+ # stub
37
+ end
38
+
39
+ # Sends a ruby object over the network
40
+ def send_object obj
41
+ data = serializer.dump(obj)
42
+ send_data [data.respond_to?(:bytesize) ? data.bytesize : data.size, data].pack('Na*')
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,246 @@
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 'postgres-pr/message'
28
+ require 'postgres-pr/connection'
29
+ require 'stringio'
30
+
31
+ # @private
32
+ class StringIO
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 an IOError is raised
38
+ def readbytes(n)
39
+ str = read(n)
40
+ if str == nil
41
+ raise EOFError, "End of file reached"
42
+ end
43
+ if str.size < n
44
+ raise IOError, "data truncated"
45
+ end
46
+ str
47
+ end
48
+ alias read_exactly_n_bytes readbytes
49
+ end
50
+
51
+
52
+ module EventMachine
53
+ module Protocols
54
+ # PROVISIONAL IMPLEMENTATION of an evented Postgres client.
55
+ # This implements version 3 of the Postgres wire protocol, which will work
56
+ # with any Postgres version from roughly 7.4 onward.
57
+ #
58
+ # Objective: we want to access Postgres databases without requiring threads.
59
+ # Until now this has been a problem because the Postgres client implementations
60
+ # have all made use of blocking I/O calls, which is incompatible with a
61
+ # thread-free evented model.
62
+ #
63
+ # But rather than re-implement the Postgres Wire3 protocol, we're taking advantage
64
+ # of the existing postgres-pr library, which was originally written by Michael
65
+ # Neumann but (at this writing) appears to be no longer maintained. Still, it's
66
+ # in basically a production-ready state, and the wire protocol isn't that complicated
67
+ # anyway.
68
+ #
69
+ # We're tucking in a bunch of require statements that may not be present in garden-variety
70
+ # EM installations. Until we find a good way to only require these if a program
71
+ # requires postgres, this file will need to be required explicitly.
72
+ #
73
+ # We need to monkeypatch StringIO because it lacks the #readbytes method needed
74
+ # by postgres-pr.
75
+ # The StringIO monkeypatch is lifted from the standard library readbytes.rb,
76
+ # which adds method #readbytes directly to class IO. But StringIO is not a subclass of IO.
77
+ # It is modified to raise an IOError instead of TruncatedDataException since the exception is unused.
78
+ #
79
+ # We cloned the handling of postgres messages from lib/postgres-pr/connection.rb
80
+ # in the postgres-pr library, and modified it for event-handling.
81
+ #
82
+ # TODO: The password handling in dispatch_conn_message is totally incomplete.
83
+ #
84
+ #
85
+ # We return Deferrables from the user-level operations surfaced by this interface.
86
+ # Experimentally, we're using the pattern of always returning a boolean value as the
87
+ # first argument of a deferrable callback to indicate success or failure. This is
88
+ # instead of the traditional pattern of calling Deferrable#succeed or #fail, and
89
+ # requiring the user to define both a callback and an errback function.
90
+ #
91
+ # === Usage
92
+ # EM.run {
93
+ # db = EM.connect_unix_domain( "/tmp/.s.PGSQL.5432", EM::P::Postgres3 )
94
+ # db.connect( dbname, username, psw ).callback do |status|
95
+ # if status
96
+ # db.query( "select * from some_table" ).callback do |status, result, errors|
97
+ # if status
98
+ # result.rows.each do |row|
99
+ # p row
100
+ # end
101
+ # end
102
+ # end
103
+ # end
104
+ # end
105
+ # }
106
+ class Postgres3 < EventMachine::Connection
107
+ include PostgresPR
108
+
109
+ def initialize
110
+ @data = ""
111
+ @params = {}
112
+ end
113
+
114
+ def connect db, user, psw=nil
115
+ d = EM::DefaultDeferrable.new
116
+ d.timeout 15
117
+
118
+ if @pending_query || @pending_conn
119
+ d.succeed false, "Operation already in progress"
120
+ else
121
+ @pending_conn = d
122
+ prms = {"user"=>user, "database"=>db}
123
+ @user = user
124
+ if psw
125
+ @password = psw
126
+ #prms["password"] = psw
127
+ end
128
+ send_data PostgresPR::StartupMessage.new( 3 << 16, prms ).dump
129
+ end
130
+
131
+ d
132
+ end
133
+
134
+ def query sql
135
+ d = EM::DefaultDeferrable.new
136
+ d.timeout 15
137
+
138
+ if @pending_query || @pending_conn
139
+ d.succeed false, "Operation already in progress"
140
+ else
141
+ @r = PostgresPR::Connection::Result.new
142
+ @e = []
143
+ @pending_query = d
144
+ send_data PostgresPR::Query.dump(sql)
145
+ end
146
+
147
+ d
148
+ end
149
+
150
+
151
+ def receive_data data
152
+ @data << data
153
+ while @data.length >= 5
154
+ pktlen = @data[1...5].unpack("N").first
155
+ if @data.length >= (1 + pktlen)
156
+ pkt = @data.slice!(0...(1+pktlen))
157
+ m = StringIO.open( pkt, "r" ) {|io| PostgresPR::Message.read( io ) }
158
+ if @pending_conn
159
+ dispatch_conn_message m
160
+ elsif @pending_query
161
+ dispatch_query_message m
162
+ else
163
+ raise "Unexpected message from database"
164
+ end
165
+ else
166
+ break # very important, break out of the while
167
+ end
168
+ end
169
+ end
170
+
171
+
172
+ def unbind
173
+ if o = (@pending_query || @pending_conn)
174
+ o.succeed false, "lost connection"
175
+ end
176
+ end
177
+
178
+ # Cloned and modified from the postgres-pr.
179
+ def dispatch_conn_message msg
180
+ case msg
181
+ when AuthentificationClearTextPassword
182
+ raise ArgumentError, "no password specified" if @password.nil?
183
+ send_data PasswordMessage.new(@password).dump
184
+
185
+ when AuthentificationCryptPassword
186
+ raise ArgumentError, "no password specified" if @password.nil?
187
+ send_data PasswordMessage.new(@password.crypt(msg.salt)).dump
188
+
189
+ when AuthentificationMD5Password
190
+ raise ArgumentError, "no password specified" if @password.nil?
191
+ require 'digest/md5'
192
+
193
+ m = Digest::MD5.hexdigest(@password + @user)
194
+ m = Digest::MD5.hexdigest(m + msg.salt)
195
+ m = 'md5' + m
196
+ send_data PasswordMessage.new(m).dump
197
+
198
+ when AuthentificationKerberosV4, AuthentificationKerberosV5, AuthentificationSCMCredential
199
+ raise "unsupported authentification"
200
+
201
+ when AuthentificationOk
202
+ when ErrorResponse
203
+ raise msg.field_values.join("\t")
204
+ when NoticeResponse
205
+ @notice_processor.call(msg) if @notice_processor
206
+ when ParameterStatus
207
+ @params[msg.key] = msg.value
208
+ when BackendKeyData
209
+ # TODO
210
+ #p msg
211
+ when ReadyForQuery
212
+ # TODO: use transaction status
213
+ pc,@pending_conn = @pending_conn,nil
214
+ pc.succeed true
215
+ else
216
+ raise "unhandled message type"
217
+ end
218
+ end
219
+
220
+ # Cloned and modified from the postgres-pr.
221
+ def dispatch_query_message msg
222
+ case msg
223
+ when DataRow
224
+ @r.rows << msg.columns
225
+ when CommandComplete
226
+ @r.cmd_tag = msg.cmd_tag
227
+ when ReadyForQuery
228
+ pq,@pending_query = @pending_query,nil
229
+ pq.succeed true, @r, @e
230
+ when RowDescription
231
+ @r.fields = msg.fields
232
+ when CopyInResponse
233
+ when CopyOutResponse
234
+ when EmptyQueryResponse
235
+ when ErrorResponse
236
+ # TODO
237
+ @e << msg
238
+ when NoticeResponse
239
+ @notice_processor.call(msg) if @notice_processor
240
+ else
241
+ # TODO
242
+ end
243
+ end
244
+ end
245
+ end
246
+ 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