Class: Backport::Machine

Inherits:
Object
  • Object
show all
Defined in:
lib/backport/machine.rb

Overview

The Backport server controller.

Instance Method Summary collapse

Constructor Details

#initializeMachine

Returns a new instance of Machine.



5
6
7
# File 'lib/backport/machine.rb', line 5

def initialize
  @stopped = true
end

Instance Method Details

#prepare(server) ⇒ void

This method returns an undefined value.

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.

Parameters:



43
44
45
46
47
# File 'lib/backport/machine.rb', line 43

def prepare server
  server.add_observer self
  servers.push server
  server.start unless stopped?
end

#runvoid

This method returns an undefined value.

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.



14
15
16
17
18
19
20
# File 'lib/backport/machine.rb', line 14

def run
  return unless stopped?
  servers.clear
  @stopped = false
  yield if block_given?
  run_server_thread
end

#serversArray<Server::Base>

Returns:



50
51
52
# File 'lib/backport/machine.rb', line 50

def servers
  @servers ||= []
end

#stopvoid

This method returns an undefined value.

Stop the machine.



25
26
27
28
29
# File 'lib/backport/machine.rb', line 25

def stop
  servers.map(&:stop)
  servers.clear
  @stopped = true
end

#stopped?Boolean

True if the machine is stopped.

Returns:

  • (Boolean)


33
34
35
# File 'lib/backport/machine.rb', line 33

def stopped?
  @stopped ||= false
end

#update(server) ⇒ Object

Parameters:



55
56
57
58
59
60
61
62
# File 'lib/backport/machine.rb', line 55

def update server
  if server.stopped?
    servers.delete server
    stop if servers.empty?
  else
    mutex.synchronize { server.tick }
  end
end