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.

Parameters:

  • addr (String)

    Address to which the client should connect

  • port (Fixnum)

    Port on which the client should connect

  • service_handler (Module)

    Module implementing service client logic

  • ssl (Boolean) (defaults to: false)

    Create a secure client

  • ssl_params (Hash) (defaults to: { verify_peer: true })

    SSL Parameters - see README



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
65
# 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,
		local: 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.



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/rxio/client.rb', line 90

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.

Parameters:

  • msg (String)


79
80
81
82
83
84
85
86
# File 'lib/rxio/client.rb', line 79

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

#set_target(addr, port) ⇒ Object

Set Target: Updates the target address & port, to be used next time a connection is established.

Parameters:

  • addr (String)
  • port (Fixnum)


71
72
73
74
# File 'lib/rxio/client.rb', line 71

def set_target addr, port
	@addr = addr
	@port = port
end

#stopObject

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



110
111
112
# File 'lib/rxio/client.rb', line 110

def stop
	@stop = true
end