Class: Arf::RPC::ClientBase

Inherits:
Object
  • Object
show all
Includes:
Types::Mixin
Defined in:
lib/arf/rpc/client_base.rb

Constant Summary

Constants included from Types::Mixin

Types::Mixin::ArrayType, Types::Mixin::InOutStream, Types::Mixin::InputStream, Types::Mixin::MapType, Types::Mixin::OutputStream, Types::Mixin::Streamer

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Types::Mixin

included

Constructor Details

#initialize(client) ⇒ ClientBase

Returns a new instance of ClientBase.



59
60
61
# File 'lib/arf/rpc/client_base.rb', line 59

def initialize(client)
  @client = client
end

Class Method Details

.arf_service_id(id) ⇒ Object



8
9
10
# File 'lib/arf/rpc/client_base.rb', line 8

def self.arf_service_id(id)
  @arf_service_id = id
end

.rpc(name, inputs: nil, outputs: nil) ⇒ Object



12
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
54
55
56
57
# File 'lib/arf/rpc/client_base.rb', line 12

def self.rpc(name, inputs: nil, outputs: nil)
  @rpc_metadata ||= {}
  inputs ||= {}
  outputs = [outputs].compact unless outputs.is_a? Array

  @rpc_metadata[name] = {
    name:, inputs: inputs.dup, outputs: outputs.dup
  }

  inputs = inputs.to_a
    .map do |type|
      if type.last.is_a? InputStream
        outputs << type.last
        next nil
      end
      type
    end
    .compact.to_h

  if outputs.length > 1 && outputs[-2].is_a?(Streamer) && outputs[-1].is_a?(Streamer)
    input = outputs.find { _1.is_a? InputStream }
    output = outputs.find { _1.is_a? OutputStream }

    outputs = outputs[...-2]
    outputs << InOutStream[input.type, output.type]
  end

  @rpc_metadata[name][:native_inputs] = inputs
  @rpc_metadata[name][:native_outputs] = outputs

  internal_name = "_#{name}"
  service_name = @arf_service_id

  class_eval(<<~RUBY, __FILE__, __LINE__ + 1)
    # def name(param, param, param, **metadata, &block)
    #   internal_name(params, param, param, **metadata, &block)
    # end
    def #{name}(#{inputs.keys.join(", ")}#{inputs.length.positive? ? ", " : ""}**metadata, &block)
      #{internal_name}(#{inputs.keys.join(", ")}#{inputs.length.positive? ? ", " : ""}**metadata, &block)
    end
  RUBY

  define_method(internal_name) do |*args, **, &block|
    _invoke_method(service_name, name, inputs.values, outputs, args, , &block)
  end
end

Instance Method Details

#_invoke_method(service_id, method_name, inputs, outputs, args, meta) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/arf/rpc/client_base.rb', line 63

def _invoke_method(service_id, method_name, inputs, outputs, args, meta)
  mapped_args = args.map.with_index do |v, idx|
    raw_type = case (t = inputs[idx])
               when Symbol then t
               when String then self.class.find_type(t)
               else
                 raise ArgumentError, "Unknown type definition #{v.inspect}"
               end

    Arf::Types.coerce_value(v, raw_type)
  end

  # Obtain a stream from @client
  str = @client.new_stream

  # Push the request
  streaming = outputs.any? { _1.is_a?(InputStream) || _1.is_a?(InOutStream) }
  req = Request.new(
    streaming:,
    metadata: meta,
    service: service_id,
    method: method_name,
    params: mapped_args
  )

  input_type = nil
  output_type = nil

  streamer = outputs.last
  case streamer
  when InputStream
    input_type = streamer.resolved_type(self.class)
  when OutputStream
    output_type = streamer.resolved_type(self.class)
  when InOutStream
    input_type = streamer.resolved_input(self.class)
    output_type = streamer.resolved_output(self.class)
  end

  resp = Responder.new(str, input_type, output_type)

  str.write_data(BaseMessage.encode(req), end_stream: !streaming)

  resp
end