Class: Master

Inherits:
Object
  • Object
show all
Includes:
Server
Defined in:
lib/master.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Server

#run_server, #shutdown_server

Constructor Details

#initializeMaster

Returns a new instance of Master.



15
16
17
# File 'lib/master.rb', line 15

def initialize
  @new_nodes = []
end

Instance Attribute Details

#scenarioObject (readonly)

Returns the value of attribute scenario.



19
20
21
# File 'lib/master.rb', line 19

def scenario
  @scenario
end

#scenario_nameObject (readonly)

Returns the value of attribute scenario_name.



19
20
21
# File 'lib/master.rb', line 19

def scenario_name
  @scenario_name
end

Instance Method Details

#change_scenario(scenario_name) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/master.rb', line 21

def change_scenario(scenario_name)
  if Scenario.index[scenario_name]
    @scenario_name = scenario_name
    @scenario = Scenario.index[scenario_name].new
  
    @scenario.run(Client.clients)
  end
end

#clear_new_nodesObject



63
64
65
66
67
# File 'lib/master.rb', line 63

def clear_new_nodes
  new_nodes = @new_nodes
  @new_nodes = []
  new_nodes
end

#get_new_nodesObject



56
57
58
59
60
61
# File 'lib/master.rb', line 56

def get_new_nodes
  new_nodes = clear_new_nodes
  new_nodes.map { |client|
    [client, client.msg('t' => 'STATE')]
  }
end


43
44
45
46
47
48
# File 'lib/master.rb', line 43

def print_state
  Client.clients.each do |client|
    msg = client.msg 't' => 'STATE'
    puts "#{client.addr} state: #{msg.inspect}"
  end
end

#runObject



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/master.rb', line 30

def run
  run_server '0.0.0.0', 9125 do |client_socket|
    if Client.clients.size > 19
      client_socket.send 't' => 'DIE', 'err' => 'Too many connections. Sorry!'
      client_socket.close
    else
      puts "+++ #{client_socket.addr} (#{client_socket.hostname}) has connected"
      c = Client.new(client_socket)
      @new_nodes << c
    end
  end
end

#statesObject



50
51
52
53
54
# File 'lib/master.rb', line 50

def states
  Client.clients.map { |client|
    [client, client.msg('t' => 'STATE')]
  }
end