Class: Unbound::Resolver
- Inherits:
-
Object
- Object
- Unbound::Resolver
- Includes:
- CallbacksMixin
- Defined in:
- lib/unbound/resolver.rb
Overview
A simple asynchronous resolver
Instance Method Summary collapse
-
#cancel_all ⇒ Object
Cancel all outstanding queries.
- #cancel_query(query) ⇒ Object
-
#close ⇒ Object
Cancel all queries and close the resolver down.
- #closed? ⇒ Boolean
-
#initialize(ctx) ⇒ Resolver
constructor
A new instance of Resolver.
- #io ⇒ Object
-
#outstanding_queries ⇒ Integer
The number of queries for which we are awaiting reply.
-
#outstanding_queries? ⇒ Boolean
True if there are any queries for which we are awaiting reply.
- #process ⇒ Object
- #send_query(query) ⇒ Object
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.
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_all ⇒ Object
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
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 |
#close ⇒ Object
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
73 74 75 |
# File 'lib/unbound/resolver.rb', line 73 def closed? @ctx.closed? end |
#io ⇒ Object
85 86 87 |
# File 'lib/unbound/resolver.rb', line 85 def io @ctx.io end |
#outstanding_queries ⇒ Integer
Returns 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.
42 43 44 |
# File 'lib/unbound/resolver.rb', line 42 def outstanding_queries? @queries.count > 0 end |
#process ⇒ Object
90 91 92 |
# File 'lib/unbound/resolver.rb', line 90 def process @ctx.process end |
#send_query(query) ⇒ Object
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 |