1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
|
/*
* Copyright (C) 2001-2012 Jacek Sieka, arnetheduck on gmail point com
* Copyright (C) 2009-2019 EiskaltDC++ developers
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "stdinc.h"
#include "QueueItem.h"
#include "HashManager.h"
#include "Download.h"
#include "File.h"
#include "Util.h"
namespace dcpp {
namespace {
const string TEMP_EXTENSION = ".dctmp";
string getTempName(const string& aFileName, const TTHValue& aRoot) {
string tmp(aFileName);
tmp += "." + aRoot.toBase32();
tmp += TEMP_EXTENSION;
return tmp;
}
}
int QueueItem::countOnlineUsers() const {
int n = 0;
for(auto& i: sources) {
if(i.getUser().user->isOnline())
n++;
}
return n;
}
void QueueItem::getOnlineUsers(HintedUserList& l) const {
for(auto& i: sources)
if(i.getUser().user->isOnline())
l.push_back(i.getUser());
}
void QueueItem::addSource(const HintedUser& aUser) {
dcassert(!isSource(aUser.user));
auto i = getBadSource(aUser);
if(i != badSources.end()) {
sources.push_back(*i);
badSources.erase(i);
} else {
sources.emplace_back(aUser);
}
}
void QueueItem::removeSource(const UserPtr& aUser, int reason) {
auto i = getSource(aUser);
dcassert(i != sources.end());
i->setFlag(reason);
badSources.push_back(*i);
sources.erase(i);
}
const string& QueueItem::getTempTarget() {
if(!isSet(QueueItem::FLAG_USER_LIST) && tempTarget.empty()) {
if(!SETTING(TEMP_DOWNLOAD_DIRECTORY).empty() && (File::getSize(getTarget()) == -1)) {
string tmp;
#ifdef _WIN32
dcpp::StringMap sm;
if(target.length() >= 3 && target[1] == ':' && target[2] == '\\')
sm["targetdrive"] = target.substr(0, 3);
else
sm["targetdrive"] = Util::getPath(Util::PATH_USER_LOCAL).substr(0, 3);
if (SETTING(NO_USE_TEMP_DIR))
tmp = Util::formatParams(target, sm, false) + getTempName("", getTTH());
else
tmp = Util::formatParams(SETTING(TEMP_DOWNLOAD_DIRECTORY), sm, false) + getTempName(getTargetFileName(), getTTH());
#else //_WIN32
if (SETTING(NO_USE_TEMP_DIR))
tmp = target + getTempName("", getTTH());
else
tmp = SETTING(TEMP_DOWNLOAD_DIRECTORY) + getTempName(getTargetFileName(), getTTH());
#endif //_WIN32
int len = Util::getFileName(tmp).size()+1;
if (len >= 255){
string tmp1 = tmp.erase(tmp.find(".dctmp")-33,tmp.find(".dctmp")+5) + ".dctmp";
tmp = tmp1;
}
len = Util::getFileName(tmp).size()+1;
if (len >= 255) {
char tmp3[206];
string tmp2 = tmp.erase(tmp.find(".dctmp")-7,tmp.find(".dctmp")+5);
memcpy (tmp3, Util::getFileName(tmp2).c_str(), 206);
tmp = Util::getFilePath(tmp) + string(tmp3) + "~." + getTTH().toBase32() + ".dctmp";
}
setTempTarget(tmp);
}
}
return tempTarget;
}
Segment QueueItem::getNextSegment(int64_t blockSize, int64_t wantedSize, int64_t lastSpeed, const PartialSource::Ptr partialSource) const {
if(getSize() == -1 || blockSize == 0) {
return Segment(0, -1);
}
if(!BOOLSETTING(SEGMENTED_DL)) {
if(!downloads.empty()) {
return Segment(0, 0);
}
int64_t start = 0;
int64_t end = getSize();
if(!done.empty()) {
const Segment& first = *done.begin();
if(first.getStart() > 0) {
end = Util::roundUp(first.getStart(), blockSize);
} else {
start = Util::roundDown(first.getEnd(), blockSize);
if(done.size() > 1) {
const Segment& second = *(++done.begin());
end = Util::roundUp(second.getStart(), blockSize);
}
}
}
return Segment(start, std::min(getSize(), end) - start);
}
/* added for PFS */
vector<int64_t> posArray;
vector<Segment> neededParts;
if(partialSource) {
posArray.reserve(partialSource->getPartialInfo().size());
// Convert block index to file position
for(PartsInfo::const_iterator i = partialSource->getPartialInfo().begin(); i != partialSource->getPartialInfo().end(); ++i)
posArray.push_back(min(getSize(), (int64_t)(*i) * blockSize));
}
/***************************/
double donePart = static_cast<double>(getDownloadedBytes()) / getSize();
// We want smaller blocks at the end of the transfer, squaring gives a nice curve...
int64_t targetSize = SETTING(SEGMENT_SIZE) > 0 ? (int64_t)(SETTING(SEGMENT_SIZE)*1024*1024) : wantedSize * std::max(0.25, (1. - (donePart * donePart)));
if(targetSize > blockSize) {
// Round off to nearest block size
targetSize = Util::roundDown(targetSize, blockSize);
} else {
targetSize = blockSize;
}
int64_t start = 0;
int64_t curSize = targetSize;
while(start < getSize()) {
const int64_t end = std::min(getSize(), start + curSize);
Segment block(start, end - start);
bool overlaps = false;
for(auto i = done.begin(); !overlaps && i != done.end(); ++i) {
if(curSize <= blockSize) {
const int64_t dstart = i->getStart();
const int64_t dend = i->getEnd();
// We accept partial overlaps, only consider the block done if it is fully consumed by the done block
if(dstart <= start && dend >= end) {
overlaps = true;
}
} else {
overlaps = block.overlaps(*i);
}
}
for(auto i = downloads.begin(); !overlaps && i !=downloads.end(); ++i) {
overlaps = block.overlaps((*i)->getSegment());
}
if(!overlaps) {
if(partialSource) {
// store all chunks we could need
for(vector<int64_t>::const_iterator j = posArray.begin(); j < posArray.end(); j += 2){
if( (*j <= start && start < *(j+1)) || (start <= *j && *j < end) ) {
int64_t b = max(start, *j);
int64_t e = min(end, *(j+1));
bool merged = false;
if(!neededParts.empty())
{
Segment& prev = neededParts.back();
if(b == prev.getEnd() && e > prev.getEnd())
{
prev.setSize(prev.getSize() + (e - b));
merged = true;
}
}
if(!merged)
neededParts.push_back(Segment(b, e - b));
}
}
} else {
return block;
}
}
if(!partialSource && curSize > blockSize) {
curSize -= blockSize;
} else {
start = end;
curSize = targetSize;
}
}
if(!neededParts.empty()) {
// select random chunk for PFS
dcdebug("Found partial chunks: %d\n", static_cast<int>(neededParts.size()));
Segment& selected = neededParts[Util::rand(0, neededParts.size())];
selected.setSize(std::min(selected.getSize(), targetSize)); // request only wanted size
return selected;
}
if(partialSource == NULL && BOOLSETTING(OVERLAP_CHUNKS) && lastSpeed > 0) {
// overlap slow running chunk
for(auto d: downloads) {
// current chunk mustn't be already overlapped
if(d->getOverlapped())
continue;
// current chunk must be running at least for 2 seconds
if(d->getStart() == 0 || GET_TIME() - d->getStart() < 2000)
continue;
// current chunk mustn't be finished in next 10 seconds
if(d->getSecondsLeft() < 10)
continue;
// overlap current chunk at last block boundary
const int64_t pos = d->getPos() - (d->getPos() % blockSize);
const int64_t size = d->getSize() - pos;
// new user should finish this chunk more than 2x faster
const int64_t newChunkLeft = size / lastSpeed;
if(2 * newChunkLeft < d->getSecondsLeft()) {
return Segment(d->getStartPos() + pos, size/*, true*/); // TODO: bool
}
}
}
return Segment(0, 0);
}
int64_t QueueItem::getDownloadedBytes() const {
int64_t total = 0;
for(auto& i: done) {
total += i.getSize();
}
return total;
}
void QueueItem::addSegment(const Segment& segment) {
done.insert(segment);
// Consolidate segments
if(done.size() == 1)
return;
for(auto i = ++done.begin() ; i != done.end(); ) {
auto prev = i;
--prev;
if(prev->getEnd() >= i->getStart()) {
Segment big(prev->getStart(), i->getEnd() - prev->getStart());
done.erase(prev);
done.erase(i++);
done.insert(big);
} else {
++i;
}
}
}
string QueueItem::getListName() const {
dcassert(isSet(QueueItem::FLAG_USER_LIST));
if(isSet(QueueItem::FLAG_XML_BZLIST)) {
return getTarget() + ".xml.bz2";
} else {
return getTarget() + ".xml";
}
}
// Partial
bool QueueItem::isNeededPart(const PartsInfo& partsInfo, int64_t blockSize) const {
auto i = done.begin();
for(auto j = partsInfo.begin(); j != partsInfo.end(); j+=2) {
while(i != done.end() && (*i).getEnd() <= (*j) * blockSize)
++i;
if(i == done.end() || !((*i).getStart() <= (*j) * blockSize && (*i).getEnd() >= (*(j+1)) * blockSize))
return true;
}
return false;
}
void QueueItem::getPartialInfo(PartsInfo& partialInfo, int64_t blockSize) const {
size_t maxSize = min(done.size() * 2, (size_t)510);
partialInfo.reserve(maxSize);
for(auto& i : done) {
if(partialInfo.size() >= maxSize)
break;
uint16_t s = (uint16_t)(i.getStart() / blockSize);
uint16_t e = (uint16_t)((i.getEnd() - 1) / blockSize + 1);
partialInfo.push_back(s);
partialInfo.push_back(e);
}
}
}
|