Module: Riakpb::Util::Decode

Included in:
Client::Rpc
Defined in:
lib/riakpb/util/decode.rb

Constant Summary collapse

PLEN =
(0..3)
PBMC =
PLEN.count
POFF =
(PBMC+1)

Instance Method Summary collapse

Instance Method Details

#decode_message(message) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/riakpb/util/decode.rb', line 11

def decode_message(message)
  
  pb_len  = 0
  pb_mc   = ''
  pb_msg  = ''
  remain  = ''

  until message.empty?
    pb_len  = message[PLEN].unpack('N')[0]    # message[0..3]unpack('N')[0]
    pb_mc   = pb_mc + message[PBMC]           # prior message codes + message[4]

    prange  = POFF..(pb_len+3)                # range for the start->finish of the pb message
    mrange  = (pb_len+4)..(message.size-1)    # range for any remaining portions of message

    break if(prange.count > message[prange].size)

    pb_msg  = pb_msg + message[prange]
    message = message[mrange]      # message[(5+pb_len)..(message.size)]
  end

  [pb_msg, pb_mc.unpack("c" * pb_mc.size), message]
end

#message_remaining?(message) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/riakpb/util/decode.rb', line 34

def message_remaining?(message)
  pb_len  = message[PLEN].unpack('N')[0]
  msg_len = message.size - PBMC

  puts "pb_len:#{pb_len}"
  puts "msg_len:#{msg_len}"
  puts "message:#{message.inspect}"

  return false  if pb_len  == msg_len
  return true   if pb_len   > msg_len

  return message_remaining?(message[(pb_len+4)..(msg_len-1)])
end