From 847c77ef0ff87340970189996fd509bf0573d86b Mon Sep 17 00:00:00 2001 From: Nick Gauthier Date: Fri, 29 Jan 2010 13:30:25 -0500 Subject: [PATCH] restructured messages --- lib/hydra/master.rb | 5 ++--- lib/hydra/message/master_messages.rb | 13 +++++++++++++ lib/hydra/message/runner_messages.rb | 21 -------------------- lib/hydra/message/worker_messages.rb | 29 +++++++++++++++------------- lib/hydra/worker.rb | 4 ++-- test/runner_test.rb | 4 ++-- test/worker_test.rb | 4 ++-- 7 files changed, 37 insertions(+), 43 deletions(-) diff --git a/lib/hydra/master.rb b/lib/hydra/master.rb index 9ab6f70..f82033a 100644 --- a/lib/hydra/master.rb +++ b/lib/hydra/master.rb @@ -25,9 +25,9 @@ module Hydra #:nodoc: def send_file(worker) f = @files.pop if f - worker[:io].write(Hydra::Messages::Worker::RunFile.new(:file => f)) + worker[:io].write(Hydra::Messages::Master::RunFile.new(:file => f)) else - worker[:io].write(Hydra::Messages::Worker::Shutdown.new) + worker[:io].write(Hydra::Messages::Master::Shutdown.new) Thread.exit end end @@ -50,7 +50,6 @@ module Hydra #:nodoc: def process_messages @running = true - # TODO catch exceptions and report to stdout Thread.abort_on_exception = true @workers.each do |w| diff --git a/lib/hydra/message/master_messages.rb b/lib/hydra/message/master_messages.rb index e97347f..0581958 100644 --- a/lib/hydra/message/master_messages.rb +++ b/lib/hydra/message/master_messages.rb @@ -1,6 +1,19 @@ module Hydra #:nodoc: module Messages #:nodoc: module Master #:nodoc: + # Message telling a worker to delegate a file to a runner + class RunFile < Hydra::Messages::Worker::RunFile + def handle(worker) #:nodoc: + worker.delegate_file(self) + end + end + + # Message telling the worker to shut down. + class Shutdown < Hydra::Messages::Worker::Shutdown + def handle(worker) #:nodoc: + worker.shutdown + end + end end end end diff --git a/lib/hydra/message/runner_messages.rb b/lib/hydra/message/runner_messages.rb index 1c739c9..1f42885 100644 --- a/lib/hydra/message/runner_messages.rb +++ b/lib/hydra/message/runner_messages.rb @@ -8,19 +8,6 @@ module Hydra #:nodoc: end end - # Message telling the Runner to run a file - # TODO: move to worker - class RunFile < Hydra::Message - # The file that should be run - attr_accessor :file - def serialize #:nodoc: - super(:file => @file) - end - def handle(runner) #:nodoc: - runner.run_file(@file) - end - end - # Message for the Runner to respond with its results class Results < Hydra::Message # The output from running the test @@ -35,14 +22,6 @@ module Hydra #:nodoc: end end - # Message to tell the Runner to shut down - # TODO: move to worker - class Shutdown < Hydra::Message - def handle(runner) #:nodoc: - runner.stop - end - end - # Message a runner sends to a worker to verify the connection class Ping < Hydra::Message def handle(worker, runner) #:nodoc: diff --git a/lib/hydra/message/worker_messages.rb b/lib/hydra/message/worker_messages.rb index 988ae31..889176a 100644 --- a/lib/hydra/message/worker_messages.rb +++ b/lib/hydra/message/worker_messages.rb @@ -8,11 +8,22 @@ module Hydra #:nodoc: end end - # Message telling a worker to delegate a file to a runner - # TODO: move to master messages - class RunFile < Hydra::Messages::Runner::RunFile - def handle(worker) #:nodoc: - worker.delegate_file(self) + # Message telling the Runner to run a file + class RunFile < Hydra::Message + # The file that should be run + attr_accessor :file + def serialize #:nodoc: + super(:file => @file) + end + def handle(runner) #:nodoc: + runner.run_file(@file) + end + end + + # Message to tell the Runner to shut down + class Shutdown < Hydra::Message + def handle(runner) #:nodoc: + runner.stop end end @@ -23,14 +34,6 @@ module Hydra #:nodoc: end end - # Message telling the worker to shut down. - # TODO: move to master - class Shutdown < Hydra::Messages::Runner::Shutdown - def handle(worker) #:nodoc: - worker.shutdown - end - end - # Message a worker sends to a master to verify the connection class Ping < Hydra::Message def handle(master, worker) #:nodoc: diff --git a/lib/hydra/worker.rb b/lib/hydra/worker.rb index b8ccdd2..873dcb7 100644 --- a/lib/hydra/worker.rb +++ b/lib/hydra/worker.rb @@ -38,7 +38,7 @@ module Hydra #:nodoc: def delegate_file(message) r = idle_runner r[:idle] = false - r[:io].write(Hydra::Messages::Runner::RunFile.new(eval(message.serialize))) + r[:io].write(Hydra::Messages::Worker::RunFile.new(eval(message.serialize))) end # When a runner finishes, it sends the results up to the worker. Then the @@ -60,7 +60,7 @@ module Hydra #:nodoc: @runners.each do |r| $stdout.write "WORKER| Sending Shutdown to Runner\n" if @verbose $stdout.write " | #{r.inspect}\n" if @verbose - r[:io].write(Hydra::Messages::Runner::Shutdown.new) + r[:io].write(Hydra::Messages::Worker::Shutdown.new) end Thread.exit end diff --git a/test/runner_test.rb b/test/runner_test.rb index f251a62..3d75299 100644 --- a/test/runner_test.rb +++ b/test/runner_test.rb @@ -41,13 +41,13 @@ class RunnerTest < Test::Unit::TestCase # make sure it asks for a file, then give it one assert pipe.gets.is_a?(Hydra::Messages::Runner::RequestFile) - pipe.write(Hydra::Messages::Runner::RunFile.new(:file => TESTFILE)) + pipe.write(Hydra::Messages::Worker::RunFile.new(:file => TESTFILE)) # grab its response. This makes us wait for it to finish response = pipe.gets # tell it to shut down - pipe.write(Hydra::Messages::Runner::Shutdown.new) + pipe.write(Hydra::Messages::Worker::Shutdown.new) # ensure it ran assert File.exists?(TARGET) diff --git a/test/worker_test.rb b/test/worker_test.rb index 5e696ea..4036b2a 100644 --- a/test/worker_test.rb +++ b/test/worker_test.rb @@ -44,11 +44,11 @@ class WorkerTest < Test::Unit::TestCase num_runners.times do assert pipe.gets.is_a?(Hydra::Messages::Worker::RequestFile) end - pipe.write(Hydra::Messages::Worker::RunFile.new(:file => TESTFILE)) + pipe.write(Hydra::Messages::Master::RunFile.new(:file => TESTFILE)) assert pipe.gets.is_a?(Hydra::Messages::Worker::Results) - pipe.write(Hydra::Messages::Worker::Shutdown.new) + pipe.write(Hydra::Messages::Master::Shutdown.new) assert File.exists?(TARGET) assert_equal "HYDRA", File.read(TARGET)