You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
1.0 KiB

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)
code = request[0..30].to_s.strip
num = request[31..42].to_s.strip
len = request[43..48].to_s.strip
puts "raw date --> '#{request[49..62]}'"
timestamp = DateTime.strptime(request[49..62].strip, DATEFMT)
msg = request[63..(63 + len.to_i)].strip
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