LogAn/lib/logan/inc/command.rb

45 lines
917 B
Ruby
Raw Normal View History

2010-03-25 10:39:30 +01:00
2010-03-29 11:50:59 +02:00
module LogAn
2010-03-25 10:39:30 +01:00
module Inc
class Command < ::Array
attr_reader :sid
def initialize sid = 0
@sid = sid
end
2010-03-30 01:16:52 +02:00
def event_line line, sock
2010-03-25 10:39:30 +01:00
cmd, l = line.unpack 'na*'
self[cmd].call( l, sock) if self[cmd]
end
end
class SID0 < Command
2010-03-30 00:32:50 +02:00
class <<self
attr_accessor :store, :config
end
def initialize sid = 0
super sid
2010-03-25 10:39:30 +01:00
self[9] = method :event_hostname
self[10] = method :event_filerotated
end
def event_filerotated line, sock
2010-03-30 00:32:50 +02:00
sid, inode, seek = line.unpack 'NNN'
@@store[ :seeks][ sid] = [inode, seek]
2010-03-25 10:39:30 +01:00
end
def event_hostname line, sock
2010-03-30 00:32:50 +02:00
@@config[ :hosts].each do |sid, host|
2010-03-25 10:39:30 +01:00
next unless line == host
2010-03-30 00:32:50 +02:00
file = @@config[ :files][ sid]
2010-03-25 10:39:30 +01:00
next unless file
# command, SID, (inode, seek), file
2010-03-30 00:32:50 +02:00
pc = [1, sid, @@store[ :seeks][ sid], file].pack 'nNNNa*'
2010-03-25 10:39:30 +01:00
sock.write [pc.length, pc].pack( 'Na*')
end
end
end
end
end