2012-07-12 01:59:11 +00:00
|
|
|
require 'date'
|
|
|
|
require 'time'
|
|
|
|
require 'eventmachine'
|
|
|
|
|
|
|
|
DATEFMT = '%Y%m%d%H%M%S'
|
|
|
|
|
|
|
|
def generic_response
|
|
|
|
code = '%-31s' % 'SNARF'
|
|
|
|
num = '%-12d' % (rand.to_s.tr('.', '0')[0,5]).to_i
|
|
|
|
filler = (' ' * 7)
|
|
|
|
timestamp = Time.now.strftime(DATEFMT)
|
|
|
|
"#{code}#{num}#{filler}#{timestamp}\r\n"
|
|
|
|
end
|
|
|
|
|
|
|
|
module LineProtocolServer
|
|
|
|
def post_init
|
|
|
|
puts "# BEGIN CONN #{Time.now}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def receive_data(request)
|
2012-07-12 13:19:10 +00:00
|
|
|
code = request[0,31].to_s.strip
|
|
|
|
num = request[31,12].to_s.strip
|
|
|
|
len = request[43,6].to_s.strip
|
|
|
|
puts "raw date --> '#{request[49,14]}'"
|
|
|
|
timestamp = DateTime.strptime(request[49,14].strip, DATEFMT)
|
|
|
|
msg = request[63,len.to_i+1].strip
|
2012-07-12 01:59:11 +00:00
|
|
|
|
|
|
|
puts "code: '#{code}'"
|
|
|
|
puts "num: '#{num}'"
|
|
|
|
puts "len: '#{len}'"
|
|
|
|
puts "timestamp: '#{timestamp}'"
|
|
|
|
puts "msg: '#{msg}'"
|
|
|
|
|
|
|
|
send_data(generic_response)
|
|
|
|
end
|
|
|
|
|
|
|
|
def unbind
|
|
|
|
puts "# END CONN #{Time.now}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
EventMachine.run do
|
|
|
|
EventMachine.start_server('0.0.0.0', 15778, LineProtocolServer)
|
|
|
|
puts 'Listening on 0.0.0.0:15778'
|
|
|
|
end
|