summaryrefslogtreecommitdiff
path: root/src/DownloadCommand.cc
diff options
context:
space:
mode:
authorTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2007-10-11 16:58:24 +0000
committerTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2007-10-11 16:58:24 +0000
commit048a2cf59737f761876e81a77e8d064c45c752d7 (patch)
tree65c9d5498aafe6a8c8e640ddfcc43c7013504d0d /src/DownloadCommand.cc
parente26bbbb9eee140ba24402fe872a752b42f0077de (diff)
2007-10-12 Tatsuhiro Tsujikawa <tujikawa at rednoah dot com>
Implemented BitTorrent/http/ftp integrated download. I've rewritten lots of files and now some headers have forward class declarations to reduce compile time. The implementation is extremely alpha stage, I recommend to use this for testing purpose only.
Diffstat (limited to 'src/DownloadCommand.cc')
-rw-r--r--src/DownloadCommand.cc61
1 files changed, 45 insertions, 16 deletions
diff --git a/src/DownloadCommand.cc b/src/DownloadCommand.cc
index 2de238d6..bbc9dfcb 100644
--- a/src/DownloadCommand.cc
+++ b/src/DownloadCommand.cc
@@ -33,16 +33,20 @@
*/
/* copyright --> */
#include "DownloadCommand.h"
+#include "SegmentMan.h"
+#include "PeerStat.h"
+#include "TransferEncoding.h"
+#include "DownloadContext.h"
#include "Util.h"
#include "DlRetryEx.h"
#include "DlAbortEx.h"
-#include "HttpInitiateConnectionCommand.h"
#include "InitiateConnectionCommandFactory.h"
#include "message.h"
#include "prefs.h"
-#ifdef ENABLE_MESSAGE_DIGEST
-# include "ChecksumCommand.h"
-#endif // ENABLE_MESSAGE_DIGEST
+#include "DiskAdaptor.h"
+#include "Segment.h"
+#include "PieceStorage.h"
+#include "Option.h"
#include <stdlib.h>
DownloadCommand::DownloadCommand(int cuid,
@@ -80,17 +84,23 @@ bool DownloadCommand::executeInternal() {
socket->readData(buf, bufSize);
if(transferDecoder.isNull()) {
- _requestGroup->getSegmentMan()->diskWriter->writeData(buf, bufSize,
- segment->getPositionToWrite());
- segment->writtenLength += bufSize;
+ _requestGroup->getPieceStorage()->getDiskAdaptor()->writeData((const unsigned char*)buf, bufSize,
+ segment->getPositionToWrite());
+ //logger->debug("bufSize = %d, posToWrite = %lld", bufSize, segment->getPositionToWrite());
+ segment->updateWrittenLength(bufSize);
+ //logger->debug("overflow length = %d, next posToWrite = %lld", segment->getOverflowLength(), segment->getPositionToWrite());
+ //logger->debug("%s", Util::toHex(segment->getPiece()->getBitfield(),
+ //segment->getPiece()->getBitfieldLength()).c_str());
+ //segment->writtenLength += bufSize;
peerStat->updateDownloadLength(bufSize);
} else {
int32_t infbufSize = 16*1024;
char infbuf[infbufSize];
transferDecoder->inflate(infbuf, infbufSize, buf, bufSize);
- _requestGroup->getSegmentMan()->diskWriter->writeData(infbuf, infbufSize,
- segment->getPositionToWrite());
- segment->writtenLength += infbufSize;
+ _requestGroup->getPieceStorage()->getDiskAdaptor()->writeData((const unsigned char*)infbuf, infbufSize,
+ segment->getPositionToWrite());
+ segment->updateWrittenLength(infbufSize);
+ //segment->writtenLength += infbufSize;
peerStat->updateDownloadLength(infbufSize);
}
// calculate downloading speed
@@ -103,7 +113,7 @@ bool DownloadCommand::executeInternal() {
req->getHost().c_str());
}
}
- if(_requestGroup->getSegmentMan()->totalSize != 0 && bufSize == 0) {
+ if(_requestGroup->getTotalLength() != 0 && bufSize == 0) {
throw new DlRetryEx(EX_GOT_EOF);
}
if(!transferDecoder.isNull() && transferDecoder->finished()
@@ -112,11 +122,22 @@ bool DownloadCommand::executeInternal() {
if(!transferDecoder.isNull()) transferDecoder->end();
logger->info(MSG_SEGMENT_DOWNLOAD_COMPLETED, cuid);
_requestGroup->getSegmentMan()->completeSegment(cuid, segment);
+ // TODO According to the current plan, checksum is held by DownloadContext.
+#ifdef ENABLE_MESSAGE_DIGEST
+ if(e->option->get(PREF_REALTIME_CHUNK_CHECKSUM) == V_TRUE) {
+ string pieceHash = _requestGroup->getDownloadContext()->getPieceHash(segment->getIndex());
+ if(!pieceHash.empty()) {
+ _requestGroup->getSegmentMan()->validatePieceHash(segment, pieceHash);
+ }
+ }
+#endif // ENABLE_MESSAGE_DIGEST
+ /*
#ifdef ENABLE_MESSAGE_DIGEST
if(e->option->get(PREF_REALTIME_CHUNK_CHECKSUM) == V_TRUE) {
_requestGroup->getSegmentMan()->tryChunkChecksumValidation(segment, _requestGroup->getChunkChecksum());
}
#endif // ENABLE_MESSAGE_DIGEST
+ */
// this unit is going to download another segment.
return prepareForNextSegment();
} else {
@@ -127,7 +148,9 @@ bool DownloadCommand::executeInternal() {
}
bool DownloadCommand::prepareForNextSegment() {
- if(_requestGroup->getSegmentMan()->finished()) {
+ if(_requestGroup->downloadFinished()) {
+ // TODO According to the current plan, checksum is held by DownloadContext.
+ /*
#ifdef ENABLE_MESSAGE_DIGEST
if(!_requestGroup->getChecksum().isNull() &&
!_requestGroup->getChecksum()->isEmpty()) {
@@ -136,6 +159,7 @@ bool DownloadCommand::prepareForNextSegment() {
e->commands.push_back(command);
}
#endif // ENABLE_MESSAGE_DIGEST
+ */
return true;
} else {
// Merge segment with next segment, if segment.index+1 == nextSegment.index
@@ -143,15 +167,15 @@ bool DownloadCommand::prepareForNextSegment() {
while(1) {
SegmentHandle nextSegment =
_requestGroup->getSegmentMan()->getSegment(cuid,
- tempSegment->index+1);
+ tempSegment->getIndex()+1);
if(nextSegment.isNull()) {
break;
} else {
- if(nextSegment->writtenLength > 0) {
+ if(nextSegment->getWrittenLength() > 0) {
return prepareForRetry(0);
}
- nextSegment->writtenLength =
- tempSegment->writtenLength-tempSegment->length;
+ nextSegment->updateWrittenLength(tempSegment->getOverflowLength());
+ //tempSegment->writtenLength-tempSegment->length;
if(nextSegment->complete()) {
_requestGroup->getSegmentMan()->completeSegment(cuid, nextSegment);
tempSegment = nextSegment;
@@ -165,3 +189,8 @@ bool DownloadCommand::prepareForNextSegment() {
return prepareForRetry(0);
}
}
+
+void DownloadCommand::setTransferDecoder(const TransferEncodingHandle& transferDecoder)
+{
+ this->transferDecoder = transferDecoder;
+}