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

Instance Method Summary collapse

Methods included from IOBase

#drop_endpoint, #process_input, #read_sock, #write_sock

Constructor Details

#initialize(addr, port, service_handler) ⇒ Client

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

Parameters:

  • addr (String)
  • port (Fixnum)
  • service_handler (Module)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rxio/client.rb', line 35

def initialize addr, port, service_handler

	# 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.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/rxio/client.rb', line 71

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)


60
61
62
63
64
65
66
67
# File 'lib/rxio/client.rb', line 60

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



91
92
93
# File 'lib/rxio/client.rb', line 91

def stop
	@stop = true
end