1 /* <!-- copyright */
2 /*
3  * aria2 - The high speed download utility
4  *
5  * Copyright (C) 2013 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 "BackupIPv4ConnectCommand.h"
36 #include "RequestGroup.h"
37 #include "DownloadEngine.h"
38 #include "SocketCore.h"
39 #include "wallclock.h"
40 #include "RecoverableException.h"
41 #include "fmt.h"
42 #include "LogFactory.h"
43 #include "prefs.h"
44 #include "Option.h"
45 
46 namespace aria2 {
47 
BackupConnectInfo()48 BackupConnectInfo::BackupConnectInfo() : cancel(false) {}
49 
BackupIPv4ConnectCommand(cuid_t cuid,const std::string & ipaddr,uint16_t port,const std::shared_ptr<BackupConnectInfo> & info,Command * mainCommand,RequestGroup * requestGroup,DownloadEngine * e)50 BackupIPv4ConnectCommand::BackupIPv4ConnectCommand(
51     cuid_t cuid, const std::string& ipaddr, uint16_t port,
52     const std::shared_ptr<BackupConnectInfo>& info, Command* mainCommand,
53     RequestGroup* requestGroup, DownloadEngine* e)
54     : Command(cuid),
55       ipaddr_(ipaddr),
56       port_(port),
57       info_(info),
58       mainCommand_(mainCommand),
59       requestGroup_(requestGroup),
60       e_(e),
61       startTime_(global::wallclock()),
62       timeoutCheck_(global::wallclock()),
63       timeout_(requestGroup_->getOption()->getAsInt(PREF_CONNECT_TIMEOUT))
64 {
65   requestGroup_->increaseStreamCommand();
66   requestGroup_->increaseNumCommand();
67 }
68 
~BackupIPv4ConnectCommand()69 BackupIPv4ConnectCommand::~BackupIPv4ConnectCommand()
70 {
71   requestGroup_->decreaseNumCommand();
72   requestGroup_->decreaseStreamCommand();
73   if (socket_) {
74     e_->deleteSocketForWriteCheck(socket_, this);
75   }
76 }
77 
execute()78 bool BackupIPv4ConnectCommand::execute()
79 {
80   bool retval = false;
81   if (requestGroup_->downloadFinished() || requestGroup_->isHaltRequested()) {
82     retval = true;
83   }
84   else if (info_->cancel) {
85     A2_LOG_INFO(
86         fmt("CUID#%" PRId64 " - Backup connection canceled", getCuid()));
87     retval = true;
88   }
89   else if (socket_) {
90     if (writeEventEnabled()) {
91       try {
92         std::string error = socket_->getSocketError();
93         if (error.empty()) {
94           A2_LOG_INFO(fmt("CUID#%" PRId64 " - Backup connection to %s "
95                           "established",
96                           getCuid(), ipaddr_.c_str()));
97           info_->ipaddr = ipaddr_;
98           e_->deleteSocketForWriteCheck(socket_, this);
99           info_->socket.swap(socket_);
100           mainCommand_->setStatus(STATUS_ONESHOT_REALTIME);
101           e_->setNoWait(true);
102           retval = true;
103         }
104         else {
105           A2_LOG_INFO(fmt("CUID#%" PRId64 " - Backup connection failed: %s",
106                           getCuid(), error.c_str()));
107           retval = true;
108         }
109       }
110       catch (RecoverableException& e) {
111         A2_LOG_INFO_EX(
112             fmt("CUID#%" PRId64 " - Backup connection failed", getCuid()), e);
113         retval = true;
114       }
115     }
116   }
117   else if (!socket_) {
118     // TODO Although we check 300ms initial timeout as described in
119     // RFC 6555, the interval will be much longer and around 1 second
120     // due to the refresh interval mechanism in DownloadEngine.
121     if (startTime_.difference(global::wallclock()) >=
122         std::chrono::milliseconds(300)) {
123       socket_ = std::make_shared<SocketCore>();
124       try {
125         socket_->establishConnection(ipaddr_, port_);
126         e_->addSocketForWriteCheck(socket_, this);
127         timeoutCheck_ = global::wallclock();
128       }
129       catch (RecoverableException& e) {
130         A2_LOG_INFO_EX(
131             fmt("CUID#%" PRId64 " - Backup connection failed", getCuid()), e);
132         socket_.reset();
133         retval = true;
134       }
135     }
136   }
137   else if (timeoutCheck_.difference(global::wallclock()) >= timeout_) {
138     A2_LOG_INFO(
139         fmt("CUID#%" PRId64 " - Backup connection command timeout", getCuid()));
140     retval = true;
141   }
142   if (!retval) {
143     e_->addCommand(std::unique_ptr<Command>(this));
144   }
145   return retval;
146 }
147 
148 } // namespace aria2
149