From 946356e153a479a4e8f181be993ee0f1e8334459 Mon Sep 17 00:00:00 2001 From: Christian Herdtweck Date: Thu, 9 Apr 2015 15:16:59 +0200 Subject: [PATCH] implemented serialization, is compiling --- src/dns/hostaddress.cpp | 8 +++++ src/dns/hostaddress.h | 5 +++ src/dns/timetolive.cpp | 9 ++++++ src/dns/timetolive.h | 7 +++++ src/dns_neww/dnscache.cpp | 64 ++++++++++++++++++++++++++++++++++++++++--- src/dns_neww/dnsresolver.h | 2 +- 6 files changed, 89 insertions(+), 6 deletions(-) diff --git a/src/dns/hostaddress.cpp b/src/dns/hostaddress.cpp index ebc01ad..89b64b5 100644 --- a/src/dns/hostaddress.cpp +++ b/src/dns/hostaddress.cpp @@ -20,6 +20,7 @@ on this file might be covered by the GNU General Public License. #include "dns/hostaddress.h" #include +#include #include "boost_assert_handler.h" @@ -72,3 +73,10 @@ void HostAddress::set_ttl( const TimeToLive &ttl ) { Ttl = ttl; } + +template +void HostAddress::serialize(Archive & ar, const unsigned int version) +{ + ar & Ip; + ar & Ttl; +} diff --git a/src/dns/hostaddress.h b/src/dns/hostaddress.h index 2a160e6..0c70827 100644 --- a/src/dns/hostaddress.h +++ b/src/dns/hostaddress.h @@ -25,6 +25,7 @@ on this file might be covered by the GNU General Public License. #include #include +#include #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 + void serialize(Archive & ar, const unsigned int version); }; //----------------------------------------------------------------------------- diff --git a/src/dns/timetolive.cpp b/src/dns/timetolive.cpp index 1497042..62f61ca 100644 --- a/src/dns/timetolive.cpp +++ b/src/dns/timetolive.cpp @@ -21,6 +21,8 @@ on this file might be covered by the GNU General Public License. #include "boost_assert_handler.h" +#include + 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 +void TimeToLive::serialize(Archive & ar, const unsigned int version) +{ + ar & Ttl; + ar & TtlSetTime; +} diff --git a/src/dns/timetolive.h b/src/dns/timetolive.h index cfd5168..a73b2a3 100644 --- a/src/dns/timetolive.h +++ b/src/dns/timetolive.h @@ -23,6 +23,7 @@ on this file might be covered by the GNU General Public License. #include #include +#include //----------------------------------------------------------------------------- // 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 + void serialize(Archive & ar, const unsigned int version); }; #endif // TIME_TO_LIVE_H diff --git a/src/dns_neww/dnscache.cpp b/src/dns_neww/dnscache.cpp index f7ae74f..6e7fcca 100644 --- a/src/dns_neww/dnscache.cpp +++ b/src/dns_neww/dnscache.cpp @@ -22,10 +22,18 @@ #include "dns_neww/dnscache.h" +#include #include +#include // I2n::file_exists #include #include #include +#include +#include +#include +#include +#include +#include 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(); + } } /** diff --git a/src/dns_neww/dnsresolver.h b/src/dns_neww/dnsresolver.h index 4565cb1..5556502 100644 --- a/src/dns_neww/dnsresolver.h +++ b/src/dns_neww/dnsresolver.h @@ -29,7 +29,7 @@ #include #include #include -#include +#include // dns_buffer_t #include "dns_neww/resolverbase.h" #include "dns_neww/dnsmaster.h" -- 1.7.1