box-o-sand/oldstuff/ruby-sockets/line-protocol/client.rb
2015-06-22 13:15:42 -05:00

38 lines
855 B
Ruby

require 'date'
require 'time'
require 'socket'
DATEFMT = '%Y%m%d%H%M%S'
def recordify(msg)
code = '%-31s' % 'DERP'
num = '%-12d' % (rand.to_s.tr('.', '0')[0,5]).to_i
timestamp = Time.now.strftime(DATEFMT)
len = '%-6d' % msg.length
"#{code}#{num}#{len}#{timestamp}#{msg.gsub(/\r/, '\r').gsub(/\n/, '\n')}\r\n"
end
def main
print "What to say?: "
msg = gets
TCPSocket.open('127.0.0.1', 15778) do |sock|
sock.send(recordify(msg), 0)
response = sock.recv(66)
sock.close
puts "raw response --> #{response.inspect}"
code = response[0,31].to_s.strip
num = response[31,12].to_s.strip
raw_timestamp = response[49,14].to_s.strip
timestamp = DateTime.strptime(raw_timestamp, DATEFMT)
puts "code: '#{code}'"
puts "num: '#{num}'"
puts "timestamp: '#{timestamp}'"
end
end
if __FILE__ == $0
main
end