LogAn/lib/logan/inc/server.rb

106 lines
1.7 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
@config = opts[:config] or raise( ArgumentError, "#{self.class} needs a Config!")
end
def event_cmd cmd
sid, line = cmd.unpack 'Na*'
2010-03-29 22:15:06 +02:00
fps = @config[:fileparser]
fp = fps[sid]
fp.event_line line, self if fileparser
2010-03-25 10:39:30 +01:00
end
end
end