Class: OsCtld::Generic::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/osctld/generic/server.rb

Overview

Generic socket server

A thread is spawned for every connected client. An instance of ClientHandler or its subclass is created to handle the connection.

Instance Method Summary collapse

Constructor Details

#initialize(socket, client_handler, opts = {}) ⇒ Server

Returns a new instance of Server.

Parameters:

  • socket (BasicSocket)

    server socket

  • client_handler (Generic::ClientHandler)

    name of a class that will be instantiated for every client

  • opts (Hash) (defaults to: {})

    options

Options Hash (opts):

  • opts (Hash)

    options passed to the client handler



12
13
14
15
16
# File 'lib/osctld/generic/server.rb', line 12

def initialize(socket, client_handler, opts = {})
  @socket = socket
  @client_handler = client_handler
  @opts = opts
end

Instance Method Details

#handle_client(socket) ⇒ Object (protected)



34
35
36
37
38
# File 'lib/osctld/generic/server.rb', line 34

def handle_client(socket)
  c = @client_handler.new(socket, @opts[:opts] || {})
  t = Thread.new { c.communicate }
  ThreadReaper.add(t, c)
end

#startObject



18
19
20
21
22
23
24
25
26
# File 'lib/osctld/generic/server.rb', line 18

def start
  loop do
    c = @socket.accept
  rescue IOError
    return
  else
    handle_client(c)
  end
end

#stopObject



28
29
30
# File 'lib/osctld/generic/server.rb', line 28

def stop
  @socket.close
end