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.
-
#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 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_all ⇒ Object
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 |
#close ⇒ Object
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
71 72 73 |
# File 'lib/unbound/resolver.rb', line 71 def closed? @ctx.closed? end |
#io ⇒ Object
83 84 85 |
# File 'lib/unbound/resolver.rb', line 83 def io @ctx.io end |
#outstanding_queries ⇒ Integer
Returns 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.
40 41 42 |
# File 'lib/unbound/resolver.rb', line 40 def outstanding_queries? @queries.count > 0 end |
#process ⇒ Object
88 89 90 |
# File 'lib/unbound/resolver.rb', line 88 def process @ctx.process end |
#send_query(query) ⇒ Object
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 |