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
|
/* <!-- copyright */
/*
* aria2 - a simple utility for downloading files faster
*
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* copyright --> */
#include "AbstractCommand.h"
#include "DlAbortEx.h"
#include "DlRetryEx.h"
#include "InitiateConnectionCommandFactory.h"
#include "Util.h"
#include "message.h"
#include "SleepCommand.h"
#include "prefs.h"
AbstractCommand::AbstractCommand(int cuid, Request* req, DownloadEngine* e,
const SocketHandle& s):
Command(cuid), req(req), e(e), socket(s),
checkSocketIsReadable(false), checkSocketIsWritable(false) {
setReadCheckSocket(socket);
timeout = this->e->option->getAsInt(PREF_TIMEOUT);
}
AbstractCommand::~AbstractCommand() {
disableReadCheckSocket();
disableWriteCheckSocket();
}
bool AbstractCommand::execute() {
try {
if(checkSocketIsReadable && readCheckTarget->isReadable(0) ||
checkSocketIsWritable && writeCheckTarget->isWritable(0) ||
!checkSocketIsReadable && !checkSocketIsWritable) {
checkPoint.reset();
Segment seg = { 0, 0, 0, false };
if(e->segmentMan->downloadStarted) {
// get segment information in order to set Range header.
if(!e->segmentMan->getSegment(seg, cuid)) {
// no segment available
logger->info(MSG_NO_SEGMENT_AVAILABLE, cuid);
return true;
}
}
return executeInternal(seg);
} else {
if(checkPoint.elapsed(timeout)) {
throw new DlRetryEx(EX_TIME_OUT);
}
e->commands.push_back(this);
return false;
}
} catch(DlAbortEx* err) {
logger->error(MSG_DOWNLOAD_ABORTED, err, cuid);
onAbort(err);
delete(err);
req->resetUrl();
e->segmentMan->errors++;
tryReserved();
return true;
} catch(DlRetryEx* err) {
logger->error(MSG_RESTARTING_DOWNLOAD, err, cuid);
req->addTryCount();
bool isAbort = e->option->getAsInt(PREF_MAX_TRIES) != 0 &&
req->getTryCount() >= e->option->getAsInt(PREF_MAX_TRIES);
if(isAbort) {
onAbort(err);
}
delete(err);
if(isAbort) {
logger->error(MSG_MAX_TRY, cuid, req->getTryCount());
e->segmentMan->errors++;
tryReserved();
return true;
} else {
return prepareForRetry(e->option->getAsInt(PREF_RETRY_WAIT));
}
}
}
void AbstractCommand::tryReserved() {
if(!e->segmentMan->reserved.empty()) {
Request* req = e->segmentMan->reserved.front();
e->segmentMan->reserved.pop_front();
Command* command = InitiateConnectionCommandFactory::createInitiateConnectionCommand(cuid, req, e);
e->commands.push_back(command);
}
}
bool AbstractCommand::prepareForRetry(int wait) {
Command* command = InitiateConnectionCommandFactory::createInitiateConnectionCommand(cuid, req, e);
if(wait == 0) {
e->commands.push_back(command);
} else {
SleepCommand* scom = new SleepCommand(cuid, e, command, wait);
e->commands.push_back(scom);
}
return true;
}
void AbstractCommand::onAbort(Exception* ex) {
logger->debug(MSG_UNREGISTER_CUID, cuid);
e->segmentMan->unregisterId(cuid);
}
void AbstractCommand::disableReadCheckSocket() {
if(checkSocketIsReadable) {
e->deleteSocketForReadCheck(readCheckTarget, getUuid());
checkSocketIsReadable = false;
readCheckTarget = SocketHandle();
}
}
void AbstractCommand::setReadCheckSocket(const SocketHandle& socket) {
if(!socket->isOpen()) {
disableReadCheckSocket();
} else {
if(checkSocketIsReadable) {
if(readCheckTarget != socket) {
e->deleteSocketForReadCheck(readCheckTarget, getUuid());
e->addSocketForReadCheck(socket, getUuid());
readCheckTarget = socket;
}
} else {
e->addSocketForReadCheck(socket, getUuid());
checkSocketIsReadable = true;
readCheckTarget = socket;
}
}
}
void AbstractCommand::disableWriteCheckSocket() {
if(checkSocketIsWritable) {
e->deleteSocketForWriteCheck(writeCheckTarget, getUuid());
checkSocketIsWritable = false;
writeCheckTarget = SocketHandle();
}
}
void AbstractCommand::setWriteCheckSocket(const SocketHandle& socket) {
if(!socket->isOpen()) {
disableWriteCheckSocket();
} else {
if(checkSocketIsWritable) {
if(writeCheckTarget != socket) {
e->deleteSocketForWriteCheck(writeCheckTarget, getUuid());
e->addSocketForWriteCheck(socket, getUuid());
writeCheckTarget = socket;
}
} else {
e->addSocketForWriteCheck(socket, getUuid());
checkSocketIsWritable = true;
writeCheckTarget = socket;
}
}
}
|