Module: Protobuf::Rpc::Connectors::Common
Instance Attribute Summary collapse
-
#error ⇒ Object
readonly
Returns the value of attribute error.
Instance Method Summary collapse
-
#_send_request ⇒ Object
Sends the request to the server, invoked by the connection_completed event.
- #any_callbacks? ⇒ Boolean
- #complete ⇒ Object
- #data_callback(data) ⇒ Object
- #fail(code, message) ⇒ Object
- #initialize_stats ⇒ Object
- #log_signature ⇒ Object
- #parse_response ⇒ Object
-
#post_init ⇒ Object
Setup the read buffer for data coming back.
- #succeed(response) ⇒ Object
- #verify_callbacks ⇒ Object
- #verify_options ⇒ Object
Instance Attribute Details
#error ⇒ Object (readonly)
Returns the value of attribute error.
6 7 8 |
# File 'lib/protobuf/rpc/connectors/common.rb', line 6 def error @error end |
Instance Method Details
#_send_request ⇒ Object
Sends the request to the server, invoked by the connection_completed event
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/protobuf/rpc/connectors/common.rb', line 108 def _send_request request_wrapper = Protobuf::Socketrpc::Request.new request_wrapper.service_name = @options[:service].name request_wrapper.method_name = @options[:method].to_s if @options[:request].class == @options[:request_type] request_wrapper.request_proto = @options[:request].serialize_to_string else expected = @options[:request_type].name actual = @options[:request].class.name fail :INVALID_REQUEST_PROTO, 'Expected request type to be type of %s, got %s instead' % [expected, actual] end log_debug "[#{log_signature}] Sending Request: %s" % request_wrapper.inspect request_buffer = Protobuf::Rpc::Buffer.new(:write, request_wrapper) @stats.request_size = request_buffer.size send_data(request_buffer.write) end |
#any_callbacks? ⇒ Boolean
8 9 10 11 12 |
# File 'lib/protobuf/rpc/connectors/common.rb', line 8 def any_callbacks? return [@complete_cb, @failure_cb, @success_cb].inject(false) do |reduction, cb| reduction = (reduction || !cb.nil?) end end |
#complete ⇒ Object
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/protobuf/rpc/connectors/common.rb', line 14 def complete @stats.end @stats.log_stats log_debug "[#{log_signature}] Response proceessing complete" @complete_cb.call(self) unless @complete_cb.nil? rescue log_error "[#{log_signature}] Complete callback error encountered: %s" % $!. log_error "[#{log_signature}] %s" % $!.backtrace.join("\n") raise end |
#data_callback(data) ⇒ Object
25 26 27 28 29 |
# File 'lib/protobuf/rpc/connectors/common.rb', line 25 def data_callback(data) log_debug "[#{log_signature}] Using data_callback" @used_data_callback = true @data = data end |
#fail(code, message) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/protobuf/rpc/connectors/common.rb', line 31 def fail(code, ) @error = ClientError.new @error.code = code.is_a?(Symbol) ? Protobuf::Socketrpc::ErrorReason.values[code] : code @error. = log_debug "[#{log_signature}] Server failed request (invoking on_failure): %s" % @error.inspect @failure_cb.call(@error) unless @failure_cb.nil? rescue log_error "[#{log_signature}] Failure callback error encountered: %s" % $!. log_error "[#{log_signature}] %s" % $!.backtrace.join("\n") raise ensure complete end |
#initialize_stats ⇒ Object
46 47 48 49 50 51 52 53 54 |
# File 'lib/protobuf/rpc/connectors/common.rb', line 46 def initialize_stats @stats = Protobuf::Rpc::Stat.new(:CLIENT, true) @stats.server = [@options[:port], @options[:host]] @stats.service = @options[:service].name @stats.method = @options[:method] self rescue => ex fail(:RPC_ERROR, "Invalid stats configuration. #{ex.}") end |
#log_signature ⇒ Object
56 57 58 |
# File 'lib/protobuf/rpc/connectors/common.rb', line 56 def log_signature @log_signature ||= "client-#{self.class}" end |
#parse_response ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/protobuf/rpc/connectors/common.rb', line 60 def parse_response # Close up the connection as we no longer need it close_connection log_debug "[#{log_signature}] Parsing response from server (connection closed)" @stats.response_size = @buffer.size # Parse out the raw response response_wrapper = Protobuf::Socketrpc::Response.new response_wrapper.parse_from_string(@buffer.data) # Determine success or failure based on parsed data if response_wrapper.has_field?(:error_reason) log_debug "[#{log_signature}] Error response parsed" # fail the call if we already know the client is failed # (don't try to parse out the response payload) fail(response_wrapper.error_reason, response_wrapper.error) else log_debug "[#{log_signature}] Successful response parsed" # Ensure client_response is an instance response_type = @options[:response_type].new parsed = response_type.parse_from_string(response_wrapper.response_proto.to_s) if parsed.nil? and not response_wrapper.has_field?(:error_reason) fail(:BAD_RESPONSE_PROTO, 'Unable to parse response from server') else verify_callbacks succeed(parsed) return @data if @used_data_callback end end end |
#post_init ⇒ Object
Setup the read buffer for data coming back
96 97 98 99 100 101 102 103 104 105 |
# File 'lib/protobuf/rpc/connectors/common.rb', line 96 def post_init # Setup an object for reponses without callbacks @data = nil log_debug "[#{log_signature}] Post init, new read buffer created" @buffer = Protobuf::Rpc::Buffer.new(:read) _send_request unless error? log_debug "[#{log_signature}] Post init, new read buffer created just sent" rescue fail(:RPC_ERROR, 'Connection error: %s' % $!.) end |
#succeed(response) ⇒ Object
127 128 129 130 131 132 133 134 135 136 |
# File 'lib/protobuf/rpc/connectors/common.rb', line 127 def succeed(response) log_debug "[#{log_signature}] Server succeeded request (invoking on_success)" @success_cb.call(response) unless @success_cb.nil? rescue log_error "[#{log_signature}] Success callback error encountered: %s" % $!. log_error "[#{log_signature}] %s" % $!.backtrace.join("\n") fail :RPC_ERROR, 'An exception occurred while calling on_success: %s' % $!. ensure complete end |
#verify_callbacks ⇒ Object
138 139 140 141 142 143 |
# File 'lib/protobuf/rpc/connectors/common.rb', line 138 def verify_callbacks if !any_callbacks? log_debug "[#{log_signature}] No callbacks set, using data_callback" @success_cb = @failure_cb = self.method(:data_callback) end end |
#verify_options ⇒ Object
145 146 147 148 149 150 |
# File 'lib/protobuf/rpc/connectors/common.rb', line 145 def # Verify the options that are necessary and merge them in [:service, :method, :host, :port].each do |opt| fail(:RPC_ERROR, "Invalid client connection configuration. #{opt} must be a defined option.") if @options[opt].nil? end end |