smparkes-eventmachine 0.12.10
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +15 -0
- data/README +81 -0
- data/Rakefile +374 -0
- data/docs/COPYING +60 -0
- data/docs/ChangeLog +211 -0
- data/docs/DEFERRABLES +133 -0
- data/docs/EPOLL +141 -0
- data/docs/GNU +281 -0
- data/docs/INSTALL +13 -0
- data/docs/KEYBOARD +38 -0
- data/docs/LEGAL +25 -0
- data/docs/LIGHTWEIGHT_CONCURRENCY +70 -0
- data/docs/PURE_RUBY +75 -0
- data/docs/RELEASE_NOTES +94 -0
- data/docs/SMTP +2 -0
- data/docs/SPAWNED_PROCESSES +89 -0
- data/docs/TODO +8 -0
- data/eventmachine.gemspec +40 -0
- data/examples/ex_channel.rb +43 -0
- data/examples/ex_queue.rb +2 -0
- data/examples/helper.rb +2 -0
- data/ext/binder.cpp +125 -0
- data/ext/binder.h +46 -0
- data/ext/cmain.cpp +827 -0
- data/ext/cplusplus.cpp +202 -0
- data/ext/ed.cpp +1901 -0
- data/ext/ed.h +424 -0
- data/ext/em.cpp +2288 -0
- data/ext/em.h +229 -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 +122 -0
- data/ext/eventmachine_cpp.h +96 -0
- data/ext/extconf.rb +150 -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/files.cpp +94 -0
- data/ext/files.h +65 -0
- data/ext/kb.cpp +81 -0
- data/ext/page.cpp +107 -0
- data/ext/page.h +51 -0
- data/ext/pipe.cpp +349 -0
- data/ext/project.h +156 -0
- data/ext/rubymain.cpp +1194 -0
- data/ext/sigs.cpp +89 -0
- data/ext/sigs.h +32 -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 +570 -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/java/src/com/rubyeventmachine/application/Application.java +194 -0
- data/java/src/com/rubyeventmachine/application/Connection.java +74 -0
- data/java/src/com/rubyeventmachine/application/ConnectionFactory.java +37 -0
- data/java/src/com/rubyeventmachine/application/DefaultConnectionFactory.java +46 -0
- data/java/src/com/rubyeventmachine/application/PeriodicTimer.java +38 -0
- data/java/src/com/rubyeventmachine/application/Timer.java +54 -0
- data/java/src/com/rubyeventmachine/tests/ApplicationTest.java +109 -0
- data/java/src/com/rubyeventmachine/tests/ConnectTest.java +148 -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 +75 -0
- data/java/src/com/rubyeventmachine/tests/TestTimers.java +90 -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 +564 -0
- data/lib/em/deferrable.rb +192 -0
- data/lib/em/file_watch.rb +54 -0
- data/lib/em/future.rb +61 -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/header_and_content.rb +138 -0
- data/lib/em/protocols/httpclient.rb +263 -0
- data/lib/em/protocols/httpclient2.rb +590 -0
- data/lib/em/protocols/line_and_text.rb +125 -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 +547 -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/protocols.rb +36 -0
- data/lib/em/queue.rb +61 -0
- data/lib/em/spawnable.rb +85 -0
- data/lib/em/streamer.rb +130 -0
- data/lib/em/timers.rb +56 -0
- data/lib/em/version.rb +3 -0
- data/lib/eventmachine.rb +1592 -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/evma.rb +32 -0
- data/lib/jeventmachine.rb +257 -0
- data/lib/pr_eventmachine.rb +1022 -0
- data/setup.rb +1585 -0
- data/tasks/cpp.rake_example +77 -0
- data/tests/client.crt +31 -0
- data/tests/client.key +51 -0
- data/tests/test_attach.rb +126 -0
- data/tests/test_basic.rb +284 -0
- data/tests/test_channel.rb +63 -0
- data/tests/test_connection_count.rb +35 -0
- data/tests/test_defer.rb +47 -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 +218 -0
- data/tests/test_httpclient.rb +218 -0
- data/tests/test_httpclient2.rb +153 -0
- data/tests/test_inactivity_timeout.rb +50 -0
- data/tests/test_kb.rb +60 -0
- data/tests/test_ltp.rb +182 -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 +48 -0
- data/tests/test_processes.rb +128 -0
- data/tests/test_proxy_connection.rb +92 -0
- data/tests/test_pure.rb +125 -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 +242 -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_timers.rb +162 -0
- data/tests/test_ud.rb +36 -0
- data/tests/testem.rb +31 -0
- data/web/whatis +7 -0
- metadata +237 -0
@@ -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
|
@@ -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
|