LogAn/lib/logan/inc/main.rb

71 lines
1.9 KiB
Ruby
Raw Normal View History

2010-03-28 14:33:49 +02:00
require 'sbdb'
require 'safebox'
require 'robustserver'
2010-03-29 11:50:59 +02:00
require 'logan/inc'
require 'logan/loglines'
2010-03-28 14:33:49 +02:00
module LogAn::Inc
class Main < RobustServer
# Open Config.
def config env, db, type = nil
2010-03-29 11:50:59 +02:00
$stderr.puts "Open Database "sids.cnf" #{db.inspect} (#{type.inspect})"
2010-03-28 14:33:49 +02:00
type ||= 1+4
2010-03-29 11:50:59 +02:00
ret = env[ 'sids.cnf', db, SBDB::RDONLY]
2010-03-28 14:33:49 +02:00
ret = AutoValueConvertHash.new ret if type&4 > 0
ret = Cache.new ret, type&3 if type&3 > 0
ret
end
# Prepare Server.
#
# * conf:
# logs
# : Where to store log-databases? default: ./logs
# etc
# : Where to find config-databases? default: ./etc
# server
# : Server-Configuration. default { port: 1087 }
2010-03-28 14:33:49 +02:00
def initialize conf
super
@conf = {}
# Copy config - changes possible
2010-03-29 11:50:59 +02:00
conf.each {|key, val| @conf[key]= val }
# Default directories
%w[logs etc].each {|key| @conf[key.to_sym] = key }
# Open Loglines-databases
@logs = LogAn::Loglines.new @conf[:logs]
# Open config-databases
Dir.mkdir @conf[:etc] rescue Errno::EEXIST
@etc = SBDB::Env.new( @conf[:etc],
2010-03-28 14:33:49 +02:00
log_config: SBDB::Env::LOG_IN_MEMORY | SBDB::Env::LOG_AUTO_REMOVE,
flags: SBDB::CREATE | SBDB::Env::INIT_TXN | Bdb::DB_INIT_MPOOL)
# Set inc-config - stored in etc/inc.cnf
@conf[:inc] = {}
%w[hosts files fileparser].each {|key| @conf[:inc][key.to_sym] = config( @etc, key) }
# Prepare Inc-server - create server
@serv = LogAn::Inc.new :sock => TCPServer.new( *@conf[:server]), :config => @conf[:inc]
# Shutdown on signals
@sigs[:INT] = @sigs[:TERM] = method( :shutdown)
2010-03-28 14:33:49 +02:00
end
# Will be called at exit. Will close all opened BDB::Env
2010-03-28 14:33:49 +02:00
def at_exit
@logs and @logs.close
@etc and @etc.close
end
# Shutdown Server cleanly.
def shutdown signal = nil
$stderr.puts [:signal, signal, Signal[signal]].inspect if signal
2010-03-28 14:33:49 +02:00
@serv.close
exit 0
end
# Runs server. Don't use it! Use #main.
2010-03-28 14:33:49 +02:00
def run
@serv.run
end
end
end