Class: IORequest::Client
- Inherits:
-
Object
- Object
- IORequest::Client
- Includes:
- Utility::MultiThread, Utility::WithProgName
- Defined in:
- lib/io_request/client.rb
Overview
Connection client.
General scheme:
Client 1 Client 2
| |
( Authorization ) See `Authorizer` class. Error in authorization should close
| | connection
| |
[ Data transition loop ] Loop runs until someone sends 0 sized data. Then everyone
| | should close connection. Any R/W errors should also finish the
| | loop
| |
|-> uint(2 bytes) ->| Specifies size of following JSON string
|-> Mesage as JSON ->| Message itself. It should contain its `type`, `id` and some
| | data hash
| |
| (Message handling) See `Handler` class
| |
|<- uint(2 bytes) <-|
|<- Mesage as JSON <-|
Instance Attribute Summary collapse
-
#authorizer ⇒ Object
readonly
Returns the value of attribute authorizer.
Instance Method Summary collapse
-
#close ⇒ Object
Close connection.
-
#initialize(authorizer: Authorizer.empty) ⇒ Client
constructor
Initialize new client.
-
#on_close(&block) ⇒ Object
Code to execute after connection is closed.
- #on_request {|| ... } ⇒ Object (also: #respond)
-
#open(read: nil, write: nil, read_write: nil) ⇒ Object
Start new client connection.
- #open? ⇒ Boolean
-
#request(data, timeout: nil, &callback) ⇒ Object
If callback block is provided, request will be sent asynchroniously.
Methods included from Utility::WithProgName
Constructor Details
#initialize(authorizer: Authorizer.empty) ⇒ Client
Initialize new client.
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/io_request/client.rb', line 32 def initialize(authorizer: Authorizer.empty) @open = false @authorizer = @mutex_r = Mutex.new @mutex_w = Mutex.new @responses = {} @responses_access_mutex = Mutex.new @responses_access_cv = ConditionVariable.new end |
Instance Attribute Details
#authorizer ⇒ Object (readonly)
Returns the value of attribute authorizer.
103 104 105 |
# File 'lib/io_request/client.rb', line 103 def @authorizer end |
Instance Method Details
#close ⇒ Object
Close connection.
66 67 68 69 70 |
# File 'lib/io_request/client.rb', line 66 def close close_internal join_threads end |
#on_close(&block) ⇒ Object
Code to execute after connection is closed.
81 82 83 84 |
# File 'lib/io_request/client.rb', line 81 def on_close(&block) IORequest.logger.debug(prog_name) { 'Saved on_close block' } @on_close = block end |
#on_request {|| ... } ⇒ Object Also known as: respond
74 75 76 77 |
# File 'lib/io_request/client.rb', line 74 def on_request(&block) IORequest.logger.debug(prog_name) { 'Saved on_request block' } @on_request = block end |
#open(read: nil, write: nil, read_write: nil) ⇒ Object
Start new client connection.
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/io_request/client.rb', line 49 def open(read: nil, write: nil, read_write: nil) @io_r = read_write || read @io_w = read_write || write IORequest.logger.debug(prog_name) { 'Starting connection' } auth_data = @open = true @data_transition_thread = in_thread(name: 'connection') { data_transition_loop } auth_data end |
#open? ⇒ Boolean
61 62 63 |
# File 'lib/io_request/client.rb', line 61 def open? @open end |
#request(data, timeout: nil, &callback) ⇒ Object
If callback block is provided, request will be sent asynchroniously.
89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/io_request/client.rb', line 89 def request(data, timeout: nil, &callback) = Message.new(data, type: :request) if block_given? # Async execution of request in_thread(callback, name: 'requesting') do |cb| cb.call(send_request_and_wait_for_response(, timeout).data) end nil else send_request_and_wait_for_response(, timeout).data end end |