Class: RxIO::Client

Inherits:
Object
  • Object
show all
Includes:
Runify, IOBase
Defined in:
lib/rxio/client.rb

Overview

Client Class

Constant Summary collapse

RECONNECT_DELAY =

Reconnect Delay (seconds)

1
SELECT_TIMEOUT =

Select Timeout (seconds)

0.1

Constants included from IOBase

IOBase::CHUNK_SIZE, IOBase::RETRY_EXCEPTIONS

Instance Method Summary collapse

Methods included from IOBase

#drop_endpoint, #peer_error, #process_input, #read_sock, #write_sock

Constructor Details

#initialize(addr, port, service_handler, ssl = false, ssl_params = { verify_peer: true }) ⇒ Client

Construct: Builds a Client around a given service_handler module, set to connect to addr on port.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rxio/client.rb', line 38

def initialize addr, port, service_handler, ssl = false, ssl_params = { verify_peer: true }

  # Set SSL Context
  if ssl
    @ssl_ctx = OpenSSL::SSL::SSLContext.new
    @ssl_ctx.verify_mode = ssl_params[:verify_peer] ? OpenSSL::SSL::VERIFY_PEER | OpenSSL::SSL::VERIFY_FAIL_IF_NO_PEER_CERT : OpenSSL::SSL::VERIFY_NONE
  end

  # Set Address & Port
  @addr = addr
  @port = port

  # Set Service Handler Module
  @service_handler = service_handler

  # Set Socket
  @sock = nil

  # Client Hash
  @client = {
    client: self,
    ibuf: '',
    obuf: '',
    lock: Mutex.new,
    msgs: []
  }
end

Instance Method Details

#runObject

Run: Executes the main client loop, taking care of I/O scheduling and message handling.



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/rxio/client.rb', line 80

def run

  # Update Loop
  begin

    # Update Service
    update until @stop
  rescue Exception => e
    puts "[!] ERROR - RxIO Client Update failed - #{e.inspect}"
    e.backtrace.each { |b| puts "    - #{b}" }
  end

  # Drop Socket
  @sock.close if @sock
  @sock = nil
end

#send_msg(msg) ⇒ Object

Send Message: Enqueues a Message to be sent to the server.



69
70
71
72
73
74
75
76
# File 'lib/rxio/client.rb', line 69

def send_msg msg

  # Verify Implementation
  raise "Method send_msg is not implemented by Service Handler #{@service_handler.name}" unless @service_handler.respond_to? :send_msg

  # Send Message through Service Handler
  @service_handler.send_msg @client, msg
end

#stopObject

Stop: Requests the client loop to stop executing. The run method should return shortly after calling stop.



100
101
102
# File 'lib/rxio/client.rb', line 100

def stop
  @stop = true
end