class Backport::Machine
The Backport server controller.
Attributes
mutex[R]
@return [Mutex]
Public Class Methods
new()
click to toggle source
# File lib/backport/machine.rb, line 5 def initialize @stopped = true @mutex = Mutex.new end
Public Instance Methods
prepare(server)
click to toggle source
Add a server to the machine. The server will be started when the machine starts. If the machine is already running, the server will be started immediately.
@param server [Server::Base] @return [void]
# File lib/backport/machine.rb, line 45 def prepare server server.add_observer self servers.push server server.start unless stopped? end
run() { |self| ... }
click to toggle source
Run the machine. If a block is provided, it gets executed before the maching starts its main loop. The main loop blocks program execution until the machine is stopped.
@yieldparam [self] @return [void]
# File lib/backport/machine.rb, line 16 def run return unless stopped? servers.clear @stopped = false yield self if block_given? run_server_thread end
servers()
click to toggle source
@return [Array<Server::Base>]
# File lib/backport/machine.rb, line 52 def servers @servers ||= [] end
stop()
click to toggle source
Stop the machine.
@return [void]
# File lib/backport/machine.rb, line 27 def stop servers.map(&:stop) servers.clear @stopped = true end
stopped?()
click to toggle source
True if the machine is stopped.
# File lib/backport/machine.rb, line 35 def stopped? @stopped ||= false end
update(server)
click to toggle source
@param server [Server::Base] @return [void]
# File lib/backport/machine.rb, line 58 def update server if server.stopped? servers.delete server stop if servers.empty? else mutex.synchronize { server.tick } end end
Private Instance Methods
run_server_thread()
click to toggle source
Start the thread that updates servers via the tick method.
@return [void]
# File lib/backport/machine.rb, line 75 def run_server_thread servers.map(&:start) sleep 0.1 until stopped? end