eventmachine 0.12.10-x86-mswin32-60 → 1.0.0.beta.2-x86-mswin32-60
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/Gemfile +1 -0
- data/README +80 -81
- data/Rakefile +7 -370
- data/docs/COPYING +60 -60
- data/docs/ChangeLog +211 -211
- data/docs/DEFERRABLES +246 -133
- data/docs/EPOLL +141 -141
- data/docs/GNU +281 -281
- data/docs/INSTALL +13 -13
- data/docs/KEYBOARD +42 -38
- data/docs/LEGAL +25 -25
- data/docs/LIGHTWEIGHT_CONCURRENCY +130 -70
- data/docs/PURE_RUBY +75 -75
- data/docs/RELEASE_NOTES +94 -94
- data/docs/SMTP +4 -2
- data/docs/SPAWNED_PROCESSES +148 -89
- data/docs/TODO +8 -8
- data/eventmachine.gemspec +19 -26
- data/examples/ex_channel.rb +42 -42
- data/examples/ex_queue.rb +2 -2
- data/examples/ex_tick_loop_array.rb +15 -0
- data/examples/ex_tick_loop_counter.rb +32 -0
- data/examples/helper.rb +1 -1
- data/ext/binder.cpp +0 -1
- data/ext/cmain.cpp +36 -25
- data/ext/ed.cpp +104 -113
- data/ext/ed.h +24 -30
- data/ext/em.cpp +349 -283
- data/ext/em.h +25 -29
- data/ext/eventmachine.h +5 -4
- data/ext/extconf.rb +58 -49
- data/ext/fastfilereader/extconf.rb +5 -3
- data/ext/fastfilereader/mapper.cpp +214 -214
- data/ext/fastfilereader/mapper.h +59 -59
- data/ext/fastfilereader/rubymain.cpp +127 -127
- data/ext/kb.cpp +1 -3
- data/ext/page.cpp +107 -107
- data/ext/page.h +51 -51
- data/ext/pipe.cpp +9 -11
- data/ext/project.h +12 -8
- data/ext/rubymain.cpp +138 -104
- data/java/.classpath +8 -8
- data/java/.project +17 -17
- data/java/src/com/rubyeventmachine/EmReactor.java +1 -0
- data/java/src/com/rubyeventmachine/EmReactorException.java +40 -40
- data/lib/em/buftok.rb +138 -138
- data/lib/em/callback.rb +25 -25
- data/lib/em/channel.rb +1 -1
- data/lib/em/connection.rb +6 -1
- data/lib/em/deferrable.rb +16 -2
- data/lib/em/file_watch.rb +53 -53
- data/lib/em/future.rb +61 -61
- data/lib/em/iterator.rb +270 -0
- data/lib/em/messages.rb +66 -66
- data/lib/em/process_watch.rb +43 -43
- data/lib/em/protocols.rb +1 -1
- data/lib/em/protocols/header_and_content.rb +138 -138
- data/lib/em/protocols/httpclient.rb +267 -262
- data/lib/em/protocols/line_protocol.rb +28 -0
- data/lib/em/protocols/memcache.rb +322 -322
- data/lib/em/protocols/postgres3.rb +247 -247
- data/lib/em/protocols/saslauth.rb +175 -175
- data/lib/em/protocols/smtpserver.rb +640 -547
- data/lib/em/protocols/stomp.rb +200 -200
- data/lib/em/protocols/tcptest.rb +52 -52
- data/lib/{pr_eventmachine.rb → em/pure_ruby.rb} +1013 -1022
- data/lib/em/queue.rb +1 -0
- data/lib/em/spawnable.rb +85 -85
- data/lib/em/streamer.rb +130 -130
- data/lib/em/tick_loop.rb +85 -0
- data/lib/em/timers.rb +2 -1
- data/lib/em/version.rb +1 -1
- data/lib/eventmachine.rb +40 -84
- data/lib/jeventmachine.rb +2 -1
- data/lib/rubyeventmachine.rb +2 -0
- data/setup.rb +1585 -1585
- data/tasks/doc.rake +30 -0
- data/tasks/package.rake +85 -0
- data/tasks/test.rake +6 -0
- data/tests/client.crt +31 -31
- data/tests/client.key +51 -51
- data/tests/test_attach.rb +13 -3
- data/tests/test_basic.rb +60 -95
- data/tests/test_channel.rb +3 -2
- data/tests/test_defer.rb +49 -47
- data/tests/test_deferrable.rb +35 -0
- data/tests/test_error_handler.rb +35 -35
- data/tests/test_errors.rb +82 -82
- data/tests/test_exc.rb +55 -55
- data/tests/test_file_watch.rb +49 -49
- data/tests/test_futures.rb +198 -198
- data/tests/test_handler_check.rb +36 -36
- data/tests/test_hc.rb +190 -218
- data/tests/test_httpclient.rb +227 -218
- data/tests/test_httpclient2.rb +3 -2
- data/tests/test_inactivity_timeout.rb +3 -3
- data/tests/test_kb.rb +60 -60
- data/tests/test_ltp.rb +13 -5
- data/tests/test_ltp2.rb +317 -317
- data/tests/test_next_tick.rb +1 -1
- data/tests/test_object_protocol.rb +36 -36
- data/tests/test_pending_connect_timeout.rb +2 -2
- data/tests/test_process_watch.rb +50 -48
- data/tests/test_proxy_connection.rb +52 -0
- data/tests/test_pure.rb +134 -125
- data/tests/test_queue.rb +44 -44
- data/tests/test_running.rb +42 -42
- data/tests/test_sasl.rb +72 -72
- data/tests/test_send_file.rb +251 -242
- data/tests/test_servers.rb +76 -76
- data/tests/test_smtpclient.rb +83 -83
- data/tests/test_smtpserver.rb +85 -85
- data/tests/test_spawn.rb +322 -322
- data/tests/test_ssl_methods.rb +49 -49
- data/tests/test_ssl_verify.rb +82 -82
- data/tests/test_tick_loop.rb +59 -0
- data/tests/test_timers.rb +13 -15
- data/tests/test_ud.rb +36 -36
- data/tests/testem.rb +31 -31
- metadata +66 -51
- data/ext/cplusplus.cpp +0 -202
- data/ext/emwin.cpp +0 -300
- data/ext/emwin.h +0 -94
- data/ext/epoll.cpp +0 -26
- data/ext/epoll.h +0 -25
- data/ext/eventmachine_cpp.h +0 -96
- data/ext/files.cpp +0 -94
- data/ext/files.h +0 -65
- data/ext/sigs.cpp +0 -89
- data/ext/sigs.h +0 -32
- data/java/src/com/rubyeventmachine/application/Application.java +0 -194
- data/java/src/com/rubyeventmachine/application/Connection.java +0 -74
- data/java/src/com/rubyeventmachine/application/ConnectionFactory.java +0 -37
- data/java/src/com/rubyeventmachine/application/DefaultConnectionFactory.java +0 -46
- data/java/src/com/rubyeventmachine/application/PeriodicTimer.java +0 -38
- data/java/src/com/rubyeventmachine/application/Timer.java +0 -54
- data/java/src/com/rubyeventmachine/tests/ApplicationTest.java +0 -109
- data/java/src/com/rubyeventmachine/tests/ConnectTest.java +0 -148
- data/java/src/com/rubyeventmachine/tests/EMTest.java +0 -80
- data/java/src/com/rubyeventmachine/tests/TestDatagrams.java +0 -53
- data/java/src/com/rubyeventmachine/tests/TestServers.java +0 -75
- data/java/src/com/rubyeventmachine/tests/TestTimers.java +0 -90
- data/lib/evma.rb +0 -32
- data/lib/evma/callback.rb +0 -32
- data/lib/evma/container.rb +0 -75
- data/lib/evma/factory.rb +0 -77
- data/lib/evma/protocol.rb +0 -87
- data/lib/evma/reactor.rb +0 -48
- data/web/whatis +0 -7
@@ -1,247 +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
|
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
|