Class: EM::Tycoon::Protocol::Binary::PlayScriptMessage

Inherits:
Message
  • Object
show all
Defined in:
lib/em/tycoon/protocol/binary/play_script_message.rb

Constant Summary collapse

KV_PACK_FMT =
"NNa*a*"
HEADER_BYTES_PER_RECORD =
8

Constants inherited from Message

Message::DEFAULT_OPTS, Message::FLAGS, Message::MAGIC, Message::MSG_TYPES, Message::NO_EXPIRATION_TIME, Message::NO_XT_HEX, Message::PARSE_PHASES

Instance Attribute Summary

Attributes inherited from Message

#buffer, #bytes_expected, #data, #item_count, #keysize, #magic, #parse_phase, #parsed, #type, #valuesize

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Message

#[], check_msg_type, message_for, msg_type_for, #parse, #parsed?

Constructor Details

#initialize(data = {}, opts = {}) ⇒ PlayScriptMessage

Returns a new instance of PlayScriptMessage.



9
10
11
12
13
14
# File 'lib/em/tycoon/protocol/binary/play_script_message.rb', line 9

def initialize(data={},opts={})
  super(:play_script,data)
  @keys_parsed = 0
  @keysize = 0
  @valuesize = 0
end

Class Method Details

.generate(data, opts = {}) ⇒ Object

Raises:

  • (ArgumentError)


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/em/tycoon/protocol/binary/play_script_message.rb', line 16

def self.generate(data,opts={})
 raise ArgumentError.new("Unsupported data type : #{data.class.name}") unless data.kind_of?(Array)
 raise ArgumentError.new("Expected array of [<String>,<Hash>]") unless (data.length == 2) && data.first.kind_of?(String) && data.last.kind_of?(Hash)
 script_name = data.first
 args = data.last
 msg_array = [MAGIC[:play_script]]
 optflags = 0
 opts.each_pair do |optkey,optval|
    optflags |= FLAGS[optkey] if (FLAGS.has_key?(optkey) and (optval == true))
 end
 msg_array << optflags
 msg_array << script_name.bytesize
 msg_array << args.keys.length
 msg_array << script_name
 args.each_pair do |key,value|
   value = value
   msg_array << key.bytesize
   msg_array << value.bytesize
   msg_array << key.to_s
   msg_array << value.to_s
 end
 return msg_array.pack("CNNNa*#{KV_PACK_FMT*args.keys.length}")
end

Instance Method Details

#parse_chunk(data) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/em/tycoon/protocol/binary/play_script_message.rb', line 40

def parse_chunk(data)
  return 0 unless data && data.bytesize > 0
  msg_hsh = {}
  bytes_parsed = 0
  case parse_phase
  when :magic,:item_count
    @magic, @item_count = data.unpack("CN")
    @parse_phase = :header
    @bytes_expected += HEADER_BYTES_PER_RECORD
    bytes_parsed = 5
    @data = Hash.new
  when :header
    @keysize,@valuesize = data.unpack("NN")
    @bytes_expected += (@keysize+@valuesize)
    bytes_parsed = HEADER_BYTES_PER_RECORD
    @parse_phase = :keys_and_values
  when :keys_and_values
    k,v = data.unpack("a#{@keysize}a#{@valuesize}")
    @data[k] = {:value => v}
    bytes_parsed = (@keysize+@valuesize)
    @keysize = 0
    @valuesize = 0
    if (@keys_parsed+=1) == item_count
      @parse_phase = :done
    else
      @parse_phase = :header
      @bytes_expected += HEADER_BYTES_PER_RECORD
    end
  end
  return bytes_parsed
end