diff --git a/ruby-sockets/marco-polo/client.rb b/ruby-sockets/marco-polo/client.rb index 1ab1eae..b831a41 100644 --- a/ruby-sockets/marco-polo/client.rb +++ b/ruby-sockets/marco-polo/client.rb @@ -1,11 +1,25 @@ require 'socket' +successes = 0 +backoff = 0.5 loop do - TCPSocket.open('127.0.0.1', 22000) do |sock| - sock.send("sally #{rand(100)} #{rand(100)}\n", 0) - sock.recv(100) - sock.close - STDOUT.write('.') + begin + TCPSocket.open('127.0.0.1', 22000) do |sock| + sock.send("sally #{rand(101)} #{rand(101)}\n", 0) + sock.recv(100) + sock.close + STDOUT.write('.') + STDOUT.flush + end + successes += 1 + backoff = 0.5 + sleep 0.01 + rescue + STDOUT.write('E') STDOUT.flush + STDERR.write("\nmade it #{successes} times. sleeping #{backoff} secs\n") + successes = 0 + sleep backoff + backoff *= 2 end end diff --git a/ruby-sockets/marco-polo/em-server.rb b/ruby-sockets/marco-polo/em-server.rb index 6eb67cc..e433a8f 100644 --- a/ruby-sockets/marco-polo/em-server.rb +++ b/ruby-sockets/marco-polo/em-server.rb @@ -1,10 +1,16 @@ require 'eventmachine' require 'sequel' -DB = Sequel.connect('sqlite://marco-polo.sqlite3') +if ENV['MPDB'] + DB = Sequel.connect(ENV['MPDB']) +else + DB = Sequel.connect('sqlite://marco-polo.sqlite3') +end -if DB[:locations].nil? - DB.create_table :locations do +begin + DB[:locations].empty? +rescue Sequel::DatabaseError + DB.create_table(:locations) do primary_key :id String :client Integer :x