Class: Net::DNS::MDNS::Answer

Inherits:
Object
  • Object
show all
Defined in:
lib/net/dns/mdns.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, ttl, data, cacheflush) ⇒ Answer

Returns a new instance of Answer.



212
213
214
215
216
217
218
219
# File 'lib/net/dns/mdns.rb', line 212

def initialize(name, ttl, data, cacheflush)
  @name = name
  @ttl = ttl
  @data = data
  @cacheflush = cacheflush
  @toa = Time.now.to_i
  @retries = 0
end

Instance Attribute Details

#cacheflushObject (readonly)

Returns the value of attribute cacheflush.



207
208
209
# File 'lib/net/dns/mdns.rb', line 207

def cacheflush
  @cacheflush
end

#dataObject (readonly)

Returns the value of attribute data.



207
208
209
# File 'lib/net/dns/mdns.rb', line 207

def data
  @data
end

#nameObject (readonly)

Returns the value of attribute name.



207
208
209
# File 'lib/net/dns/mdns.rb', line 207

def name
  @name
end

#retriesObject

Returns the value of attribute retries.



210
211
212
# File 'lib/net/dns/mdns.rb', line 210

def retries
  @retries
end

#toaObject (readonly)

TOA - time of arrival (of an answer)



209
210
211
# File 'lib/net/dns/mdns.rb', line 209

def toa
  @toa
end

#ttlObject (readonly)

Returns the value of attribute ttl.



207
208
209
# File 'lib/net/dns/mdns.rb', line 207

def ttl
  @ttl
end

Instance Method Details

#absolute?Boolean

Returns:

  • (Boolean)


241
242
243
# File 'lib/net/dns/mdns.rb', line 241

def absolute?
  @cacheflush
end

#expired?Boolean

Returns:

  • (Boolean)


237
238
239
# File 'lib/net/dns/mdns.rb', line 237

def expired?
  true if Time.now.to_i > expiry
end

#expiryObject



233
234
235
# File 'lib/net/dns/mdns.rb', line 233

def expiry
  toa + (ttl == 0 ? 1 : ttl)
end

#refreshObject



225
226
227
228
229
230
231
# File 'lib/net/dns/mdns.rb', line 225

def refresh
  # Percentage points are from mDNS
  percent = [80,85,90,95][retries]

  # TODO - add a 2% of TTL jitter
  toa + ttl * percent / 100 if percent
end

#to_sObject



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
# File 'lib/net/dns/mdns.rb', line 245

def to_s
  s = "#{name.to_s} (#{ttl}) "
  s << '!' if absolute?
  s << '-' if ttl == 0
  s << " #{DNS.rrname(data)}"

  case data
  when IN::A
    s << " #{data.address.to_s}"
  when IN::PTR
    s << " #{data.name}"
  when IN::SRV
    s << " #{data.target}:#{data.port}"
  when IN::TXT
    s << " #{data.strings.first.inspect}#{data.strings.length > 1 ? ', ...' : ''}"
  when IN::HINFO
    s << " os=#{data.os}, cpu=#{data.cpu}"
  else
    s << data.inspect
  end
  s
end

#typeObject



221
222
223
# File 'lib/net/dns/mdns.rb', line 221

def type
  data.class
end