#include <list>
#include <boost/asio.hpp>
+#include <boost/serialization/access.hpp>
#include "dns/timetolive.h"
/// 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);
};
//-----------------------------------------------------------------------------
#include <stdint.h>
#include <boost/asio.hpp>
+#include <boost/serialization/access.hpp>
//-----------------------------------------------------------------------------
// TimeToLive
/// 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
#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;
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();
+ }
}
/**