eventmachine 0.12.6-java
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +13 -0
- data/Rakefile +262 -0
- data/docs/COPYING +60 -0
- data/docs/ChangeLog +211 -0
- data/docs/DEFERRABLES +138 -0
- data/docs/EPOLL +141 -0
- data/docs/GNU +281 -0
- data/docs/INSTALL +15 -0
- data/docs/KEYBOARD +38 -0
- data/docs/LEGAL +25 -0
- data/docs/LIGHTWEIGHT_CONCURRENCY +72 -0
- data/docs/PURE_RUBY +77 -0
- data/docs/README +74 -0
- data/docs/RELEASE_NOTES +96 -0
- data/docs/SMTP +9 -0
- data/docs/SPAWNED_PROCESSES +93 -0
- data/docs/TODO +10 -0
- data/eventmachine.gemspec +32 -0
- data/ext/binder.cpp +126 -0
- data/ext/binder.h +48 -0
- data/ext/cmain.cpp +586 -0
- data/ext/cplusplus.cpp +193 -0
- data/ext/ed.cpp +1522 -0
- data/ext/ed.h +380 -0
- data/ext/em.cpp +1937 -0
- data/ext/em.h +186 -0
- data/ext/emwin.cpp +300 -0
- data/ext/emwin.h +94 -0
- data/ext/epoll.cpp +26 -0
- data/ext/epoll.h +25 -0
- data/ext/eventmachine.h +98 -0
- data/ext/eventmachine_cpp.h +95 -0
- data/ext/extconf.rb +129 -0
- data/ext/fastfilereader/extconf.rb +77 -0
- data/ext/fastfilereader/mapper.cpp +214 -0
- data/ext/fastfilereader/mapper.h +59 -0
- data/ext/fastfilereader/rubymain.cpp +127 -0
- data/ext/files.cpp +94 -0
- data/ext/files.h +65 -0
- data/ext/kb.cpp +82 -0
- data/ext/page.cpp +107 -0
- data/ext/page.h +51 -0
- data/ext/pipe.cpp +351 -0
- data/ext/project.h +119 -0
- data/ext/rubymain.cpp +847 -0
- data/ext/sigs.cpp +89 -0
- data/ext/sigs.h +32 -0
- data/ext/ssl.cpp +423 -0
- data/ext/ssl.h +90 -0
- data/java/.classpath +8 -0
- data/java/.project +17 -0
- data/java/src/com/rubyeventmachine/Application.java +196 -0
- data/java/src/com/rubyeventmachine/Connection.java +74 -0
- data/java/src/com/rubyeventmachine/ConnectionFactory.java +37 -0
- data/java/src/com/rubyeventmachine/DefaultConnectionFactory.java +46 -0
- data/java/src/com/rubyeventmachine/EmReactor.java +408 -0
- data/java/src/com/rubyeventmachine/EmReactorException.java +40 -0
- data/java/src/com/rubyeventmachine/EventableChannel.java +57 -0
- data/java/src/com/rubyeventmachine/EventableDatagramChannel.java +171 -0
- data/java/src/com/rubyeventmachine/EventableSocketChannel.java +244 -0
- data/java/src/com/rubyeventmachine/PeriodicTimer.java +38 -0
- data/java/src/com/rubyeventmachine/Timer.java +54 -0
- data/java/src/com/rubyeventmachine/tests/ApplicationTest.java +108 -0
- data/java/src/com/rubyeventmachine/tests/ConnectTest.java +124 -0
- data/java/src/com/rubyeventmachine/tests/EMTest.java +80 -0
- data/java/src/com/rubyeventmachine/tests/TestDatagrams.java +53 -0
- data/java/src/com/rubyeventmachine/tests/TestServers.java +74 -0
- data/java/src/com/rubyeventmachine/tests/TestTimers.java +89 -0
- data/lib/em/deferrable.rb +208 -0
- data/lib/em/eventable.rb +39 -0
- data/lib/em/future.rb +62 -0
- data/lib/em/messages.rb +66 -0
- data/lib/em/processes.rb +113 -0
- data/lib/em/spawnable.rb +88 -0
- data/lib/em/streamer.rb +112 -0
- data/lib/eventmachine.rb +1926 -0
- data/lib/eventmachine_version.rb +31 -0
- data/lib/evma.rb +32 -0
- data/lib/evma/callback.rb +32 -0
- data/lib/evma/container.rb +75 -0
- data/lib/evma/factory.rb +77 -0
- data/lib/evma/protocol.rb +87 -0
- data/lib/evma/reactor.rb +48 -0
- data/lib/jeventmachine.rb +137 -0
- data/lib/pr_eventmachine.rb +1011 -0
- data/lib/protocols/buftok.rb +127 -0
- data/lib/protocols/header_and_content.rb +129 -0
- data/lib/protocols/httpcli2.rb +803 -0
- data/lib/protocols/httpclient.rb +270 -0
- data/lib/protocols/line_and_text.rb +126 -0
- data/lib/protocols/linetext2.rb +161 -0
- data/lib/protocols/memcache.rb +293 -0
- data/lib/protocols/postgres.rb +261 -0
- data/lib/protocols/saslauth.rb +179 -0
- data/lib/protocols/smtpclient.rb +308 -0
- data/lib/protocols/smtpserver.rb +556 -0
- data/lib/protocols/stomp.rb +153 -0
- data/lib/protocols/tcptest.rb +57 -0
- data/setup.rb +1585 -0
- data/tasks/cpp.rake +77 -0
- data/tasks/project.rake +78 -0
- data/tasks/tests.rake +193 -0
- data/tests/test_attach.rb +83 -0
- data/tests/test_basic.rb +231 -0
- data/tests/test_connection_count.rb +45 -0
- data/tests/test_defer.rb +47 -0
- data/tests/test_epoll.rb +163 -0
- data/tests/test_error_handler.rb +35 -0
- data/tests/test_errors.rb +82 -0
- data/tests/test_eventables.rb +77 -0
- data/tests/test_exc.rb +58 -0
- data/tests/test_futures.rb +214 -0
- data/tests/test_handler_check.rb +37 -0
- data/tests/test_hc.rb +218 -0
- data/tests/test_httpclient.rb +215 -0
- data/tests/test_httpclient2.rb +155 -0
- data/tests/test_kb.rb +61 -0
- data/tests/test_ltp.rb +188 -0
- data/tests/test_ltp2.rb +320 -0
- data/tests/test_next_tick.rb +109 -0
- data/tests/test_processes.rb +95 -0
- data/tests/test_pure.rb +129 -0
- data/tests/test_running.rb +47 -0
- data/tests/test_sasl.rb +74 -0
- data/tests/test_send_file.rb +243 -0
- data/tests/test_servers.rb +80 -0
- data/tests/test_smtpclient.rb +83 -0
- data/tests/test_smtpserver.rb +93 -0
- data/tests/test_spawn.rb +329 -0
- data/tests/test_ssl_args.rb +68 -0
- data/tests/test_ssl_methods.rb +50 -0
- data/tests/test_timers.rb +148 -0
- data/tests/test_ud.rb +43 -0
- data/tests/testem.rb +31 -0
- data/web/whatis +7 -0
- metadata +214 -0
@@ -0,0 +1,293 @@
|
|
1
|
+
module EventMachine
|
2
|
+
module Protocols
|
3
|
+
# Implements the Memcache protocol (http://code.sixapart.com/svn/memcached/trunk/server/doc/protocol.txt).
|
4
|
+
# Requires memcached >= 1.2.4 w/ noreply support
|
5
|
+
#
|
6
|
+
# == Usage example
|
7
|
+
#
|
8
|
+
# EM.run{
|
9
|
+
# cache = EM::P::Memcache.connect 'localhost', 11211
|
10
|
+
#
|
11
|
+
# cache.set :a, 'hello'
|
12
|
+
# cache.set :b, 'hi'
|
13
|
+
# cache.set :c, 'how are you?'
|
14
|
+
# cache.set :d, ''
|
15
|
+
#
|
16
|
+
# cache.get(:a){ |v| p v }
|
17
|
+
# cache.get_hash(:a, :b, :c, :d){ |v| p v }
|
18
|
+
# cache.get(:a,:b,:c,:d){ |a,b,c,d| p [a,b,c,d] }
|
19
|
+
#
|
20
|
+
# cache.get(:a,:z,:b,:y,:d){ |a,z,b,y,d| p [a,z,b,y,d] }
|
21
|
+
#
|
22
|
+
# cache.get(:missing){ |m| p [:missing=, m] }
|
23
|
+
# cache.set(:missing, 'abc'){ p :stored }
|
24
|
+
# cache.get(:missing){ |m| p [:missing=, m] }
|
25
|
+
# cache.del(:missing){ p :deleted }
|
26
|
+
# cache.get(:missing){ |m| p [:missing=, m] }
|
27
|
+
# }
|
28
|
+
#
|
29
|
+
module Memcache
|
30
|
+
include EM::Deferrable
|
31
|
+
|
32
|
+
##
|
33
|
+
# constants
|
34
|
+
|
35
|
+
unless defined? Cempty
|
36
|
+
Cstored = 'STORED'.freeze
|
37
|
+
Cend = 'END'.freeze
|
38
|
+
Cdeleted = 'DELETED'.freeze
|
39
|
+
Cunknown = 'NOT_FOUND'.freeze
|
40
|
+
Cerror = 'ERROR'.freeze
|
41
|
+
|
42
|
+
Cempty = ''.freeze
|
43
|
+
Cdelimiter = "\r\n".freeze
|
44
|
+
end
|
45
|
+
|
46
|
+
##
|
47
|
+
# commands
|
48
|
+
|
49
|
+
def get *keys
|
50
|
+
raise ArgumentError unless block_given?
|
51
|
+
|
52
|
+
callback{
|
53
|
+
keys = keys.map{|k| k.to_s.gsub(/\s/,'_') }
|
54
|
+
send_data "get #{keys.join(' ')}\r\n"
|
55
|
+
@get_cbs << [keys, proc{ |values|
|
56
|
+
yield *keys.map{ |k| values[k] }
|
57
|
+
}]
|
58
|
+
}
|
59
|
+
end
|
60
|
+
|
61
|
+
def set key, val, exptime = 0, &cb
|
62
|
+
callback{
|
63
|
+
val = val.to_s
|
64
|
+
send_cmd :set, key, 0, exptime, val.respond_to?(:bytesize) ? val.bytesize : val.size, !block_given?
|
65
|
+
send_data val
|
66
|
+
send_data Cdelimiter
|
67
|
+
@set_cbs << cb if cb
|
68
|
+
}
|
69
|
+
end
|
70
|
+
|
71
|
+
def get_hash *keys
|
72
|
+
raise ArgumentError unless block_given?
|
73
|
+
|
74
|
+
get *keys do |*values|
|
75
|
+
yield keys.inject({}){ |hash, k| hash.update k => values[keys.index(k)] }
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def delete key, expires = 0, &cb
|
80
|
+
callback{
|
81
|
+
send_data "delete #{key} #{expires}#{cb ? '' : ' noreply'}\r\n"
|
82
|
+
@del_cbs << cb if cb
|
83
|
+
}
|
84
|
+
end
|
85
|
+
alias del delete
|
86
|
+
|
87
|
+
def send_cmd cmd, key, flags = 0, exptime = 0, bytes = 0, noreply = false
|
88
|
+
send_data "#{cmd} #{key} #{flags} #{exptime} #{bytes}#{noreply ? ' noreply' : ''}\r\n"
|
89
|
+
end
|
90
|
+
private :send_cmd
|
91
|
+
|
92
|
+
##
|
93
|
+
# errors
|
94
|
+
|
95
|
+
class ParserError < StandardError; end
|
96
|
+
|
97
|
+
##
|
98
|
+
# em hooks
|
99
|
+
|
100
|
+
def self.connect host = 'localhost', port = 11211
|
101
|
+
EM.connect host, port, self, host, port
|
102
|
+
end
|
103
|
+
|
104
|
+
def initialize host, port = 11211
|
105
|
+
@host, @port = host, port
|
106
|
+
end
|
107
|
+
|
108
|
+
def connection_completed
|
109
|
+
@get_cbs = []
|
110
|
+
@set_cbs = []
|
111
|
+
@del_cbs = []
|
112
|
+
|
113
|
+
@values = {}
|
114
|
+
|
115
|
+
@reconnecting = false
|
116
|
+
@connected = true
|
117
|
+
succeed
|
118
|
+
# set_delimiter "\r\n"
|
119
|
+
# set_line_mode
|
120
|
+
end
|
121
|
+
|
122
|
+
# 19Feb09 Switched to a custom parser, LineText2 is recursive and can cause
|
123
|
+
# stack overflows when there is too much data.
|
124
|
+
# include EM::P::LineText2
|
125
|
+
def receive_data data
|
126
|
+
(@buffer||='') << data
|
127
|
+
|
128
|
+
while index = @buffer.index(Cdelimiter)
|
129
|
+
begin
|
130
|
+
line = @buffer.slice!(0,index+2)
|
131
|
+
process_cmd line
|
132
|
+
rescue ParserError
|
133
|
+
@buffer[0...0] = line
|
134
|
+
break
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
# def receive_line line
|
140
|
+
def process_cmd line
|
141
|
+
case line.strip
|
142
|
+
when /^VALUE\s+(.+?)\s+(\d+)\s+(\d+)/ # VALUE <key> <flags> <bytes>
|
143
|
+
bytes = Integer($3)
|
144
|
+
# set_binary_mode bytes+2
|
145
|
+
# @cur_key = $1
|
146
|
+
if @buffer.size >= bytes + 2
|
147
|
+
@values[$1] = @buffer.slice!(0,bytes)
|
148
|
+
@buffer.slice!(0,2) # \r\n
|
149
|
+
else
|
150
|
+
raise ParserError
|
151
|
+
end
|
152
|
+
|
153
|
+
when Cend # END
|
154
|
+
if entry = @get_cbs.shift
|
155
|
+
keys, cb = entry
|
156
|
+
cb.call(@values)
|
157
|
+
end
|
158
|
+
@values = {}
|
159
|
+
|
160
|
+
when Cstored # STORED
|
161
|
+
if cb = @set_cbs.shift
|
162
|
+
cb.call(true)
|
163
|
+
end
|
164
|
+
|
165
|
+
when Cdeleted # DELETED
|
166
|
+
if cb = @del_cbs.shift
|
167
|
+
cb.call(true)
|
168
|
+
end
|
169
|
+
|
170
|
+
when Cunknown # NOT_FOUND
|
171
|
+
if cb = @del_cbs.shift
|
172
|
+
cb.call(false)
|
173
|
+
end
|
174
|
+
|
175
|
+
else
|
176
|
+
p [:MEMCACHE_UNKNOWN, line]
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
# def receive_binary_data data
|
181
|
+
# @values[@cur_key] = data[0..-3]
|
182
|
+
# end
|
183
|
+
|
184
|
+
def unbind
|
185
|
+
if @connected or @reconnecting
|
186
|
+
EM.add_timer(1){ reconnect @host, @port }
|
187
|
+
@connected = false
|
188
|
+
@reconnecting = true
|
189
|
+
@deferred_status = nil
|
190
|
+
else
|
191
|
+
raise 'Unable to connect to memcached server'
|
192
|
+
end
|
193
|
+
end
|
194
|
+
end
|
195
|
+
end
|
196
|
+
end
|
197
|
+
|
198
|
+
if __FILE__ == $0
|
199
|
+
# ruby -I ext:lib -r eventmachine -rubygems lib/protocols/memcache.rb
|
200
|
+
require 'em/spec'
|
201
|
+
|
202
|
+
class TestConnection
|
203
|
+
include EM::P::Memcache
|
204
|
+
def send_data data
|
205
|
+
sent_data << data
|
206
|
+
end
|
207
|
+
def sent_data
|
208
|
+
@sent_data ||= ''
|
209
|
+
end
|
210
|
+
|
211
|
+
def initialize
|
212
|
+
connection_completed
|
213
|
+
end
|
214
|
+
end
|
215
|
+
|
216
|
+
EM.describe EM::Protocols::Memcache do
|
217
|
+
|
218
|
+
before{
|
219
|
+
@c = TestConnection.new
|
220
|
+
}
|
221
|
+
|
222
|
+
should 'send get requests' do
|
223
|
+
@c.get('a'){}
|
224
|
+
@c.sent_data.should == "get a\r\n"
|
225
|
+
done
|
226
|
+
end
|
227
|
+
|
228
|
+
should 'send set requests' do
|
229
|
+
@c.set('a', 1){}
|
230
|
+
@c.sent_data.should == "set a 0 0 1\r\n1\r\n"
|
231
|
+
done
|
232
|
+
end
|
233
|
+
|
234
|
+
should 'use noreply on set without block' do
|
235
|
+
@c.set('a', 1)
|
236
|
+
@c.sent_data.should == "set a 0 0 1 noreply\r\n1\r\n"
|
237
|
+
done
|
238
|
+
end
|
239
|
+
|
240
|
+
should 'send delete requests' do
|
241
|
+
@c.del('a')
|
242
|
+
@c.sent_data.should == "delete a 0 noreply\r\n"
|
243
|
+
done
|
244
|
+
end
|
245
|
+
|
246
|
+
should 'work when get returns no values' do
|
247
|
+
@c.get('a'){ |a|
|
248
|
+
a.should.be.nil
|
249
|
+
done
|
250
|
+
}
|
251
|
+
|
252
|
+
@c.receive_data "END\r\n"
|
253
|
+
end
|
254
|
+
|
255
|
+
should 'invoke block on set' do
|
256
|
+
@c.set('a', 1){
|
257
|
+
done
|
258
|
+
}
|
259
|
+
|
260
|
+
@c.receive_data "STORED\r\n"
|
261
|
+
end
|
262
|
+
|
263
|
+
should 'invoke block on delete' do
|
264
|
+
@c.delete('a'){ |found|
|
265
|
+
found.should.be.false
|
266
|
+
}
|
267
|
+
@c.delete('b'){ |found|
|
268
|
+
found.should.be.true
|
269
|
+
done
|
270
|
+
}
|
271
|
+
|
272
|
+
@c.receive_data "NOT_FOUND\r\n"
|
273
|
+
@c.receive_data "DELETED\r\n"
|
274
|
+
end
|
275
|
+
|
276
|
+
should 'parse split responses' do
|
277
|
+
@c.get('a'){ |a|
|
278
|
+
a.should == 'abc'
|
279
|
+
done
|
280
|
+
}
|
281
|
+
|
282
|
+
@c.receive_data "VAL"
|
283
|
+
@c.receive_data "UE a 0 "
|
284
|
+
@c.receive_data "3\r\n"
|
285
|
+
@c.receive_data "ab"
|
286
|
+
@c.receive_data "c"
|
287
|
+
@c.receive_data "\r\n"
|
288
|
+
@c.receive_data "EN"
|
289
|
+
@c.receive_data "D\r\n"
|
290
|
+
end
|
291
|
+
|
292
|
+
end
|
293
|
+
end
|
@@ -0,0 +1,261 @@
|
|
1
|
+
#
|
2
|
+
# $Id$
|
3
|
+
#
|
4
|
+
# Author:: Francis Cianfrocca (gmail: blackhedd)
|
5
|
+
# Homepage:: http://rubyeventmachine.com
|
6
|
+
# Date:: 15 November 2006
|
7
|
+
#
|
8
|
+
# See EventMachine and EventMachine::Connection for documentation and
|
9
|
+
# usage examples.
|
10
|
+
#
|
11
|
+
#----------------------------------------------------------------------------
|
12
|
+
#
|
13
|
+
# Copyright (C) 2006-08 by Francis Cianfrocca. All Rights Reserved.
|
14
|
+
# Gmail: blackhedd
|
15
|
+
#
|
16
|
+
# This program is free software; you can redistribute it and/or modify
|
17
|
+
# it under the terms of either: 1) the GNU General Public License
|
18
|
+
# as published by the Free Software Foundation; either version 2 of the
|
19
|
+
# License, or (at your option) any later version; or 2) Ruby's License.
|
20
|
+
#
|
21
|
+
# See the file COPYING for complete licensing information.
|
22
|
+
#
|
23
|
+
#---------------------------------------------------------------------------
|
24
|
+
#
|
25
|
+
#
|
26
|
+
#
|
27
|
+
|
28
|
+
|
29
|
+
=begin
|
30
|
+
PROVISIONAL IMPLEMENTATION of an evented Postgres client.
|
31
|
+
This implements version 3 of the Postgres wire protocol, which will work
|
32
|
+
with any Postgres version from roughly 7.4 onward.
|
33
|
+
|
34
|
+
Until this code is judged ready for prime time, you have to access it by
|
35
|
+
explicitly requiring protocols/postgres.
|
36
|
+
|
37
|
+
Objective: we want to access Postgres databases without requiring threads.
|
38
|
+
Until now this has been a problem because the Postgres client implementations
|
39
|
+
have all made use of blocking I/O calls, which is incompatible with a
|
40
|
+
thread-free evented model.
|
41
|
+
|
42
|
+
But rather than re-implement the Postgres Wire3 protocol, we're taking advantage
|
43
|
+
of the existing postgres-pr library, which was originally written by Michael
|
44
|
+
Neumann but (at this writing) appears to be no longer maintained. Still, it's
|
45
|
+
in basically a production-ready state, and the wire protocol isn't that complicated
|
46
|
+
anyway.
|
47
|
+
|
48
|
+
We need to monkeypatch StringIO because it lacks the #readbytes method needed
|
49
|
+
by postgres-pr.
|
50
|
+
|
51
|
+
We're tucking in a bunch of require statements that may not be present in garden-variety
|
52
|
+
EM installations. Until we find a good way to only require these if a program
|
53
|
+
requires postgres, this file will need to be required explicitly.
|
54
|
+
|
55
|
+
The StringIO monkeypatch is lifted verbatim from the standard library readbytes.rb,
|
56
|
+
which adds method #readbytes directly to class IO. But StringIO is not a subclass of IO.
|
57
|
+
|
58
|
+
We cloned the handling of postgres messages from lib/postgres-pr/connection.rb
|
59
|
+
in the postgres-pr library, and modified it for event-handling.
|
60
|
+
|
61
|
+
TODO: The password handling in dispatch_conn_message is totally incomplete.
|
62
|
+
|
63
|
+
|
64
|
+
We return Deferrables from the user-level operations surfaced by this interface.
|
65
|
+
Experimentally, we're using the pattern of always returning a boolean value as the
|
66
|
+
first argument of a deferrable callback to indicate success or failure. This is
|
67
|
+
instead of the traditional pattern of calling Deferrable#succeed or #fail, and
|
68
|
+
requiring the user to define both a callback and an errback function.
|
69
|
+
|
70
|
+
Sample code:
|
71
|
+
require 'eventmachine'
|
72
|
+
require 'protocols/postgres' # provisionally needed
|
73
|
+
|
74
|
+
EM.run {
|
75
|
+
db = EM.connect_unix_domain( "/tmp/.s.PGSQL.5432", EM::P::Postgres3 )
|
76
|
+
db.connect( dbname, username, psw ).callback do |status|
|
77
|
+
if status
|
78
|
+
db.query( "select * from some_table" ).callback do |status, result, errors|
|
79
|
+
if status
|
80
|
+
result.rows.each do |row|
|
81
|
+
p row
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
}
|
88
|
+
|
89
|
+
|
90
|
+
|
91
|
+
=end
|
92
|
+
|
93
|
+
|
94
|
+
require 'readbytes'
|
95
|
+
require 'postgres-pr/message'
|
96
|
+
require 'postgres-pr/connection'
|
97
|
+
require 'stringio'
|
98
|
+
|
99
|
+
include PostgresPR
|
100
|
+
|
101
|
+
class StringIO
|
102
|
+
# Reads exactly +n+ bytes.
|
103
|
+
#
|
104
|
+
# If the data read is nil an EOFError is raised.
|
105
|
+
#
|
106
|
+
# If the data read is too short a TruncatedDataError is raised and the read
|
107
|
+
# data is obtainable via its #data method.
|
108
|
+
def readbytes(n)
|
109
|
+
str = read(n)
|
110
|
+
if str == nil
|
111
|
+
raise EOFError, "End of file reached"
|
112
|
+
end
|
113
|
+
if str.size < n
|
114
|
+
raise TruncatedDataError.new("data truncated", str)
|
115
|
+
end
|
116
|
+
str
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
|
121
|
+
module EventMachine; module Protocols; class Postgres3 < EventMachine::Connection
|
122
|
+
|
123
|
+
|
124
|
+
def initialize
|
125
|
+
@data = ""
|
126
|
+
@params = {}
|
127
|
+
end
|
128
|
+
|
129
|
+
def connect db, user, psw=nil
|
130
|
+
d = EM::DefaultDeferrable.new
|
131
|
+
d.timeout 15
|
132
|
+
|
133
|
+
if @pending_query || @pending_conn
|
134
|
+
d.succeed false, "Operation already in progress"
|
135
|
+
else
|
136
|
+
@pending_conn = d
|
137
|
+
prms = {"user"=>user, "database"=>db}
|
138
|
+
@user = user
|
139
|
+
if psw
|
140
|
+
@password = psw
|
141
|
+
#prms["password"] = psw
|
142
|
+
end
|
143
|
+
send_data PostgresPR::StartupMessage.new( 3 << 16, prms ).dump
|
144
|
+
end
|
145
|
+
|
146
|
+
d
|
147
|
+
end
|
148
|
+
|
149
|
+
def query sql
|
150
|
+
d = EM::DefaultDeferrable.new
|
151
|
+
d.timeout 15
|
152
|
+
|
153
|
+
if @pending_query || @pending_conn
|
154
|
+
d.succeed false, "Operation already in progress"
|
155
|
+
else
|
156
|
+
@r = PostgresPR::Connection::Result.new
|
157
|
+
@e = []
|
158
|
+
@pending_query = d
|
159
|
+
send_data PostgresPR::Query.dump(sql)
|
160
|
+
end
|
161
|
+
|
162
|
+
d
|
163
|
+
end
|
164
|
+
|
165
|
+
|
166
|
+
def receive_data data
|
167
|
+
@data << data
|
168
|
+
while @data.length >= 5
|
169
|
+
pktlen = @data[1...5].unpack("N").first
|
170
|
+
if @data.length >= (1 + pktlen)
|
171
|
+
pkt = @data.slice!(0...(1+pktlen))
|
172
|
+
m = StringIO.open( pkt, "r" ) {|io| PostgresPR::Message.read( io ) }
|
173
|
+
if @pending_conn
|
174
|
+
dispatch_conn_message m
|
175
|
+
elsif @pending_query
|
176
|
+
dispatch_query_message m
|
177
|
+
else
|
178
|
+
raise "Unexpected message from database"
|
179
|
+
end
|
180
|
+
else
|
181
|
+
break # very important, break out of the while
|
182
|
+
end
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
186
|
+
|
187
|
+
def unbind
|
188
|
+
if o = (@pending_query || @pending_conn)
|
189
|
+
o.succeed false, "lost connection"
|
190
|
+
end
|
191
|
+
end
|
192
|
+
|
193
|
+
# Cloned and modified from the postgres-pr.
|
194
|
+
def dispatch_conn_message msg
|
195
|
+
case msg
|
196
|
+
when AuthentificationClearTextPassword
|
197
|
+
raise ArgumentError, "no password specified" if @password.nil?
|
198
|
+
send_data PasswordMessage.new(@password).dump
|
199
|
+
|
200
|
+
when AuthentificationCryptPassword
|
201
|
+
raise ArgumentError, "no password specified" if @password.nil?
|
202
|
+
send_data PasswordMessage.new(@password.crypt(msg.salt)).dump
|
203
|
+
|
204
|
+
when AuthentificationMD5Password
|
205
|
+
raise ArgumentError, "no password specified" if @password.nil?
|
206
|
+
require 'digest/md5'
|
207
|
+
|
208
|
+
m = Digest::MD5.hexdigest(@password + @user)
|
209
|
+
m = Digest::MD5.hexdigest(m + msg.salt)
|
210
|
+
m = 'md5' + m
|
211
|
+
send_data PasswordMessage.new(m).dump
|
212
|
+
|
213
|
+
when AuthentificationKerberosV4, AuthentificationKerberosV5, AuthentificationSCMCredential
|
214
|
+
raise "unsupported authentification"
|
215
|
+
|
216
|
+
when AuthentificationOk
|
217
|
+
when ErrorResponse
|
218
|
+
raise msg.field_values.join("\t")
|
219
|
+
when NoticeResponse
|
220
|
+
@notice_processor.call(msg) if @notice_processor
|
221
|
+
when ParameterStatus
|
222
|
+
@params[msg.key] = msg.value
|
223
|
+
when BackendKeyData
|
224
|
+
# TODO
|
225
|
+
#p msg
|
226
|
+
when ReadyForQuery
|
227
|
+
# TODO: use transaction status
|
228
|
+
pc,@pending_conn = @pending_conn,nil
|
229
|
+
pc.succeed true
|
230
|
+
else
|
231
|
+
raise "unhandled message type"
|
232
|
+
end
|
233
|
+
end
|
234
|
+
|
235
|
+
# Cloned and modified from the postgres-pr.
|
236
|
+
def dispatch_query_message msg
|
237
|
+
case msg
|
238
|
+
when DataRow
|
239
|
+
@r.rows << msg.columns
|
240
|
+
when CommandComplete
|
241
|
+
@r.cmd_tag = msg.cmd_tag
|
242
|
+
when ReadyForQuery
|
243
|
+
pq,@pending_query = @pending_query,nil
|
244
|
+
pq.succeed true, @r, @e
|
245
|
+
when RowDescription
|
246
|
+
@r.fields = msg.fields
|
247
|
+
when CopyInResponse
|
248
|
+
when CopyOutResponse
|
249
|
+
when EmptyQueryResponse
|
250
|
+
when ErrorResponse
|
251
|
+
# TODO
|
252
|
+
@e << msg
|
253
|
+
when NoticeResponse
|
254
|
+
@notice_processor.call(msg) if @notice_processor
|
255
|
+
else
|
256
|
+
# TODO
|
257
|
+
end
|
258
|
+
end
|
259
|
+
|
260
|
+
end; end; end
|
261
|
+
|