Class: Protobuf::Rpc::Service
- Inherits:
-
Object
- Object
- Protobuf::Rpc::Service
- Includes:
- Logging, ServiceFilters
- Defined in:
- lib/protobuf/rpc/service.rb
Constant Summary collapse
- DEFAULT_HOST =
'127.0.0.1'.freeze
- DEFAULT_PORT =
9399
Class Attribute Summary collapse
-
.host ⇒ Object
The host location of the service.
-
.port ⇒ Object
The port of the service on the destination server.
Instance Attribute Summary collapse
-
#env ⇒ Object
readonly
Returns the value of attribute env.
-
#request ⇒ Object
readonly
Returns the value of attribute request.
Class Method Summary collapse
-
.client(options = {}) ⇒ Object
Class Methods.
-
.configure(config = {}) ⇒ Object
Allows service-level configuration of location.
-
.implemented_services ⇒ Object
An array of defined service classes that contain implementation code.
-
.located_at(location) ⇒ Object
Shorthand call to configure, passing a string formatted as hostname:port e.g.
-
.rpc(method, request_type, response_type) ⇒ Object
Define an rpc method with the given request and response types.
-
.rpc_method?(name) ⇒ Boolean
Check if the given method name is a known rpc endpoint.
-
.rpcs ⇒ Object
Hash containing the set of methods defined via
rpc
.
Instance Method Summary collapse
- #call(method_name) ⇒ Object
-
#initialize(env) ⇒ Service
constructor
Constructor!.
-
#response ⇒ Object
Response object for this rpc cycle.
-
#rpc_method?(name) ⇒ Boolean
Convenience method to get back to class method.
-
#rpcs ⇒ Object
Convenience method to get back to class rpcs hash.
Methods included from Logging
initialize_logger, #log_exception, #log_signature, #logger, #sign_message
Constructor Details
#initialize(env) ⇒ Service
Constructor!
Initialize a service with the rpc endpoint name and the bytes for the request.
28 29 30 31 |
# File 'lib/protobuf/rpc/service.rb', line 28 def initialize(env) @env = env.dup # Dup the env so it doesn't change out from under us @request = env.request end |
Class Attribute Details
.host ⇒ Object
The host location of the service.
58 59 60 |
# File 'lib/protobuf/rpc/service.rb', line 58 def self.host @host ||= DEFAULT_HOST end |
.port ⇒ Object
The port of the service on the destination server.
92 93 94 |
# File 'lib/protobuf/rpc/service.rb', line 92 def self.port @port ||= DEFAULT_PORT end |
Instance Attribute Details
#env ⇒ Object (readonly)
Returns the value of attribute env.
21 22 23 |
# File 'lib/protobuf/rpc/service.rb', line 21 def env @env end |
#request ⇒ Object (readonly)
Returns the value of attribute request.
21 22 23 |
# File 'lib/protobuf/rpc/service.rb', line 21 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.
40 41 42 43 44 |
# File 'lib/protobuf/rpc/service.rb', line 40 def self.client( = {}) ::Protobuf::Rpc::Client.new({ :service => self, :host => host, :port => port }.merge()) 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.
51 52 53 54 |
# File 'lib/protobuf/rpc/service.rb', line 51 def self.configure(config = {}) self.host = config[:host] if config.key?(:host) self.port = config[:port] if config.key?(:port) end |
.implemented_services ⇒ Object
An array of defined service classes that contain implementation code
70 71 72 73 74 75 76 77 78 |
# File 'lib/protobuf/rpc/service.rb', line 70 def self.implemented_services classes = (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
84 85 86 87 88 |
# File 'lib/protobuf/rpc/service.rb', line 84 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 |
.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.
106 107 108 |
# File 'lib/protobuf/rpc/service.rb', line 106 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.
118 119 120 |
# File 'lib/protobuf/rpc/service.rb', line 118 def self.rpc_method?(name) rpcs.key?(name) end |
.rpcs ⇒ Object
Hash containing the set of methods defined via rpc
.
112 113 114 |
# File 'lib/protobuf/rpc/service.rb', line 112 def self.rpcs @rpcs ||= {} end |
Instance Method Details
#call(method_name) ⇒ Object
122 123 124 |
# File 'lib/protobuf/rpc/service.rb', line 122 def call(method_name) run_filters(method_name) end |
#response ⇒ Object
Response object for this rpc cycle. Not assignable.
128 129 130 |
# File 'lib/protobuf/rpc/service.rb', line 128 def response @response ||= response_type.new end |
#rpc_method?(name) ⇒ Boolean
Convenience method to get back to class method.
134 135 136 |
# File 'lib/protobuf/rpc/service.rb', line 134 def rpc_method?(name) self.class.rpc_method?(name) end |
#rpcs ⇒ Object
Convenience method to get back to class rpcs hash.
140 141 142 |
# File 'lib/protobuf/rpc/service.rb', line 140 def rpcs self.class.rpcs end |