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
# 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_finish do |query|
    @queries.delete_query(query)
  end
end

Instance Method Details

#cancel_allObject

Cancel all outstanding queries.



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

def cancel_all
  @queries.each do |query|
    cancel_query(query)
  end
  @queries.clear
end

#cancel_query(query) ⇒ Object

Parameters:



29
30
31
32
33
# File 'lib/unbound/resolver.rb', line 29

def cancel_query(query)
  return if query.async_id.nil?
  @ctx.cancel_async_query(query.async_id)
  query.cancel!
end

#closeObject

Cancel all queries and close the resolver down.



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

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

#closed?Boolean

Returns:

  • (Boolean)


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

def closed?
  @ctx.closed?
end

#ioObject

See Also:



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

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



36
37
38
# File 'lib/unbound/resolver.rb', line 36

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



42
43
44
# File 'lib/unbound/resolver.rb', line 42

def outstanding_queries?
  @queries.count > 0
end

#processObject

See Also:



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

def process
  @ctx.process
end

#send_query(query) ⇒ Object

Parameters:



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

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) unless @callbacks_cancel.empty?

  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