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, 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_params = nil) ⇒ Service

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



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

def initialize addr, port, service_handler, ssl_params = nil

  # Set SSL Params
  if ssl_params
    @ssl_ctx = OpenSSL::SSL::SSLContext.new
    @ssl_ctx.cert = OpenSSL::X509::Certificate.new File.read(ssl_params[:cert])
    @ssl_ctx.key = OpenSSL::PKey::RSA.new File.read(ssl_params[:pkey])
  end

  # 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. This method blocks until the service loop terminates.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/rxio/service.rb', line 64

def run

  # Update Loop
  begin

    # Create TCP Socket Server
    @serv = TCPServer.new @addr, @port
    @serv = OpenSSL::SSL::SSLServer.new @serv, @ssl_ctx if @ssl_ctx
    @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.



96
97
98
# File 'lib/rxio/service.rb', line 96

def stop
  @stop = true
end