Class: RobotArmy::Soldier

Inherits:
Object show all
Defined in:
lib/robot-army/soldier.rb

Direct Known Subclasses

Officer

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(messenger) ⇒ Soldier

Returns a new instance of Soldier.



4
5
6
# File 'lib/robot-army/soldier.rb', line 4

def initialize(messenger)
  @messenger = messenger
end

Instance Attribute Details

#messengerObject (readonly)

Returns the value of attribute messenger.



2
3
4
# File 'lib/robot-army/soldier.rb', line 2

def messenger
  @messenger
end

Instance Method Details

#listenObject



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/robot-army/soldier.rb', line 8

def listen
  request  = messenger.get
  result   = run(request[:command], request[:data])
  if result.marshalable?
    response = {:status => 'ok', :data => result}
  else
    response = {
      :status => 'warning', 
      :data => "ignoring invalid remote return value #{result.inspect}"}
  end
  debug "#{self.class} post(#{response.inspect})"
  messenger.post response
end

#run(command, data) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/robot-army/soldier.rb', line 22

def run(command, data)
  debug "#{self.class} running command=#{command.inspect}"
  case command
  when :info
    {:pid => Process.pid, :type => self.class.name}
  when :eval
    instance_eval(data[:code], data[:file], data[:line])
  when :exit
    # tell the parent we're okay before we exit
    messenger.post(:status => 'ok')
    raise RobotArmy::Exit
  else
    raise ArgumentError, "Unrecognized command #{command.inspect}"
  end
end