Class: Node

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

Constant Summary collapse

ACK =
{ 't' => 'ACK', 'name' => ENV['LOGNAME'] }

Instance Method Summary collapse

Constructor Details

#initialize(domain, port) ⇒ Node

Returns a new instance of Node.



9
10
11
12
# File 'lib/node.rb', line 9

def initialize(domain, port)
  @socket = DemoSocket.connect(domain, port)
  @current_role = nil
end

Instance Method Details

#runObject



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

def run
  loop do
    msg = @socket.recv
    case msg['t']
    when 'HRT'
      @socket.send(ACK)
    when 'ROLE'
      puts "You, my friend, have a new role. It is '#{msg['role']}'."
      set_role msg
      @socket.send(ACK)
    when 'STOP'
      puts "Cancelling current role."
      stop_role
      @socket.send(ACK)
    when 'DOWN'
      puts "It's nothing to be concerned about, but you're being temporarily disabled."
      disable_role
      @socket.send(ACK)
    when 'UP'
      puts "You're back!"
      enable_role
      @socket.send(ACK)
    when 'LINKDOWN'
      puts "Temporarily disabling the '#{msg['type']}' link to #{msg['addr']}..."
      disable_link msg['type'], msg['addr']
      @socket.send(ACK)
    when 'LINKUP'
      puts "Reenabling the '#{msg['type']}' link to #{msg['addr']}!"
      enable_link msg['type'], msg['addr']
      @socket.send(ACK)
    when 'STATE'
      @socket.send get_state.merge('status' => @status)
    when 'DIE'
      puts msg['err']
      exit
    end
  end
end