Class: RxIO::Service

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

Overview

Service Class

Constant Summary collapse

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) ⇒ Service

Construct Builds a Service around a given service_handler module, set to listen for incoming connections @ addr on port.

Parameters:

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


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rxio/service.rb', line 32

def initialize addr, port, service_handler

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

	# Set Service Handler Module
	@service_handler = service_handler

	# Create Sockets
	@socks = []

	# Create Clients
	@clients = []

	# Create Client Map
	@cmap = {}
end

Instance Method Details

#runObject

Run Executes the main service loop, taking care of I/O scheduling, client management and message handling. Note: this method blocks until the service loop terminates.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/rxio/service.rb', line 54

def run

	# Update Loop
	begin

		# Create TCP Socket Server
		@serv = TCPServer.new @addr, @port
		@socks << @serv

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

	# Drop all Clients
	@service_handler.on_drop @clients.shift until @clients.empty? if @service_handler.respond_to? :on_drop
	@cmap = {}

	# Close all Sockets
	@socks.each { |s| s.close }
	@socks = []

	# Drop Server
	@serv = nil
end

#stopObject

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



85
86
87
# File 'lib/rxio/service.rb', line 85

def stop
	@stop = true
end