/* */ #include "DHTAutoSaveCommand.h" #include "DHTRoutingTable.h" #include "DHTNode.h" #include "File.h" #include "DHTRoutingTableSerializer.h" #include "RecoverableException.h" #include "DownloadEngine.h" #include "Util.h" #include "DHTBucket.h" #include "RequestGroupMan.h" #include "prefs.h" #include "Option.h" #include DHTAutoSaveCommand::DHTAutoSaveCommand(int32_t cuid, DownloadEngine* e, int32_t interval): TimeBasedCommand(cuid, e, interval), _localNode(0), _routingTable(0) {} DHTAutoSaveCommand::~DHTAutoSaveCommand() {} void DHTAutoSaveCommand::preProcess() { if(_e->_requestGroupMan->downloadFinished() || _e->isHaltRequested()) { save(); _exit = true; } } void DHTAutoSaveCommand::process() { save(); } void DHTAutoSaveCommand::save() { DHTNodes nodes; DHTBuckets buckets = _routingTable->getBuckets(); for(DHTBuckets::const_iterator i = buckets.begin(); i != buckets.end(); ++i) { const DHTBucketHandle& bucket = *i; DHTNodes goodNodes = bucket->getGoodNodes(); nodes.insert(nodes.end(), goodNodes.begin(), goodNodes.end()); } DHTRoutingTableSerializer serializer; serializer.setLocalNode(_localNode); serializer.setNodes(nodes); string dhtFile = _e->option->get(PREF_DHT_FILE_PATH); string tempFile = dhtFile+"__temp"; ofstream o(tempFile.c_str(), ios::out|ios::binary); o.exceptions(ios::failbit); try { serializer.serialize(o); if(!File(tempFile).renameTo(dhtFile)) { logger->error("Cannot move file from %s to %s.", tempFile.c_str(), dhtFile.c_str()); } } catch(RecoverableException* e) { logger->error("Exception caught while saving DHT routing table to %s", e, tempFile.c_str()); delete e; } } void DHTAutoSaveCommand::setLocalNode(const DHTNodeHandle& localNode) { _localNode = localNode; } void DHTAutoSaveCommand::setRoutingTable(const DHTRoutingTableHandle& routingTable) { _routingTable = routingTable; }