require 'socket'
require 'time'
SERVER = "localhost"
PORT = 6567
STATIONS = 1
#EVENTS = [ 'mouseMove', 'mouseMove', 'mouseMove', 'mouseMove', 'mouseMove', 'mouseMove', 'mouseMove', 'mouseMove',
# 'mouseMove', 'mouseMove', 'mouseMove', 'mouseMove', 'mouseMove', 'mouseMove', 'mouseMove', 'mouseMove',
# 'mouseMove', 'mouseMove', 'mouseMove', 'mouseMove', 'mouseMove', 'mouseMove', 'mouseMove', 'mouseMove',
# 'mouseDown', 'mouseUp', 'mouseDown', 'mouseUp',
# "swipeDown", "swipeUp", "swipeLeft", "swipeRight", "zoomIn", "zoomOut",
# "cancel", "mouseDown", 'bigSwipe', "mouseMove", 'mouseUp', 'otherunknown'
# ]
EVENTS = ['mouseMove']
XML_EVENT_TEMPLATE = <<-XML
%s, %s, 0
XML
XML_MOVE_EVENT_TEMPLATE = <<-XML
%s, %s, 0
XML
XML_EVENT_TEMPLATE_NO_DATA = <<-XML
XML
XML_NO_EVENT_TEMPLATE = <<-XML
XML
def generate_payload
if [true].choice
my_event = EVENTS.choice
puts "event >>>>>> #{my_event}"
my_now = Time.now
if ['mouseMove', 'mouseDown', 'mouseUp'].include? my_event
sprintf(XML_EVENT_TEMPLATE, (rand*STATIONS).to_i, my_event, Math.sin(my_now), Math.cos(my_now))
else
sprintf(XML_MOVE_EVENT_TEMPLATE, (rand*STATIONS).to_i, Math.sin(my_now), Math.cos(my_now), my_event)
end
else
puts "no event"
sprintf(XML_NO_EVENT_TEMPLATE, (rand*STATIONS).to_i)
end
end
def send_gesture(server_addr, server_port, payload)
resp, sock = nil, nil
begin
sock = UDPSocket.open
puts payload
sock.send("#{payload}", 0, server_addr, server_port)
resp = ["sent"]
rescue IOError, SystemCallError
ensure
sock.close if sock
end
resp ? resp[0] : nil
end
(1..30000).each do |i|
sleep 1.0/40.0 # 40 Hertz
result = send_gesture(SERVER, PORT, generate_payload)
puts "#{result} - #{i}"
end