#!/usr/bin/env ruby

def mac_info(win)
  win.remove(win.child)

  table = Gtk::Table.new(4, 2, false)

# イメージ
  image_show(0, 1, 0, 4, table)

# メッセージ
  str = "MAC Addressを集めます。\nスレーブノードの開始IPアドレスを入力し、\n「STARTボタン」を押して下さい。"
  $flag="mac_pre"
  dummy = ""

  label = Gtk::Label.new(NKF.nkf("-m0 -w", str))
  table.attach(label, 1, 3, 0, 1, Gtk::FILL, Gtk::FILL, 0, 10)
  
  str2 = "START IP Address"
  label2 = Gtk::Label.new(str2)
  table.attach(label2, 1, 2, 1, 2, Gtk::FILL, Gtk::FILL, 0, 0)
  table.set_column_spacing(2, 15)
  
  startip = Gtk::Entry.new
  startip.set_text($h["startip"])

  #table.attach(startip, 2, 3, 1, 2, 10, 10)
  table.attach(startip, 2, 3, 1, 2, Gtk::FILL, Gtk::FILL, 0 ,10)

# ruby-gtk で CList を使って、4列の表を作る。
# ListStore を作る
  model = Gtk::ListStore.new(String,String,String,String)
  
# TreeView を作る
  @lst = Gtk::TreeView.new(model)

# 1列目の表示方法をきめる。
  renderer1 = Gtk::CellRendererText.new
  renderer1.expander = true
  column1 = Gtk::TreeViewColumn.new('Num',renderer1,'text' => 0) \
                .set_fixed_width(10) \
                .set_min_width(1) \
                .set_sizing(Gtk::TreeViewColumn::AUTOSIZE)
  @lst.append_column(column1)

# 2列目
  renderer2 = Gtk::CellRendererText.new
  renderer2.expander=true
  column2 = Gtk::TreeViewColumn.new('Host',renderer2,'text' => 1) \
                .set_fixed_width(200) \
                .set_min_width(1) \
                .set_sizing(Gtk::TreeViewColumn::AUTOSIZE)
  @lst.append_column(column2)

# 3列目
  renderer3 = Gtk::CellRendererText.new
  renderer3.expander=true
  column3 = Gtk::TreeViewColumn.new('IP Address',renderer3,'text' => 2) \
                .set_fixed_width(200) \
                .set_min_width(1) \
                .set_sizing(Gtk::TreeViewColumn::AUTOSIZE)
  @lst.append_column(column3)

# 4列目
  renderer4 = Gtk::CellRendererText.new
  renderer4.expander=true
  column4 = Gtk::TreeViewColumn.new('MAC Address',renderer4,'text' => 3) \
                .set_fixed_width(200) \
                .set_min_width(1) \
                .set_sizing(Gtk::TreeViewColumn::AUTOSIZE)
  @lst.append_column(column4)

# 行を付け加える。
  model = @lst.model
  iter = model.append

# スクロールバーの設定
  sw = Gtk::ScrolledWindow.new
  sw.add(@lst)
  table.attach(sw, 1, 3, 3, 4)

# STARTボタンの設定
  button = Gtk::Button.new("start")
  table.attach(button, 1, 3, 2, 3, Gtk::FILL, Gtk::FILL, 0, 10)

  mac = get_interfaces_info("mac_address", $h["eth"])
  $h.store("mac_address", mac)
  mac_store = Array.new
  
  button.signal_connect("clicked") do
    $flag = "mac_after"
    th = Thread.new { $dia.wait }
    th2 = Thread.new {
      $h.store("startip", startip.text)
      get_mac($h, $tmpfile)
    }

    $cancel = false
    $finish = false
    File.unlink($tmpfile) if FileTest.exist?($tmpfile)

    mac_store.clear
    (0..1000).each {|i|
      break if ($cancel) || ($finish)
      sleep(2)
      win.remove(win.child)
      button = Gtk::Button.new("start")
      table.attach(button, 1, 3, 2, 3, Gtk::FILL, Gtk::FILL, 0, 10)
      if FileTest.exist?($tmpfile)
        open($tmpfile, "r"){|f|
          while line = f.gets
            mac_store.push(line)
          end
        }

        line = ""
        open($tmpfile){|f|
          nil while f.gets
          line = f.lineno
        }

        $finish = true if line >= $h["number"]

        model.clear
        mac_store.each_with_index{|value, number|
          iter = model.prepend
          number = number + 1
          puts number.to_s + " : " + value
          iter.set_value(0, number.to_s)
          iter.set_value(1, value.split[0])
          iter.set_value(2, value.split[1])
          iter.set_value(3, value.split[2].chomp)
        }

        mac_store.clear
      end
      sw.add(@lst)
      table.attach(sw, 1, 3, 3, 4, Gtk::FILL, Gtk::FILL, 0, 0)
      win.add(table)
      win.show_all
    }

    Gtk.main_quit
    th.join
    $dia.finish
  end
  
##############################
# main
##############################
  $dia = MyDialog.new
  bottom_show(0, 3, 4, 5, win, table, dummy)
  win.add(table)
  win.set_default_size(500,300)
  win.show_all
end


if __FILE__ == $0
  require "load"

  Gtk.init

  window = Gtk::Window.new
  window.signal_connect("destroy") {
    Gtk::main_quit
  }

  $h = Hash.new
  $h.store("eth", "eth1")
  $h.store("ip_address", get_interfaces_info("ip_address", $h["eth"]))
  $h.store("netmask", get_interfaces_info("netmask", $h["eth"]))
  $h.store("network", get_interfaces_info("network", $h["eth"]))
  $h.store("broadcast", get_interfaces_info("broadcast", $h["eth"]))
  # local IP Address + 1
  b = IP_Addr.new($h["netmask"], $h["ip_address"], 2)
  ip = ("%032b") % (b.ipaddr_change_10_2($h["ip_address"]).to_i(2) + 1)
  $h.store("startip", b.ipaddr_change_2_10_dot(ip))

  num = 1
  $h.store("number", num)
  $h.store("swap", 512)
  $h.store("prefix", "pc")
  $h.store("domain", "dcast.org")
  $h.store("digit", 3)

  mac_info(window)

  Gtk.main
end
