79 lines
1.9 KiB
Ruby
79 lines
1.9 KiB
Ruby
# = ildus/server - main Ildus server library
|
|
#
|
|
# Copyright (C) 2005 Paul van Tilburg <paul@luon.net>
|
|
#
|
|
# Ildus is free software; you can redistribute it and/or modify it under
|
|
# the terms of the GNU General Public License as published by the Free
|
|
# Software Foundation; either version 2 of the License, or (at your option)
|
|
# any later version.
|
|
|
|
require 'yaml'
|
|
require 'gserver'
|
|
|
|
module Ildus
|
|
|
|
class Server < GServer
|
|
|
|
# The interface to listen specified default by the hostname.
|
|
DEFAULT_HOST = '::'
|
|
|
|
# The maximal number of connections.
|
|
MAX_CONNECTIONS = 20
|
|
|
|
# The server configuration.
|
|
attr_reader :config
|
|
|
|
# Create a Ildus server instance that is a derivate of a generic
|
|
# server. Accepts an alternative _config_file_ as optional argument.
|
|
def initialize(config_file=nil)
|
|
@config = nil
|
|
parse_config(config_file)
|
|
|
|
super(config['port'], DEFAULT_HOST, 20, $stderr, true, false)
|
|
end
|
|
|
|
def serve(io)
|
|
hdl = Handler.new(self, io)
|
|
hdl.handle_client
|
|
rescue => e
|
|
$stderr.puts "#{e.class}: #{e}"
|
|
$stderr.puts e.backtrace
|
|
end
|
|
|
|
# Start the Ildus server.
|
|
def start
|
|
super
|
|
join
|
|
end
|
|
|
|
# Stop and shutdown the Ildus server. An _status_ code (defaults to 0)
|
|
# can be used as an optional argument.
|
|
def shutdown(status=0)
|
|
super()
|
|
exit(status)
|
|
end
|
|
|
|
#########
|
|
private
|
|
#########
|
|
|
|
# Parses the configuration file _filename_ and stores the parsed
|
|
# contents.
|
|
def parse_config(filename)
|
|
raise ArgumentError unless FileTest.exists? filename
|
|
|
|
File.open(filename, "r") do |io|
|
|
# FIXME: merge the YAML with a default config
|
|
@config = YAML.load(io)
|
|
end
|
|
|
|
# FIXME: test config for existance of certain fields:
|
|
# port, passwd, backend, backend/type
|
|
end
|
|
|
|
end # class Server
|
|
|
|
end # module Ildus
|
|
|
|
require 'ildus/server/handler'
|