implemented serialization, is compiling
authorChristian Herdtweck <christian.herdtweck@intra2net.com>
Thu, 9 Apr 2015 13:16:59 +0000 (15:16 +0200)
committerChristian Herdtweck <christian.herdtweck@intra2net.com>
Mon, 4 May 2015 14:57:57 +0000 (16:57 +0200)
src/dns/hostaddress.cpp
src/dns/hostaddress.h
src/dns/timetolive.cpp
src/dns/timetolive.h
src/dns_neww/dnscache.cpp
src/dns_neww/dnsresolver.h

index ebc01ad..89b64b5 100644 (file)
@@ -20,6 +20,7 @@ on this file might be covered by the GNU General Public License.
 #include "dns/hostaddress.h"
 
 #include <boost/version.hpp>
+#include <boost/serialization/serialization.hpp>
 
 #include "boost_assert_handler.h"
 
@@ -72,3 +73,10 @@ void HostAddress::set_ttl( const TimeToLive &ttl )
 {
     Ttl = ttl;
 }
+
+template<class Archive>
+void HostAddress::serialize(Archive & ar, const unsigned int version)
+{
+    ar & Ip;
+    ar & Ttl;
+}
index 2a160e6..0c70827 100644 (file)
@@ -25,6 +25,7 @@ on this file might be covered by the GNU General Public License.
 #include <list>
 
 #include <boost/asio.hpp>
+#include <boost/serialization/access.hpp>
 
 #include "dns/timetolive.h"
 
@@ -54,6 +55,10 @@ private:
     /// time-to-live of the host IP
     TimeToLive Ttl;
 
+    /// serialization
+    friend class boost::serialization::access;
+    template<class Archive>
+    void serialize(Archive & ar, const unsigned int version);
 };
 
 //-----------------------------------------------------------------------------
index 1497042..62f61ca 100644 (file)
@@ -21,6 +21,8 @@ on this file might be covered by the GNU General Public License.
 
 #include "boost_assert_handler.h"
 
+#include <boost/serialization/serialization.hpp>
+
 using boost::date_time::time_resolution_traits_adapted64_impl;
 using boost::posix_time::microsec_clock;
 using boost::posix_time::ptime;
@@ -72,3 +74,10 @@ uint32_t TimeToLive::get_updated_value() const
 
     return remaining_seconds;
 }
+
+template<class Archive>
+void TimeToLive::serialize(Archive & ar, const unsigned int version)
+{
+    ar & Ttl;
+    ar & TtlSetTime;
+}
index cfd5168..a73b2a3 100644 (file)
@@ -23,6 +23,7 @@ on this file might be covered by the GNU General Public License.
 #include <stdint.h>
 
 #include <boost/asio.hpp>
+#include <boost/serialization/access.hpp>
 
 //-----------------------------------------------------------------------------
 // TimeToLive
@@ -45,6 +46,12 @@ private:
     /// the time when the time-to-live was set, so it is possible to know the
     /// elapsed time
     boost::posix_time::ptime TtlSetTime;
+
+
+    /// serialization
+    friend class boost::serialization::access;
+    template<class Archive>
+    void serialize(Archive & ar, const unsigned int version);
 };
 
 #endif // TIME_TO_LIVE_H
index f7ae74f..6e7fcca 100644 (file)
 
 #include "dns_neww/dnscache.h"
 
+#include <fstream>
 #include <logfunc.hpp>
+#include <filefunc.hxx>   // I2n::file_exists
 #include <boost/bind.hpp>
 #include <boost/date_time/posix_time/posix_time.hpp>
 #include <boost/asio/placeholders.hpp>
+#include <boost/serialization/serialization.hpp>
+#include <boost/serialization/map.hpp>
+#include <boost/serialization/string.hpp>
+#include <boost/serialization/vector.hpp>
+#include <boost/archive/xml_oarchive.hpp>
+#include <boost/archive/xml_iarchive.hpp>
 
 using boost::bind;
 using boost::posix_time::seconds;
@@ -98,17 +106,63 @@ void DnsCache::save_to_cachefile()
         GlobalLogger.info() << "DNS Cache: skip saving because has not changed";
         return;
     }
+    else if (CacheFile.empty())
+    {
+        GlobalLogger.warning()
+                           << "DNS Cache: skip saving because file name empty!";
+        return;
+    }
 
-    // TODO (see trusted_net_helper, boost serialization, xml)
-    GlobalLogger.error() << "DNS Cache: Actual saving not implemented yet!";
-    HasChanged = false;
+    try
+    {
+        std::ofstream ofs( CacheFile.c_str() );
+        boost::archive::xml_oarchive oa(ofs);
+        oa << boost::serialization::make_nvp("DataCache", DataCache);
+        GlobalLogger.info() << "DNS Cache: saved to cache file " << CacheFile;
+
+        HasChanged = false;
+    }
+    catch (std::exception &exc)
+    {
+        GlobalLogger.warning() << "Saving failed: " << exc.what();
+    }
 }
 
 
 void DnsCache::load_from_cachefile()
 {
-    // TODO: some boost serialization and xml stuff, see trusted_net_helper
-    GlobalLogger.error() << "DNS Cache: Actual loading not implemented yet!";
+    if (CacheFile.empty())
+    {
+        GlobalLogger.warning()
+                  << "DNS Cache: cannot load because cache file name is empty!";
+        return;
+    }
+    else if ( !I2n::file_exists(CacheFile) )
+    {
+        GlobalLogger.warning() << "DNS Cache: cannot load because cache file "
+                               << CacheFile << " does not exist!";
+        return;
+    }
+    try
+    {
+        HostAddressVec cache;
+
+        std::ifstream ifs( CacheFile.c_str() );
+        boost::archive::xml_iarchive ia(ifs);
+
+        ia >> boost::serialization::make_nvp("DataCache", cache);
+        GlobalLogger.info() << "DNS Cache: loaded from file " << CacheFile;
+    }
+    catch (boost::archive::archive_exception &exc)
+    {
+        GlobalLogger.warning() << "DNS Cache: archive exception loading from "
+                               << CacheFile << ": " << exc.what();
+    }
+    catch (std::exception &exc)
+    {
+        GlobalLogger.warning() << "DNS Cache: exception while loading from "
+                               << CacheFile << ": " << exc.what();
+    }
 }
 
 /**
index 4565cb1..5556502 100644 (file)
@@ -29,7 +29,7 @@
 #include <boost/asio/ip/udp.hpp>
 #include <boost/asio/ip/address.hpp>
 #include <boost/system/error_code.hpp>
-#include <boost/net/network_array.hpp>
+#include <boost/net/network_array.hpp>   // dns_buffer_t
 
 #include "dns_neww/resolverbase.h"
 #include "dns_neww/dnsmaster.h"