summaryrefslogtreecommitdiff
path: root/src/SegmentMan.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/SegmentMan.cc')
-rw-r--r--src/SegmentMan.cc60
1 files changed, 20 insertions, 40 deletions
diff --git a/src/SegmentMan.cc b/src/SegmentMan.cc
index 8dca0ba0..8046bf7b 100644
--- a/src/SegmentMan.cc
+++ b/src/SegmentMan.cc
@@ -28,6 +28,7 @@
#include "Util.h"
#include "File.h"
#include "message.h"
+#include "prefs.h"
SegmentMan::SegmentMan():totalSize(0),isSplittable(true),downloadStarted(false),dir(".") {}
@@ -51,6 +52,7 @@ bool SegmentMan::getSegment(Segment& seg, int cuid) {
seg.sp = 0;
seg.ep = totalSize == 0 ? 0 : totalSize-1;
seg.ds = 0;
+ seg.speed = 0;
seg.finish = false;
segments.push_back(seg);
return true;
@@ -80,35 +82,7 @@ bool SegmentMan::getSegment(Segment& seg, int cuid) {
return true;
}
}
- for(vector<Segment>::iterator itr = segments.begin(); itr != segments.end(); itr++) {
- Segment& s = *itr;
- if(s.finish) {
- continue;
- }
- if(s.ep-(s.sp+s.ds) > 524288) {
- long long int nep = (s.ep-(s.sp+s.ds))/2+(s.sp+s.ds);
- //nseg = { cuid, nep+1, s.ep, 0, false };
- seg.cuid = cuid;
- seg.sp = nep+1;
- seg.ep = s.ep;
- seg.ds = 0;
- seg.finish = false;
- s.ep = nep;
- logger->debug("return new segment { "
- "sp = "+Util::llitos(seg.sp)+", "+
- "ep = "+Util::llitos(seg.ep)+", "
- "ds = "+Util::llitos(seg.ds)+" } to "+
- "cuid "+Util::llitos(cuid));
- logger->debug("update segment { "
- "sp = "+Util::llitos(s.sp)+", "+
- "ep = "+Util::llitos(s.ep)+", "
- "ds = "+Util::llitos(s.ds)+" } of "+
- "cuid "+Util::llitos(s.cuid));
- segments.push_back(seg);
- return true;
- }
- }
- return false;
+ return splitter->splitSegment(seg, cuid, segments);
}
void SegmentMan::updateSegment(const Segment& segment) {
@@ -123,7 +97,7 @@ void SegmentMan::updateSegment(const Segment& segment) {
}
-bool SegmentMan::segmentFileExists() {
+bool SegmentMan::segmentFileExists() const {
if(!isSplittable) {
return false;
}
@@ -154,14 +128,17 @@ void SegmentMan::load() {
}
}
-void SegmentMan::save() {
- if(!isSplittable) {
+void SegmentMan::save() const {
+ if(!isSplittable || totalSize == 0) {
return;
}
string segFilename = getSegmentFilePath();
logger->info(MSG_SAVING_SEGMENT_FILE, segFilename.c_str());
FILE* segFile = openSegFile(segFilename, "w");
- for(vector<Segment>::iterator itr = segments.begin(); itr != segments.end(); itr++) {
+ if(fwrite(&totalSize, sizeof(totalSize), 1, segFile) < 1) {
+ throw new DlAbortEx(strerror(errno));
+ }
+ for(vector<Segment>::const_iterator itr = segments.begin(); itr != segments.end(); itr++) {
if(fwrite(&*itr, sizeof(Segment), 1, segFile) < 1) {
throw new DlAbortEx(strerror(errno));
}
@@ -170,7 +147,7 @@ void SegmentMan::save() {
logger->info(MSG_SAVED_SEGMENT_FILE);
}
-FILE* SegmentMan::openSegFile(string segFilename, string mode) {
+FILE* SegmentMan::openSegFile(string segFilename, string mode) const {
FILE* segFile = fopen(segFilename.c_str(), mode.c_str());
if(segFile == NULL) {
throw new DlAbortEx(strerror(errno));
@@ -180,6 +157,9 @@ FILE* SegmentMan::openSegFile(string segFilename, string mode) {
void SegmentMan::read(FILE* file) {
assert(file != NULL);
+ if(fread(&totalSize, sizeof(totalSize), 1, file) < 1) {
+ throw new DlAbortEx(strerror(errno));
+ }
while(1) {
Segment seg;
if(fread(&seg, sizeof(Segment), 1, file) < 1) {
@@ -193,7 +173,7 @@ void SegmentMan::read(FILE* file) {
}
}
-void SegmentMan::remove() {
+void SegmentMan::remove() const {
if(!isSplittable) {
return;
}
@@ -203,11 +183,11 @@ void SegmentMan::remove() {
}
}
-bool SegmentMan::finished() {
+bool SegmentMan::finished() const {
if(!downloadStarted || segments.size() == 0) {
return false;
}
- for(vector<Segment>::iterator itr = segments.begin(); itr != segments.end(); itr++) {
+ for(vector<Segment>::const_iterator itr = segments.begin(); itr != segments.end(); itr++) {
if(!(*itr).finish) {
return false;
}
@@ -215,15 +195,15 @@ bool SegmentMan::finished() {
return true;
}
-void SegmentMan::removeIfFinished() {
+void SegmentMan::removeIfFinished() const {
if(finished()) {
remove();
}
}
-long long int SegmentMan::getDownloadedSize() {
+long long int SegmentMan::getDownloadedSize() const {
long long int size = 0;
- for(vector<Segment>::iterator itr = segments.begin(); itr != segments.end(); itr++) {
+ for(vector<Segment>::const_iterator itr = segments.begin(); itr != segments.end(); itr++) {
size += (*itr).ds;
}
return size;