Class: EventMachine::DNS::Socket

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

Instance Attribute Summary

Attributes inherited from Connection

#signature

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Connection

#associate_callback_target, #close_connection, #close_connection_after_writing, #comm_inactivity_timeout, #comm_inactivity_timeout=, #connection_completed, #detach, #error?, #get_cipher_bits, #get_cipher_name, #get_cipher_protocol, #get_idle_time, #get_outbound_data_size, #get_peer_cert, #get_peername, #get_pid, #get_proxied_bytes, #get_sni_hostname, #get_sock_opt, #get_sockname, #get_status, new, #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



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

def initialize
  @nameserver = nil
end

Class Method Details

.openObject



82
83
84
# File 'lib/em/resolver.rb', line 82

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

Instance Method Details

#deregister_request(id, req) ⇒ Object



122
123
124
125
# File 'lib/em/resolver.rb', line 122

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

#nameserverObject



135
136
137
# File 'lib/em/resolver.rb', line 135

def nameserver
  @nameserver || Resolver.nameserver
end

#nameserver=(ns) ⇒ Object



131
132
133
# File 'lib/em/resolver.rb', line 131

def nameserver=(ns)
  @nameserver = ns
end

#post_initObject



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

def post_init
  @requests = {}
end

#receive_data(data) ⇒ Object

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



141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/em/resolver.rb', line 141

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



112
113
114
115
116
117
118
119
120
# File 'lib/em/resolver.rb', line 112

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

  start_timer
end

#send_packet(pkt) ⇒ Object



127
128
129
# File 'lib/em/resolver.rb', line 127

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

#start_timerObject



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

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

#stop_timerObject



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

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

#tickObject



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

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

#unbindObject



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

def unbind
end