Bring aboard a factory to create DNS resolvers.
authorGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Mon, 5 Mar 2012 00:03:45 +0000 (21:03 -0300)
committerGuilherme Maciel Ferreira <guilherme.maciel.ferreira@gmail.com>
Mon, 5 Mar 2012 00:03:45 +0000 (21:03 -0300)
src/CMakeLists.txt
src/dns/dnsresolverfactory.cpp [new file with mode: 0644]
src/dns/dnsresolverfactory.h [new file with mode: 0644]

index befa52e..89f4aeb 100644 (file)
@@ -56,6 +56,7 @@ set(SOURCES
     config/option/statusnotifiercmdoption.cpp
     config/option/versionoption.cpp
     dns/dnsresolver.cpp
+    dns/dnsresolverfactory.cpp
     dns/hostaddress.cpp
     dns/timetolive.cpp
     host/hoststatus.cpp
diff --git a/src/dns/dnsresolverfactory.cpp b/src/dns/dnsresolverfactory.cpp
new file mode 100644 (file)
index 0000000..12f8da2
--- /dev/null
@@ -0,0 +1,76 @@
+/*
+ The software in this package is distributed under the GNU General
+ Public License version 2 (with a special exception described below).
+
+ A copy of GNU General Public License (GPL) is included in this distribution,
+ in the file COPYING.GPL.
+
+ As a special exception, if other files instantiate templates or use macros
+ or inline functions from this file, or you compile this file and link it
+ with other works to produce a work based on this file, this file
+ does not by itself cause the resulting work to be covered
+ by the GNU General Public License.
+
+ However the source code for this file must still be made available
+ in accordance with section (3) of the GNU General Public License.
+
+ This exception does not invalidate any other reasons why a work based
+ on this file might be covered by the GNU General Public License.
+ */
+
+#include "dns/dnsresolverfactory.h"
+
+#include <boost/net/dns.hpp>
+
+using namespace std;
+using boost::net::dns::type_t;
+
+//-----------------------------------------------------------------------------
+// DnsResolverFactory
+//-----------------------------------------------------------------------------
+
+/**
+ * @brief Default constructor.
+ */
+DnsResolverFactory::DnsResolverFactory()
+{
+}
+
+/**
+ * @brief Destructor.
+ */
+DnsResolverFactory::~DnsResolverFactory()
+{
+}
+
+/**
+ * @brief Creates a DNS resolver suitable to the given protocol.
+ *
+ * @param destination_address The remote address to ping.
+ * @param nameserver Server to resolve the addresses.
+ * @param ping_protocol The protocol used to ping, in order to find the DNS resolver more
+ * appropriated to resolve the addresses for this protocol.
+ */
+DnsResolverItem DnsResolverFactory::createResolver(
+        const string &destination_address,
+        const string &nameserver,
+        const PingProtocol ping_protocol
+)
+{
+    switch ( ping_protocol )
+    {
+    case PingProtocol_ICMP:
+    case PingProtocol_TCP:
+        return DnsResolverItem(
+                new DnsResolver( destination_address, nameserver, boost::net::dns::type_a )
+        );
+    case PingProtocol_ICMPv6:
+    case PingProtocol_TCP_IPv6:
+        return DnsResolverItem(
+                new DnsResolver( destination_address, nameserver, boost::net::dns::type_a6 )
+        );
+    default:
+        BOOST_ASSERT( !"Try to create a DNS from an invalid address resource record" );             //lint !e506
+        return DnsResolverItem();                                                                   //lint !e527
+    }
+}
diff --git a/src/dns/dnsresolverfactory.h b/src/dns/dnsresolverfactory.h
new file mode 100644 (file)
index 0000000..ff698be
--- /dev/null
@@ -0,0 +1,48 @@
+/*
+ The software in this package is distributed under the GNU General
+ Public License version 2 (with a special exception described below).
+
+ A copy of GNU General Public License (GPL) is included in this distribution,
+ in the file COPYING.GPL.
+
+ As a special exception, if other files instantiate templates or use macros
+ or inline functions from this file, or you compile this file and link it
+ with other works to produce a work based on this file, this file
+ does not by itself cause the resulting work to be covered
+ by the GNU General Public License.
+
+ However the source code for this file must still be made available
+ in accordance with section (3) of the GNU General Public License.
+
+ This exception does not invalidate any other reasons why a work based
+ on this file might be covered by the GNU General Public License.
+ */
+
+#ifndef DNS_RESOLVER_FACTORY_H
+#define DNS_RESOLVER_FACTORY_H
+
+#include <string>
+
+#include "dns/dnsresolver.h"
+#include "host/pingprotocol.h"
+
+//-----------------------------------------------------------------------------
+// DnsResolverFactory
+//-----------------------------------------------------------------------------
+
+class DnsResolverFactory
+{
+public:
+    static DnsResolverItem createResolver(
+            const std::string &destination_address,
+            const std::string &nameserver,
+            const PingProtocol ping_protocol
+    );
+
+private:
+    DnsResolverFactory();
+    ~DnsResolverFactory();
+
+};
+
+#endif // DNS_RESOLVER_FACTORY_H