Class: EventMachine::DNS::Socket

Inherits:
Connection show all
Defined in:
lib/em/resolver.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Connection

#close_connection, #close_connection_after_writing, #comm_inactivity_timeout, #comm_inactivity_timeout=, #connection_completed, #detach, #error?, #get_idle_time, #get_peer_cert, #get_peername, #get_pid, #get_proxied_bytes, #get_sock_opt, #get_sockname, #get_status, #notify_readable=, #notify_readable?, #notify_writable=, #notify_writable?, #pause, #paused?, #pending_connect_timeout, #pending_connect_timeout=, #proxy_completed, #proxy_incoming_to, #proxy_target_unbound, #reconnect, #resume, #send_data, #send_datagram, #send_file_data, #set_sock_opt, #ssl_handshake_completed, #ssl_verify_peer, #start_tls, #stop_proxying, #stream_file_data

Constructor Details

#initializeSocket

Returns a new instance of Socket.



63
64
65
# File 'lib/em/resolver.rb', line 63

def initialize
  @nameserver = nil
end

Class Method Details

.openObject



59
60
61
# File 'lib/em/resolver.rb', line 59

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

Instance Method Details

#nameserverObject



97
98
99
# File 'lib/em/resolver.rb', line 97

def nameserver
  @nameserver || Resolver.nameserver
end

#nameserver=(ns) ⇒ Object



93
94
95
# File 'lib/em/resolver.rb', line 93

def nameserver=(ns)
  @nameserver = ns
end

#post_initObject



67
68
69
70
# File 'lib/em/resolver.rb', line 67

def post_init
  @requests = {}
  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



103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/em/resolver.rb', line 103

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



81
82
83
84
85
86
87
# File 'lib/em/resolver.rb', line 81

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

#send_packet(pkt) ⇒ Object



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

def send_packet(pkt)
  send_datagram(pkt, nameserver, 53)
end

#tickObject



75
76
77
78
79
# File 'lib/em/resolver.rb', line 75

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

#unbindObject



72
73
# File 'lib/em/resolver.rb', line 72

def unbind
end