summaryrefslogtreecommitdiff
path: root/src/DownloadEngine.cc
diff options
context:
space:
mode:
authorTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2015-12-27 18:39:47 +0900
committerTatsuhiro Tsujikawa <tatsuhiro.t@gmail.com>2015-12-27 18:40:08 +0900
commitb1132d6b10cd3791b9529d7031d723fd0e2d8215 (patch)
tree9ce465b42bf09372118335c7c619bb2b41869eaf /src/DownloadEngine.cc
parent4abad2f64c9ff80144a61c5a9c2bea144852b883 (diff)
make clang-format using clang-format-3.6
Diffstat (limited to 'src/DownloadEngine.cc')
-rw-r--r--src/DownloadEngine.cc303
1 files changed, 144 insertions, 159 deletions
diff --git a/src/DownloadEngine.cc b/src/DownloadEngine.cc
index 3bc7b18e..9ea907ac 100644
--- a/src/DownloadEngine.cc
+++ b/src/DownloadEngine.cc
@@ -68,10 +68,10 @@
#include "fmt.h"
#include "wallclock.h"
#ifdef ENABLE_BITTORRENT
-# include "BtRegistry.h"
+#include "BtRegistry.h"
#endif // ENABLE_BITTORRENT
#ifdef ENABLE_WEBSOCKET
-# include "WebSocketSessionMan.h"
+#include "WebSocketSessionMan.h"
#endif // ENABLE_WEBSOCKET
#include "Option.h"
#include "util_security.h"
@@ -95,24 +95,24 @@ constexpr auto DEFAULT_REFRESH_INTERVAL = 1_s;
} // namespace
DownloadEngine::DownloadEngine(std::unique_ptr<EventPoll> eventPoll)
- : eventPoll_(std::move(eventPoll)),
- haltRequested_(0),
- noWait_(true),
- refreshInterval_(DEFAULT_REFRESH_INTERVAL),
- lastRefresh_(Timer::zero()),
- cookieStorage_(make_unique<CookieStorage>()),
+ : eventPoll_(std::move(eventPoll)),
+ haltRequested_(0),
+ noWait_(true),
+ refreshInterval_(DEFAULT_REFRESH_INTERVAL),
+ lastRefresh_(Timer::zero()),
+ cookieStorage_(make_unique<CookieStorage>()),
#ifdef ENABLE_BITTORRENT
- btRegistry_(make_unique<BtRegistry>()),
+ btRegistry_(make_unique<BtRegistry>()),
#endif // ENABLE_BITTORRENT
#ifdef HAVE_ARES_ADDR_NODE
- asyncDNSServers_(nullptr),
+ asyncDNSServers_(nullptr),
#endif // HAVE_ARES_ADDR_NODE
- dnsCache_(make_unique<DNSCache>()),
- option_(nullptr)
+ dnsCache_(make_unique<DNSCache>()),
+ option_(nullptr)
{
unsigned char sessionId[20];
util::generateRandomKey(sessionId);
- sessionId_.assign(&sessionId[0], & sessionId[sizeof(sessionId)]);
+ sessionId_.assign(&sessionId[0], &sessionId[sizeof(sessionId)]);
}
DownloadEngine::~DownloadEngine()
@@ -127,7 +127,7 @@ void executeCommand(std::deque<std::unique_ptr<Command>>& commands,
Command::STATUS statusFilter)
{
size_t max = commands.size();
- for(size_t i = 0; i < max; ++i) {
+ for (size_t i = 0; i < max; ++i) {
auto com = std::move(commands.front());
commands.pop_front();
if (!com->statusMatch(statusFilter)) {
@@ -151,34 +151,32 @@ namespace {
class GlobalHaltRequestedFinalizer {
public:
GlobalHaltRequestedFinalizer() {}
- ~GlobalHaltRequestedFinalizer()
- {
- global::globalHaltRequested = 5;
- }
+ ~GlobalHaltRequestedFinalizer() { global::globalHaltRequested = 5; }
};
} // namespace
int DownloadEngine::run(bool oneshot)
{
GlobalHaltRequestedFinalizer ghrf;
- while(!commands_.empty() || !routineCommands_.empty()) {
- if(!commands_.empty()) {
+ while (!commands_.empty() || !routineCommands_.empty()) {
+ if (!commands_.empty()) {
waitData();
}
noWait_ = false;
global::wallclock().reset();
calculateStatistics();
- if(lastRefresh_.difference(global::wallclock())+A2_DELTA_MILLIS >=
- refreshInterval_) {
+ if (lastRefresh_.difference(global::wallclock()) + A2_DELTA_MILLIS >=
+ refreshInterval_) {
refreshInterval_ = DEFAULT_REFRESH_INTERVAL;
lastRefresh_ = global::wallclock();
executeCommand(commands_, Command::STATUS_ALL);
- } else {
+ }
+ else {
executeCommand(commands_, Command::STATUS_ACTIVE);
}
executeCommand(routineCommands_, Command::STATUS_ALL);
afterEachIteration();
- if(!noWait_ && oneshot) {
+ if (!noWait_ && oneshot) {
return 1;
}
}
@@ -189,9 +187,10 @@ int DownloadEngine::run(bool oneshot)
void DownloadEngine::waitData()
{
struct timeval tv;
- if(noWait_) {
+ if (noWait_) {
tv.tv_sec = tv.tv_usec = 0;
- } else {
+ }
+ else {
auto t =
std::chrono::duration_cast<std::chrono::microseconds>(refreshInterval_);
tv.tv_sec = t.count() / 1000000;
@@ -200,29 +199,29 @@ void DownloadEngine::waitData()
eventPoll_->poll(tv);
}
-bool DownloadEngine::addSocketForReadCheck(const std::shared_ptr<SocketCore>& socket,
- Command* command)
+bool DownloadEngine::addSocketForReadCheck(
+ const std::shared_ptr<SocketCore>& socket, Command* command)
{
return eventPoll_->addEvents(socket->getSockfd(), command,
EventPoll::EVENT_READ);
}
-bool DownloadEngine::deleteSocketForReadCheck(const std::shared_ptr<SocketCore>& socket,
- Command* command)
+bool DownloadEngine::deleteSocketForReadCheck(
+ const std::shared_ptr<SocketCore>& socket, Command* command)
{
return eventPoll_->deleteEvents(socket->getSockfd(), command,
EventPoll::EVENT_READ);
}
-bool DownloadEngine::addSocketForWriteCheck(const std::shared_ptr<SocketCore>& socket,
- Command* command)
+bool DownloadEngine::addSocketForWriteCheck(
+ const std::shared_ptr<SocketCore>& socket, Command* command)
{
return eventPoll_->addEvents(socket->getSockfd(), command,
EventPoll::EVENT_WRITE);
}
-bool DownloadEngine::deleteSocketForWriteCheck(const std::shared_ptr<SocketCore>& socket,
- Command* command)
+bool DownloadEngine::deleteSocketForWriteCheck(
+ const std::shared_ptr<SocketCore>& socket, Command* command)
{
return eventPoll_->deleteEvents(socket->getSockfd(), command,
EventPoll::EVENT_WRITE);
@@ -230,7 +229,7 @@ bool DownloadEngine::deleteSocketForWriteCheck(const std::shared_ptr<SocketCore>
void DownloadEngine::calculateStatistics()
{
- if(statCalc_) {
+ if (statCalc_) {
statCalc_->calculateStat(this);
}
}
@@ -244,7 +243,7 @@ void DownloadEngine::onEndOfRun()
void DownloadEngine::afterEachIteration()
{
- if(global::globalHaltRequested == 1) {
+ if (global::globalHaltRequested == 1) {
A2_LOG_NOTICE(_("Shutdown sequence commencing..."
" Press Ctrl-C again for emergency shutdown."));
requestHalt();
@@ -254,7 +253,7 @@ void DownloadEngine::afterEachIteration()
return;
}
- if(global::globalHaltRequested == 3) {
+ if (global::globalHaltRequested == 3) {
A2_LOG_NOTICE(_("Emergency shutdown sequence commencing..."));
requestForceHalt();
global::globalHaltRequested = 4;
@@ -282,23 +281,20 @@ void DownloadEngine::setStatCalc(std::unique_ptr<StatCalc> statCalc)
}
#ifdef ENABLE_ASYNC_DNS
-bool DownloadEngine::addNameResolverCheck
-(const std::shared_ptr<AsyncNameResolver>& resolver, Command* command)
+bool DownloadEngine::addNameResolverCheck(
+ const std::shared_ptr<AsyncNameResolver>& resolver, Command* command)
{
return eventPoll_->addNameResolver(resolver, command);
}
-bool DownloadEngine::deleteNameResolverCheck
-(const std::shared_ptr<AsyncNameResolver>& resolver, Command* command)
+bool DownloadEngine::deleteNameResolverCheck(
+ const std::shared_ptr<AsyncNameResolver>& resolver, Command* command)
{
return eventPoll_->deleteNameResolver(resolver, command);
}
#endif // ENABLE_ASYNC_DNS
-void DownloadEngine::setNoWait(bool b)
-{
- noWait_ = b;
-}
+void DownloadEngine::setNoWait(bool b) { noWait_ = b; }
void DownloadEngine::addRoutineCommand(std::unique_ptr<Command> command)
{
@@ -326,55 +322,52 @@ void DownloadEngine::evictSocketPool()
newPool.insert(elem);
}
}
- A2_LOG_DEBUG(fmt("%lu entries removed.",
- static_cast<unsigned long>
- (socketPool_.size() - newPool.size())));
+ A2_LOG_DEBUG(
+ fmt("%lu entries removed.",
+ static_cast<unsigned long>(socketPool_.size() - newPool.size())));
socketPool_ = std::move(newPool);
}
namespace {
-std::string createSockPoolKey
-(const std::string& host, uint16_t port,
- const std::string& username,
- const std::string& proxyhost, uint16_t proxyport)
+std::string createSockPoolKey(const std::string& host, uint16_t port,
+ const std::string& username,
+ const std::string& proxyhost, uint16_t proxyport)
{
std::string key;
- if(!username.empty()) {
+ if (!username.empty()) {
key += util::percentEncode(username);
key += "@";
}
key += fmt("%s(%u)", host.c_str(), port);
- if(!proxyhost.empty()) {
+ if (!proxyhost.empty()) {
key += fmt("/%s(%u)", proxyhost.c_str(), proxyport);
}
return key;
}
} // namespace
-void DownloadEngine::poolSocket
-(const std::string& ipaddr,
- uint16_t port,
- const std::string& username,
- const std::string& proxyhost,
- uint16_t proxyport,
- const std::shared_ptr<SocketCore>& sock,
- const std::string& options,
- std::chrono::seconds timeout)
+void DownloadEngine::poolSocket(const std::string& ipaddr, uint16_t port,
+ const std::string& username,
+ const std::string& proxyhost,
+ uint16_t proxyport,
+ const std::shared_ptr<SocketCore>& sock,
+ const std::string& options,
+ std::chrono::seconds timeout)
{
SocketPoolEntry e(sock, options, std::move(timeout));
- poolSocket(createSockPoolKey(ipaddr, port, username, proxyhost, proxyport),e);
+ poolSocket(createSockPoolKey(ipaddr, port, username, proxyhost, proxyport),
+ e);
}
-void DownloadEngine::poolSocket
-(const std::string& ipaddr,
- uint16_t port,
- const std::string& proxyhost,
- uint16_t proxyport,
- const std::shared_ptr<SocketCore>& sock,
- std::chrono::seconds timeout)
+void DownloadEngine::poolSocket(const std::string& ipaddr, uint16_t port,
+ const std::string& proxyhost,
+ uint16_t proxyport,
+ const std::shared_ptr<SocketCore>& sock,
+ std::chrono::seconds timeout)
{
SocketPoolEntry e(sock, std::move(timeout));
- poolSocket(createSockPoolKey(ipaddr, port, A2STR::NIL,proxyhost,proxyport),e);
+ poolSocket(createSockPoolKey(ipaddr, port, A2STR::NIL, proxyhost, proxyport),
+ e);
}
namespace {
@@ -384,7 +377,8 @@ bool getPeerInfo(std::pair<std::string, uint16_t>& res,
try {
socket->getPeerInfo(res);
return true;
- } catch(RecoverableException& e) {
+ }
+ catch (RecoverableException& e) {
// socket->getPeerInfo() can fail if the socket has been
// disconnected.
A2_LOG_INFO_EX("Getting peer info failed. Pooling socket canceled.", e);
@@ -398,41 +392,39 @@ void DownloadEngine::poolSocket(const std::shared_ptr<Request>& request,
const std::shared_ptr<SocketCore>& socket,
std::chrono::seconds timeout)
{
- if(proxyRequest) {
+ if (proxyRequest) {
// If proxy is defined, then pool socket with its hostname.
- poolSocket(request->getHost(), request->getPort(),
- proxyRequest->getHost(), proxyRequest->getPort(),
- socket, std::move(timeout));
+ poolSocket(request->getHost(), request->getPort(), proxyRequest->getHost(),
+ proxyRequest->getPort(), socket, std::move(timeout));
return;
}
std::pair<std::string, uint16_t> peerInfo;
- if(getPeerInfo(peerInfo, socket)) {
- poolSocket(peerInfo.first, peerInfo.second,
- A2STR::NIL, 0, socket, std::move(timeout));
+ if (getPeerInfo(peerInfo, socket)) {
+ poolSocket(peerInfo.first, peerInfo.second, A2STR::NIL, 0, socket,
+ std::move(timeout));
}
}
-void DownloadEngine::poolSocket
-(const std::shared_ptr<Request>& request,
- const std::string& username,
- const std::shared_ptr<Request>& proxyRequest,
- const std::shared_ptr<SocketCore>& socket,
- const std::string& options,
- std::chrono::seconds timeout)
+void DownloadEngine::poolSocket(const std::shared_ptr<Request>& request,
+ const std::string& username,
+ const std::shared_ptr<Request>& proxyRequest,
+ const std::shared_ptr<SocketCore>& socket,
+ const std::string& options,
+ std::chrono::seconds timeout)
{
- if(proxyRequest) {
+ if (proxyRequest) {
// If proxy is defined, then pool socket with its hostname.
poolSocket(request->getHost(), request->getPort(), username,
- proxyRequest->getHost(), proxyRequest->getPort(),
- socket, options, std::move(timeout));
+ proxyRequest->getHost(), proxyRequest->getPort(), socket,
+ options, std::move(timeout));
return;
}
std::pair<std::string, uint16_t> peerInfo;
- if(getPeerInfo(peerInfo, socket)) {
- poolSocket(peerInfo.first, peerInfo.second, username,
- A2STR::NIL, 0, socket, options, std::move(timeout));
+ if (getPeerInfo(peerInfo, socket)) {
+ poolSocket(peerInfo.first, peerInfo.second, username, A2STR::NIL, 0, socket,
+ options, std::move(timeout));
}
}
@@ -440,13 +432,13 @@ std::multimap<std::string, DownloadEngine::SocketPoolEntry>::iterator
DownloadEngine::findSocketPoolEntry(const std::string& key)
{
std::pair<std::multimap<std::string, SocketPoolEntry>::iterator,
- std::multimap<std::string, SocketPoolEntry>::iterator> range =
- socketPool_.equal_range(key);
- for(auto i = range.first, eoi = range.second; i != eoi; ++i) {
+ std::multimap<std::string, SocketPoolEntry>::iterator> range =
+ socketPool_.equal_range(key);
+ for (auto i = range.first, eoi = range.second; i != eoi; ++i) {
const SocketPoolEntry& e = (*i).second;
// We assume that if socket is readable it means peer shutdowns
// connection and the socket will receive EOF. So skip it.
- if(!e.isTimeout() && !e.getSocket()->isReadable(0)) {
+ if (!e.isTimeout() && !e.getSocket()->isReadable(0)) {
A2_LOG_INFO(fmt("Found socket for %s", key.c_str()));
return i;
}
@@ -455,14 +447,14 @@ DownloadEngine::findSocketPoolEntry(const std::string& key)
}
std::shared_ptr<SocketCore>
-DownloadEngine::popPooledSocket
-(const std::string& ipaddr, uint16_t port,
- const std::string& proxyhost, uint16_t proxyport)
+DownloadEngine::popPooledSocket(const std::string& ipaddr, uint16_t port,
+ const std::string& proxyhost,
+ uint16_t proxyport)
{
std::shared_ptr<SocketCore> s;
- auto i = findSocketPoolEntry(createSockPoolKey(ipaddr, port, A2STR::NIL,
- proxyhost, proxyport));
- if(i != socketPool_.end()) {
+ auto i = findSocketPoolEntry(
+ createSockPoolKey(ipaddr, port, A2STR::NIL, proxyhost, proxyport));
+ if (i != socketPool_.end()) {
s = (*i).second.getSocket();
socketPool_.erase(i);
}
@@ -470,16 +462,15 @@ DownloadEngine::popPooledSocket
}
std::shared_ptr<SocketCore>
-DownloadEngine::popPooledSocket
-(std::string& options,
- const std::string& ipaddr, uint16_t port,
- const std::string& username,
- const std::string& proxyhost, uint16_t proxyport)
+DownloadEngine::popPooledSocket(std::string& options, const std::string& ipaddr,
+ uint16_t port, const std::string& username,
+ const std::string& proxyhost,
+ uint16_t proxyport)
{
std::shared_ptr<SocketCore> s;
- auto i = findSocketPoolEntry(createSockPoolKey(ipaddr, port, username,
- proxyhost, proxyport));
- if(i != socketPool_.end()) {
+ auto i = findSocketPoolEntry(
+ createSockPoolKey(ipaddr, port, username, proxyhost, proxyport));
+ if (i != socketPool_.end()) {
s = (*i).second.getSocket();
options = (*i).second.getOptions();
socketPool_.erase(i);
@@ -488,13 +479,13 @@ DownloadEngine::popPooledSocket
}
std::shared_ptr<SocketCore>
-DownloadEngine::popPooledSocket
-(const std::vector<std::string>& ipaddrs, uint16_t port)
+DownloadEngine::popPooledSocket(const std::vector<std::string>& ipaddrs,
+ uint16_t port)
{
std::shared_ptr<SocketCore> s;
- for(const auto & ipaddr : ipaddrs) {
+ for (const auto& ipaddr : ipaddrs) {
s = popPooledSocket(ipaddr, port, A2STR::NIL, 0);
- if(s) {
+ if (s) {
break;
}
}
@@ -502,35 +493,32 @@ DownloadEngine::popPooledSocket
}
std::shared_ptr<SocketCore>
-DownloadEngine::popPooledSocket
-(std::string& options,
- const std::vector<std::string>& ipaddrs, uint16_t port,
- const std::string& username)
+DownloadEngine::popPooledSocket(std::string& options,
+ const std::vector<std::string>& ipaddrs,
+ uint16_t port, const std::string& username)
{
std::shared_ptr<SocketCore> s;
- for(const auto & ipaddr : ipaddrs) {
+ for (const auto& ipaddr : ipaddrs) {
s = popPooledSocket(options, ipaddr, port, username, A2STR::NIL, 0);
- if(s) {
+ if (s) {
break;
}
}
return s;
}
-DownloadEngine::SocketPoolEntry::SocketPoolEntry
-(const std::shared_ptr<SocketCore>& socket,
- const std::string& options,
- std::chrono::seconds timeout)
- : socket_(socket),
- options_(options),
- timeout_(std::move(timeout))
-{}
+DownloadEngine::SocketPoolEntry::SocketPoolEntry(
+ const std::shared_ptr<SocketCore>& socket, const std::string& options,
+ std::chrono::seconds timeout)
+ : socket_(socket), options_(options), timeout_(std::move(timeout))
+{
+}
-DownloadEngine::SocketPoolEntry::SocketPoolEntry
-(const std::shared_ptr<SocketCore>& socket, std::chrono::seconds timeout)
- : socket_(socket),
- timeout_(std::move(timeout))
-{}
+DownloadEngine::SocketPoolEntry::SocketPoolEntry(
+ const std::shared_ptr<SocketCore>& socket, std::chrono::seconds timeout)
+ : socket_(socket), timeout_(std::move(timeout))
+{
+}
DownloadEngine::SocketPoolEntry::~SocketPoolEntry() {}
@@ -539,37 +527,35 @@ bool DownloadEngine::SocketPoolEntry::isTimeout() const
return registeredTime_.difference(global::wallclock()) >= timeout_;
}
-cuid_t DownloadEngine::newCUID()
-{
- return cuidCounter_.newID();
-}
+cuid_t DownloadEngine::newCUID() { return cuidCounter_.newID(); }
-const std::string& DownloadEngine::findCachedIPAddress
-(const std::string& hostname, uint16_t port) const
+const std::string&
+DownloadEngine::findCachedIPAddress(const std::string& hostname,
+ uint16_t port) const
{
return dnsCache_->find(hostname, port);
}
-void DownloadEngine::cacheIPAddress
-(const std::string& hostname, const std::string& ipaddr, uint16_t port)
+void DownloadEngine::cacheIPAddress(const std::string& hostname,
+ const std::string& ipaddr, uint16_t port)
{
dnsCache_->put(hostname, ipaddr, port);
}
-void DownloadEngine::markBadIPAddress
-(const std::string& hostname, const std::string& ipaddr, uint16_t port)
+void DownloadEngine::markBadIPAddress(const std::string& hostname,
+ const std::string& ipaddr, uint16_t port)
{
dnsCache_->markBad(hostname, ipaddr, port);
}
-void DownloadEngine::removeCachedIPAddress
-(const std::string& hostname, uint16_t port)
+void DownloadEngine::removeCachedIPAddress(const std::string& hostname,
+ uint16_t port)
{
dnsCache_->remove(hostname, port);
}
-void DownloadEngine::setAuthConfigFactory
-(std::unique_ptr<AuthConfigFactory> factory)
+void DownloadEngine::setAuthConfigFactory(
+ std::unique_ptr<AuthConfigFactory> factory)
{
authConfigFactory_ = std::move(factory);
}
@@ -590,8 +576,7 @@ void DownloadEngine::setRefreshInterval(std::chrono::milliseconds interval)
refreshInterval_ = std::move(interval);
}
-void DownloadEngine::addCommand
-(std::vector<std::unique_ptr<Command>> commands)
+void DownloadEngine::addCommand(std::vector<std::unique_ptr<Command>> commands)
{
commands_.insert(commands_.end(),
std::make_move_iterator(std::begin(commands)),
@@ -608,14 +593,14 @@ void DownloadEngine::setRequestGroupMan(std::unique_ptr<RequestGroupMan> rgman)
requestGroupMan_ = std::move(rgman);
}
-void DownloadEngine::setFileAllocationMan
-(std::unique_ptr<FileAllocationMan> faman)
+void DownloadEngine::setFileAllocationMan(
+ std::unique_ptr<FileAllocationMan> faman)
{
fileAllocationMan_ = std::move(faman);
}
-void DownloadEngine::setCheckIntegrityMan
-(std::unique_ptr<CheckIntegrityMan> ciman)
+void DownloadEngine::setCheckIntegrityMan(
+ std::unique_ptr<CheckIntegrityMan> ciman)
{
checkIntegrityMan_ = std::move(ciman);
}
@@ -624,7 +609,7 @@ void DownloadEngine::setCheckIntegrityMan
void DownloadEngine::setAsyncDNSServers(ares_addr_node* asyncDNSServers)
{
ares_addr_node* node = asyncDNSServers_;
- while(node) {
+ while (node) {
ares_addr_node* next = node->next;
delete node;
node = next;
@@ -634,8 +619,8 @@ void DownloadEngine::setAsyncDNSServers(ares_addr_node* asyncDNSServers)
#endif // HAVE_ARES_ADDR_NODE
#ifdef ENABLE_WEBSOCKET
-void DownloadEngine::setWebSocketSessionMan
-(std::unique_ptr<rpc::WebSocketSessionMan> wsman)
+void DownloadEngine::setWebSocketSessionMan(
+ std::unique_ptr<rpc::WebSocketSessionMan> wsman)
{
webSocketSessionMan_ = std::move(wsman);
}
@@ -655,8 +640,8 @@ bool DownloadEngine::validateToken(const std::string& token)
A2_LOG_ERROR("Failed to create HMAC");
return false;
}
- tokenExpected_ = make_unique<HMACResult>(tokenHMAC_->getResult(
- option_->get(PREF_RPC_SECRET)));
+ tokenExpected_ = make_unique<HMACResult>(
+ tokenHMAC_->getResult(option_->get(PREF_RPC_SECRET)));
}
return *tokenExpected_ == tokenHMAC_->getResult(token);