Changeset

8266:9a97dd174ec9

net.adns: Restructure to allow creating separate resolver objects, like net.dns
author Matthew Wild <mwild1@gmail.com>
date Mon, 25 Sep 2017 23:06:00 +0100
parents 8265:17ebd8ac8545
children 8267:42fad8465537
files net/adns.lua
diffstat 1 files changed, 46 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- a/net/adns.lua	Sun Sep 24 18:23:45 2017 -0400
+++ b/net/adns.lua	Mon Sep 25 23:06:00 2017 +0100
@@ -7,7 +7,7 @@
 --
 
 local server = require "net.server";
-local dns = require "net.dns";
+local new_resolver = require "net.dns".resolver;
 
 local log = require "util.logger".init("adns");
 
@@ -17,35 +17,11 @@
 
 local _ENV = nil;
 
-local function lookup(handler, qname, qtype, qclass)
-	return coroutine.wrap(function (peek)
-				if peek then
-					log("debug", "Records for %s already cached, using those...", qname);
-					handler(peek);
-					return;
-				end
-				log("debug", "Records for %s not in cache, sending query (%s)...", qname, tostring(coroutine.running()));
-				local ok, err = dns.query(qname, qtype, qclass);
-				if ok then
-					coroutine.yield({ qclass or "IN", qtype or "A", qname, coroutine.running()}); -- Wait for reply
-					log("debug", "Reply for %s (%s)", qname, tostring(coroutine.running()));
-				end
-				if ok then
-					ok, err = pcall(handler, dns.peek(qname, qtype, qclass));
-				else
-					log("error", "Error sending DNS query: %s", err);
-					ok, err = pcall(handler, nil, err);
-				end
-				if not ok then
-					log("error", "Error in DNS response handler: %s", tostring(err));
-				end
-			end)(dns.peek(qname, qtype, qclass));
-end
+local async_resolver_methods = {};
+local async_resolver_mt = { __index = async_resolver_methods };
 
-local function cancel(handle, call_handler, reason)
-	log("warn", "Cancelling DNS lookup for %s", tostring(handle[3]));
-	dns.cancel(handle[1], handle[2], handle[3], handle[4], call_handler);
-end
+local query_methods = {};
+local query_mt = { __index = query_methods };
 
 local function new_async_socket(sock, resolver)
 	local peername = "<unknown>";
@@ -54,7 +30,7 @@
 	local err;
 	function listener.onincoming(conn, data)
 		if data then
-			dns.feed(handler, data);
+			resolver:feed(handler, data);
 		end
 	end
 	function listener.ondisconnect(conn, err)
@@ -85,10 +61,47 @@
 	return handler;
 end
 
-dns.socket_wrapper_set(new_async_socket);
+function async_resolver_methods:lookup(handler, qname, qtype, qclass)
+	local resolver = self._resolver;
+	return coroutine.wrap(function (peek)
+				if peek then
+					log("debug", "Records for %s already cached, using those...", qname);
+					handler(peek);
+					return;
+				end
+				log("debug", "Records for %s not in cache, sending query (%s)...", qname, tostring(coroutine.running()));
+				local ok, err = resolver:query(qname, qtype, qclass);
+				if ok then
+					coroutine.yield(setmetatable({ resolver, qclass or "IN", qtype or "A", qname, coroutine.running()}, query_mt)); -- Wait for reply
+					log("debug", "Reply for %s (%s)", qname, tostring(coroutine.running()));
+				end
+				if ok then
+					ok, err = pcall(handler, resolver:peek(qname, qtype, qclass));
+				else
+					log("error", "Error sending DNS query: %s", err);
+					ok, err = pcall(handler, nil, err);
+				end
+				if not ok then
+					log("error", "Error in DNS response handler: %s", tostring(err));
+				end
+			end)(resolver:peek(qname, qtype, qclass));
+end
+
+function query_methods:cancel(call_handler, reason)
+	log("warn", "Cancelling DNS lookup for %s", tostring(self[4]));
+	self[1].cancel(self[2], self[3], self[4], self[5], call_handler);
+end
+
+local function new_async_resolver()
+	local resolver = new_resolver();
+	resolver:socket_wrapper_set(new_async_socket);
+	return setmetatable({ _resolver = resolver}, async_resolver_mt);
+end
 
 return {
-	lookup = lookup;
-	cancel = cancel;
+	lookup = function (...)
+		return new_async_resolver():lookup(...);
+	end;
+	resolver = new_async_resolver;
 	new_async_socket = new_async_socket;
 };