Class: Equity::Manager

Inherits:
Object
  • Object
show all
Defined in:
lib/equity/manager.rb

Constant Summary collapse

CMD_NODE_STATUS =
"S"
CMD_SHUTDOWN =
"K"
CMD_NAMES =
{
  CMD_NODE_STATUS => "status",
  CMD_SHUTDOWN    => "shutdown"
}

Instance Method Summary collapse

Constructor Details

#initialize(nodes, port, debugging = false) ⇒ Manager

Returns a new instance of Manager.



14
15
16
17
18
19
# File 'lib/equity/manager.rb', line 14

def initialize(nodes, port, debugging = false)
  @nodes = nodes
  @socket = UDPSocket.new
  @socket.bind(nil, port)
  @debugging = debugging
end

Instance Method Details

#process_commands ⇒ Object



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
46
47
48
49
50
51
52
53
# File 'lib/equity/manager.rb', line 21

def process_commands
  # Read commands from the network.
  begin
    data, address = @socket.recvfrom_nonblock(4096)
  rescue Errno::EAGAIN
  end
  return unless data
  command = data.split(/#{Controller::Key::SIGNATURE_SEPARATOR}/).first
  debug("received manager command: #{command_name(command)}", address)
  # Verify the signature and ensure that the key used to sign the command
  # is authorized.
  unless verified_data = Controller::Key.verify(data)
    debug("signature verification failed - ignoring command", address)
    return
  end
  unless Controller::Key.signature_key(data).authorized?
    debug("command signed by unauthorized key - ignoring command", address)
    return
  end
  data = verified_data
  # Process the command.
  debug("signature verified - processing command", address)
  case data
  when CMD_NODE_STATUS
    reply = node_status_message
  when CMD_SHUTDOWN
    UDPSocket.new.send(generic_ok_message, 0, address[2], address[1])
    exit
  else
    return
  end
  UDPSocket.new.send(reply, 0, address[2], address[1])
end