Class: Unbound::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/unbound/context.rb

Instance Method Summary collapse

Constructor Details

#initializeContext

Returns a new instance of Context.



6
7
8
9
# File 'lib/unbound/context.rb', line 6

def initialize
  @ub_ctx = Unbound::Bindings.ub_ctx_create()
  @raise_on_noid = false
end

Instance Method Details

#cancel_async_query(async_id) ⇒ Object



69
70
71
72
# File 'lib/unbound/context.rb', line 69

def cancel_async_query(async_id)
  check_closed!
  raise_if_error!(Unbound::Bindings.ub_cancel(@ub_ctx, async_id))
end

#check_closed!Object



19
20
21
22
23
# File 'lib/unbound/context.rb', line 19

def check_closed!
  if @ub_ctx.nil?
    raise ContextClosedError.new
  end
end

#closeObject



58
59
60
61
62
# File 'lib/unbound/context.rb', line 58

def close
  check_closed!
  Unbound::Bindings.ub_ctx_delete(@ub_ctx)
  @ub_ctx = nil
end

#closed?Boolean

Returns:

  • (Boolean)


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

def closed?
  @ub_ctx.nil?
end

#fdObject

Returns the number for the file descriptor.



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

def fd
  check_closed!
  Unbound::Bindings.ub_fd(@ub_ctx)
end

#get_option(varname) ⇒ Object



43
44
45
46
47
48
# File 'lib/unbound/context.rb', line 43

def get_option(varname)
  check_closed!
  ret_ptr = FFI::MemoryPointer.new(:pointer)
  raise_if_error!(Unbound::Bindings.ub_ctx_get_option(@ub_ctx, varname, ret_ptr))
  ret_ptr.read_pointer().read_string()
end

#ioFFI::IO

Returns a brand new IO for the file descriptor that can be selected to determine if processing needs to be done. Warning: DO NOT CLOSE THIS IO OBJECT! Use Context#close, instead.

Returns:

  • (FFI::IO)

    a brand new IO for the context’s file descriptor



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

def io
  ret = ::FFI::IO.for_fd(self.fd)
  ret.autoclose = false
  return ret
end

#load_config(filename) ⇒ Object



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

def load_config(filename)
  check_closed!
  raise_if_error!(Unbound::Bindings.ub_ctx_config(@ub_ctx, filename))
end

#processObject



64
65
66
67
# File 'lib/unbound/context.rb', line 64

def process
  check_closed!
  raise_if_error!(Unbound::Bindings.ub_process(@ub_ctx))
end

#raise_if_error!(retval) ⇒ Object

Raises:



25
26
27
28
29
30
31
32
# File 'lib/unbound/context.rb', line 25

def raise_if_error!(retval)
  return retval if retval == :noerror

  if retval == :noid && @raise_on_noid == false
    return retval
  end
  raise APIError.new(retval)
end

#raise_on_noid=(b) ⇒ Object



11
12
13
# File 'lib/unbound/context.rb', line 11

def raise_on_noid=(b)
  @raise_on_noid = (b == true)
end

#raise_on_noid?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/unbound/context.rb', line 15

def raise_on_noid?
  @raise_on_noid
end

#resolve_async(name, rrtype, rrclass, callback, private_data = nil) ⇒ Object



90
91
92
93
94
95
96
97
# File 'lib/unbound/context.rb', line 90

def resolve_async(name, rrtype, rrclass, callback, private_data = nil)
  check_closed!
  async_id_ptr = FFI::MemoryPointer.new :int
  raise_if_error!(
    Unbound::Bindings.ub_resolve_async(
      @ub_ctx, name, rrtype, rrclass, private_data, callback, async_id_ptr))
  return async_id_ptr.get_int(0)
end

#set_option(varname, val) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/unbound/context.rb', line 50

def set_option(varname, val)
  check_closed!
  unless varname.end_with?(':')
    varname = varname + ":"
  end
  raise_if_error!(Unbound::Bindings.ub_ctx_set_option(@ub_ctx, varname, val))
end