Class: Arf::RPC::BaseMessage

Inherits:
Object
  • Object
show all
Defined in:
lib/arf/rpc/base_message.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**kwargs) ⇒ BaseMessage

Returns a new instance of BaseMessage.



88
89
90
91
92
93
# File 'lib/arf/rpc/base_message.rb', line 88

def initialize(**kwargs)
  kwargs.each { |k, v| send("#{k}=", v) }
  @params ||= [] if respond_to? :params=
  @streaming ||= false if respond_to? :streaming=
  @metadata ||= Metadata.new if respond_to? :metadata=
end

Class Method Details

.encode(message) ⇒ Object



25
26
27
28
29
30
# File 'lib/arf/rpc/base_message.rb', line 25

def self.encode(message)
  IO::Buffer.new
    .write(MESSAGE_KIND_FROM_SYMBOL[message.kind])
    .write_raw(message.encode)
    .string
end

.has_metadataObject



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/arf/rpc/base_message.rb', line 56

def self.
  attr_reader :metadata

  define_method(:metadata=) do |val|
    case val
    when NilClass
      @metadata = Metadata.new
    when Metadata
      @metadata = val
    when Hash
      @metadata = Metadata.new(**val)
    else
      raise ArgumentError, "Invalid value #{val.inspect} for metadata: Expected nil, Arf::RPC::Metadata, or Hash"
    end
  end
end

.has_statusObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/arf/rpc/base_message.rb', line 32

def self.has_status
  define_method(:status=) do |val|
    case val
    when Symbol
      @status = Status::FROM_SYMBOL[val]
      raise ArgumentError, "Invalid value #{val.inspect} for status: Unknown status" unless @status
    when Integer
      if Status::TO_SYMBOL[val].nil?
        raise ArgumentError, "Invalid value #{val.inspect} for status: Unknown status"
      end

      @status = val
    else
      raise ArgumentError, "Invalid value #{val.inspect} for status: Expected symbol or integer"
    end
  end

  define_method(:status) do
    return nil if @status.nil?

    Status::TO_SYMBOL[@status] || :unknown
  end
end

.has_streamingObject



73
74
75
76
77
# File 'lib/arf/rpc/base_message.rb', line 73

def self.has_streaming
  attr_accessor :streaming

  define_method(:streaming?) { @streaming }
end

.initialize_from(data) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/arf/rpc/base_message.rb', line 16

def self.initialize_from(data)
  type = MESSAGE_KIND_FROM_BYTE[data.readbyte] || :invalid
  raise "Cannot decode invalid message" if type == :invalid

  instance = message_by_kind(type).new
  instance.decode(data)
  instance
end

.kind(kind = nil) ⇒ Object



79
80
81
82
83
84
85
86
# File 'lib/arf/rpc/base_message.rb', line 79

def self.kind(kind = nil)
  return @kind if kind.nil?

  @kind = kind
  define_method(:kind) { self.class.kind }
  BaseMessage.register(kind, self)
  kind
end

.message_by_kind(kind) ⇒ Object



11
12
13
14
# File 'lib/arf/rpc/base_message.rb', line 11

def self.message_by_kind(kind)
  @messages ||= {}
  @messages[kind]
end

.register(kind, cls) ⇒ Object



6
7
8
9
# File 'lib/arf/rpc/base_message.rb', line 6

def self.register(kind, cls)
  @messages ||= {}
  @messages[kind] = cls
end

Instance Method Details

#decode(_data) ⇒ Object



97
# File 'lib/arf/rpc/base_message.rb', line 97

def decode(_data) = nil

#decode_bytes(data) ⇒ Object



106
107
108
109
110
111
# File 'lib/arf/rpc/base_message.rb', line 106

def decode_bytes(data)
  type, header = Proto.read_type(data)
  raise "Invalid message payload" if type != Proto::TYPE_BYTES

  Proto.decode_bytes(header, data)
end

#decode_string(data) ⇒ Object



99
100
101
102
103
104
# File 'lib/arf/rpc/base_message.rb', line 99

def decode_string(data)
  type, header = Proto.read_type(data)
  raise "Invalid message payload" if type != Proto::TYPE_STRING

  Proto.decode_string(header, data)
end

#decode_uint16(data) ⇒ Object



113
# File 'lib/arf/rpc/base_message.rb', line 113

def decode_uint16(data) = Wire.decode_uint16(data)

#encodeObject



95
# File 'lib/arf/rpc/base_message.rb', line 95

def encode = ""

#encode_bytes(data) ⇒ Object



116
# File 'lib/arf/rpc/base_message.rb', line 116

def encode_bytes(data) = Proto.encode_bytes(data)

#encode_string(str) ⇒ Object



115
# File 'lib/arf/rpc/base_message.rb', line 115

def encode_string(str) = Proto.encode_string(str)

#encode_uint16(value) ⇒ Object



114
# File 'lib/arf/rpc/base_message.rb', line 114

def encode_uint16(value) = Wire.encode_uint16(value)