Last active
May 6, 2019 01:50
-
-
Save adamgotterer/c13f7cf4d8d2e42a545aeab6881df62f to your computer and use it in GitHub Desktop.
Ruby Elevator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
# elevator.rb | |
require 'drb/drb' | |
requests = [] | |
DRb.start_service('druby://localhost:9999', requests) | |
tick = 0 | |
loop do | |
puts "Tick: #{tick}" | |
req = requests.shift | |
puts "Someone on floor #{req[0]} requested to go to floor #{req[1]}" if req | |
tick += 1 | |
sleep 1 | |
end | |
#------------------------------ | |
#!/usr/bin/ruby | |
require 'drb/drb' | |
class People | |
attr_reader :queue | |
MAX_FLOOR = 10 | |
def connect | |
DRb.start_service | |
@queue = DRbObject.new_with_uri('druby://localhost:9999') | |
end | |
def reconnect | |
puts 'Connection failure. Retrying...' | |
connect | |
end | |
def start_request_loop | |
connect | |
loop do | |
floors = (0..MAX_FLOOR).to_a | |
start, dest = floors.sample(2) | |
queue.push([start, dest]) | |
p [start, dest] | |
sleep rand(0.0..10.0) | |
end | |
rescue DRb::DRbConnError | |
reconnect | |
sleep 2 | |
retry | |
end | |
end | |
people = People.new | |
people.start_request_loop |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment