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.
- data/.gitignore +16 -0
- data/Gemfile +1 -0
- data/README +81 -0
- data/Rakefile +11 -0
- data/docs/COPYING +60 -0
- data/docs/ChangeLog +211 -0
- data/docs/DEFERRABLES +246 -0
- data/docs/EPOLL +141 -0
- data/docs/GNU +281 -0
- data/docs/INSTALL +13 -0
- data/docs/KEYBOARD +42 -0
- data/docs/LEGAL +25 -0
- data/docs/LIGHTWEIGHT_CONCURRENCY +130 -0
- data/docs/PURE_RUBY +75 -0
- data/docs/RELEASE_NOTES +94 -0
- data/docs/SMTP +4 -0
- data/docs/SPAWNED_PROCESSES +148 -0
- data/docs/TODO +8 -0
- data/eventmachine.gemspec +33 -0
- data/examples/ex_channel.rb +43 -0
- data/examples/ex_queue.rb +2 -0
- data/examples/ex_tick_loop_array.rb +15 -0
- data/examples/ex_tick_loop_counter.rb +32 -0
- data/examples/helper.rb +2 -0
- data/ext/binder.cpp +124 -0
- data/ext/binder.h +46 -0
- data/ext/cmain.cpp +838 -0
- data/ext/ed.cpp +1884 -0
- data/ext/ed.h +418 -0
- data/ext/em.cpp +2348 -0
- data/ext/em.h +228 -0
- data/ext/eventmachine.h +123 -0
- data/ext/extconf.rb +157 -0
- data/ext/fastfilereader/extconf.rb +85 -0
- data/ext/fastfilereader/mapper.cpp +214 -0
- data/ext/fastfilereader/mapper.h +59 -0
- data/ext/fastfilereader/rubymain.cpp +127 -0
- data/ext/kb.cpp +79 -0
- data/ext/page.cpp +107 -0
- data/ext/page.h +51 -0
- data/ext/pipe.cpp +347 -0
- data/ext/project.h +155 -0
- data/ext/rubymain.cpp +1200 -0
- data/ext/ssl.cpp +460 -0
- data/ext/ssl.h +94 -0
- data/java/.classpath +8 -0
- data/java/.project +17 -0
- data/java/src/com/rubyeventmachine/EmReactor.java +571 -0
- data/java/src/com/rubyeventmachine/EmReactorException.java +40 -0
- data/java/src/com/rubyeventmachine/EventableChannel.java +69 -0
- data/java/src/com/rubyeventmachine/EventableDatagramChannel.java +189 -0
- data/java/src/com/rubyeventmachine/EventableSocketChannel.java +364 -0
- data/lib/em/buftok.rb +138 -0
- data/lib/em/callback.rb +26 -0
- data/lib/em/channel.rb +57 -0
- data/lib/em/connection.rb +569 -0
- data/lib/em/deferrable.rb +206 -0
- data/lib/em/file_watch.rb +54 -0
- data/lib/em/future.rb +61 -0
- data/lib/em/iterator.rb +270 -0
- data/lib/em/messages.rb +66 -0
- data/lib/em/process_watch.rb +44 -0
- data/lib/em/processes.rb +119 -0
- data/lib/em/protocols.rb +36 -0
- data/lib/em/protocols/header_and_content.rb +138 -0
- data/lib/em/protocols/httpclient.rb +268 -0
- data/lib/em/protocols/httpclient2.rb +590 -0
- data/lib/em/protocols/line_and_text.rb +125 -0
- data/lib/em/protocols/line_protocol.rb +28 -0
- data/lib/em/protocols/linetext2.rb +161 -0
- data/lib/em/protocols/memcache.rb +323 -0
- data/lib/em/protocols/object_protocol.rb +45 -0
- data/lib/em/protocols/postgres3.rb +247 -0
- data/lib/em/protocols/saslauth.rb +175 -0
- data/lib/em/protocols/smtpclient.rb +357 -0
- data/lib/em/protocols/smtpserver.rb +640 -0
- data/lib/em/protocols/socks4.rb +66 -0
- data/lib/em/protocols/stomp.rb +200 -0
- data/lib/em/protocols/tcptest.rb +53 -0
- data/lib/em/pure_ruby.rb +1013 -0
- data/lib/em/queue.rb +62 -0
- data/lib/em/spawnable.rb +85 -0
- data/lib/em/streamer.rb +130 -0
- data/lib/em/tick_loop.rb +85 -0
- data/lib/em/timers.rb +57 -0
- data/lib/em/version.rb +3 -0
- data/lib/eventmachine.rb +1548 -0
- data/lib/jeventmachine.rb +258 -0
- data/lib/rubyeventmachine.rb +2 -0
- data/setup.rb +1585 -0
- data/tasks/cpp.rake_example +77 -0
- data/tasks/doc.rake +30 -0
- data/tasks/package.rake +85 -0
- data/tasks/test.rake +6 -0
- data/tests/client.crt +31 -0
- data/tests/client.key +51 -0
- data/tests/test_attach.rb +136 -0
- data/tests/test_basic.rb +249 -0
- data/tests/test_channel.rb +64 -0
- data/tests/test_connection_count.rb +35 -0
- data/tests/test_defer.rb +49 -0
- data/tests/test_deferrable.rb +35 -0
- data/tests/test_epoll.rb +160 -0
- data/tests/test_error_handler.rb +35 -0
- data/tests/test_errors.rb +82 -0
- data/tests/test_exc.rb +55 -0
- data/tests/test_file_watch.rb +49 -0
- data/tests/test_futures.rb +198 -0
- data/tests/test_get_sock_opt.rb +30 -0
- data/tests/test_handler_check.rb +37 -0
- data/tests/test_hc.rb +190 -0
- data/tests/test_httpclient.rb +227 -0
- data/tests/test_httpclient2.rb +154 -0
- data/tests/test_inactivity_timeout.rb +50 -0
- data/tests/test_kb.rb +60 -0
- data/tests/test_ltp.rb +190 -0
- data/tests/test_ltp2.rb +317 -0
- data/tests/test_next_tick.rb +133 -0
- data/tests/test_object_protocol.rb +37 -0
- data/tests/test_pause.rb +70 -0
- data/tests/test_pending_connect_timeout.rb +48 -0
- data/tests/test_process_watch.rb +50 -0
- data/tests/test_processes.rb +128 -0
- data/tests/test_proxy_connection.rb +144 -0
- data/tests/test_pure.rb +134 -0
- data/tests/test_queue.rb +44 -0
- data/tests/test_running.rb +42 -0
- data/tests/test_sasl.rb +72 -0
- data/tests/test_send_file.rb +251 -0
- data/tests/test_servers.rb +76 -0
- data/tests/test_smtpclient.rb +83 -0
- data/tests/test_smtpserver.rb +85 -0
- data/tests/test_spawn.rb +322 -0
- data/tests/test_ssl_args.rb +79 -0
- data/tests/test_ssl_methods.rb +50 -0
- data/tests/test_ssl_verify.rb +82 -0
- data/tests/test_tick_loop.rb +59 -0
- data/tests/test_timers.rb +160 -0
- data/tests/test_ud.rb +36 -0
- data/tests/testem.rb +31 -0
- metadata +240 -0
@@ -0,0 +1,125 @@
|
|
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
|
+
# A protocol that handles line-oriented data with interspersed binary text.
|
30
|
+
#
|
31
|
+
# This version is optimized for performance. See EventMachine::Protocols::LineText2
|
32
|
+
# for a version which is optimized for correctness with regard to binary text blocks
|
33
|
+
# that can switch back to line mode.
|
34
|
+
class LineAndTextProtocol < Connection
|
35
|
+
MaxLineLength = 16*1024
|
36
|
+
MaxBinaryLength = 32*1024*1024
|
37
|
+
|
38
|
+
def initialize *args
|
39
|
+
super
|
40
|
+
lbp_init_line_state
|
41
|
+
end
|
42
|
+
def receive_data data
|
43
|
+
if @lbp_mode == :lines
|
44
|
+
begin
|
45
|
+
@lpb_buffer.extract(data).each do |line|
|
46
|
+
receive_line(line.chomp) if respond_to?(:receive_line)
|
47
|
+
end
|
48
|
+
rescue Exception
|
49
|
+
receive_error('overlength line') if respond_to?(:receive_error)
|
50
|
+
close_connection
|
51
|
+
return
|
52
|
+
end
|
53
|
+
else
|
54
|
+
if @lbp_binary_limit > 0
|
55
|
+
wanted = @lbp_binary_limit - @lbp_binary_bytes_received
|
56
|
+
chunk = nil
|
57
|
+
if data.length > wanted
|
58
|
+
chunk = data.slice!(0...wanted)
|
59
|
+
else
|
60
|
+
chunk = data
|
61
|
+
data = ""
|
62
|
+
end
|
63
|
+
@lbp_binary_buffer[@lbp_binary_bytes_received...(@lbp_binary_bytes_received+chunk.length)] = chunk
|
64
|
+
@lbp_binary_bytes_received += chunk.length
|
65
|
+
if @lbp_binary_bytes_received == @lbp_binary_limit
|
66
|
+
receive_binary_data(@lbp_binary_buffer) if respond_to?(:receive_binary_data)
|
67
|
+
lbp_init_line_state
|
68
|
+
end
|
69
|
+
receive_data(data) if data.length > 0
|
70
|
+
else
|
71
|
+
receive_binary_data(data) if respond_to?(:receive_binary_data)
|
72
|
+
data = ""
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def unbind
|
78
|
+
if @lbp_mode == :binary and @lbp_binary_limit > 0
|
79
|
+
if respond_to?(:receive_binary_data)
|
80
|
+
receive_binary_data( @lbp_binary_buffer[0...@lbp_binary_bytes_received] )
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
# Set up to read the supplied number of binary bytes.
|
86
|
+
# This recycles all the data currently waiting in the line buffer, if any.
|
87
|
+
# If the limit is nil, then ALL subsequent data will be treated as binary
|
88
|
+
# data and passed to the upstream protocol handler as we receive it.
|
89
|
+
# If a limit is given, we'll hold the incoming binary data and not
|
90
|
+
# pass it upstream until we've seen it all, or until there is an unbind
|
91
|
+
# (in which case we'll pass up a partial).
|
92
|
+
# Specifying nil for the limit (the default) means there is no limit.
|
93
|
+
# Specifiyng zero for the limit will cause an immediate transition back to line mode.
|
94
|
+
#
|
95
|
+
def set_binary_mode size = nil
|
96
|
+
if @lbp_mode == :lines
|
97
|
+
if size == 0
|
98
|
+
receive_binary_data("") if respond_to?(:receive_binary_data)
|
99
|
+
# Do no more work here. Stay in line mode and keep consuming data.
|
100
|
+
else
|
101
|
+
@lbp_binary_limit = size.to_i # (nil will be stored as zero)
|
102
|
+
if @lbp_binary_limit > 0
|
103
|
+
raise "Overlength" if @lbp_binary_limit > MaxBinaryLength # arbitrary sanity check
|
104
|
+
@lbp_binary_buffer = "\0" * @lbp_binary_limit
|
105
|
+
@lbp_binary_bytes_received = 0
|
106
|
+
end
|
107
|
+
|
108
|
+
@lbp_mode = :binary
|
109
|
+
receive_data @lpb_buffer.flush
|
110
|
+
end
|
111
|
+
else
|
112
|
+
raise "invalid operation"
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
#--
|
117
|
+
# For internal use, establish protocol baseline for handling lines.
|
118
|
+
def lbp_init_line_state
|
119
|
+
@lpb_buffer = BufferedTokenizer.new("\n", MaxLineLength)
|
120
|
+
@lbp_mode = :lines
|
121
|
+
end
|
122
|
+
private :lbp_init_line_state
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module EventMachine
|
2
|
+
module Protocols
|
3
|
+
# LineProtocol will parse out newline terminated strings from a receive_data stream
|
4
|
+
#
|
5
|
+
# module Server
|
6
|
+
# include EM::P::LineProtocol
|
7
|
+
#
|
8
|
+
# def receive_line(line)
|
9
|
+
# send_data("you said: #{line}")
|
10
|
+
# end
|
11
|
+
# end
|
12
|
+
#
|
13
|
+
module LineProtocol
|
14
|
+
def receive_data data # :nodoc:
|
15
|
+
(@buf ||= '') << data
|
16
|
+
|
17
|
+
while line = @buf.slice!(/(.*)\r?\n/)
|
18
|
+
receive_line(line)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
# Invoked with lines received over the network
|
23
|
+
def receive_line(line)
|
24
|
+
# stub
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,161 @@
|
|
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
|
+
module EventMachine
|
27
|
+
module Protocols
|
28
|
+
# In the grand, time-honored tradition of re-inventing the wheel, we offer
|
29
|
+
# here YET ANOTHER protocol that handles line-oriented data with interspersed
|
30
|
+
# binary text. This one trades away some of the performance optimizations of
|
31
|
+
# EventMachine::Protocols::LineAndTextProtocol in order to get better correctness
|
32
|
+
# with regard to binary text blocks that can switch back to line mode. It also
|
33
|
+
# permits the line-delimiter to change in midstream.
|
34
|
+
# This was originally written to support Stomp.
|
35
|
+
module LineText2
|
36
|
+
# TODO! We're not enforcing the limits on header lengths and text-lengths.
|
37
|
+
# When we get around to that, call #receive_error if the user defined it, otherwise
|
38
|
+
# throw exceptions.
|
39
|
+
|
40
|
+
MaxLineLength = 16*1024
|
41
|
+
MaxBinaryLength = 32*1024*1024
|
42
|
+
|
43
|
+
#--
|
44
|
+
# Will be called recursively until there's no data to read.
|
45
|
+
# That way the user-defined handlers we call can modify the
|
46
|
+
# handling characteristics on a per-token basis.
|
47
|
+
#
|
48
|
+
def receive_data data
|
49
|
+
return unless (data and data.length > 0)
|
50
|
+
|
51
|
+
# Do this stuff in lieu of a constructor.
|
52
|
+
@lt2_mode ||= :lines
|
53
|
+
@lt2_delimiter ||= "\n"
|
54
|
+
@lt2_linebuffer ||= []
|
55
|
+
|
56
|
+
if @lt2_mode == :lines
|
57
|
+
if ix = data.index( @lt2_delimiter )
|
58
|
+
@lt2_linebuffer << data[0...ix]
|
59
|
+
ln = @lt2_linebuffer.join
|
60
|
+
@lt2_linebuffer.clear
|
61
|
+
if @lt2_delimiter == "\n"
|
62
|
+
ln.chomp!
|
63
|
+
end
|
64
|
+
receive_line ln
|
65
|
+
receive_data data[(ix+@lt2_delimiter.length)..-1]
|
66
|
+
else
|
67
|
+
@lt2_linebuffer << data
|
68
|
+
end
|
69
|
+
elsif @lt2_mode == :text
|
70
|
+
if @lt2_textsize
|
71
|
+
needed = @lt2_textsize - @lt2_textpos
|
72
|
+
will_take = if data.length > needed
|
73
|
+
needed
|
74
|
+
else
|
75
|
+
data.length
|
76
|
+
end
|
77
|
+
|
78
|
+
@lt2_textbuffer << data[0...will_take]
|
79
|
+
tail = data[will_take..-1]
|
80
|
+
|
81
|
+
@lt2_textpos += will_take
|
82
|
+
if @lt2_textpos >= @lt2_textsize
|
83
|
+
# Reset line mode (the default behavior) BEFORE calling the
|
84
|
+
# receive_binary_data. This makes it possible for user code
|
85
|
+
# to call set_text_mode, enabling chains of text blocks
|
86
|
+
# (which can possibly be of different sizes).
|
87
|
+
set_line_mode
|
88
|
+
receive_binary_data @lt2_textbuffer.join
|
89
|
+
receive_end_of_binary_data
|
90
|
+
end
|
91
|
+
|
92
|
+
receive_data tail
|
93
|
+
else
|
94
|
+
receive_binary_data data
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
|
100
|
+
def set_delimiter delim
|
101
|
+
@lt2_delimiter = delim.to_s
|
102
|
+
end
|
103
|
+
|
104
|
+
# Called internally but also exposed to user code, for the case in which
|
105
|
+
# processing of binary data creates a need to transition back to line mode.
|
106
|
+
# We support an optional parameter to "throw back" some data, which might
|
107
|
+
# be an umprocessed chunk of the transmitted binary data, or something else
|
108
|
+
# entirely.
|
109
|
+
def set_line_mode data=""
|
110
|
+
@lt2_mode = :lines
|
111
|
+
(@lt2_linebuffer ||= []).clear
|
112
|
+
receive_data data.to_s
|
113
|
+
end
|
114
|
+
|
115
|
+
def set_text_mode size=nil
|
116
|
+
if size == 0
|
117
|
+
set_line_mode
|
118
|
+
else
|
119
|
+
@lt2_mode = :text
|
120
|
+
(@lt2_textbuffer ||= []).clear
|
121
|
+
@lt2_textsize = size # which can be nil, signifying no limit
|
122
|
+
@lt2_textpos = 0
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
# Alias for #set_text_mode, added for back-compatibility with LineAndTextProtocol.
|
127
|
+
def set_binary_mode size=nil
|
128
|
+
set_text_mode size
|
129
|
+
end
|
130
|
+
|
131
|
+
# In case of a dropped connection, we'll send a partial buffer to user code
|
132
|
+
# when in sized text mode. User overrides of #receive_binary_data need to
|
133
|
+
# be aware that they may get a short buffer.
|
134
|
+
def unbind
|
135
|
+
@lt2_mode ||= nil
|
136
|
+
if @lt2_mode == :text and @lt2_textpos > 0
|
137
|
+
receive_binary_data @lt2_textbuffer.join
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
# Stub. Should be subclassed by user code.
|
142
|
+
def receive_line ln
|
143
|
+
# no-op
|
144
|
+
end
|
145
|
+
|
146
|
+
# Stub. Should be subclassed by user code.
|
147
|
+
def receive_binary_data data
|
148
|
+
# no-op
|
149
|
+
end
|
150
|
+
|
151
|
+
# Stub. Should be subclassed by user code.
|
152
|
+
# This is called when transitioning internally from text mode
|
153
|
+
# back to line mode. Useful when client code doesn't want
|
154
|
+
# to keep track of how much data it's received.
|
155
|
+
def receive_end_of_binary_data
|
156
|
+
# no-op
|
157
|
+
end
|
158
|
+
end
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
@@ -0,0 +1,323 @@
|
|
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
|
+
# :stopdoc:
|
36
|
+
unless defined? Cempty
|
37
|
+
Cstored = 'STORED'.freeze
|
38
|
+
Cend = 'END'.freeze
|
39
|
+
Cdeleted = 'DELETED'.freeze
|
40
|
+
Cunknown = 'NOT_FOUND'.freeze
|
41
|
+
Cerror = 'ERROR'.freeze
|
42
|
+
|
43
|
+
Cempty = ''.freeze
|
44
|
+
Cdelimiter = "\r\n".freeze
|
45
|
+
end
|
46
|
+
# :startdoc:
|
47
|
+
|
48
|
+
##
|
49
|
+
# commands
|
50
|
+
|
51
|
+
# Get the value associated with one or multiple keys
|
52
|
+
#
|
53
|
+
# cache.get(:a){ |v| p v }
|
54
|
+
# cache.get(:a,:b,:c,:d){ |a,b,c,d| p [a,b,c,d] }
|
55
|
+
#
|
56
|
+
def get *keys
|
57
|
+
raise ArgumentError unless block_given?
|
58
|
+
|
59
|
+
callback{
|
60
|
+
keys = keys.map{|k| k.to_s.gsub(/\s/,'_') }
|
61
|
+
send_data "get #{keys.join(' ')}\r\n"
|
62
|
+
@get_cbs << [keys, proc{ |values|
|
63
|
+
yield *keys.map{ |k| values[k] }
|
64
|
+
}]
|
65
|
+
}
|
66
|
+
end
|
67
|
+
|
68
|
+
# Set the value for a given key
|
69
|
+
#
|
70
|
+
# cache.set :a, 'hello'
|
71
|
+
# cache.set(:missing, 'abc'){ puts "stored the value!" }
|
72
|
+
#
|
73
|
+
def set key, val, exptime = 0, &cb
|
74
|
+
callback{
|
75
|
+
val = val.to_s
|
76
|
+
send_cmd :set, key, 0, exptime, val.respond_to?(:bytesize) ? val.bytesize : val.size, !block_given?
|
77
|
+
send_data val
|
78
|
+
send_data Cdelimiter
|
79
|
+
@set_cbs << cb if cb
|
80
|
+
}
|
81
|
+
end
|
82
|
+
|
83
|
+
# Gets multiple values as a hash
|
84
|
+
#
|
85
|
+
# cache.get_hash(:a, :b, :c, :d){ |h| puts h[:a] }
|
86
|
+
#
|
87
|
+
def get_hash *keys
|
88
|
+
raise ArgumentError unless block_given?
|
89
|
+
|
90
|
+
get *keys do |*values|
|
91
|
+
yield keys.inject({}){ |hash, k| hash.update k => values[keys.index(k)] }
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
# Delete the value associated with a key
|
96
|
+
#
|
97
|
+
# cache.del :a
|
98
|
+
# cache.del(:b){ puts "deleted the value!" }
|
99
|
+
#
|
100
|
+
def delete key, expires = 0, &cb
|
101
|
+
callback{
|
102
|
+
send_data "delete #{key} #{expires}#{cb ? '' : ' noreply'}\r\n"
|
103
|
+
@del_cbs << cb if cb
|
104
|
+
}
|
105
|
+
end
|
106
|
+
alias del delete
|
107
|
+
|
108
|
+
# Connect to a memcached server (must support NOREPLY, memcached >= 1.2.4)
|
109
|
+
def self.connect host = 'localhost', port = 11211
|
110
|
+
EM.connect host, port, self, host, port
|
111
|
+
end
|
112
|
+
|
113
|
+
# :stopdoc:
|
114
|
+
|
115
|
+
def send_cmd cmd, key, flags = 0, exptime = 0, bytes = 0, noreply = false # :nodoc:
|
116
|
+
send_data "#{cmd} #{key} #{flags} #{exptime} #{bytes}#{noreply ? ' noreply' : ''}\r\n"
|
117
|
+
end
|
118
|
+
private :send_cmd
|
119
|
+
|
120
|
+
##
|
121
|
+
# errors
|
122
|
+
|
123
|
+
class ParserError < StandardError
|
124
|
+
end
|
125
|
+
|
126
|
+
##
|
127
|
+
# em hooks
|
128
|
+
|
129
|
+
def initialize host, port = 11211
|
130
|
+
@host, @port = host, port
|
131
|
+
end
|
132
|
+
|
133
|
+
def connection_completed
|
134
|
+
@get_cbs = []
|
135
|
+
@set_cbs = []
|
136
|
+
@del_cbs = []
|
137
|
+
|
138
|
+
@values = {}
|
139
|
+
|
140
|
+
@reconnecting = false
|
141
|
+
@connected = true
|
142
|
+
succeed
|
143
|
+
# set_delimiter "\r\n"
|
144
|
+
# set_line_mode
|
145
|
+
end
|
146
|
+
|
147
|
+
#--
|
148
|
+
# 19Feb09 Switched to a custom parser, LineText2 is recursive and can cause
|
149
|
+
# stack overflows when there is too much data.
|
150
|
+
# include EM::P::LineText2
|
151
|
+
def receive_data data
|
152
|
+
(@buffer||='') << data
|
153
|
+
|
154
|
+
while index = @buffer.index(Cdelimiter)
|
155
|
+
begin
|
156
|
+
line = @buffer.slice!(0,index+2)
|
157
|
+
process_cmd line
|
158
|
+
rescue ParserError
|
159
|
+
@buffer[0...0] = line
|
160
|
+
break
|
161
|
+
end
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
#--
|
166
|
+
# def receive_line line
|
167
|
+
def process_cmd line
|
168
|
+
case line.strip
|
169
|
+
when /^VALUE\s+(.+?)\s+(\d+)\s+(\d+)/ # VALUE <key> <flags> <bytes>
|
170
|
+
bytes = Integer($3)
|
171
|
+
# set_binary_mode bytes+2
|
172
|
+
# @cur_key = $1
|
173
|
+
if @buffer.size >= bytes + 2
|
174
|
+
@values[$1] = @buffer.slice!(0,bytes)
|
175
|
+
@buffer.slice!(0,2) # \r\n
|
176
|
+
else
|
177
|
+
raise ParserError
|
178
|
+
end
|
179
|
+
|
180
|
+
when Cend # END
|
181
|
+
if entry = @get_cbs.shift
|
182
|
+
keys, cb = entry
|
183
|
+
cb.call(@values)
|
184
|
+
end
|
185
|
+
@values = {}
|
186
|
+
|
187
|
+
when Cstored # STORED
|
188
|
+
if cb = @set_cbs.shift
|
189
|
+
cb.call(true)
|
190
|
+
end
|
191
|
+
|
192
|
+
when Cdeleted # DELETED
|
193
|
+
if cb = @del_cbs.shift
|
194
|
+
cb.call(true)
|
195
|
+
end
|
196
|
+
|
197
|
+
when Cunknown # NOT_FOUND
|
198
|
+
if cb = @del_cbs.shift
|
199
|
+
cb.call(false)
|
200
|
+
end
|
201
|
+
|
202
|
+
else
|
203
|
+
p [:MEMCACHE_UNKNOWN, line]
|
204
|
+
end
|
205
|
+
end
|
206
|
+
|
207
|
+
#--
|
208
|
+
# def receive_binary_data data
|
209
|
+
# @values[@cur_key] = data[0..-3]
|
210
|
+
# end
|
211
|
+
|
212
|
+
def unbind
|
213
|
+
if @connected or @reconnecting
|
214
|
+
EM.add_timer(1){ reconnect @host, @port }
|
215
|
+
@connected = false
|
216
|
+
@reconnecting = true
|
217
|
+
@deferred_status = nil
|
218
|
+
else
|
219
|
+
raise 'Unable to connect to memcached server'
|
220
|
+
end
|
221
|
+
end
|
222
|
+
|
223
|
+
# :startdoc:
|
224
|
+
end
|
225
|
+
end
|
226
|
+
end
|
227
|
+
|
228
|
+
if __FILE__ == $0
|
229
|
+
# ruby -I ext:lib -r eventmachine -rubygems lib/protocols/memcache.rb
|
230
|
+
require 'em/spec'
|
231
|
+
|
232
|
+
class TestConnection # :nodoc:
|
233
|
+
include EM::P::Memcache
|
234
|
+
def send_data data
|
235
|
+
sent_data << data
|
236
|
+
end
|
237
|
+
def sent_data
|
238
|
+
@sent_data ||= ''
|
239
|
+
end
|
240
|
+
|
241
|
+
def initialize
|
242
|
+
connection_completed
|
243
|
+
end
|
244
|
+
end
|
245
|
+
|
246
|
+
EM.describe EM::Protocols::Memcache do
|
247
|
+
|
248
|
+
before{
|
249
|
+
@c = TestConnection.new
|
250
|
+
}
|
251
|
+
|
252
|
+
should 'send get requests' do
|
253
|
+
@c.get('a'){}
|
254
|
+
@c.sent_data.should == "get a\r\n"
|
255
|
+
done
|
256
|
+
end
|
257
|
+
|
258
|
+
should 'send set requests' do
|
259
|
+
@c.set('a', 1){}
|
260
|
+
@c.sent_data.should == "set a 0 0 1\r\n1\r\n"
|
261
|
+
done
|
262
|
+
end
|
263
|
+
|
264
|
+
should 'use noreply on set without block' do
|
265
|
+
@c.set('a', 1)
|
266
|
+
@c.sent_data.should == "set a 0 0 1 noreply\r\n1\r\n"
|
267
|
+
done
|
268
|
+
end
|
269
|
+
|
270
|
+
should 'send delete requests' do
|
271
|
+
@c.del('a')
|
272
|
+
@c.sent_data.should == "delete a 0 noreply\r\n"
|
273
|
+
done
|
274
|
+
end
|
275
|
+
|
276
|
+
should 'work when get returns no values' do
|
277
|
+
@c.get('a'){ |a|
|
278
|
+
a.should.be.nil
|
279
|
+
done
|
280
|
+
}
|
281
|
+
|
282
|
+
@c.receive_data "END\r\n"
|
283
|
+
end
|
284
|
+
|
285
|
+
should 'invoke block on set' do
|
286
|
+
@c.set('a', 1){
|
287
|
+
done
|
288
|
+
}
|
289
|
+
|
290
|
+
@c.receive_data "STORED\r\n"
|
291
|
+
end
|
292
|
+
|
293
|
+
should 'invoke block on delete' do
|
294
|
+
@c.delete('a'){ |found|
|
295
|
+
found.should.be.false
|
296
|
+
}
|
297
|
+
@c.delete('b'){ |found|
|
298
|
+
found.should.be.true
|
299
|
+
done
|
300
|
+
}
|
301
|
+
|
302
|
+
@c.receive_data "NOT_FOUND\r\n"
|
303
|
+
@c.receive_data "DELETED\r\n"
|
304
|
+
end
|
305
|
+
|
306
|
+
should 'parse split responses' do
|
307
|
+
@c.get('a'){ |a|
|
308
|
+
a.should == 'abc'
|
309
|
+
done
|
310
|
+
}
|
311
|
+
|
312
|
+
@c.receive_data "VAL"
|
313
|
+
@c.receive_data "UE a 0 "
|
314
|
+
@c.receive_data "3\r\n"
|
315
|
+
@c.receive_data "ab"
|
316
|
+
@c.receive_data "c"
|
317
|
+
@c.receive_data "\r\n"
|
318
|
+
@c.receive_data "EN"
|
319
|
+
@c.receive_data "D\r\n"
|
320
|
+
end
|
321
|
+
|
322
|
+
end
|
323
|
+
end
|