Class: Wamupd::DNSAvahiController

Inherits:
Object
  • Object
show all
Includes:
Signals
Defined in:
lib/wamupd/dns_avahi_controller.rb

Overview

Coordinate between a set of Avahi Services and DNS records

Signals

:added

Raised when a new service is added to the controller and successfully registered. has two parameters: the AvahiService added and the queue ID of the DNS request (or true if the request was synchronous)

:deleted

Raised when a service is deleted from the controller. Contains the deleted service as its parameter

:renewed

Raised when a service’s lease is renewed. Contains the renewed service as its parameter

:quit

Raised when the controller is quitting

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Signals

#on

Constructor Details

#initializeDNSAvahiController

Initialize the controller.



63
64
65
66
67
68
69
70
71
72
# File 'lib/wamupd/dns_avahi_controller.rb', line 63

def initialize()
    @sa = MainSettings.instance
    # Services stored as a hash from 
    @services = {}
    @resolver = @sa.resolver
    @added = []
    @queue = Queue.new
    # Make a min priority queue for leases
    @lease_queue = Containers::PriorityQueue.new { |x,y| (x<=>y) == -1 }
end

Instance Attribute Details

#queueObject (readonly)

A queue to put actions into.



60
61
62
# File 'lib/wamupd/dns_avahi_controller.rb', line 60

def queue
  @queue
end

Instance Method Details

#add_service(service) ⇒ Object

Add a single service record to the controller



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/wamupd/dns_avahi_controller.rb', line 80

def add_service(service)
    if service.kind_of?(AvahiService)
        if (not @services.has_key?(service.identifier))
            @services[service.identifier] = service
        else
            raise DuplicateServiceError.new("Got a duplicate")
        end
    elsif (service.kind_of?(AvahiServiceFile))
        service.each { |service_entry|
            add_service(service_entry)
        }
    else
        raise ArgumentError.new("Not an AvahiService")
    end
end

#add_services(services) ⇒ Object

Add an array of services to the controller



75
76
77
# File 'lib/wamupd/dns_avahi_controller.rb', line 75

def add_services(services)
    services.each { |s| add_service(s) }
end

#delete_service(service) ⇒ Object

Delete a signle service record from the service



97
98
99
100
101
102
103
# File 'lib/wamupd/dns_avahi_controller.rb', line 97

def delete_service(service)
    if service.kind_of?(AvahiService)
        @services.delete(service.identifier)
    else
        raise ArgumentError.new("Not an AvahiService")
    end
end

#exitObject

Exit out of the main loop



201
202
203
# File 'lib/wamupd/dns_avahi_controller.rb', line 201

def exit
    @queue << Wamupd::Action.new(Wamupd::ActionType::QUIT)
end

#keysObject

Keys



111
112
113
# File 'lib/wamupd/dns_avahi_controller.rb', line 111

def keys
    return @services.keys
end

#publish_allObject

Publish all currently stored records



116
117
118
119
120
121
122
# File 'lib/wamupd/dns_avahi_controller.rb', line 116

def publish_all
    ids = []
    @services.each { |key,service|
        ids << publish(service)
    }
    return ids
end

#runObject

Wait for data to go into the queue, and handle it when it does



206
207
208
209
210
# File 'lib/wamupd/dns_avahi_controller.rb', line 206

def run
    while true
        process_action(@queue.pop)
    end
end

#sizeObject

Return the number of elements in the controller



106
107
108
# File 'lib/wamupd/dns_avahi_controller.rb', line 106

def size
    return @services.size
end

#unpublish(service, ttl = @sa.ttl) ⇒ Object

Unpublish a single service



163
164
165
166
167
168
169
170
171
172
173
# File 'lib/wamupd/dns_avahi_controller.rb', line 163

def unpublish(service, ttl=@sa.ttl)
    todo = []
    to_update << {:target=>service.type_in_zone,
        :type=>Dnsruby::Types.PTR,
        :value=>service.type_in_zone_with_name}
    to_update << {:target=>service.type_in_zone_with_name, 
        :type=>DnsRuby::Types.SRV}
    to_update << {:target=>service.type_in_zone_with_name,
        :type=>Dnsruby::Types.TXT}
    DNSUpdate.unpublish_all(todo)
end

#unpublish_allObject

Unpublish all stored records



125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/wamupd/dns_avahi_controller.rb', line 125

def unpublish_all
    todo = []
    @services.each { |key,service|
        todo << { :target=>service.type_in_zone_with_name, 
            :type=>Dnsruby::Types.SRV,
            :value=> "#{@sa.priority} #{@sa.weight} #{service.port} #{service.target}"}
        todo << { :target=>service.type_in_zone, 
            :type=>Dnsruby::Types.PTR,
            :value=>service.type_in_zone_with_name}
        todo << { :target=>service.type_in_zone_with_name,
            :type=>Dnsruby::Types.TXT}
    }
    DNSUpdate.unpublish_all(*todo)
end

#update_leasesObject

Takes care of updating leases. Run it in a separate thread from the main “run” function



214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/wamupd/dns_avahi_controller.rb', line 214

def update_leases
    while true
        now = Time.now
        while (not @lease_queue.empty?) and (@lease_queue.next.date < now)
            item = @lease_queue.pop
            if @services.has_key?(item.service.identifier)
                signal(:renewed, item.service)
                publish(item.service)
            end
        end
        sleep(@sa.sleep_time)
    end
end