summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2006-03-02 02:54:49 +0000
committerTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2006-03-02 02:54:49 +0000
commit0f6d2fd451a1ef2a8756a5c5ce4117b6685eb908 (patch)
tree865551d7282ad2c03ec56c6df2fa7a4fe190958d /src
parentbea6e0e01635d42212cc21a334b030b88f08cf92 (diff)
* DownloadEngine.{h, cc}:
* ConsoleDownloadEngine.{h, cc}: Console output message(size, speed) is now generated by ConsoleDownloadEngine not by DownloadEngine. * main.cc: Download complete/abort message is now generated by main.
Diffstat (limited to 'src')
-rw-r--r--src/DownloadEngine.cc62
-rw-r--r--src/DownloadEngine.h9
-rw-r--r--src/Makefile.am1
-rw-r--r--src/Makefile.in17
-rw-r--r--src/main.cc10
5 files changed, 60 insertions, 39 deletions
diff --git a/src/DownloadEngine.cc b/src/DownloadEngine.cc
index 965699d0..4ffb8093 100644
--- a/src/DownloadEngine.cc
+++ b/src/DownloadEngine.cc
@@ -38,9 +38,7 @@ DownloadEngine::~DownloadEngine() {
}
void DownloadEngine::run() {
- struct timeval cp = { 0, 0 };
- int speed = 0;
- long long int psize = 0;
+ initStatistics();
while(!commands.empty()) {
int max = commands.size();
for(int i = 0; i < max; i++) {
@@ -54,48 +52,40 @@ void DownloadEngine::run() {
waitData();
}
noWait = false;
-
- long long int dlSize = segmentMan->getDownloadedSize();
- struct timeval now;
- gettimeofday(&now, NULL);
- if(cp.tv_sec == 0 && cp.tv_usec == 0) {
- cp = now;
- psize = dlSize;
- } else {
- long long int elapsed = Util::difftv(now, cp);
- if(elapsed >= 500000) {
- int nspeed = (int)((dlSize-psize)/(elapsed/1000000.0));
- speed = (nspeed+speed)/2;
- cp = now;
- psize = dlSize;
- cout << "\r ";
- cout << "\rProgress " << Util::llitos(dlSize, true) << " Bytes/" <<
- Util::llitos(segmentMan->totalSize, true) << " Bytes " <<
- (segmentMan->totalSize == 0 ? 0 : (dlSize*100)/segmentMan->totalSize) << "% " <<
- speed/1000.0 << "KB/s " <<
- "(" << commands.size() << " connections)" << flush;
- }
- }
-
+ calculateStatistics();
}
diskWriter->closeFile();
if(segmentMan->finished()) {
segmentMan->remove();
- cout << "\nThe download was complete. <" << segmentMan->getFilePath() << ">" << endl;
} else {
segmentMan->save();
- cout << "\nThe download was not complete because of errors. Check the log." << endl;
}
}
-// void DownloadEngine::shortSleep() {
-// int wait = rpm == 0 ? 0 : 4096*1000/rpm;
-// struct timeval tv;
-// int retval;
-// tv.tv_sec = 0;
-// tv.tv_usec = wait*1000;
-// retval = select(0, NULL, NULL, NULL, &tv);
-// }
+void DownloadEngine::initStatistics() {
+ cp.tv_sec = cp.tv_usec = 0;
+ speed = 0;
+ psize = 0;
+}
+
+void DownloadEngine::calculateStatistics() {
+ long long int dlSize = segmentMan->getDownloadedSize();
+ struct timeval now;
+ gettimeofday(&now, NULL);
+ if(cp.tv_sec == 0 && cp.tv_usec == 0) {
+ cp = now;
+ psize = dlSize;
+ } else {
+ long long int elapsed = Util::difftv(now, cp);
+ if(elapsed >= 500000) {
+ int nspeed = (int)((dlSize-psize)/(elapsed/1000000.0));
+ speed = (nspeed+speed)/2;
+ cp = now;
+ psize = dlSize;
+ sendStatistics(dlSize, segmentMan->totalSize);
+ }
+ }
+}
void DownloadEngine::waitData() {
fd_set rfds;
diff --git a/src/DownloadEngine.h b/src/DownloadEngine.h
index b3d2a70b..9ed5e0c0 100644
--- a/src/DownloadEngine.h
+++ b/src/DownloadEngine.h
@@ -42,6 +42,13 @@ private:
bool addSocket(vector<Socket*>& sockets, Socket* socket);
bool deleteSocket(vector<Socket*>& sockets, Socket* socket);
+ struct timeval cp;
+ long long int psize;
+ void initStatistics();
+ void calculateStatistics();
+protected:
+ int speed;
+ virtual void sendStatistics(long long int currentSize, long long int totalSize) {};
public:
bool noWait;
queue<Command*> commands;
@@ -51,7 +58,7 @@ public:
const Option* option;
DownloadEngine();
- ~DownloadEngine();
+ virtual ~DownloadEngine();
void run();
diff --git a/src/Makefile.am b/src/Makefile.am
index 796cdd82..3a21655f 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -22,6 +22,7 @@ SRCS = Socket.cc Socket.h\
FtpTunnelResponseCommand.cc FtpTunnelResponseCommand.h\
SleepCommand.cc SleepCommand.h\
DownloadEngine.cc DownloadEngine.h\
+ ConsoleDownloadEngine.cc ConsoleDownloadEngine.h\
Segment.h\
SegmentMan.cc SegmentMan.h\
SegmentSplitter.cc SegmentSplitter.h\
diff --git a/src/Makefile.in b/src/Makefile.in
index 51f4454a..a030db26 100644
--- a/src/Makefile.in
+++ b/src/Makefile.in
@@ -71,6 +71,7 @@ am__objects_1 = libaria2c_a-Socket.$(OBJEXT) \
libaria2c_a-FtpTunnelResponseCommand.$(OBJEXT) \
libaria2c_a-SleepCommand.$(OBJEXT) \
libaria2c_a-DownloadEngine.$(OBJEXT) \
+ libaria2c_a-ConsoleDownloadEngine.$(OBJEXT) \
libaria2c_a-SegmentMan.$(OBJEXT) \
libaria2c_a-SegmentSplitter.$(OBJEXT) \
libaria2c_a-SplitFirstSegmentSplitter.$(OBJEXT) \
@@ -212,6 +213,7 @@ SRCS = Socket.cc Socket.h\
FtpTunnelResponseCommand.cc FtpTunnelResponseCommand.h\
SleepCommand.cc SleepCommand.h\
DownloadEngine.cc DownloadEngine.h\
+ ConsoleDownloadEngine.cc ConsoleDownloadEngine.h\
Segment.h\
SegmentMan.cc SegmentMan.h\
SegmentSplitter.cc SegmentSplitter.h\
@@ -320,6 +322,7 @@ distclean-compile:
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libaria2c_a-AbstractDiskWriter.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libaria2c_a-Base64.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libaria2c_a-ChunkedEncoding.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libaria2c_a-ConsoleDownloadEngine.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libaria2c_a-CookieBox.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libaria2c_a-DefaultDiskWriter.Po@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libaria2c_a-DownloadCommand.Po@am__quote@
@@ -660,6 +663,20 @@ libaria2c_a-DownloadEngine.obj: DownloadEngine.cc
@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@
@am__fastdepCXX_FALSE@ $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libaria2c_a_CXXFLAGS) $(CXXFLAGS) -c -o libaria2c_a-DownloadEngine.obj `if test -f 'DownloadEngine.cc'; then $(CYGPATH_W) 'DownloadEngine.cc'; else $(CYGPATH_W) '$(srcdir)/DownloadEngine.cc'; fi`
+libaria2c_a-ConsoleDownloadEngine.o: ConsoleDownloadEngine.cc
+@am__fastdepCXX_TRUE@ if $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libaria2c_a_CXXFLAGS) $(CXXFLAGS) -MT libaria2c_a-ConsoleDownloadEngine.o -MD -MP -MF "$(DEPDIR)/libaria2c_a-ConsoleDownloadEngine.Tpo" -c -o libaria2c_a-ConsoleDownloadEngine.o `test -f 'ConsoleDownloadEngine.cc' || echo '$(srcdir)/'`ConsoleDownloadEngine.cc; \
+@am__fastdepCXX_TRUE@ then mv -f "$(DEPDIR)/libaria2c_a-ConsoleDownloadEngine.Tpo" "$(DEPDIR)/libaria2c_a-ConsoleDownloadEngine.Po"; else rm -f "$(DEPDIR)/libaria2c_a-ConsoleDownloadEngine.Tpo"; exit 1; fi
+@AMDEP_TRUE@@am__fastdepCXX_FALSE@ source='ConsoleDownloadEngine.cc' object='libaria2c_a-ConsoleDownloadEngine.o' libtool=no @AMDEPBACKSLASH@
+@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@
+@am__fastdepCXX_FALSE@ $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libaria2c_a_CXXFLAGS) $(CXXFLAGS) -c -o libaria2c_a-ConsoleDownloadEngine.o `test -f 'ConsoleDownloadEngine.cc' || echo '$(srcdir)/'`ConsoleDownloadEngine.cc
+
+libaria2c_a-ConsoleDownloadEngine.obj: ConsoleDownloadEngine.cc
+@am__fastdepCXX_TRUE@ if $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libaria2c_a_CXXFLAGS) $(CXXFLAGS) -MT libaria2c_a-ConsoleDownloadEngine.obj -MD -MP -MF "$(DEPDIR)/libaria2c_a-ConsoleDownloadEngine.Tpo" -c -o libaria2c_a-ConsoleDownloadEngine.obj `if test -f 'ConsoleDownloadEngine.cc'; then $(CYGPATH_W) 'ConsoleDownloadEngine.cc'; else $(CYGPATH_W) '$(srcdir)/ConsoleDownloadEngine.cc'; fi`; \
+@am__fastdepCXX_TRUE@ then mv -f "$(DEPDIR)/libaria2c_a-ConsoleDownloadEngine.Tpo" "$(DEPDIR)/libaria2c_a-ConsoleDownloadEngine.Po"; else rm -f "$(DEPDIR)/libaria2c_a-ConsoleDownloadEngine.Tpo"; exit 1; fi
+@AMDEP_TRUE@@am__fastdepCXX_FALSE@ source='ConsoleDownloadEngine.cc' object='libaria2c_a-ConsoleDownloadEngine.obj' libtool=no @AMDEPBACKSLASH@
+@AMDEP_TRUE@@am__fastdepCXX_FALSE@ DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@
+@am__fastdepCXX_FALSE@ $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libaria2c_a_CXXFLAGS) $(CXXFLAGS) -c -o libaria2c_a-ConsoleDownloadEngine.obj `if test -f 'ConsoleDownloadEngine.cc'; then $(CYGPATH_W) 'ConsoleDownloadEngine.cc'; else $(CYGPATH_W) '$(srcdir)/ConsoleDownloadEngine.cc'; fi`
+
libaria2c_a-SegmentMan.o: SegmentMan.cc
@am__fastdepCXX_TRUE@ if $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(libaria2c_a_CXXFLAGS) $(CXXFLAGS) -MT libaria2c_a-SegmentMan.o -MD -MP -MF "$(DEPDIR)/libaria2c_a-SegmentMan.Tpo" -c -o libaria2c_a-SegmentMan.o `test -f 'SegmentMan.cc' || echo '$(srcdir)/'`SegmentMan.cc; \
@am__fastdepCXX_TRUE@ then mv -f "$(DEPDIR)/libaria2c_a-SegmentMan.Tpo" "$(DEPDIR)/libaria2c_a-SegmentMan.Po"; else rm -f "$(DEPDIR)/libaria2c_a-SegmentMan.Tpo"; exit 1; fi
diff --git a/src/main.cc b/src/main.cc
index b01853b9..266d1d15 100644
--- a/src/main.cc
+++ b/src/main.cc
@@ -20,7 +20,7 @@
*/
/* copyright --> */
#include "HttpInitiateConnectionCommand.h"
-#include "DownloadEngine.h"
+#include "ConsoleDownloadEngine.h"
#include "SegmentMan.h"
#include "SplitSlowestSegmentSplitter.h"
#include "SimpleLogger.h"
@@ -411,7 +411,7 @@ int main(int argc, char* argv[]) {
SegmentSplitter* splitter = new SplitSlowestSegmentSplitter();
splitter->setMinSegmentSize(op->getAsLLInt(PREF_MIN_SEGMENT_SIZE));
splitter->logger = logger;
- e = new DownloadEngine();
+ e = new ConsoleDownloadEngine();
e->logger = logger;
e->option = op;
e->diskWriter = new DefaultDiskWriter();
@@ -434,6 +434,12 @@ int main(int argc, char* argv[]) {
sigaction(SIGINT, &sigact, NULL);
e->run();
+
+ if(e->segmentMan->finished()) {
+ cout << "\nThe download was complete. <" << e->segmentMan->getFilePath() << ">" << endl;
+ } else {
+ cout << "\nThe download was not complete because of errors. Check the log." << endl;
+ }
for_each(requests.begin(), requests.end(), clearRequest);
requests.clear();