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
8
# File 'lib/backport/machine.rb', line 5

def initialize
  @stopped = true
  @mutex = Mutex.new
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:



45
46
47
48
49
# File 'lib/backport/machine.rb', line 45

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

#run {|| ... } ⇒ void

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.

Yield Parameters:

  • (self)


16
17
18
19
20
21
22
# 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

#serversArray<Server::Base>

Returns:



52
53
54
# File 'lib/backport/machine.rb', line 52

def servers
  @servers ||= []
end

#stopvoid

This method returns an undefined value.

Stop the machine.



27
28
29
30
31
# File 'lib/backport/machine.rb', line 27

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

#stopped?Boolean

True if the machine is stopped.

Returns:

  • (Boolean)


35
36
37
# File 'lib/backport/machine.rb', line 35

def stopped?
  @stopped ||= false
end

#update(server) ⇒ void

This method returns an undefined value.

Parameters:



58
59
60
61
62
63
64
65
# 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