Class: IORequest::Client

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Methods included from Utility::WithProgName

#prog_name

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 = authorizer

  @mutex_r = Mutex.new
  @mutex_w = Mutex.new

  @responses = {}
  @responses_access_mutex = Mutex.new
  @responses_access_cv = ConditionVariable.new
end

Instance Attribute Details

#authorizerObject (readonly)

Returns the value of attribute authorizer.



103
104
105
# File 'lib/io_request/client.rb', line 103

def authorizer
  @authorizer
end

Instance Method Details

#closeObject

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

Yield Parameters:

  • (Hash)

Yield Returns:

  • (Hash)


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.

Parameters:

  • r (IO)

    object to read from.

  • w (IO)

    object to write to.

  • rw (IO)

    read-write object (replaces ‘r` and `w` arguments).

Returns:



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 = authorization
  @open = true
  @data_transition_thread = in_thread(name: 'connection') { data_transition_loop }
  auth_data
end

#open?Boolean

Returns:

  • (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.

Parameters:

  • data (Hash)
  • timeout (Integer) (defaults to: nil)

    timeout in seconds.



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 = 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(message, timeout).data)
    end
    nil
  else
    send_request_and_wait_for_response(message, timeout).data
  end
end