Hey guys... here's my attempt. I'm a bit of n00b, so feedback
welcome!
I attacked this with DRb (distributed ruby), since it hadn't been
touched yet. It meant taking a serious hit in performance, but it was
at least interesting. It's threaded, but could be forked (haven't
tried that just yet).
Thanks!
---BEGIN SOLUTION---
# Num. 135
# process_rings.rb
require 'drb'
BasePort = 7654
class RingParent
def initialize(processes = 3, cycles = 5)
@processes = processes
@cycles = cycles
@message = "Message from parent\n"
end
def start
spawn_processes
connect_ring
send_messages
end
def spawn_processes
t = []
for i in 0...@processes-1
t << Thread.new do
RingMember.new(BasePort+i, BasePort+i+1, self)
end
end
t << Thread.new do
RingMember.new(BasePort+@processes-1, BasePort)
end
end
def connect_ring
DRb.start_service
@ring = DRbObject.new(nil, "druby://127.0.0.1:#{BasePort}")
end
def send_messages
@start = Time.now
@cycles.times do
@ring.parent_receive("Hi ring!")
end
end
def return_message(message)
puts "Parent: Got message back- circulation time: #{Time.now -
@start}"
end
end
class RingMember
def initialize(port, next_port, parent = nil)
@port = port
@parent = parent
@current_message = ""
@next_member = connect_next(next_port)
DRBService.new(self, @port)
end
def connect_next(port)
DRb.start_service
DRbObject.new(nil, "druby://127.0.0.1:#{port}")
end
def parent_receive(message)
@current_message = message
forward_message(@current_message)
end
def receive_message(message)
begin
message == @current_message ?
(@parent.return_message(message);(@current_message = "")) :
forward_message(message)
rescue
puts "#{@port}: Received duplicate message, couldn't talk to
parent: #{$!}"
end
end
def forward_message(message)
@next_member.receive_message(message)
end
def test(message)
return "#{@port}: Got message #{message}"
end
end
class DRBService
def initialize(process, port)
DRb.start_service("druby://:#{port}", process)
DRb.thread.join
end
end
processes = ARGV[0].to_i
cycles = ARGV[1].to_i
parent = RingParent.new(processes, cycles)
On Aug 22, 3:00 pm, "Adam Shelly" <adam.she... / gmail.com> wrote:
> On 8/22/07, James Edward Gray II <ja... / grayproductions.net> wrote:> On Aug 22, 2007, at 1:45 PM, Adam Shelly wrote:
> > > I've been thinking about virtual machines recently, so I decided to
> > > implement one in Ruby for this quiz.
>
> > That gets my vote for the craziest Ruby Quiz solution ever. Wow!
>
> > You have no idea how much I regret that I wrote the quiz summary
> > earlier today. :(
>
> You have no idea how much I regret that I had a bunch of real work to
> do and didn't have time to finish it up sooner. :)
>
> I also wish I had time and a valid reason to keep working on extending
> the VM. The first thing I'd do is find a good name for the
> language...
>
> -Adam