Class: Protobuf::Rpc::Clients::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/protobuf/rpc/clients/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host: nil, port: nil, timeout: 1, stdout: STDOUT, stderr: STDERR, namespace: nil) ⇒ Base

Returns a new instance of Base.



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/protobuf/rpc/clients/base.rb', line 10

def initialize(host: nil, port: nil, timeout: 1, stdout: STDOUT, stderr: STDERR, namespace: nil)
  @host = host
  @port = port
  @timeout = timeout
  @namespace = namespace
  @logger = Logger.new(stdout)
  @error_logger = Logger.new(stderr)

  names = self.class.name.split('::')
  @service =  Object.const_get([names[0..-3], 'Services', names[-1]].flatten.join('::'))
end

Instance Attribute Details

#error_loggerObject

Returns the value of attribute error_logger.



5
6
7
# File 'lib/protobuf/rpc/clients/base.rb', line 5

def error_logger
  @error_logger
end

#hostObject

Returns the value of attribute host.



6
7
8
# File 'lib/protobuf/rpc/clients/base.rb', line 6

def host
  @host
end

#loggerObject

Returns the value of attribute logger.



5
6
7
# File 'lib/protobuf/rpc/clients/base.rb', line 5

def logger
  @logger
end

#portObject

Returns the value of attribute port.



6
7
8
# File 'lib/protobuf/rpc/clients/base.rb', line 6

def port
  @port
end

#timeoutObject

Returns the value of attribute timeout.



6
7
8
# File 'lib/protobuf/rpc/clients/base.rb', line 6

def timeout
  @timeout
end

Instance Method Details

#check_response_error(res, raise_error: true) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/protobuf/rpc/clients/base.rb', line 42

def check_response_error(res, raise_error: true)
  case res
    when ::Protobuf::Rpc::Messages::Error
      error_class = self.class.mutex.synchronize { define_error_class(res) }
      error = error_class.new(res.error_message)
      error.set_backtrace(res.error_backtrace)
      raise_error(error, raise_error)
    when ::Protobuf::Error, ::Protobuf::Rpc::ClientError
      error_reason = ::Protobuf::Socketrpc::ErrorReason.name_for_tag(res.code).to_s.downcase
      error_class = ::Protobuf::Rpc.const_get(error_reason.camelize)
      error = error_class.new(res.message)
      raise_error(error, raise_error)
    else
      res
  end
end

#internal_clientObject



22
23
24
# File 'lib/protobuf/rpc/clients/base.rb', line 22

def internal_client
  @service.client(host: host, port: port, timeout: timeout)
end

#send_rpc_request(method, msg, serializer:) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/protobuf/rpc/clients/base.rb', line 26

def send_rpc_request(method, msg, serializer:)
  res = nil

  internal_client.send(method, ::Protobuf::Rpc::Serializer.dump(msg, serializer: serializer)) do |c|
    c.on_success do |rpc_compressed_message|
      res = ::Protobuf::Rpc::Serializer.load(rpc_compressed_message)
    end

    c.on_failure do |error|
      res = error
    end
  end

  check_response_error(res)
end