Class: Unbound::Resolver

Inherits:
Object
  • Object
show all
Includes:
CallbacksMixin
Defined in:
lib/unbound/resolver.rb

Overview

A simple asynchronous resolver

Instance Method Summary collapse

Methods included from CallbacksMixin

#on_answer, #on_cancel, #on_error, #on_finish, #on_start

Constructor Details

#initialize(ctx) ⇒ Resolver

Returns a new instance of Resolver.

Parameters:

  • ctx (Unbound::Context)

    The context around which we will wrap this resolver.



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/unbound/resolver.rb', line 14

def initialize(ctx)
  @ctx = ctx
  @queries = QueryStore.new
  @resolve_callback_func = FFI::Function.new(
    :void, [:pointer, :int, :pointer], 
    self.method(:resolve_callback))

  init_callbacks

  on_cancel do |query|
    if query.async_id
      @ctx.cancel_async_query(query.async_id)
    end
  end
  on_finish do |query|
    @queries.delete_query(query)
  end
end

Instance Method Details

#cancel_allObject

Cancel all outstanding queries.



64
65
66
67
68
69
# File 'lib/unbound/resolver.rb', line 64

def cancel_all
  @queries.each do |query|
    query.cancel!
  end
  @queries.clear
end

#closeObject

Cancel all queries and close the resolver down.



76
77
78
79
80
# File 'lib/unbound/resolver.rb', line 76

def close
  return if self.closed? == true
  self.cancel_all
  @ctx.close
end

#closed?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/unbound/resolver.rb', line 71

def closed?
  @ctx.closed?
end

#ioObject

See Also:



83
84
85
# File 'lib/unbound/resolver.rb', line 83

def io
  @ctx.io
end

#outstanding_queriesInteger

Returns the number of queries for which we are awaiting reply.

Returns:

  • (Integer)

    the number of queries for which we are awaiting reply



34
35
36
# File 'lib/unbound/resolver.rb', line 34

def outstanding_queries
  @queries.count
end

#outstanding_queries?Boolean

Returns true if there are any queries for which we are awaiting reply.

Returns:

  • (Boolean)

    true if there are any queries for which we are awaiting reply



40
41
42
# File 'lib/unbound/resolver.rb', line 40

def outstanding_queries?
  @queries.count > 0
end

#processObject

See Also:



88
89
90
# File 'lib/unbound/resolver.rb', line 88

def process
  @ctx.process
end

#send_query(query) ⇒ Object

Parameters:



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/unbound/resolver.rb', line 93

def send_query(query)
  if query.started?
    raise QueryAlreadyStarted.new
  end
  # Add all of our callbacks, if any have been registered.
  query.on_start(*@callbacks_start) unless @callbacks_start.empty?
  query.on_answer(*@callbacks_answer) unless @callbacks_answer.empty?
  query.on_error(*@callbacks_error) unless @callbacks_error.empty?
  query.on_cancel(*@callbacks_cancel)
  query.on_finish(*@callbacks_finish)
  ptr = @queries.store(query)
  async_id = @ctx.resolve_async(
    query.name, 
    query.rrtype, 
    query.rrclass, 
    @resolve_callback_func, 
    ptr)
  query.start!(async_id)
end