From f342fa273459c92493cac3c6f7fba90a7d471dad Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Sat, 16 Jun 2012 00:41:18 -0400 Subject: [PATCH] putting some load on a tiny io-bound application --- ruby-sockets/marco-polo/.gitignore | 1 + ruby-sockets/marco-polo/client.rb | 11 ++++++++++ ruby-sockets/marco-polo/em-server.rb | 33 ++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+) create mode 100644 ruby-sockets/marco-polo/.gitignore create mode 100644 ruby-sockets/marco-polo/client.rb create mode 100644 ruby-sockets/marco-polo/em-server.rb diff --git a/ruby-sockets/marco-polo/.gitignore b/ruby-sockets/marco-polo/.gitignore new file mode 100644 index 0000000..e2186fa --- /dev/null +++ b/ruby-sockets/marco-polo/.gitignore @@ -0,0 +1 @@ +/*.sqlite3* diff --git a/ruby-sockets/marco-polo/client.rb b/ruby-sockets/marco-polo/client.rb new file mode 100644 index 0000000..1ab1eae --- /dev/null +++ b/ruby-sockets/marco-polo/client.rb @@ -0,0 +1,11 @@ +require 'socket' + +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('.') + STDOUT.flush + end +end diff --git a/ruby-sockets/marco-polo/em-server.rb b/ruby-sockets/marco-polo/em-server.rb new file mode 100644 index 0000000..6eb67cc --- /dev/null +++ b/ruby-sockets/marco-polo/em-server.rb @@ -0,0 +1,33 @@ +require 'eventmachine' +require 'sequel' + +DB = Sequel.connect('sqlite://marco-polo.sqlite3') + +if DB[:locations].nil? + DB.create_table :locations do + 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