Class: EventMachine::DnsResolver::DnsSocket

Inherits:
EM::Connection
  • Object
show all
Defined in:
lib/em-dns-resolver.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.openObject



65
66
67
# File 'lib/em-dns-resolver.rb', line 65

def self.open
  EM::open_datagram_socket('0.0.0.0', 0, self)
end

Instance Method Details

#connected?Boolean

Returns:

  • (Boolean)


110
# File 'lib/em-dns-resolver.rb', line 110

def connected?; @connected; end

#nameserver_portObject



92
93
94
# File 'lib/em-dns-resolver.rb', line 92

def nameserver_port
  @nameserver_port ||= DnsResolver.nameserver_port
end

#nameserver_port=(ns_p) ⇒ Object



89
90
91
# File 'lib/em-dns-resolver.rb', line 89

def nameserver_port= (ns_p)
  @nameserver_port = ns_p
end

#post_initObject



68
69
70
71
72
# File 'lib/em-dns-resolver.rb', line 68

def post_init
  @requests = {}
  @connected = true
  EM.add_periodic_timer(0.1, &method(:tick))
end

#receive_data(data) ⇒ Object

Decodes the packet, looks for the request and passes the response over to the requester



97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/em-dns-resolver.rb', line 97

def receive_data(data)
  msg = nil
  begin
    msg = Resolv::DNS::Message.decode data
  rescue
  else
    req = @requests[msg.id]
    if req
      @requests.delete(msg.id)
      req.receive_answer(msg)
    end
  end
end

#register_request(id, req) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/em-dns-resolver.rb', line 79

def register_request(id, req)
  if @requests.has_key?(id)
    raise RequestIdAlreadyUsed
  else
    @requests[id] = req
  end
end

#send_packet(pkt) ⇒ Object



86
87
88
# File 'lib/em-dns-resolver.rb', line 86

def send_packet(pkt)
  send_datagram pkt, *nameserver_port
end

#tickObject

Periodically called each second to fire request retries



74
75
76
77
78
# File 'lib/em-dns-resolver.rb', line 74

def tick
  @requests.each do |id,req|
    req.tick
  end
end

#unbindObject



111
112
113
# File 'lib/em-dns-resolver.rb', line 111

def unbind
  @connected = false
end