class Object

Constants

NoProtoStub

Public Instance Methods

debug_symbols_output_dir() click to toggle source
# File src/ruby/ext/grpc/extconf.rb, line 34
def debug_symbols_output_dir
  d = ENV['GRPC_RUBY_DEBUG_SYMBOLS_OUTPUT_DIR']
  return nil if d.nil? or d.size == 0
  d
end
do_div(stub) click to toggle source
# File src/ruby/bin/math_client.rb, line 44
def do_div(stub)
  GRPC.logger.info('request_response')
  GRPC.logger.info('----------------')
  req = Math::DivArgs.new(dividend: 7, divisor: 3)
  GRPC.logger.info("div(7/3): req=#{req.inspect}")
  resp = stub.div(req)
  GRPC.logger.info("Answer: #{resp.inspect}")
  GRPC.logger.info('----------------')
end
do_div_many(stub) click to toggle source
# File src/ruby/bin/math_client.rb, line 77
def do_div_many(stub)
  GRPC.logger.info('bidi_streamer')
  GRPC.logger.info('-------------')
  reqs = []
  reqs << Math::DivArgs.new(dividend: 7, divisor: 3)
  reqs << Math::DivArgs.new(dividend: 5, divisor: 2)
  reqs << Math::DivArgs.new(dividend: 7, divisor: 2)
  GRPC.logger.info("div(7/3), div(5/2), div(7/2): reqs=#{reqs.inspect}")
  resp = stub.div_many(reqs)
  resp.each do |r|
    GRPC.logger.info("Answer: #{r.inspect}")
  end
  GRPC.logger.info('----------------')
end
do_fib(stub) click to toggle source
# File src/ruby/bin/math_client.rb, line 65
def do_fib(stub)
  GRPC.logger.info('server_streamer')
  GRPC.logger.info('----------------')
  req = Math::FibArgs.new(limit: 11)
  GRPC.logger.info("fib(11): req=#{req.inspect}")
  resp = stub.fib(req)
  resp.each do |r|
    GRPC.logger.info("Answer: #{r.inspect}")
  end
  GRPC.logger.info('----------------')
end
do_sum(stub) click to toggle source
# File src/ruby/bin/math_client.rb, line 54
def do_sum(stub)
  # to make client streaming requests, pass an enumerable of the inputs
  GRPC.logger.info('client_streamer')
  GRPC.logger.info('---------------')
  reqs = [1, 2, 3, 4, 5].map { |x| Math::Num.new(num: x) }
  GRPC.logger.info("sum(1, 2, 3, 4, 5): reqs=#{reqs.inspect}")
  resp = stub.sum(reqs)  # reqs.is_a?(Enumerable)
  GRPC.logger.info("Answer: #{resp.inspect}")
  GRPC.logger.info('---------------')
end
env_append(name, string) click to toggle source
# File src/ruby/ext/grpc/extconf.rb, line 72
def env_append(name, string)
  ENV[name] += ' ' + string
end
env_unset?(name) click to toggle source
# File src/ruby/ext/grpc/extconf.rb, line 55
def env_unset?(name)
  ENV[name].nil? || ENV[name].size == 0
end
ext_export_filename() click to toggle source
# File src/ruby/ext/grpc/extconf.rb, line 169
def ext_export_filename()
  name = 'ext-export'
  name += '-truffleruby' if RUBY_ENGINE == 'truffleruby'
  name += '-with-ruby-abi-version' if have_ruby_abi_version()
  name
end
have_ruby_abi_version() click to toggle source
# File src/ruby/ext/grpc/extconf.rb, line 149
def have_ruby_abi_version()
  return true if RUBY_ENGINE == 'truffleruby'
  # ruby_abi_version is only available in development versions: https://github.com/ruby/ruby/pull/6231
  return false if RUBY_PATCHLEVEL >= 0

  m = /(\d+)\.(\d+)/.match(RUBY_VERSION)
  if m.nil?
    puts "Failed to parse ruby version: #{RUBY_VERSION}. Assuming ruby_abi_version symbol is NOT present."
    return false
  end
  major = m[1].to_i
  minor = m[2].to_i
  if major >= 3 and minor >= 2
    puts "Ruby version #{RUBY_VERSION} >= 3.2. Assuming ruby_abi_version symbol is present."
    return true
  end
  puts "Ruby version #{RUBY_VERSION} < 3.2. Assuming ruby_abi_version symbol is NOT present."
  false
end
inherit_env_or_rbconfig(name) click to toggle source
# File src/ruby/ext/grpc/extconf.rb, line 59
def inherit_env_or_rbconfig(name)
  ENV[name] = inherit_rbconfig(name) if env_unset?(name)
end
inherit_rbconfig(name, linker_flag: false) click to toggle source
# File src/ruby/ext/grpc/extconf.rb, line 63
def inherit_rbconfig(name, linker_flag: false)
  value = RbConfig::CONFIG[name] || ''
  if linker_flag
    value = maybe_remove_strip_all_linker_flag(value)
  end
  p "extconf.rb setting ENV[#{name}] = #{value}"
  ENV[name] = value
end
load_test_certs() click to toggle source
# File src/ruby/bin/math_client.rb, line 92
def load_test_certs
  this_dir = File.expand_path(File.dirname(__FILE__))
  data_dir = File.join(File.dirname(this_dir), 'spec/testdata')
  files = ['ca.pem', 'server1.key', 'server1.pem']
  files.map { |f| File.open(File.join(data_dir, f)).read }
end
main() click to toggle source
# File src/ruby/bin/math_client.rb, line 104
def main
  options = {
    'host' => 'localhost:7071',
    'secure' => false
  }
  OptionParser.new do |opts|
    opts.banner = 'Usage: [--host <hostname>:<port>] [--secure|-s]'
    opts.on('--host HOST', '<hostname>:<port>') do |v|
      options['host'] = v
    end
    opts.on('-s', '--secure', 'access using test creds') do |v|
      options['secure'] = v
    end
  end.parse!

  # The Math::Math:: module occurs because the service has the same name as its
  # package. That practice should be avoided by defining real services.
  if options['secure']
    stub_opts = {
      :creds => test_creds,
      GRPC::Core::Channel::SSL_TARGET => 'foo.test.google.fr',
      timeout: INFINITE_FUTURE,
    }
    stub = Math::Math::Stub.new(options['host'], **stub_opts)
    GRPC.logger.info("... connecting securely on #{options['host']}")
  else
    stub = Math::Math::Stub.new(options['host'], :this_channel_is_insecure, timeout: INFINITE_FUTURE)
    GRPC.logger.info("... connecting insecurely on #{options['host']}")
  end

  do_div(stub)
  do_sum(stub)
  do_fib(stub)
  do_div_many(stub)
end
maybe_remove_strip_all_linker_flag(flags) click to toggle source
# File src/ruby/ext/grpc/extconf.rb, line 40
def maybe_remove_strip_all_linker_flag(flags)
  if debug_symbols_output_dir
    # Hack to prevent automatic stripping during shared library linking.
    # rake-compiler-dock sets the -s LDFLAG when building rubies for
    # cross compilation, and this -s flag propagates into RbConfig. Stripping
    # during the link is problematic because it prevents us from saving
    # debug symbols. We want to first link our shared library, then save
    # debug symbols, and only after that strip.
    flags = flags.split(' ')
    flags = flags.reject {|flag| flag == '-s'}
    flags = flags.join(' ')
  end
  flags
end
test_creds() click to toggle source
# File src/ruby/bin/math_client.rb, line 99
def test_creds
  certs = load_test_certs
  GRPC::Core::ChannelCredentials.new(certs[0])
end
test_server_creds() click to toggle source
# File src/ruby/bin/math_server.rb, line 157
def test_server_creds
  certs = load_test_certs
  GRPC::Core::ServerCredentials.new(
    nil, [{ private_key: certs[1], cert_chain: certs[2] }], false)
end