view scripts/paxos.rb @ 169:1e696b2d3c6d

add paxos.rb paxos.dot
author akahori
date Tue, 22 Jan 2019 18:32:13 +0900
parents scripts/ring.rb@77169cd8a5e8
children
line wrap: on
line source

node_name_list = ["proposer", "acceptor", "learner"]

def create_nodes(name, node_num)
  (0..node_num - 1).map { |i|
    i = name + i.to_s
  }
end


def print_dot(connections)
  puts "digraph paxos {"
  connections.each { |connection|
    print "\t"
    print connection[0]
    print " -> "
    print connection[1]
    print ' [label="' + connection[2] + '"]'
    puts
  }
  puts "}"
end

node_list = Array.new
node_name_list.each_with_index { |node_name, i|
    node_num = ARGV.shift
    while !node_num
      printf("input %s num : ", node_name)
      node_num = STDIN.gets.chomp
    end
    node_list << create_nodes(node_name, node_num.to_i)
}
connections = Array.new

node_list.each_with_index { |nodes1, i|
  if i == node_name_list.size - 1
    break
  end
  nodes2 = node_list[i+1]
  nodes1.each_with_index{ |node1, j|

    nodes2.each_with_index{ |node2, k|
      connections << [node1, node2, node2]
      if i == 0
        connections << [node2,node1, node1]
      end
    }
  }
}

print_dot(connections.sort!)