Class: Wamupd::AvahiServiceFile

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/wamupd/avahi_service_file.rb

Overview

AvahiServiceFile is a class to abstract Avahi’s .service files. It is capable of loading, parsing, and validating these files into a group of AvahiService objects.

Constant Summary collapse

SERVICE_DTD_PATH =
"/usr/local/share/avahi/avahi-service.dtd"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ AvahiServiceFile

Initialize the service from a service file



89
90
91
92
93
94
# File 'lib/wamupd/avahi_service_file.rb', line 89

def initialize(filename)
    @valid = false
    @services = []
    @replace_wildcards = false
    load_from_file(filename)
end

Class Method Details

.load_from_directory(dir) ⇒ Object

Load all of the service definitions in a directory

Returns: an array of AvahiService objects



151
152
153
154
155
156
157
# File 'lib/wamupd/avahi_service_file.rb', line 151

def self.load_from_directory(dir)
    retval = []
    Dir.glob(File.join(dir, "*.service")).each { |f|
        retval.push(AvahiServiceFile.new(f))
    }
    return retval
end

.replace_wildcards(string) ⇒ Object

Replace the wildcards in a string using the Avahi rules



79
80
81
# File 'lib/wamupd/avahi_service_file.rb', line 79

def self.replace_wildcards(string)
    return string.sub("%h", MainSettings.instance.hostname)
end

Instance Method Details

#each(&block) ⇒ Object

Access each service in turn



57
58
59
60
61
# File 'lib/wamupd/avahi_service_file.rb', line 57

def each(&block)
    @services.each { |c|
        yield c
    }
end

#firstObject

The first service defined



69
70
71
72
73
74
75
# File 'lib/wamupd/avahi_service_file.rb', line 69

def first
    if (self.size > 0)
        return @services[0]
    else
        return nil
    end
end

#load_from_file(filename) ⇒ Object

Load this AvahiService entry from a .service defintion file

Arguments:

filename

The file to load



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/wamupd/avahi_service_file.rb', line 100

def load_from_file(filename)
    d = XML::Document.file(filename)
    if (not File.exists?(SERVICE_DTD_PATH))
        $stderr.puts "Could not find service DTD at #{SERVICE_DTD_PATH}"
        exit(1)
    end
    dtd = File.open(SERVICE_DTD_PATH, "r") { |f|
        lines = f.readlines().join("")
    }
    validator = XML::Dtd.new(dtd)
    @valid = d.validate(validator)
    if (not @valid)
        $stderr.puts "Service file #{filename} failed validation"
        exit(1)
    end
    sg = d.root
    sg.children.each { |c|
        case c.name
        when "name"
            @name = c.content
            if ((not c["replace-wildcards"].nil?) and (c["replace-wildcards"] == "yes"))
                @replace_wildcards = true
            end
        when "service"
            node = c
            params = {}
            node.children.each { |c|
                case c.name
                when "type"
                    params[:type] = c.content
                when "subtype"
                    params[:subtype] = c.content
                when "host-name"
                    params[:hostname] = c.content
                when "port"
                    params[:port] = c.content.to_i
                when "txt-record"
                    params[:txt] = c.content
                when "domain-name"
                    params[:domainname] = c.content
                end
            }
            @services.push(AvahiService.new(self.name, params))
        end
    }
end

#nameObject

Get the name of this service



49
50
51
52
53
54
# File 'lib/wamupd/avahi_service_file.rb', line 49

def name
    if (@replace_wildcards)
        return AvahiServiceFile.replace_wildcards(@name)
    end
    return @name
end

#sizeObject

The number of services in this definition



64
65
66
# File 'lib/wamupd/avahi_service_file.rb', line 64

def size
    return @services.count
end

#valid?Boolean

Has this service file entry been validated against the DTD?

Returns:

  • (Boolean)


84
85
86
# File 'lib/wamupd/avahi_service_file.rb', line 84

def valid?
    return @valid
end