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.



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

def initialize(ctx)
  @ctx = ctx
  @queries = {}
  @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.object_id)
  end
end

Instance Method Details

#cancel_allObject

Cancel all outstanding queries.



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

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

#closeObject

Cancel all queries and close the resolver down.



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

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

#closed?Boolean

Returns:

  • (Boolean)


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

def closed?
  @ctx.closed?
end

#ioObject

See Also:



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

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



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

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



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

def outstanding_queries?
  @queries.count > 0
end

#processObject

See Also:



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

def process
  @ctx.process
end

#send_query(query) ⇒ Object

Parameters:



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

def send_query(query)
  if query.started?
    raise QueryAlreadyStarted.new
  end
  @queries[query.object_id] = query
  # 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)
  oid_ptr = FFI::Pointer.new query.object_id
  async_id = @ctx.resolve_async(
    query.name, 
    query.rrtype, 
    query.rrclass, 
    @resolve_callback_func, 
    oid_ptr)
  query.start!(async_id)
end