Skip to content

Commit

Permalink
namespace under EM::DNS
Browse files Browse the repository at this point in the history
  • Loading branch information
igrigorik committed Feb 4, 2011
1 parent 8189472 commit cdbbcd7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 27 deletions.
43 changes: 22 additions & 21 deletions lib/em/resolver.rb
Original file line number Diff line number Diff line change
@@ -1,36 +1,37 @@
module EventMachine
module DNSResolver
module DNS
class Resolver
def self.resolve(hostname)
Request.new(socket, hostname)
end

def self.resolve(hostname)
Request.new(socket, hostname)
end
def self.socket
if !@socket || (@socket && @socket.error?)
@socket = Socket.open
end

def self.socket
if !@socket || (@socket && @socket.error?)
@socket = DNSSocket.open
@socket
end

@socket
end

def self.nameserver=(ns)
@nameserver = ns
end
def self.nameserver=(ns)
@nameserver = ns
end

def self.nameserver
unless defined?(@nameserver)
IO::readlines('/etc/resolv.conf').each do |line|
if line =~ /^nameserver (.+)$/
@nameserver = $1.split(/\s+/).first
def self.nameserver
unless defined?(@nameserver)
IO::readlines('/etc/resolv.conf').each do |line|
if line =~ /^nameserver (.+)$/
@nameserver = $1.split(/\s+/).first
end
end
end
@nameserver
end
@nameserver
end

class RequestIdAlreadyUsed < RuntimeError; end

class DNSSocket < EventMachine::Connection
class Socket < EventMachine::Connection
def self.open
EventMachine::open_datagram_socket('0.0.0.0', 0, self)
end
Expand Down Expand Up @@ -67,7 +68,7 @@ def nameserver=(ns)
end

def nameserver
@nameserver ||= DNSResolver.nameserver
@nameserver ||= Resolver.nameserver
end

# Decodes the packet, looks for the request and passes the
Expand Down
10 changes: 4 additions & 6 deletions tests/test_resolver.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
require 'em_test_helper'

class TestBasic < Test::Unit::TestCase

def test_a
EM.run {
d = EM::DNSResolver.resolve "google.com"
d = EM::DNS::Resolver.resolve "google.com"
d.errback { assert false }
d.callback { |r|
assert r
Expand All @@ -15,15 +14,15 @@ def test_a

def test_bad_host
EM.run {
d = EM::DNSResolver.resolve "asdfasasdf"
d = EM::DNS::Resolver.resolve "asdfasasdf"
d.callback { assert false }
d.errback { assert true; EM.stop }
}
end

def test_a_pair
EM.run {
d = EM::DNSResolver.resolve "google.com"
d = EM::DNS::Resolver.resolve "google.com"
d.errback { assert false }
d.callback { |r|
assert_equal(Array, r.class)
Expand All @@ -32,5 +31,4 @@ def test_a_pair
}
}
end

end
end

0 comments on commit cdbbcd7

Please sign in to comment.