Class: Protobuf::Rpc::Service

Inherits:
Object
  • Object
show all
Includes:
Logging, ServiceFilters
Defined in:
lib/protobuf/rpc/service.rb

Constant Summary collapse

DEFAULT_HOST =
'127.0.0.1'.freeze
DEFAULT_PORT =
9399

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

initialize_logger, #log_exception, #log_signature, #logger, logger, logger=, #sign_message

Constructor Details

#initialize(env) ⇒ Service

Constructor!

Initialize a service with the rpc endpoint name and the bytes for the request.



26
27
28
29
# File 'lib/protobuf/rpc/service.rb', line 26

def initialize(env)
  @env = env.dup # Dup the env so it doesn't change out from under us
  @request = env.request
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



19
20
21
# File 'lib/protobuf/rpc/service.rb', line 19

def env
  @env
end

#requestObject (readonly)

Returns the value of attribute request.



19
20
21
# File 'lib/protobuf/rpc/service.rb', line 19

def request
  @request
end

Class Method Details

.client(options = {}) ⇒ Object

Class Methods

Create a new client for the given service. See Client#initialize and ClientConnection::DEFAULT_OPTIONS for all available options.



38
39
40
41
42
# File 'lib/protobuf/rpc/service.rb', line 38

def self.client(options = {})
  ::Protobuf::Rpc::Client.new({ :service => self,
                                :host => host,
                                :port => port }.merge(options))
end

.configure(config = {}) ⇒ Object

Allows service-level configuration of location. Useful for system-startup configuration of a service so that any Clients using the Service.client sugar will not have to configure the location each time.



49
50
51
52
# File 'lib/protobuf/rpc/service.rb', line 49

def self.configure(config = {})
  self.host = config[:host] if config.key?(:host)
  self.port = config[:port] if config.key?(:port)
end

.hostObject

The host location of the service.



56
57
58
# File 'lib/protobuf/rpc/service.rb', line 56

def self.host
  @_host ||= DEFAULT_HOST
end

.host=(new_host) ⇒ Object

The host location setter.



62
63
64
# File 'lib/protobuf/rpc/service.rb', line 62

def self.host=(new_host)
  @_host = new_host
end

.implemented_servicesObject

An array of defined service classes that contain implementation code



68
69
70
71
72
73
74
75
76
# File 'lib/protobuf/rpc/service.rb', line 68

def self.implemented_services
  classes = (self.subclasses || []).select do |subclass|
    subclass.rpcs.any? do |(name, _)|
      subclass.method_defined? name
    end
  end

  classes.map(&:name)
end

.located_at(location) ⇒ Object

Shorthand call to configure, passing a string formatted as hostname:port e.g. 127.0.0.1:9933 e.g. localhost:0



82
83
84
85
86
# File 'lib/protobuf/rpc/service.rb', line 82

def self.located_at(location)
  return if location.nil? || location.downcase.strip !~ /.+:\d+/
  host, port = location.downcase.strip.split ':'
  configure(:host => host, :port => port.to_i)
end

.portObject

The port of the service on the destination server.



90
91
92
# File 'lib/protobuf/rpc/service.rb', line 90

def self.port
  @_port ||= DEFAULT_PORT
end

.port=(new_port) ⇒ Object

The port location setter.



96
97
98
# File 'lib/protobuf/rpc/service.rb', line 96

def self.port=(new_port)
  @_port = new_port
end

.rpc(method, request_type, response_type) ⇒ Object

Define an rpc method with the given request and response types. This methods is only used by the generated service definitions and not useful for user code.



104
105
106
# File 'lib/protobuf/rpc/service.rb', line 104

def self.rpc(method, request_type, response_type)
  rpcs[method] = RpcMethod.new(method, request_type, response_type)
end

.rpc_method?(name) ⇒ Boolean

Check if the given method name is a known rpc endpoint.

Returns:

  • (Boolean)


116
117
118
# File 'lib/protobuf/rpc/service.rb', line 116

def self.rpc_method?(name)
  rpcs.key?(name)
end

.rpcsObject

Hash containing the set of methods defined via rpc.



110
111
112
# File 'lib/protobuf/rpc/service.rb', line 110

def self.rpcs
  @_rpcs ||= {}
end

Instance Method Details

#callable_rpc_method(method_name) ⇒ Object

Instance Methods

Get a callable object that will be used by the dispatcher to invoke the specified rpc method. Facilitates callback dispatch. The returned lambda is expected to be called at a later time (which is why we wrap the method call).



128
129
130
# File 'lib/protobuf/rpc/service.rb', line 128

def callable_rpc_method(method_name)
  lambda { run_filters(method_name) }
end

#responseObject

Response object for this rpc cycle. Not assignable.



134
135
136
# File 'lib/protobuf/rpc/service.rb', line 134

def response
  @_response ||= response_type.new
end

#rpc_method?(name) ⇒ Boolean

Convenience method to get back to class method.

Returns:

  • (Boolean)


140
141
142
# File 'lib/protobuf/rpc/service.rb', line 140

def rpc_method?(name)
  self.class.rpc_method?(name)
end

#rpcsObject

Convenience method to get back to class rpcs hash.



146
147
148
# File 'lib/protobuf/rpc/service.rb', line 146

def rpcs
  self.class.rpcs
end