Class: Statt::Motor

Inherits:
Object
  • Object
show all
Defined in:
lib/statt/motor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Motor

Returns a new instance of Motor.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/statt/motor.rb', line 9

def initialize(options={})
  
  options = { :hub_host => "localhost", 
              :hub_port => 4444,
              :rc_options => {:host => "localhost", :port => 5555 } }.merge(options)
  
  hub_options = { :host => options[:hub_host], 
                  :port => options[:hub_port] }
  
  # Start up Hub server in its own thread.
  @hub_thread = Thread.new do 
    Thread.current[:hub] = SGrid::Hub.new(hub_options)
    Thread.current[:hub].start
    self.pass
  end
  
  @hub_thread[:hub].wait_until_up_and_running
  
  # Start up the RC Servers, each within their own thread.
  @rc_threads = []
  @rc_threads << Thread.new do
    rc_options = options[:rc_options] 
    if options[:rc_options].is_a?(Hash)
      Thread.current[:server] = SGrid::RemoteControl.new(rc_options)
      Thread.current[:server].start if rc_options[:host] == "localhost"
    elsif options[:rc_options].is_a?(Array)
      rc_options.each do |rc_option|
        Thread.current[:server] = SGrid::RemoteControl.new(rc_option)
        Thread.current[:server].start if rc_option[:host] == "localhost"
      end
    else
      raise Exception, "there was a problem starting the rc server(s)", caller
    end
    self.pass
  end
  
end

Instance Attribute Details

#rcObject

Returns the value of attribute rc.



7
8
9
# File 'lib/statt/motor.rb', line 7

def rc
  @rc
end

Instance Method Details

#shutdown_hubObject



53
54
55
56
# File 'lib/statt/motor.rb', line 53

def shutdown_hub
  @hub_thread[:hub].shutdown
  puts "."
end

#shutdown_rcsObject



58
59
60
61
62
63
# File 'lib/statt/motor.rb', line 58

def shutdown_rcs
  @rc_threads.each do |thr|
    thr[:server].shutdown
    puts "."
  end
end

#stopObject



47
48
49
50
51
# File 'lib/statt/motor.rb', line 47

def stop
  Thread.new { shutdown_hub }
  Thread.new { shutdown_rcs }
  puts "Shutting down all servers "
end