Class: Net::DNS::MDNS::Cache

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

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCache

Returns a new instance of Cache.



314
315
316
317
318
# File 'lib/net/dns/mdns.rb', line 314

def initialize
  @asked = Hash.new { |h,k| h[k] = Hash.new }

  @cached = Hash.new { |h,k| h[k] = (Hash.new { |a,b| a[b] = Array.new }) }
end

Instance Attribute Details

#askedObject (readonly)

asked: Hash -> Hash -> Question



309
310
311
# File 'lib/net/dns/mdns.rb', line 309

def asked
  @asked
end

#cachedObject (readonly)

cached: Hash -> Hash -> Array -> Answer



312
313
314
# File 'lib/net/dns/mdns.rb', line 312

def cached
  @cached
end

Instance Method Details

#add_question(qu) ⇒ Object

Return the question if we added it, or nil if question is already being asked.



321
322
323
324
325
# File 'lib/net/dns/mdns.rb', line 321

def add_question(qu)
  if qu && !@asked[qu.name][qu.type]
    @asked[qu.name][qu.type] = qu
  end
end

#answers_for(name, type) ⇒ Object



369
370
371
372
373
374
375
376
377
378
379
# File 'lib/net/dns/mdns.rb', line 369

def answers_for(name, type)
  answers = []
  if( name.to_s == '*' )
    @cached.keys.each { |n| answers += answers_for(n, type) }
  elsif( type == IN::ANY )
    @cached[name].each { |rtype,rdata| answers += rdata }
  else
    answers += @cached[name][type]
  end
  answers
end

#asked?(name, type) ⇒ Boolean

Returns:

  • (Boolean)


381
382
383
384
385
386
387
388
389
# File 'lib/net/dns/mdns.rb', line 381

def asked?(name, type)
  return true if name.to_s == '*'

  t = @asked[name][type] || @asked[name][IN::ANY]

  # TODO - true if (Time.now - t) < some threshold...

  t
end

#cache_answer(an) ⇒ Object

Return cached answer, or nil if answer wasn’t cached.



336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# File 'lib/net/dns/mdns.rb', line 336

def cache_answer(an)
  answers = @cached[an.name][an.type]

  if( an.absolute? )
    # Replace all answers older than a ~1 sec [mDNS].
    # If the data is the same, don't delete it, we don't want it to look new.
    now_m1 = Time.now.to_i - 1
    answers.delete_if { |a| a.toa < now_m1 && a.data != an.data }
  end

  old_an = answers.detect { |a| a.name == an.name && a.data == an.data }

  if( !old_an )
    # new answer, cache it
    answers << an
  elsif( an.ttl == 0 )
    # it's a "remove" notice, replace old_an
    answers.delete( old_an )
    answers << an
  elsif( an.expiry > old_an.expiry)
    # it's a fresher record than we have, cache it but the data is the
    # same so don't report it as cached
    answers.delete( old_an )
    answers << an
    an = nil
  else
    # don't cache it
    an = nil
  end

  an
end

#cache_question(name, type) ⇒ Object

Cache question. Increase the number of times we’ve seen it.



328
329
330
331
332
333
# File 'lib/net/dns/mdns.rb', line 328

def cache_question(name, type)
  if qu = @asked[name][type]
    qu.update
  end
  qu
end