/* */ #include "CheckIntegrityCommand.h" #include "FileAllocationEntry.h" #include "InitiateConnectionCommandFactory.h" #include "DlAbortEx.h" #include "message.h" void CheckIntegrityCommand::initValidator() { _validator = new IteratableChunkChecksumValidator(); _validator->setChunkChecksum(_requestGroup->getChunkChecksum()); _validator->setDiskWriter(_requestGroup->getSegmentMan()->diskWriter); _validator->setBitfield(_requestGroup->getSegmentMan()->getBitfield()); if(!_validator->canValidate()) { // insufficient checksums. throw new DlAbortEx("Insufficient checksums."); } _validator->init(); } bool CheckIntegrityCommand::execute() { setStatusRealtime(); _e->noWait = true; try { _validator->validateChunk(); if(_validator->finished()) { if(_requestGroup->downloadFinished()) { logger->notice(MSG_DOWNLOAD_ALREADY_COMPLETED, cuid, _requestGroup->getFilePath().c_str()); return true; } if(_requestGroup->needsFileAllocation()) { _e->_fileAllocationMan->pushFileAllocationEntry(new FileAllocationEntry(cuid, _req, _requestGroup)); } else { int32_t numCommandsToGenerate = 15; Commands commands = _requestGroup->getNextCommand(_e, numCommandsToGenerate); Command* command = InitiateConnectionCommandFactory::createInitiateConnectionCommand(cuid, _req, _requestGroup, _e); commands.push_front(command); _e->addCommand(commands); } return true; } else { _e->commands.push_back(this); return false; } } catch(Exception* e) { _requestGroup->getSegmentMan()->errors++; logger->error("CUID#%d - Exception caught while validating file integrity.", e, cuid); delete e; logger->error(MSG_DOWNLOAD_NOT_COMPLETE, cuid, _requestGroup->getFilePath().c_str()); // TODO this is wrong. There may exist invalid chunk data before catching // exception. Fix this. _requestGroup->markPieceDone(_validator->getCurrentOffset()); return true; } }