2012-06-16 04:41:18 +00:00
|
|
|
require 'eventmachine'
|
|
|
|
require 'sequel'
|
|
|
|
|
2012-06-16 05:07:53 +00:00
|
|
|
if ENV['MPDB']
|
|
|
|
DB = Sequel.connect(ENV['MPDB'])
|
|
|
|
else
|
|
|
|
DB = Sequel.connect('sqlite://marco-polo.sqlite3')
|
|
|
|
end
|
2012-06-16 04:41:18 +00:00
|
|
|
|
2012-06-16 05:07:53 +00:00
|
|
|
begin
|
|
|
|
DB[:locations].empty?
|
|
|
|
rescue Sequel::DatabaseError
|
|
|
|
DB.create_table(:locations) do
|
2012-06-16 04:41:18 +00:00
|
|
|
primary_key :id
|
|
|
|
String :client
|
|
|
|
Integer :x
|
|
|
|
Integer :y
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
module MarcoPoloServer
|
|
|
|
def post_init
|
|
|
|
@locations = DB[:locations]
|
|
|
|
end
|
|
|
|
|
|
|
|
def receive_data(data)
|
|
|
|
client, x, y = data.split
|
|
|
|
@locations.insert(:client => client, :x => x.to_i, :y => y.to_i)
|
|
|
|
send_data("thanks\n")
|
|
|
|
rescue => e
|
|
|
|
STDERR.puts("#{e.class.name} #{e.message}: #{e.backtrace.join("\n")}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
EventMachine.run do
|
|
|
|
host, port = '0.0.0.0', 22000
|
|
|
|
EventMachine.start_server(host, port, MarcoPoloServer)
|
|
|
|
puts "Listening on #{host}:#{port}"
|
|
|
|
end
|