Class: Async::GRPC::Service

Inherits:
Object
  • Object
show all
Defined in:
lib/async/grpc/service.rb

Overview

Represents a concrete service implementation that uses an Interface. Subclass this and implement the RPC methods defined in the interface. Services are registered with Dispatcher for routing.

Examples:

Example service implementation:

class GreeterInterface < Protocol::GRPC::Interface
  rpc :SayHello, request_class: Hello::HelloRequest, response_class: Hello::HelloReply
  # Optional: explicit method name for edge cases
  rpc :XMLParser, request_class: Hello::ParseRequest, response_class: Hello::ParseReply,
      method: :xml_parser  # Explicit method name (otherwise would be :xmlparser)
end

class GreeterService < Async::GRPC::Service
  def say_hello(input, output, call)
    request = input.read
    reply = Hello::HelloReply.new(message: "Hello, #{request.name}!")
    output.write(reply)
  end

  def xml_parser(input, output, call)
    # Implementation using explicit method name
  end
end

# Register with dispatcher:
dispatcher = Dispatcher.new
dispatcher.register(GreeterService.new(GreeterInterface, "hello.Greeter"))
server = Async::HTTP::Server.for(endpoint, dispatcher)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(interface_class, service_name) ⇒ Service

Initialize a new service instance.



42
43
44
45
# File 'lib/async/grpc/service.rb', line 42

def initialize(interface_class, service_name)
	@interface_class = interface_class
	@service_name = service_name
end

Instance Attribute Details

#interface_classObject (readonly)

Returns the value of attribute interface_class.



48
49
50
# File 'lib/async/grpc/service.rb', line 48

def interface_class
  @interface_class
end

#service_nameObject (readonly)

Returns the value of attribute service_name.



51
52
53
# File 'lib/async/grpc/service.rb', line 51

def service_name
  @service_name
end

#The service name.(servicename.) ⇒ Object (readonly)



51
# File 'lib/async/grpc/service.rb', line 51

attr_reader :service_name

Instance Method Details

#rpc_descriptionsObject

Get RPC descriptions from the interface class. Converts Interface RPC definitions (PascalCase) to rpc_descriptions format. Maps gRPC method names (PascalCase) to Ruby method names (snake_case).



57
58
59
60
61
62
63
64
65
66
# File 'lib/async/grpc/service.rb', line 57

def rpc_descriptions
	descriptions = {}
	
	@interface_class.rpcs.each do |pascal_case_name, rpc|
		# rpc.method is always set (either explicitly or auto-converted in Interface.rpc)
		descriptions[pascal_case_name.to_s] = rpc
	end
	
	descriptions
end

#The interface class.=(interface) ⇒ Object



48
# File 'lib/async/grpc/service.rb', line 48

attr_reader :interface_class