still buggy

This commit is contained in:
Denis Knauf 2010-03-29 11:50:59 +02:00
parent 313be57c1d
commit a5aaab685e
11 changed files with 160 additions and 132 deletions

View file

@ -1,5 +1,5 @@
module Logan
module LogAn
module Inc
class Command < ::Array
attr_reader :sid

View file

@ -1,5 +1,5 @@
module Logan
module LogAn
module Inc
module FileParser
module Base

View file

@ -2,13 +2,16 @@
require 'sbdb'
require 'safebox'
require 'robustserver'
require 'logan/inc'
require 'logan/loglines'
module LogAn::Inc
class Main < RobustServer
# Open Config.
def config env, db, type = nil
$stderr.puts "Open Database "sids.cnf" #{db.inspect} (#{type.inspect})"
type ||= 1+4
ret = env[ 'inc.cnf', db, SBDB::RDONLY]
ret = env[ 'sids.cnf', db, SBDB::RDONLY]
ret = AutoValueConvertHash.new ret if type&4 > 0
ret = Cache.new ret, type&3 if type&3 > 0
ret
@ -27,7 +30,7 @@ module LogAn::Inc
super
@conf = {}
# Copy config - changes possible
conf.each &@conf.method(:[]=)
conf.each {|key, val| @conf[key]= val }
# Default directories
%w[logs etc].each {|key| @conf[key.to_sym] = key }
# Open Loglines-databases

View file

@ -1,106 +1,104 @@
require 'select'
module Logan
module LogAn
module Inc
class Select <::Select
attr_reader :entries
def initialize *p
super *p
@entries=[]
end
end
end
def run
until @exit || (@exit_on_empty && self.empty?)
cron
run_once 1
end
end
class LogAn::Inc::Select <::Select
attr_reader :entries
def initialize *p
super *p
@entries=[]
end
def cron
@entries.each do |e|
return if e > Time.now
e.call
@entries.shift
end
end
def run
until @exit || (@exit_on_empty && self.empty?)
cron
run_once 1
end
end
class Entry < Time
attr_reader :do
def do &e
@do = e
end
def cron
@entries.each do |e|
return if e > Time.now
e.call
@entries.shift
end
end
def call *p
@do.call *p
end
def self.new *a, &e
x = self.at *a
x.do &e
x
end
end
def at a, &e
a = Entry.new a, &e if e
@entries << a
@entries.sort!
end
class Entry < Time
attr_reader :do
def do &e
@do = e
end
class Socket <::Select::Socket
def event_read sock = @sock, event = :read
begin
@linebuf += sock.readpartial( @bufsize)
rescue EOFError
self.event_eof sock
rescue Errno::EPIPE => e
self.event_errno e, sock, event
rescue IOError
self.event_ioerror sock, event
rescue Errno::ECONNRESET => e
self.event_errno e, sock, event
end
loop do
return if @linebuf.size < 4
l = @linebuf.unpack( 'N')[0]
return if l > @linebuf.length
@linebuf.remove 4
event_cmd @linebuf.remove( l)
end
end
def call *p
@do.call *p
end
class Server < ::Select::Server
attr_reader :config
def self.new *a, &e
x = self.at *a
x.do &e
x
end
end
def init opts
super opts
@config = opts[:config] or raise( ArgumentError, "#{self.class} needs a Config!")
end
def at a, &e
a = Entry.new a, &e if e
@entries << a
@entries.sort!
end
end
def event_new_client sock
{ :clientclass => LogAn::Inc::Server::Socket, :config => @config }
end
class Socket < LogAn::Inc::Server::Socket
attr_reader :config
def init opts
super opts
@config = opts[:config] or raise( ArgumentError, "#{self.class} needs a Config!")
end
def event_cmd cmd
sid, line = cmd.unpack 'Na*'
begin
@config[ :fileparser][sid].event_line line, self
rescue Didi::Config::NoSIDFound, Didi::Config::SIDLoadingError
$stderr.puts( {:sid => sid, :exception => $!, :backtrace => $!.backtrace}.inspect)
end
end
end
class LogAn::Inc::Socket <::Select::Socket
def event_read sock = @sock, event = :read
begin
@linebuf += sock.readpartial( @bufsize)
rescue EOFError
self.event_eof sock
rescue Errno::EPIPE => e
self.event_errno e, sock, event
rescue IOError
self.event_ioerror sock, event
rescue Errno::ECONNRESET => e
self.event_errno e, sock, event
end
loop do
return if @linebuf.size < 4
l = @linebuf.unpack( 'N')[0]
return if l > @linebuf.length
@linebuf.remove 4
event_cmd @linebuf.remove( l)
end
end
end
class LogAn::Inc::Server < ::Select::Server
attr_reader :config
def init opts
super opts
@config = opts[:config] or raise( ArgumentError, "#{self.class} needs a Config!")
end
def event_new_client sock
{ :clientclass => LogAn::Inc::Server::Socket, :config => @config }
end
class Socket < LogAn::Inc::Socket
attr_reader :config
def init opts
super opts
@config = opts[:config] or raise( ArgumentError, "#{self.class} needs a Config!")
end
def event_cmd cmd
sid, line = cmd.unpack 'Na*'
fileparser = @config[:fileparser][sid]
fileparser.event_line line, self if fileparser
end
end
end