LogAn/lib/logan/inc/server.rb

106 lines
1.8 KiB
Ruby
Raw Normal View History

2010-03-25 10:39:30 +01:00
require 'select'
2010-03-29 11:50:59 +02:00
module LogAn
2010-03-25 10:39:30 +01:00
module Inc
2010-03-29 11:50:59 +02:00
end
end
2010-03-25 10:39:30 +01:00
2010-03-29 11:50:59 +02:00
class LogAn::Inc::Select <::Select
attr_reader :entries
def initialize *p
super *p
@entries=[]
end
2010-03-25 10:39:30 +01:00
2010-03-29 11:50:59 +02:00
def run
until @exit || (@exit_on_empty && self.empty?)
cron
run_once 1
end
end
2010-03-25 10:39:30 +01:00
2010-03-29 11:50:59 +02:00
def cron
@entries.each do |e|
return if e > Time.now
e.call
@entries.shift
end
end
2010-03-25 10:39:30 +01:00
2010-03-29 11:50:59 +02:00
class Entry < Time
attr_reader :do
def do &e
@do = e
end
2010-03-25 10:39:30 +01:00
2010-03-29 11:50:59 +02:00
def call *p
@do.call *p
end
2010-03-25 10:39:30 +01:00
2010-03-29 11:50:59 +02:00
def self.new *a, &e
x = self.at *a
x.do &e
x
2010-03-25 10:39:30 +01:00
end
2010-03-29 11:50:59 +02:00
end
2010-03-25 10:39:30 +01:00
2010-03-29 11:50:59 +02:00
def at a, &e
a = Entry.new a, &e if e
@entries << a
@entries.sort!
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
2010-03-25 10:39:30 +01:00
end
2010-03-29 11:50:59 +02:00
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
2010-03-25 10:39:30 +01:00
2010-03-29 11:50:59 +02:00
class LogAn::Inc::Server < ::Select::Server
attr_reader :config
2010-03-25 10:39:30 +01:00
2010-03-29 11:50:59 +02:00
def init opts
super opts
@config = opts[:config] or raise( ArgumentError, "#{self.class} needs a Config!")
end
2010-03-25 10:39:30 +01:00
2010-03-29 11:50:59 +02:00
def event_new_client sock
{ :clientclass => LogAn::Inc::Server::Socket, :config => @config }
end
2010-03-25 10:39:30 +01:00
2010-03-29 11:50:59 +02:00
class Socket < LogAn::Inc::Socket
attr_reader :config
2010-03-25 10:39:30 +01:00
2010-03-29 11:50:59 +02:00
def init opts
super opts
2010-03-30 01:15:03 +02:00
@sid0 = LogAn::Inc::SID0.new
2010-03-29 11:50:59 +02:00
@config = opts[:config] or raise( ArgumentError, "#{self.class} needs a Config!")
end
def event_cmd cmd
sid, line = cmd.unpack 'Na*'
2010-03-30 01:15:03 +02:00
fp = sid == 0 ? @sid0 : @config[:fileparser][sid]
2010-03-30 00:32:50 +02:00
fp.event_line line, self if fp
2010-03-25 10:39:30 +01:00
end
end
end