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
|
2010-03-29 01:12:22 +02:00
|
|
|
# Open Config.
|
2010-03-29 16:00:55 +02:00
|
|
|
def config env, db, type = nil, flags = nil
|
2010-03-29 15:37:16 +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 16:00:55 +02:00
|
|
|
ret = env[ 'sids.cnf', db, flags || 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
|
|
|
|
|
2010-03-29 01:12:22 +02:00
|
|
|
# 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
|
2010-03-29 01:12:22 +02:00
|
|
|
@conf = {}
|
|
|
|
# Copy config - changes possible
|
2010-03-29 11:50:59 +02:00
|
|
|
conf.each {|key, val| @conf[key]= val }
|
2010-03-29 01:12:22 +02:00
|
|
|
# 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)
|
2010-03-29 01:12:22 +02:00
|
|
|
# Set inc-config - stored in etc/inc.cnf
|
|
|
|
@conf[:inc] = {}
|
|
|
|
%w[hosts files fileparser].each {|key| @conf[:inc][key.to_sym] = config( @etc, key) }
|
2010-03-29 16:00:55 +02:00
|
|
|
@store = Cache.new AutoValueConvertHash.new( @etc[ 'sids.store', 'seeks', SBDB::CREATE | SBDB::AUTO_COMMIT]), 3
|
2010-03-29 01:12:22 +02:00
|
|
|
# Prepare Inc-server - create server
|
2010-03-29 16:00:55 +02:00
|
|
|
LogAn::Inc::Fileparser::Base.logdb = @logs
|
|
|
|
LogAn::Inc::Fileparser::Base.store = @store
|
2010-03-29 01:12:22 +02:00
|
|
|
@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
|
|
|
|
|
2010-03-29 01:12:22 +02:00
|
|
|
# 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
|
|
|
|
|
2010-03-29 01:12:22 +02:00
|
|
|
# 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
|
|
|
|
|
2010-03-29 01:12:22 +02:00
|
|
|
# Runs server. Don't use it! Use #main.
|
2010-03-28 14:33:49 +02:00
|
|
|
def run
|
|
|
|
@serv.run
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|