Class: Murakumo::Server

Inherits:
RExec::Daemon::Base
  • Object
show all
Defined in:
lib/srv/murakumo_server.rb

Overview

RExecに依存しているのでこんな設計に…

Constant Summary collapse

IN =
Resolv::DNS::Resource::IN

Class Method Summary collapse

Class Method Details

.init(options) ⇒ Object



22
23
24
25
26
# File 'lib/srv/murakumo_server.rb', line 22

def init(options)
  # クラスインスタンス変数を使わないこと
  @@options = options
  @@cloud = Cloud.new(options)
end

.runObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/srv/murakumo_server.rb', line 32

def run
  EventMachine.epoll

  RubyDNS.run_server(:listen => [[:udp, @@options[:dns_address], @@options[:dns_port]]]) do
    # RubyDNS::Serverのコンテキスト
    @logger = @@options[:logger] if @@options[:logger]

    on(:start) do
      if @@options[:socket]
        # 既存のソケットファイルは削除
        FileUtils.rm_f(@@options[:socket])

        # ServerクラスをDRuby化
        DRb.start_service("drbunix:#{@@options[:socket]}", @@cloud)
        File.chmod(0700, @@options[:socket])
        at_exit { FileUtils.rm_f(@@options[:socket]) }
      end

      # HUPでログをローテート
      if @@options[:logger]
        trap(:HUP) do
          if logger = @@options[:logger]
            logdev = logger.instance_variable_get(:@logdev)

            if (dev = logdev.dev).kind_of?(File)
              path = dev.path
              mutex = logdev.instance_variable_get(:@mutex)

              mutex.synchronize do
                dev.reopen(path, 'a')
                dev.sync = true
              end
            end
          end
        end
      end

      # ゴシッププロトコルを開始
      @@cloud.start
    end

    # look up A record
    match(@@cloud.method(:address_exist?), IN::A) do |transaction|
      records = @@cloud.lookup_addresses(transaction.name)

      addrs = []
      min_ttl = nil # 最小のTTLをセット

      records.each do |r|
        address, ttl = r

        if min_ttl.nil? or ttl < min_ttl
          min_ttl = ttl
        end

        addrs << Resolv::DNS::Resource::IN::A.new(address)
      end

      # 直接引数に渡せないので…
      addrs << {:ttl => min_ttl}

      # スループットをあげるためrespond!は呼ばない
      transaction.append!(*addrs)
    end # match

    # look up PTR record
    match(@@cloud.method(:name_exist?), IN::PTR) do |transaction|
      name, ttl = @@cloud.lookup_name(transaction.name)
      name += ".#{@@options[:domain]}" if @@options[:domain]
      transaction.respond!(Resolv::DNS::Name.create("#{name}."), :ttl => ttl)
    end

    if @@options[:resolver]
      otherwise do |transaction|
        transaction.passthrough!(@@options[:resolver])
      end
    end
  end # RubyDNS.run_server
end

.shutdownObject

run



112
113
114
# File 'lib/srv/murakumo_server.rb', line 112

def shutdown
  @@cloud.stop
end

.working_directory=(path) ⇒ Object



28
29
30
# File 'lib/srv/murakumo_server.rb', line 28

def working_directory=(path)
  @@base_directory = path
end