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

#deregister_request(id, req) ⇒ Object



99
100
101
102
# File 'lib/em/resolver.rb', line 99

def deregister_request(id, req)
  @requests.delete(id)
  stop_timer if @requests.length == 0
end

#nameserverObject



112
113
114
# File 'lib/em/resolver.rb', line 112

def nameserver
  @nameserver || Resolver.nameserver
end

#nameserver=(ns) ⇒ Object



108
109
110
# File 'lib/em/resolver.rb', line 108

def nameserver=(ns)
  @nameserver = ns
end

#post_initObject



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

def post_init
  @requests = {}
end

#receive_data(data) ⇒ Object

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



118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/em/resolver.rb', line 118

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)
      stop_timer if @requests.length == 0
      req.receive_answer(msg)
    end
  end
end

#register_request(id, req) ⇒ Object



89
90
91
92
93
94
95
96
97
# File 'lib/em/resolver.rb', line 89

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

  start_timer
end

#send_packet(pkt) ⇒ Object



104
105
106
# File 'lib/em/resolver.rb', line 104

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

#start_timerObject



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

def start_timer
  @timer ||= EM.add_periodic_timer(0.1, &method(:tick))
end

#stop_timerObject



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

def stop_timer
  EM.cancel_timer(@timer)
  @timer = nil
end

#tickObject



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

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

#unbindObject



80
81
# File 'lib/em/resolver.rb', line 80

def unbind
end