Class: Cod::Beanstalk::Serializer
- Inherits:
-
Object
- Object
- Cod::Beanstalk::Serializer
- Defined in:
- lib/cod/beanstalk/serializer.rb
Overview
This is a kind of beanstalk message middleware: It generates and parses beanstalk messages from a ruby format into raw bytes. The raw bytes go directly into the tcp channel that underlies the beanstalk channel.
Messages are represented as simple Ruby arrays, specifying first the beanstalkd command, then arguments. Examples:
[:use, 'a_tube']
[:delete, 123]
One exception: The commands that have a body attached will be described like so in protocol.txt:
put <pri> <delay> <ttr> <bytes>\r\n
<data>\r\n
To generate this message, just put the data where the bytes would be and the serializer will do the right thing.
[:put, pri, delay, ttr, "my_small_data"]
Results come back in the same way, except that the answers take the place of the commands. Answers are always in upper case.
Also see raw.github.com/kr/beanstalkd/master/doc/protocol.txt.
Instance Method Summary collapse
Instance Method Details
#de(io) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/cod/beanstalk/serializer.rb', line 37 def de(io) str = io.gets("\r\n") raw = str.split cmd = convert_cmd(raw.first) msg = [cmd, *convert_args(raw[1..-1])] if [:ok, :reserved].include?(cmd) # More data to read: size = msg.last data = io.read(size+2) fail "No crlf at end of data?" unless data[-2..-1] == "\r\n" msg[-1] = data[0..-3] end msg end |
#en(msg) ⇒ Object
26 27 28 29 30 31 32 33 34 35 |
# File 'lib/cod/beanstalk/serializer.rb', line 26 def en(msg) cmd = msg.first if cmd == :put body = msg.last format(*msg[0..-2], body.bytesize) << format(body) else format(*msg) end end |