Module: KRPC::Encoder

Defined in:
lib/krpc/encoder.rb

Constant Summary collapse

RPC_HELLO_MESSAGE =
"\x48\x45\x4C\x4C\x4F\x2D\x52\x50\x43\x00\x00\x00"
STREAM_HELLO_MESSAGE =
"\x48\x45\x4C\x4C\x4F\x2D\x53\x54\x52\x45\x41\x4D"
NAME_LENGTH =
32

Class Method Summary collapse

Class Method Details

.encode(obj, type) ⇒ Object

Given a type object, and ruby object, encode the ruby object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/krpc/encoder.rb', line 13

def encode(obj, type)
  if type.is_a?(Types::MessageType) then type.ruby_type.encode(obj)
  elsif type.is_a?(Types::ValueType) then encode_value(obj, type)
  elsif type.is_a?(Types::EnumType)
    enum_value = type.ruby_type[obj]
    encode_value(enum_value, TypeStore["int32"])
  elsif type.is_a?(Types::ClassType)
    remote_oid = if obj == nil then 0 else obj.remote_oid end
    encode_value(remote_oid, TypeStore["uint64"])
  elsif type.is_a?(Types::ListType)
    ruby_type = TypeStore["KRPC.List"].ruby_type
    msg = ruby_type.new(
      items: obj.map{|x| encode(TypeStore.coerce_to(x, type.value_type), type.value_type)}.to_a
    )
    ruby_type.encode(msg)
  elsif type.is_a?(Types::DictionaryType)
    ruby_type = TypeStore["KRPC.Dictionary"].ruby_type
    entry_type = TypeStore["KRPC.DictionaryEntry"].ruby_type
    entries = obj.map do |k,v|
      entry_type.new(
        key: encode(TypeStore.coerce_to(k, type.key_type), type.key_type),
        value: encode(TypeStore.coerce_to(v, type.value_type), type.value_type)
      )
    end
    msg = ruby_type.new(entries: entries)
    ruby_type.encode(msg)
  elsif type.is_a?(Types::SetType)
    ruby_type = TypeStore["KRPC.Set"].ruby_type
    msg = ruby_type.new(
      items: obj.map{|x| encode( TypeStore.coerce_to(x, type.value_type), type.value_type )}.to_a
    )
    ruby_type.encode(msg)
  elsif type.is_a?(Types::TupleType)
    ruby_type = TypeStore["KRPC.Tuple"].ruby_type
    msg = ruby_type.new(
      items: obj.zip(type.value_types).map{|x,t| encode( TypeStore.coerce_to(x, t), t )}.to_a
    )
    ruby_type.encode(msg)
  else raise(RuntimeError, "Cannot encode object #{obj} of type #{type}")
  end
end

.encode_request(req) ⇒ Object



59
60
61
62
63
# File 'lib/krpc/encoder.rb', line 59

def encode_request(req)
  data = PB::Request.encode(req)
  length = ProtobufUtils::Encoder.encode_nonnegative_varint(data.length)
  length + data
end

.encode_value(value, type) ⇒ Object



55
56
57
# File 'lib/krpc/encoder.rb', line 55

def encode_value(value, type)
  ProtobufUtils::Encoder.encode(value, type.protobuf_type)
end

.hash_to_enumeration_values(hash) ⇒ Object



65
66
67
# File 'lib/krpc/encoder.rb', line 65

def hash_to_enumeration_values(hash)
  hash.map {|k,v| PB::EnumerationValue.new(name: k.to_s, value: v) }
end