summaryrefslogtreecommitdiff
path: root/src/RequestGroup.h
blob: b1cf85736cb3cef296294f14ce9a7a47562e756f (plain)
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
/* <!-- copyright */
/*
 * aria2 - The high speed download utility
 *
 * Copyright (C) 2006 Tatsuhiro Tsujikawa
 *
 * 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *
 * In addition, as a special exception, the copyright holders give
 * permission to link the code of portions of this program with the
 * OpenSSL library under certain conditions as described in each
 * individual source file, and distribute linked combinations
 * including the two.
 * You must obey the GNU General Public License in all respects
 * for all of the code used other than OpenSSL.  If you modify
 * file(s) with this exception, you may extend this exception to your
 * version of the file(s), but you are not obligated to do so.  If you
 * do not wish to do so, delete this exception statement from your
 * version.  If you delete this exception statement from all source
 * files in the program, then also delete it here.
 */
/* copyright --> */
#ifndef _D_REQUEST_GROUP_H_
#define _D_REQUEST_GROUP_H_

#include "common.h"

#include <string>
#include <algorithm>
#include <vector>

#include "SharedHandle.h"
#include "TransferStat.h"
#include "TimeA2.h"
#include "Request.h"
#include "DownloadResultCode.h"
#include "MetadataInfo.h"

namespace aria2 {

class DownloadEngine;
class SegmentMan;
class Command;
class DownloadCommand;
class DownloadContext;
class PieceStorage;
class BtProgressInfoFile;
class Dependency;
class PreDownloadHandler;
class PostDownloadHandler;
class DiskWriterFactory;
class Option;
class Logger;
class RequestGroup;
class CheckIntegrityEntry;
struct DownloadResult;
class URISelector;
class URIResult;
class RequestGroupMan;
#ifdef ENABLE_BITTORRENT
class BtRuntime;
class PeerStorage;
#endif // ENABLE_BITTORRENT

typedef int64_t gid_t;

class RequestGroup {
public:
  enum HaltReason {
    NONE,
    SHUTDOWN_SIGNAL,
    USER_REQUEST
  };
private:
  static gid_t gidCounter_;

  gid_t gid_;

  SharedHandle<Option> option_;

  size_t numConcurrentCommand_;

  /**
   * This is the number of connections used in streaming protocol(http/ftp)
   */
  unsigned int numStreamConnection_;

  unsigned int numStreamCommand_;

  unsigned int numCommand_;

  SharedHandle<SegmentMan> segmentMan_;

  SharedHandle<DownloadContext> downloadContext_;

  SharedHandle<PieceStorage> pieceStorage_;

  bool saveControlFile_;

  SharedHandle<BtProgressInfoFile> progressInfoFile_;

  SharedHandle<DiskWriterFactory> diskWriterFactory_;

  SharedHandle<Dependency> dependency_;

  bool fileAllocationEnabled_;

  bool preLocalFileCheckEnabled_;

  bool haltRequested_;

  bool forceHaltRequested_;

  HaltReason haltReason_;

  bool pauseRequested_;

  std::vector<SharedHandle<PreDownloadHandler> > preDownloadHandlers_;

  std::vector<SharedHandle<PostDownloadHandler> > postDownloadHandlers_;

  std::vector<std::string> acceptTypes_;

  SharedHandle<URISelector> uriSelector_;

  Time lastModifiedTime_;

  unsigned int fileNotFoundCount_;

  // Timeout used for HTTP/FTP downloads.
  time_t timeout_;

#ifdef ENABLE_BITTORRENT
  WeakHandle<BtRuntime> btRuntime_;

  WeakHandle<PeerStorage> peerStorage_;
#endif // ENABLE_BITTORRENT

  // This flag just indicates that the downloaded file is not saved disk but
  // just sits in memory.
  bool inMemoryDownload_;

  unsigned int maxDownloadSpeedLimit_;

  unsigned int maxUploadSpeedLimit_;

  SharedHandle<URIResult> lastUriResult_;

  // If this download generates another downloads when completed(for
  // example, downloads generated by PostDownloadHandler), this field
  // has the GID of generated RequestGroups. empty list means there is
  // no such RequestGroup.
  std::vector<gid_t> followedByGIDs_;

  // If this download is a part of another download(for example,
  // downloading torrent file described in Metalink file), this field
  // has the GID of parent RequestGroup. 0 means this is a parent
  // RequestGroup.
  gid_t belongsToGID_;

  SharedHandle<MetadataInfo> metadataInfo_;

  RequestGroupMan* requestGroupMan_;

  int resumeFailureCount_;

  Logger* logger_;

  void validateFilename(const std::string& expectedFilename,
                        const std::string& actualFilename) const;

  void initializePreDownloadHandler();

  void initializePostDownloadHandler();

  bool tryAutoFileRenaming();

  // Returns the result code of this RequestGroup.  If the download
  // finished, then returns downloadresultcode::FINISHED.  If the
  // download didn't finish and error result is available in
  // _uriResults, then last result code is returned.  Otherwise
  // returns downloadresultcode::UNKNOWN_ERROR.
  downloadresultcode::RESULT downloadResult() const;

  void removeDefunctControlFile
  (const SharedHandle<BtProgressInfoFile>& progressInfoFile);
public:
  // The copy of option is stored in RequestGroup object.
  RequestGroup(const SharedHandle<Option>& option);

  ~RequestGroup();

  const SharedHandle<SegmentMan>& getSegmentMan() const
  {
    return segmentMan_;
  }

  // Returns first bootstrap commands to initiate a download.
  // If this is HTTP/FTP download and file size is unknown, only 1 command
  // (usually, HttpInitiateConnection or FtpInitiateConnection) will be created.
  void createInitialCommand(std::vector<Command*>& commands,
                            DownloadEngine* e);

  void createNextCommandWithAdj(std::vector<Command*>& commands,
                                DownloadEngine* e, int numAdj);

  void createNextCommand(std::vector<Command*>& commands,
                         DownloadEngine* e, unsigned int numCommand);
  
  void createNextCommand(std::vector<Command*>& commands, DownloadEngine* e);

  bool downloadFinished() const;

  bool allDownloadFinished() const;

  void closeFile();

  std::string getFirstFilePath() const;

  uint64_t getTotalLength() const;

  uint64_t getCompletedLength() const;

  /**
   * Compares expected filename with specified actualFilename.
   * The expected filename refers to FileEntry::getBasename() of the first
   * element of DownloadContext::getFileEntries()
   */
  void validateFilename(const std::string& actualFilename) const;

  void validateTotalLength(uint64_t expectedTotalLength,
                           uint64_t actualTotalLength) const;

  void validateTotalLength(uint64_t actualTotalLength) const;

  void setNumConcurrentCommand(unsigned int num)
  {
    numConcurrentCommand_ = num;
  }

  unsigned int getNumConcurrentCommand() const
  {
    return numConcurrentCommand_;
  }

  gid_t getGID() const
  {
    return gid_;
  }

  TransferStat calculateStat() const;

  const SharedHandle<DownloadContext>& getDownloadContext() const
  {
    return downloadContext_;
  }

  // This function also calls
  // downloadContext->setOwnerRequestGroup(this).
  void setDownloadContext(const SharedHandle<DownloadContext>& downloadContext);

  const SharedHandle<PieceStorage>& getPieceStorage() const
  {
    return pieceStorage_;
  }

  void setPieceStorage(const SharedHandle<PieceStorage>& pieceStorage);

  void setProgressInfoFile(const SharedHandle<BtProgressInfoFile>& progressInfoFile);

  void increaseStreamCommand();

  void decreaseStreamCommand();

  void increaseStreamConnection();

  void decreaseStreamConnection();

  unsigned int getNumConnection() const;

  void increaseNumCommand();

  void decreaseNumCommand();

  unsigned int getNumCommand() const
  {
    return numCommand_;
  }

  // TODO is it better to move the following 2 methods to SingleFileDownloadContext?
  void setDiskWriterFactory(const SharedHandle<DiskWriterFactory>& diskWriterFactory);

  const SharedHandle<DiskWriterFactory>& getDiskWriterFactory() const
  {
    return diskWriterFactory_;
  }

  void setFileAllocationEnabled(bool f)
  {
    fileAllocationEnabled_ = f;
  }

  bool isFileAllocationEnabled() const
  {
    return fileAllocationEnabled_;
  }

  bool needsFileAllocation() const;

  /**
   * Setting preLocalFileCheckEnabled_ to false, then skip the check to see
   * if a file is already exists and control file exists etc.
   * Always open file with DiskAdaptor::initAndOpenFile()
   */
  void setPreLocalFileCheckEnabled(bool f)
  {
    preLocalFileCheckEnabled_ = f;
  }

  bool isPreLocalFileCheckEnabled() const
  {
    return preLocalFileCheckEnabled_;
  }

  void setHaltRequested(bool f, HaltReason = SHUTDOWN_SIGNAL);

  void setForceHaltRequested(bool f, HaltReason = SHUTDOWN_SIGNAL);

  bool isHaltRequested() const
  {
    return haltRequested_;
  }

  bool isForceHaltRequested() const
  {
    return forceHaltRequested_;
  }

  void setPauseRequested(bool f);

  bool isPauseRequested() const
  {
    return pauseRequested_;
  }

  void dependsOn(const SharedHandle<Dependency>& dep);

  bool isDependencyResolved();

  void releaseRuntimeResource(DownloadEngine* e);

  void postDownloadProcessing(std::vector<SharedHandle<RequestGroup> >& groups);

  void addPostDownloadHandler(const SharedHandle<PostDownloadHandler>& handler);

  void clearPostDownloadHandler();

  void preDownloadProcessing();

  void addPreDownloadHandler(const SharedHandle<PreDownloadHandler>& handler);

  void clearPreDownloadHandler();

  void processCheckIntegrityEntry(std::vector<Command*>& commands,
                                  const SharedHandle<CheckIntegrityEntry>& entry,
                                  DownloadEngine* e);

  // Initializes pieceStorage_ and segmentMan_.  We guarantee that
  // either both of pieceStorage_ and segmentMan_ are initialized or
  // they are not.
  void initPieceStorage();

  void dropPieceStorage();

  bool downloadFinishedByFileLength();

  void loadAndOpenFile(const SharedHandle<BtProgressInfoFile>& progressInfoFile);

  void shouldCancelDownloadForSafety();

  void adjustFilename(const SharedHandle<BtProgressInfoFile>& infoFile);

  SharedHandle<DownloadResult> createDownloadResult() const;

  const SharedHandle<Option>& getOption() const
  {
    return option_;
  }

  void reportDownloadFinished();

  const std::vector<std::string>& getAcceptTypes() const
  {
    return acceptTypes_;
  }

  void addAcceptType(const std::string& type);

  template<typename InputIterator>
  void addAcceptType(InputIterator first, InputIterator last)
  {
    for(; first != last; ++first) {
      if(std::find(acceptTypes_.begin(), acceptTypes_.end(), *first) ==
	 acceptTypes_.end()) {
	acceptTypes_.push_back(*first);
      }
    }
  }

  void removeAcceptType(const std::string& type);

  void setURISelector(const SharedHandle<URISelector>& uriSelector);

  const SharedHandle<URISelector>& getURISelector() const
  {
    return uriSelector_;
  }

  void applyLastModifiedTimeToLocalFiles();

  void updateLastModifiedTime(const Time& time);

  void increaseAndValidateFileNotFoundCount();

  // Just set inMemoryDownload flag true.
  void markInMemoryDownload();

  // Returns inMemoryDownload flag.
  bool inMemoryDownload() const
  {
    return inMemoryDownload_;
  }

  void setTimeout(time_t timeout);

  time_t getTimeout() const
  {
    return timeout_;
  }

  // Returns true if current download speed exceeds
  // maxDownloadSpeedLimit_.  Always returns false if
  // maxDownloadSpeedLimit_ == 0.  Otherwise returns false.
  bool doesDownloadSpeedExceed();

  // Returns true if current upload speed exceeds
  // maxUploadSpeedLimit_. Always returns false if
  // maxUploadSpeedLimit_ == 0. Otherwise returns false.
  bool doesUploadSpeedExceed();

  unsigned int getMaxDownloadSpeedLimit() const
  {
    return maxDownloadSpeedLimit_;
  }

  void setMaxDownloadSpeedLimit(unsigned int speed)
  {
    maxDownloadSpeedLimit_ = speed;
  }

  unsigned int getMaxUploadSpeedLimit() const
  {
    return maxUploadSpeedLimit_;
  }

  void setMaxUploadSpeedLimit(unsigned int speed)
  {
    maxUploadSpeedLimit_ = speed;
  }

  void setLastUriResult(std::string uri, downloadresultcode::RESULT result);

  void saveControlFile() const;

  void removeControlFile() const;

  void enableSaveControlFile() { saveControlFile_ = true; }

  void disableSaveControlFile() { saveControlFile_ = false; }

  template<typename InputIterator>
  void followedBy(InputIterator groupFirst, InputIterator groupLast)
  {
    followedByGIDs_.clear();
    for(; groupFirst != groupLast; ++groupFirst) {
      followedByGIDs_.push_back((*groupFirst)->getGID());
    }
  }

  const std::vector<gid_t>& followedBy() const
  {
    return followedByGIDs_;
  }

  void belongsTo(gid_t gid)
  {
    belongsToGID_ = gid;
  }

  gid_t belongsTo() const
  {
    return belongsToGID_;
  }

  void setRequestGroupMan(RequestGroupMan* requestGroupMan)
  {
    requestGroupMan_ = requestGroupMan;
  }

  int getResumeFailureCount() const
  {
    return resumeFailureCount_;
  }

  void increaseResumeFailureCount()
  {
    ++resumeFailureCount_;
  }

  bool p2pInvolved() const;

  void setMetadataInfo(const SharedHandle<MetadataInfo>& info)
  {
    metadataInfo_ = info;
  }

  const SharedHandle<MetadataInfo>& getMetadataInfo() const
  {
    return metadataInfo_;
  }

  static void resetGIDCounter() { gidCounter_ = 0; }

  static gid_t newGID();
};

} // namespace aria2

#endif // _D_REQUEST_GROUP_H_