Class: OsCtld::Generic::Server
- Inherits:
-
Object
- Object
- OsCtld::Generic::Server
- 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
- #handle_client(socket) ⇒ Object protected
-
#initialize(socket, client_handler, opts = {}) ⇒ Server
constructor
A new instance of Server.
- #start ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize(socket, client_handler, opts = {}) ⇒ Server
Returns a new instance of Server.
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 |
#start ⇒ Object
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 |
#stop ⇒ Object
28 29 30 |
# File 'lib/osctld/generic/server.rb', line 28 def stop @socket.close end |