Class: JRPC::SimpleClient
- Inherits:
-
Object
- Object
- JRPC::SimpleClient
- Includes:
- PayloadLogging
- Defined in:
- lib/jrpc/simple_client.rb
Overview
NOT thread-safe: a SimpleClient instance must not be shared across threads or fibers. It owns a single transport/socket plus an unsynchronized id counter, so concurrent calls would interleave socket reads/writes and corrupt the framing buffer. Use one instance per thread/fiber (or a pool of instances).
Constant Summary
Constants included from PayloadLogging
PayloadLogging::RECV_MARK, PayloadLogging::SEND_MARK
Instance Attribute Summary collapse
-
#server ⇒ Object
readonly
Returns the value of attribute server.
Instance Method Summary collapse
- #close ⇒ Object
- #closed? ⇒ Boolean
-
#initialize(server, **options) ⇒ SimpleClient
constructor
A new instance of SimpleClient.
- #notification(method, params = nil, write_timeout: @write_timeout) ⇒ Object
- #request(method, params = nil, read_timeout: @read_timeout, write_timeout: @write_timeout) ⇒ Object
Methods included from PayloadLogging
Constructor Details
#initialize(server, **options) ⇒ SimpleClient
Returns a new instance of SimpleClient.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/jrpc/simple_client.rb', line 13 def initialize(server, **) @closed = false @server = server @read_timeout = .fetch(:read_timeout, 60) @write_timeout = .fetch(:write_timeout, 60) @autoclose = .fetch(:autoclose, false) @logger = [:logger] @transport = .fetch(:transport) do Transport.build(server, **) end @id_gen = .fetch(:id_gen) do IdGenerator.new(prefix: [:id_prefix], thread_safe: false) end end |
Instance Attribute Details
#server ⇒ Object (readonly)
Returns the value of attribute server.
11 12 13 |
# File 'lib/jrpc/simple_client.rb', line 11 def server @server end |
Instance Method Details
#close ⇒ Object
61 62 63 64 65 66 67 |
# File 'lib/jrpc/simple_client.rb', line 61 def close return true if @closed @transport.close @closed = true true end |
#closed? ⇒ Boolean
69 70 71 |
# File 'lib/jrpc/simple_client.rb', line 69 def closed? @closed end |
#notification(method, params = nil, write_timeout: @write_timeout) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/jrpc/simple_client.rb', line 48 def notification(method, params = nil, write_timeout: @write_timeout) raise Errors::ClientError, 'client closed' if @closed json = Message.dump(Message.build_notification(method, params)) with_transport_error_handling do connect_if_needed! log_sent(json) @transport.write_frame(json, timeout: write_timeout) nil end end |
#request(method, params = nil, read_timeout: @read_timeout, write_timeout: @write_timeout) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/jrpc/simple_client.rb', line 28 def request(method, params = nil, read_timeout: @read_timeout, write_timeout: @write_timeout) raise Errors::ClientError, 'client closed' if @closed id = @id_gen.next json = Message.dump(Message.build_request(method, params, id)) with_transport_error_handling do connect_if_needed! log_sent(json) @transport.write_frame(json, timeout: write_timeout) raw = @transport.read_frame(timeout: read_timeout) log_received(raw) response = Message.parse(raw) Message.validate_response!(response, id) raise Message.error_to_exception(response['error']) if response.key?('error') response['result'] end end |