1 /* <!-- copyright */
2 /*
3  * aria2 - The high speed download utility
4  *
5  * Copyright (C) 2006 Tatsuhiro Tsujikawa
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  *
21  * In addition, as a special exception, the copyright holders give
22  * permission to link the code of portions of this program with the
23  * OpenSSL library under certain conditions as described in each
24  * individual source file, and distribute linked combinations
25  * including the two.
26  * You must obey the GNU General Public License in all respects
27  * for all of the code used other than OpenSSL.  If you modify
28  * file(s) with this exception, you may extend this exception to your
29  * version of the file(s), but you are not obligated to do so.  If you
30  * do not wish to do so, delete this exception statement from your
31  * version.  If you delete this exception statement from all source
32  * files in the program, then also delete it here.
33  */
34 /* copyright --> */
35 #include "PeerAbstractCommand.h"
36 #include "Peer.h"
37 #include "DownloadEngine.h"
38 #include "Option.h"
39 #include "DlAbortEx.h"
40 #include "SocketCore.h"
41 #include "Logger.h"
42 #include "LogFactory.h"
43 #include "message.h"
44 #include "prefs.h"
45 #include "DownloadFailureException.h"
46 #include "fmt.h"
47 #include "wallclock.h"
48 #include "util.h"
49 
50 namespace aria2 {
51 
PeerAbstractCommand(cuid_t cuid,const std::shared_ptr<Peer> & peer,DownloadEngine * e,const std::shared_ptr<SocketCore> & s)52 PeerAbstractCommand::PeerAbstractCommand(cuid_t cuid,
53                                          const std::shared_ptr<Peer>& peer,
54                                          DownloadEngine* e,
55                                          const std::shared_ptr<SocketCore>& s)
56     : Command(cuid),
57       checkPoint_(global::wallclock()),
58       // TODO referring global option
59       timeout_(std::chrono::seconds(e->getOption()->getAsInt(PREF_BT_TIMEOUT))),
60       e_(e),
61       socket_(s),
62       peer_(peer),
63       checkSocketIsReadable_(false),
64       checkSocketIsWritable_(false),
65       noCheck_(false)
66 {
67   if (socket_ && socket_->isOpen()) {
68     setReadCheckSocket(socket_);
69   }
70 }
71 
~PeerAbstractCommand()72 PeerAbstractCommand::~PeerAbstractCommand()
73 {
74   disableReadCheckSocket();
75   disableWriteCheckSocket();
76 }
77 
execute()78 bool PeerAbstractCommand::execute()
79 {
80   A2_LOG_DEBUG(fmt("CUID#%" PRId64 " -"
81                    " socket: read:%d, write:%d, hup:%d, err:%d, noCheck:%d",
82                    getCuid(), readEventEnabled(), writeEventEnabled(),
83                    hupEventEnabled(), errorEventEnabled(), noCheck_));
84   if (exitBeforeExecute()) {
85     onAbort();
86     return true;
87   }
88   try {
89     if (noCheck_ || (checkSocketIsReadable_ && readEventEnabled()) ||
90         (checkSocketIsWritable_ && writeEventEnabled()) || hupEventEnabled()) {
91       checkPoint_ = global::wallclock();
92     }
93     else if (errorEventEnabled()) {
94       throw DL_ABORT_EX(
95           fmt(MSG_NETWORK_PROBLEM, socket_->getSocketError().c_str()));
96     }
97     if (checkPoint_.difference(global::wallclock()) >= timeout_) {
98       throw DL_ABORT_EX(EX_TIME_OUT);
99     }
100     return executeInternal();
101   }
102   catch (DownloadFailureException& err) {
103     A2_LOG_ERROR_EX(EX_DOWNLOAD_ABORTED, err);
104     onAbort();
105     onFailure(err);
106     return true;
107   }
108   catch (RecoverableException& err) {
109     A2_LOG_DEBUG_EX(fmt(MSG_TORRENT_DOWNLOAD_ABORTED, getCuid()), err);
110     A2_LOG_DEBUG(fmt(MSG_PEER_BANNED, getCuid(), peer_->getIPAddress().c_str(),
111                      peer_->getPort()));
112     onAbort();
113     return prepareForNextPeer(0);
114   }
115 }
116 
117 // TODO this method removed when PeerBalancerCommand is implemented
prepareForNextPeer(time_t wait)118 bool PeerAbstractCommand::prepareForNextPeer(time_t wait) { return true; }
119 
disableReadCheckSocket()120 void PeerAbstractCommand::disableReadCheckSocket()
121 {
122   if (checkSocketIsReadable_) {
123     e_->deleteSocketForReadCheck(readCheckTarget_, this);
124     checkSocketIsReadable_ = false;
125     readCheckTarget_.reset();
126   }
127 }
128 
setReadCheckSocket(const std::shared_ptr<SocketCore> & socket)129 void PeerAbstractCommand::setReadCheckSocket(
130     const std::shared_ptr<SocketCore>& socket)
131 {
132   if (!socket->isOpen()) {
133     disableReadCheckSocket();
134   }
135   else {
136     if (checkSocketIsReadable_) {
137       if (*readCheckTarget_ != *socket) {
138         e_->deleteSocketForReadCheck(readCheckTarget_, this);
139         e_->addSocketForReadCheck(socket, this);
140         readCheckTarget_ = socket;
141       }
142     }
143     else {
144       e_->addSocketForReadCheck(socket, this);
145       checkSocketIsReadable_ = true;
146       readCheckTarget_ = socket;
147     }
148   }
149 }
150 
disableWriteCheckSocket()151 void PeerAbstractCommand::disableWriteCheckSocket()
152 {
153   if (checkSocketIsWritable_) {
154     e_->deleteSocketForWriteCheck(writeCheckTarget_, this);
155     checkSocketIsWritable_ = false;
156     writeCheckTarget_.reset();
157   }
158 }
159 
setWriteCheckSocket(const std::shared_ptr<SocketCore> & socket)160 void PeerAbstractCommand::setWriteCheckSocket(
161     const std::shared_ptr<SocketCore>& socket)
162 {
163   if (!socket->isOpen()) {
164     disableWriteCheckSocket();
165   }
166   else {
167     if (checkSocketIsWritable_) {
168       if (*writeCheckTarget_ != *socket) {
169         e_->deleteSocketForWriteCheck(writeCheckTarget_, this);
170         e_->addSocketForWriteCheck(socket, this);
171         writeCheckTarget_ = socket;
172       }
173     }
174     else {
175       e_->addSocketForWriteCheck(socket, this);
176       checkSocketIsWritable_ = true;
177       writeCheckTarget_ = socket;
178     }
179   }
180 }
181 
setNoCheck(bool check)182 void PeerAbstractCommand::setNoCheck(bool check) { noCheck_ = check; }
183 
updateKeepAlive()184 void PeerAbstractCommand::updateKeepAlive()
185 {
186   checkPoint_ = global::wallclock();
187 }
188 
createSocket()189 void PeerAbstractCommand::createSocket()
190 {
191   socket_ = std::make_shared<SocketCore>();
192 }
193 
addCommandSelf()194 void PeerAbstractCommand::addCommandSelf()
195 {
196   e_->addCommand(std::unique_ptr<Command>(this));
197 }
198 
199 } // namespace aria2
200