1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561 |
- /* <!-- copyright */
- /*
- * aria2 - The high speed download utility
- *
- * Copyright (C) 2009 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- *
- * In addition, as a special exception, the copyright holders give
- * permission to link the code of portions of this program with the
- * OpenSSL library under certain conditions as described in each
- * individual source file, and distribute linked combinations
- * including the two.
- * You must obey the GNU General Public License in all respects
- * for all of the code used other than OpenSSL. If you modify
- * file(s) with this exception, you may extend this exception to your
- * version of the file(s), but you are not obligated to do so. If you
- * do not wish to do so, delete this exception statement from your
- * version. If you delete this exception statement from all source
- * files in the program, then also delete it here.
- */
- /* copyright --> */
- #include "RpcMethodImpl.h"
- #include <cassert>
- #include <algorithm>
- #include <sstream>
- #include "Logger.h"
- #include "LogFactory.h"
- #include "DlAbortEx.h"
- #include "Option.h"
- #include "OptionParser.h"
- #include "OptionHandler.h"
- #include "DownloadEngine.h"
- #include "RequestGroup.h"
- #include "download_helper.h"
- #include "util.h"
- #include "RequestGroupMan.h"
- #include "fmt.h"
- #include "RpcRequest.h"
- #include "PieceStorage.h"
- #include "DownloadContext.h"
- #include "DiskAdaptor.h"
- #include "FileEntry.h"
- #include "prefs.h"
- #include "message.h"
- #include "FeatureConfig.h"
- #include "array_fun.h"
- #include "RpcMethodFactory.h"
- #include "RpcResponse.h"
- #include "SegmentMan.h"
- #include "TimedHaltCommand.h"
- #include "PeerStat.h"
- #include "Base64.h"
- #include "BitfieldMan.h"
- #ifdef ENABLE_MESSAGE_DIGEST
- # include "MessageDigest.h"
- # include "message_digest_helper.h"
- #endif // ENABLE_MESSAGE_DIGEST
- #ifdef ENABLE_BITTORRENT
- # include "bittorrent_helper.h"
- # include "BtRegistry.h"
- # include "PeerStorage.h"
- # include "Peer.h"
- # include "BtRuntime.h"
- # include "BtAnnounce.h"
- #endif // ENABLE_BITTORRENT
- namespace aria2 {
- namespace rpc {
- namespace {
- const SharedHandle<String> VLB_TRUE = String::g("true");
- const SharedHandle<String> VLB_FALSE = String::g("false");
- const SharedHandle<String> VLB_OK = String::g("OK");
- const SharedHandle<String> VLB_ACTIVE = String::g("active");
- const SharedHandle<String> VLB_WAITING = String::g("waiting");
- const SharedHandle<String> VLB_PAUSED = String::g("paused");
- const SharedHandle<String> VLB_REMOVED = String::g("removed");
- const SharedHandle<String> VLB_ERROR = String::g("error");
- const SharedHandle<String> VLB_COMPLETE = String::g("complete");
- const SharedHandle<String> VLB_USED = String::g("used");
- const SharedHandle<String> VLB_ZERO = String::g("0");
- const std::string KEY_GID = "gid";
- const std::string KEY_ERROR_CODE = "errorCode";
- const std::string KEY_STATUS = "status";
- const std::string KEY_TOTAL_LENGTH = "totalLength";
- const std::string KEY_COMPLETED_LENGTH = "completedLength";
- const std::string KEY_DOWNLOAD_SPEED = "downloadSpeed";
- const std::string KEY_UPLOAD_SPEED = "uploadSpeed";
- const std::string KEY_UPLOAD_LENGTH = "uploadLength";
- const std::string KEY_CONNECTIONS = "connections";
- const std::string KEY_BITFIELD = "bitfield";
- const std::string KEY_PIECE_LENGTH = "pieceLength";
- const std::string KEY_NUM_PIECES = "numPieces";
- const std::string KEY_FOLLOWED_BY = "followedBy";
- const std::string KEY_BELONGS_TO = "belongsTo";
- const std::string KEY_INFO_HASH = "infoHash";
- const std::string KEY_NUM_SEEDERS = "numSeeders";
- const std::string KEY_PEER_ID = "peerId";
- const std::string KEY_IP = "ip";
- const std::string KEY_PORT = "port";
- const std::string KEY_AM_CHOKING = "amChoking";
- const std::string KEY_PEER_CHOKING = "peerChoking";
- const std::string KEY_SEEDER = "seeder";
- const std::string KEY_INDEX = "index";
- const std::string KEY_PATH = "path";
- const std::string KEY_SELECTED = "selected";
- const std::string KEY_LENGTH = "length";
- const std::string KEY_URI = "uri";
- const std::string KEY_CURRENT_URI = "currentUri";
- const std::string KEY_VERSION = "version";
- const std::string KEY_ENABLED_FEATURES = "enabledFeatures";
- const std::string KEY_METHOD_NAME = "methodName";
- const std::string KEY_PARAMS = "params";
- const std::string KEY_SESSION_ID = "sessionId";
- const std::string KEY_FILES = "files";
- const std::string KEY_DIR = "dir";
- const std::string KEY_URIS = "uris";
- const std::string KEY_BITTORRENT = "bittorrent";
- const std::string KEY_INFO = "info";
- const std::string KEY_NAME = "name";
- const std::string KEY_ANNOUNCE_LIST = "announceList";
- const std::string KEY_COMMENT = "comment";
- const std::string KEY_CREATION_DATE = "creationDate";
- const std::string KEY_MODE = "mode";
- const std::string KEY_SERVERS = "servers";
- const std::string KEY_NUM_WAITING = "numWaiting";
- const std::string KEY_NUM_STOPPED = "numStopped";
- const std::string KEY_NUM_ACTIVE = "numActive";
- } // namespace
- namespace {
- SharedHandle<ValueBase> createGIDResponse(a2_gid_t gid)
- {
- return String::g(util::itos(gid));
- }
- } // namespace
- namespace {
- SharedHandle<ValueBase>
- addRequestGroup(const SharedHandle<RequestGroup>& group,
- DownloadEngine* e,
- bool posGiven, int pos)
- {
- if(posGiven) {
- e->getRequestGroupMan()->insertReservedGroup(pos, group);
- } else {
- e->getRequestGroupMan()->addReservedGroup(group);
- }
- return createGIDResponse(group->getGID());
- }
- } // namespace
- namespace {
- SharedHandle<RequestGroup>
- findRequestGroup(const SharedHandle<RequestGroupMan>& rgman, a2_gid_t gid)
- {
- SharedHandle<RequestGroup> group = rgman->findRequestGroup(gid);
- if(!group) {
- group = rgman->findReservedGroup(gid);
- }
- return group;
- }
- } // namespace
- namespace {
- bool checkPosParam(const Integer* posParam)
- {
- if(posParam) {
- if(posParam->i() >= 0) {
- return true;
- } else {
- throw DL_ABORT_EX("Position must be greater than or equal to 0.");
- }
- }
- return false;
- }
- } // namespace
- namespace {
- a2_gid_t str2Gid(const String* str)
- {
- assert(str);
- return util::parseLLInt(str->s().begin(), str->s().end());
- }
- } // namespace
- namespace {
- template<typename OutputIterator>
- void extractUris(OutputIterator out, const List* src)
- {
- if(src) {
- for(List::ValueType::const_iterator i = src->begin(), eoi = src->end();
- i != eoi; ++i) {
- const String* uri = downcast<String>(*i);
- if(uri) {
- out++ = uri->s();
- }
- }
- }
- }
- } // namespace
- SharedHandle<ValueBase> AddUriRpcMethod::process
- (const RpcRequest& req, DownloadEngine* e)
- {
- const List* urisParam = checkRequiredParam<List>(req, 0);
- const Dict* optsParam = checkParam<Dict>(req, 1);
- const Integer* posParam = checkParam<Integer>(req, 2);
- std::vector<std::string> uris;
- extractUris(std::back_inserter(uris), urisParam);
- if(uris.empty()) {
- throw DL_ABORT_EX("URI is not provided.");
- }
- SharedHandle<Option> requestOption(new Option(*e->getOption()));
- gatherRequestOption(requestOption.get(), optsParam);
- bool posGiven = checkPosParam(posParam);
- size_t pos = posGiven ? posParam->i() : 0;
- std::vector<SharedHandle<RequestGroup> > result;
- createRequestGroupForUri(result, requestOption, uris,
- /* ignoreForceSeq = */ true,
- /* ignoreLocalPath = */ true);
- if(!result.empty()) {
- return addRequestGroup(result.front(), e, posGiven, pos);
- } else {
- throw DL_ABORT_EX("No URI to download.");
- }
- }
- #ifdef ENABLE_MESSAGE_DIGEST
- namespace {
- std::string getHexSha1(const std::string& s)
- {
- unsigned char hash[20];
- message_digest::digest(hash, sizeof(hash), MessageDigest::sha1(),
- s.data(), s.size());
- return util::toHex(hash, sizeof(hash));
- }
- } // namespace
- #endif // ENABLE_MESSAGE_DIGEST
- #ifdef ENABLE_BITTORRENT
- SharedHandle<ValueBase> AddTorrentRpcMethod::process
- (const RpcRequest& req, DownloadEngine* e)
- {
- const String* torrentParam = checkRequiredParam<String>(req, 0);
- const List* urisParam = checkParam<List>(req, 1);
- const Dict* optsParam = checkParam<Dict>(req, 2);
- const Integer* posParam = checkParam<Integer>(req, 3);
- SharedHandle<String> tempTorrentParam;
- if(req.jsonRpc) {
- tempTorrentParam = String::g(Base64::decode(torrentParam->s()));
- torrentParam = tempTorrentParam.get();
- }
- std::vector<std::string> uris;
- extractUris(std::back_inserter(uris), urisParam);
- SharedHandle<Option> requestOption(new Option(*e->getOption()));
- gatherRequestOption(requestOption.get(), optsParam);
- bool posGiven = checkPosParam(posParam);
- size_t pos = posGiven ? posParam->i() : 0;
- std::string filename = util::applyDir
- (requestOption->get(PREF_DIR), getHexSha1(torrentParam->s())+".torrent");
- std::vector<SharedHandle<RequestGroup> > result;
- // Save uploaded data in order to save this download in
- // --save-session file.
- if(util::saveAs(filename, torrentParam->s(), true)) {
- A2_LOG_INFO(fmt("Uploaded torrent data was saved as %s", filename.c_str()));
- requestOption->put(PREF_TORRENT_FILE, filename);
- createRequestGroupForBitTorrent(result, requestOption, uris);
- } else {
- A2_LOG_INFO(fmt("Uploaded torrent data was not saved."
- " Failed to write file %s", filename.c_str()));
- createRequestGroupForBitTorrent(result, requestOption,
- uris, torrentParam->s());
- }
- if(!result.empty()) {
- return addRequestGroup(result.front(), e, posGiven, pos);
- } else {
- throw DL_ABORT_EX("No Torrent to download.");
- }
- }
- #endif // ENABLE_BITTORRENT
- #ifdef ENABLE_METALINK
- SharedHandle<ValueBase> AddMetalinkRpcMethod::process
- (const RpcRequest& req, DownloadEngine* e)
- {
- const String* metalinkParam = checkRequiredParam<String>(req, 0);
- const Dict* optsParam = checkParam<Dict>(req, 1);
- const Integer* posParam = checkParam<Integer>(req, 2);
- SharedHandle<String> tempMetalinkParam;
- if(req.jsonRpc) {
- tempMetalinkParam = String::g(Base64::decode(metalinkParam->s()));
- metalinkParam = tempMetalinkParam.get();
- }
- SharedHandle<Option> requestOption(new Option(*e->getOption()));
- gatherRequestOption(requestOption.get(), optsParam);
- bool posGiven = checkPosParam(posParam);
- size_t pos = posGiven ? posParam->i() : 0;
- std::vector<SharedHandle<RequestGroup> > result;
- #ifdef ENABLE_MESSAGE_DIGEST
- // TODO RFC5854 Metalink has the extension .meta4 and Metalink
- // Version 3 uses .metalink extension. We use .meta4 for both
- // RFC5854 Metalink and Version 3. aria2 can detect which of which
- // by reading content rather than extension.
- std::string filename = util::applyDir
- (requestOption->get(PREF_DIR), getHexSha1(metalinkParam->s())+".meta4");
- // Save uploaded data in order to save this download in
- // --save-session file.
- if(util::saveAs(filename, metalinkParam->s(), true)) {
- A2_LOG_INFO(fmt("Uploaded metalink data was saved as %s",
- filename.c_str()));
- requestOption->put(PREF_METALINK_FILE, filename);
- createRequestGroupForMetalink(result, requestOption);
- } else {
- A2_LOG_INFO(fmt("Uploaded metalink data was not saved."
- " Failed to write file %s", filename.c_str()));
- createRequestGroupForMetalink(result, requestOption, metalinkParam->s());
- }
- #else // !ENABLE_MESSAGE_DIGEST
- createRequestGroupForMetalink(result, requestOption, metalinkParam->s());
- #endif // !ENABLE_MESSAGE_DIGEST
- SharedHandle<List> gids = List::g();
- if(!result.empty()) {
- if(posGiven) {
- e->getRequestGroupMan()->insertReservedGroup(pos, result);
- } else {
- e->getRequestGroupMan()->addReservedGroup(result);
- }
- for(std::vector<SharedHandle<RequestGroup> >::const_iterator i =
- result.begin(), eoi = result.end(); i != eoi; ++i) {
- gids->append(util::itos((*i)->getGID()));
- }
- }
- return gids;
- }
- #endif // ENABLE_METALINK
- namespace {
- SharedHandle<ValueBase> removeDownload
- (const RpcRequest& req, DownloadEngine* e, bool forceRemove)
- {
- const String* gidParam = checkRequiredParam<String>(req, 0);
- a2_gid_t gid = str2Gid(gidParam);
- SharedHandle<RequestGroup> group =
- e->getRequestGroupMan()->findRequestGroup(gid);
- if(!group) {
- group = e->getRequestGroupMan()->findReservedGroup(gid);
- if(!group) {
- throw DL_ABORT_EX
- (fmt("Active Download not found for GID#%s",
- util::itos(gid).c_str()));
- }
- if(group->isDependencyResolved()) {
- e->getRequestGroupMan()->removeReservedGroup(gid);
- } else {
- throw DL_ABORT_EX
- (fmt("GID#%s cannot be removed now",
- util::itos(gid).c_str()));
- }
- } else {
- if(forceRemove) {
- group->setForceHaltRequested(true, RequestGroup::USER_REQUEST);
- } else {
- group->setHaltRequested(true, RequestGroup::USER_REQUEST);
- }
- e->setRefreshInterval(0);
- }
- return createGIDResponse(gid);
- }
- } // namespace
- SharedHandle<ValueBase> RemoveRpcMethod::process
- (const RpcRequest& req, DownloadEngine* e)
- {
- return removeDownload(req, e, false);
- }
- SharedHandle<ValueBase> ForceRemoveRpcMethod::process
- (const RpcRequest& req, DownloadEngine* e)
- {
- return removeDownload(req, e, true);
- }
- namespace {
- bool pauseRequestGroup
- (const SharedHandle<RequestGroup>& group, bool reserved, bool forcePause)
- {
- if((reserved && !group->isPauseRequested()) ||
- (!reserved &&
- !group->isForceHaltRequested() &&
- ((forcePause && group->isHaltRequested() && group->isPauseRequested()) ||
- (!group->isHaltRequested() && !group->isPauseRequested())))) {
- if(!reserved) {
- // Call setHaltRequested before setPauseRequested because
- // setHaltRequested calls setPauseRequested(false) internally.
- if(forcePause) {
- group->setForceHaltRequested(true, RequestGroup::NONE);
- } else {
- group->setHaltRequested(true, RequestGroup::NONE);
- }
- }
- group->setPauseRequested(true);
- return true;
- } else {
- return false;
- }
- }
- } // namespace
- namespace {
- SharedHandle<ValueBase> pauseDownload
- (const RpcRequest& req, DownloadEngine* e, bool forcePause)
- {
- const String* gidParam = checkRequiredParam<String>(req, 0);
- a2_gid_t gid = str2Gid(gidParam);
- bool reserved = false;
- SharedHandle<RequestGroup> group =
- e->getRequestGroupMan()->findRequestGroup(gid);
- if(!group) {
- reserved = true;
- group = e->getRequestGroupMan()->findReservedGroup(gid);
- }
- if(group && pauseRequestGroup(group, reserved, forcePause)) {
- e->setRefreshInterval(0);
- return createGIDResponse(gid);
- } else {
- throw DL_ABORT_EX
- (fmt("GID#%s cannot be paused now",
- util::itos(gid).c_str()));
- }
- }
- } // namespace
- SharedHandle<ValueBase> PauseRpcMethod::process
- (const RpcRequest& req, DownloadEngine* e)
- {
- return pauseDownload(req, e, false);
- }
- SharedHandle<ValueBase> ForcePauseRpcMethod::process
- (const RpcRequest& req, DownloadEngine* e)
- {
- return pauseDownload(req, e, true);
- }
- namespace {
- template<typename InputIterator>
- void pauseRequestGroups
- (InputIterator first, InputIterator last, bool reserved, bool forcePause)
- {
- for(; first != last; ++first) {
- pauseRequestGroup(*first, reserved, forcePause);
- }
- }
- } // namespace
- namespace {
- SharedHandle<ValueBase> pauseAllDownloads
- (const RpcRequest& req, DownloadEngine* e, bool forcePause)
- {
- const std::deque<SharedHandle<RequestGroup> >& groups =
- e->getRequestGroupMan()->getRequestGroups();
- pauseRequestGroups(groups.begin(), groups.end(), false, forcePause);
- const std::deque<SharedHandle<RequestGroup> >& reservedGroups =
- e->getRequestGroupMan()->getReservedGroups();
- pauseRequestGroups(reservedGroups.begin(), reservedGroups.end(),
- true, forcePause);
- return VLB_OK;
- }
- } // namespace
- SharedHandle<ValueBase> PauseAllRpcMethod::process
- (const RpcRequest& req, DownloadEngine* e)
- {
- return pauseAllDownloads(req, e, false);
- }
- SharedHandle<ValueBase> ForcePauseAllRpcMethod::process
- (const RpcRequest& req, DownloadEngine* e)
- {
- return pauseAllDownloads(req, e, true);
- }
- SharedHandle<ValueBase> UnpauseRpcMethod::process
- (const RpcRequest& req, DownloadEngine* e)
- {
- const String* gidParam = checkRequiredParam<String>(req, 0);
- a2_gid_t gid = str2Gid(gidParam);
- SharedHandle<RequestGroup> group =
- e->getRequestGroupMan()->findReservedGroup(gid);
- if(!group || !group->isPauseRequested()) {
- throw DL_ABORT_EX
- (fmt("GID#%s cannot be unpaused now",
- util::itos(gid).c_str()));
- } else {
- group->setPauseRequested(false);
- e->getRequestGroupMan()->requestQueueCheck();
- }
- return createGIDResponse(gid);
- }
- SharedHandle<ValueBase> UnpauseAllRpcMethod::process
- (const RpcRequest& req, DownloadEngine* e)
- {
- const std::deque<SharedHandle<RequestGroup> >& groups =
- e->getRequestGroupMan()->getReservedGroups();
- std::for_each(groups.begin(), groups.end(),
- std::bind2nd(mem_fun_sh(&RequestGroup::setPauseRequested),
- false));
- e->getRequestGroupMan()->requestQueueCheck();
- return VLB_OK;
- }
- namespace {
- template<typename InputIterator>
- void createUriEntry
- (const SharedHandle<List>& uriList,
- InputIterator first, InputIterator last,
- const SharedHandle<String>& status)
- {
- for(; first != last; ++first) {
- SharedHandle<Dict> entry = Dict::g();
- entry->put(KEY_URI, *first);
- entry->put(KEY_STATUS, status);
- uriList->append(entry);
- }
- }
- } // namespace
- namespace {
- void createUriEntry
- (const SharedHandle<List>& uriList, const SharedHandle<FileEntry>& file)
- {
- createUriEntry(uriList,
- file->getSpentUris().begin(),
- file->getSpentUris().end(),
- VLB_USED);
- createUriEntry(uriList,
- file->getRemainingUris().begin(),
- file->getRemainingUris().end(),
- VLB_WAITING);
- }
- } // namespace
- namespace {
- template<typename InputIterator>
- void createFileEntry
- (const SharedHandle<List>& files,
- InputIterator first, InputIterator last,
- const BitfieldMan* bf)
- {
- size_t index = 1;
- for(; first != last; ++first, ++index) {
- SharedHandle<Dict> entry = Dict::g();
- entry->put(KEY_INDEX, util::uitos(index));
- entry->put(KEY_PATH, (*first)->getPath());
- entry->put(KEY_SELECTED, (*first)->isRequested()?VLB_TRUE:VLB_FALSE);
- entry->put(KEY_LENGTH, util::uitos((*first)->getLength()));
- uint64_t completedLength = bf->getOffsetCompletedLength
- ((*first)->getOffset(), (*first)->getLength());
- entry->put(KEY_COMPLETED_LENGTH, util::uitos(completedLength));
- SharedHandle<List> uriList = List::g();
- createUriEntry(uriList, *first);
- entry->put(KEY_URIS, uriList);
- files->append(entry);
- }
- }
- } // namespace
- namespace {
- template<typename InputIterator>
- void createFileEntry
- (const SharedHandle<List>& files,
- InputIterator first, InputIterator last,
- uint64_t totalLength,
- size_t pieceLength,
- const std::string& bitfield)
- {
- BitfieldMan bf(pieceLength, totalLength);
- bf.setBitfield(reinterpret_cast<const unsigned char*>(bitfield.data()),
- bitfield.size());
- createFileEntry(files, first, last, &bf);
- }
- } // namespace
- namespace {
- template<typename InputIterator>
- void createFileEntry
- (const SharedHandle<List>& files,
- InputIterator first, InputIterator last,
- uint64_t totalLength,
- size_t pieceLength,
- const SharedHandle<PieceStorage>& ps)
- {
- BitfieldMan bf(pieceLength, totalLength);
- if(ps) {
- bf.setBitfield(ps->getBitfield(), ps->getBitfieldLength());
- }
- createFileEntry(files, first, last, &bf);
- }
- } // namespace
- namespace {
- bool requested_key
- (const std::vector<std::string>& keys, const std::string& k)
- {
- return keys.empty() || std::find(keys.begin(), keys.end(), k) != keys.end();
- }
- } // namespace
- void gatherProgressCommon
- (const SharedHandle<Dict>& entryDict,
- const SharedHandle<RequestGroup>& group,
- const std::vector<std::string>& keys)
- {
- const SharedHandle<PieceStorage>& ps = group->getPieceStorage();
- if(requested_key(keys, KEY_GID)) {
- entryDict->put(KEY_GID, util::itos(group->getGID()));
- }
- if(requested_key(keys, KEY_TOTAL_LENGTH)) {
- // This is "filtered" total length if --select-file is used.
- entryDict->put(KEY_TOTAL_LENGTH, util::uitos(group->getTotalLength()));
- }
- if(requested_key(keys, KEY_COMPLETED_LENGTH)) {
- // This is "filtered" total length if --select-file is used.
- entryDict->put
- (KEY_COMPLETED_LENGTH,util::uitos(group->getCompletedLength()));
- }
- TransferStat stat = group->calculateStat();
- if(requested_key(keys, KEY_DOWNLOAD_SPEED)) {
- entryDict->put(KEY_DOWNLOAD_SPEED, util::uitos(stat.getDownloadSpeed()));
- }
- if(requested_key(keys, KEY_UPLOAD_SPEED)) {
- entryDict->put(KEY_UPLOAD_SPEED, util::uitos(stat.getUploadSpeed()));
- }
- if(requested_key(keys, KEY_UPLOAD_LENGTH)) {
- entryDict->put
- (KEY_UPLOAD_LENGTH, util::uitos(stat.getAllTimeUploadLength()));
- }
- if(requested_key(keys, KEY_CONNECTIONS)) {
- entryDict->put(KEY_CONNECTIONS, util::uitos(group->getNumConnection()));
- }
- if(requested_key(keys, KEY_BITFIELD)) {
- if(ps) {
- if(ps->getBitfieldLength() > 0) {
- entryDict->put(KEY_BITFIELD,
- util::toHex(ps->getBitfield(), ps->getBitfieldLength()));
- }
- }
- }
- const SharedHandle<DownloadContext>& dctx = group->getDownloadContext();
- if(requested_key(keys, KEY_PIECE_LENGTH)) {
- entryDict->put(KEY_PIECE_LENGTH, util::uitos(dctx->getPieceLength()));
- }
- if(requested_key(keys, KEY_NUM_PIECES)) {
- entryDict->put(KEY_NUM_PIECES, util::uitos(dctx->getNumPieces()));
- }
- if(requested_key(keys, KEY_FOLLOWED_BY)) {
- if(!group->followedBy().empty()) {
- SharedHandle<List> list = List::g();
- // The element is GID.
- for(std::vector<a2_gid_t>::const_iterator i = group->followedBy().begin(),
- eoi = group->followedBy().end(); i != eoi; ++i) {
- list->append(util::itos(*i));
- }
- entryDict->put(KEY_FOLLOWED_BY, list);
- }
- }
- if(requested_key(keys, KEY_BELONGS_TO)) {
- if(group->belongsTo()) {
- entryDict->put(KEY_BELONGS_TO, util::itos(group->belongsTo()));
- }
- }
- if(requested_key(keys, KEY_FILES)) {
- SharedHandle<List> files = List::g();
- createFileEntry
- (files, dctx->getFileEntries().begin(), dctx->getFileEntries().end(),
- dctx->getTotalLength(), dctx->getPieceLength(), ps);
- entryDict->put(KEY_FILES, files);
- }
- if(requested_key(keys, KEY_DIR)) {
- entryDict->put(KEY_DIR, group->getOption()->get(PREF_DIR));
- }
- }
- #ifdef ENABLE_BITTORRENT
- void gatherBitTorrentMetadata
- (const SharedHandle<Dict>& btDict,
- const SharedHandle<TorrentAttribute>& torrentAttrs)
- {
- if(!torrentAttrs->comment.empty()) {
- btDict->put(KEY_COMMENT, torrentAttrs->comment);
- }
- if(torrentAttrs->creationDate) {
- btDict->put(KEY_CREATION_DATE, Integer::g(torrentAttrs->creationDate));
- }
- if(!torrentAttrs->mode.empty()) {
- btDict->put(KEY_MODE, torrentAttrs->mode);
- }
- SharedHandle<List> destAnnounceList = List::g();
- for(std::vector<std::vector<std::string> >::const_iterator l =
- torrentAttrs->announceList.begin(),
- eoi = torrentAttrs->announceList.end(); l != eoi; ++l) {
- SharedHandle<List> destAnnounceTier = List::g();
- for(std::vector<std::string>::const_iterator t = (*l).begin(),
- eoi2 = (*l).end(); t != eoi2; ++t) {
- destAnnounceTier->append(*t);
- }
- destAnnounceList->append(destAnnounceTier);
- }
- btDict->put(KEY_ANNOUNCE_LIST, destAnnounceList);
- if(!torrentAttrs->metadata.empty()) {
- SharedHandle<Dict> infoDict = Dict::g();
- infoDict->put(KEY_NAME, torrentAttrs->name);
- btDict->put(KEY_INFO, infoDict);
- }
- }
- namespace {
- void gatherProgressBitTorrent
- (const SharedHandle<Dict>& entryDict,
- const SharedHandle<TorrentAttribute>& torrentAttrs,
- const SharedHandle<BtObject>& btObject,
- const std::vector<std::string>& keys)
- {
- if(requested_key(keys, KEY_INFO_HASH)) {
- entryDict->put(KEY_INFO_HASH, util::toHex(torrentAttrs->infoHash));
- }
- if(requested_key(keys, KEY_BITTORRENT)) {
- SharedHandle<Dict> btDict = Dict::g();
- gatherBitTorrentMetadata(btDict, torrentAttrs);
- entryDict->put(KEY_BITTORRENT, btDict);
- }
- if(requested_key(keys, KEY_NUM_SEEDERS)) {
- if(!btObject) {
- entryDict->put(KEY_NUM_SEEDERS, VLB_ZERO);
- } else {
- const SharedHandle<PeerStorage>& peerStorage = btObject->peerStorage;
- assert(peerStorage);
- std::vector<SharedHandle<Peer> > peers;
- peerStorage->getActivePeers(peers);
- entryDict->put(KEY_NUM_SEEDERS,
- util::uitos(countSeeder(peers.begin(), peers.end())));
- }
- }
- }
- } // namespace
- namespace {
- void gatherPeer
- (const SharedHandle<List>& peers, const SharedHandle<PeerStorage>& ps)
- {
- std::vector<SharedHandle<Peer> > activePeers;
- ps->getActivePeers(activePeers);
- for(std::vector<SharedHandle<Peer> >::const_iterator i =
- activePeers.begin(), eoi = activePeers.end(); i != eoi; ++i) {
- SharedHandle<Dict> peerEntry = Dict::g();
- peerEntry->put(KEY_PEER_ID, util::torrentPercentEncode((*i)->getPeerId(),
- PEER_ID_LENGTH));
- peerEntry->put(KEY_IP, (*i)->getIPAddress());
- if((*i)->isIncomingPeer()) {
- peerEntry->put(KEY_PORT, VLB_ZERO);
- } else {
- peerEntry->put(KEY_PORT, util::uitos((*i)->getPort()));
- }
- peerEntry->put(KEY_BITFIELD,
- util::toHex((*i)->getBitfield(), (*i)->getBitfieldLength()));
- peerEntry->put(KEY_AM_CHOKING, (*i)->amChoking()?VLB_TRUE:VLB_FALSE);
- peerEntry->put(KEY_PEER_CHOKING, (*i)->peerChoking()?VLB_TRUE:VLB_FALSE);
- TransferStat stat = ps->getTransferStatFor(*i);
- peerEntry->put(KEY_DOWNLOAD_SPEED, util::uitos(stat.getDownloadSpeed()));
- peerEntry->put(KEY_UPLOAD_SPEED, util::uitos(stat.getUploadSpeed()));
- peerEntry->put(KEY_SEEDER, (*i)->isSeeder()?VLB_TRUE:VLB_FALSE);
- peers->append(peerEntry);
- }
- }
- } // namespace
- #endif // ENABLE_BITTORRENT
- namespace {
- void gatherProgress
- (const SharedHandle<Dict>& entryDict,
- const SharedHandle<RequestGroup>& group,
- DownloadEngine* e,
- const std::vector<std::string>& keys)
- {
- gatherProgressCommon(entryDict, group, keys);
- #ifdef ENABLE_BITTORRENT
- if(group->getDownloadContext()->hasAttribute(bittorrent::BITTORRENT)) {
- SharedHandle<TorrentAttribute> torrentAttrs =
- bittorrent::getTorrentAttrs(group->getDownloadContext());
- const SharedHandle<BtObject>& btObject =
- e->getBtRegistry()->get(group->getGID());
- gatherProgressBitTorrent(entryDict, torrentAttrs, btObject, keys);
- }
- #endif // ENABLE_BITTORRENT
- }
- } // namespace
- void gatherStoppedDownload
- (const SharedHandle<Dict>& entryDict, const SharedHandle<DownloadResult>& ds,
- const std::vector<std::string>& keys)
- {
- if(requested_key(keys, KEY_GID)) {
- entryDict->put(KEY_GID, util::itos(ds->gid));
- }
- if(requested_key(keys, KEY_ERROR_CODE)) {
- entryDict->put(KEY_ERROR_CODE, util::itos(static_cast<int>(ds->result)));
- }
- if(requested_key(keys, KEY_STATUS)) {
- if(ds->result == error_code::REMOVED) {
- entryDict->put(KEY_STATUS, VLB_REMOVED);
- } else if(ds->result == error_code::FINISHED) {
- entryDict->put(KEY_STATUS, VLB_COMPLETE);
- } else {
- entryDict->put(KEY_STATUS, VLB_ERROR);
- }
- }
- if(requested_key(keys, KEY_FOLLOWED_BY)) {
- if(!ds->followedBy.empty()) {
- SharedHandle<List> list = List::g();
- // The element is GID.
- for(std::vector<a2_gid_t>::const_iterator i = ds->followedBy.begin(),
- eoi = ds->followedBy.end(); i != eoi; ++i) {
- list->append(util::itos(*i));
- }
- entryDict->put(KEY_FOLLOWED_BY, list);
- }
- }
- if(requested_key(keys, KEY_BELONGS_TO)) {
- if(ds->belongsTo) {
- entryDict->put(KEY_BELONGS_TO, util::itos(ds->belongsTo));
- }
- }
- if(requested_key(keys, KEY_FILES)) {
- SharedHandle<List> files = List::g();
- createFileEntry(files, ds->fileEntries.begin(), ds->fileEntries.end(),
- ds->totalLength, ds->pieceLength, ds->bitfield);
- entryDict->put(KEY_FILES, files);
- }
- if(requested_key(keys, KEY_TOTAL_LENGTH)) {
- entryDict->put(KEY_TOTAL_LENGTH, util::uitos(ds->totalLength));
- }
- if(requested_key(keys, KEY_COMPLETED_LENGTH)) {
- entryDict->put(KEY_COMPLETED_LENGTH, util::uitos(ds->completedLength));
- }
- if(requested_key(keys, KEY_UPLOAD_LENGTH)) {
- entryDict->put(KEY_UPLOAD_LENGTH, util::uitos(ds->uploadLength));
- }
- if(requested_key(keys, KEY_BITFIELD)) {
- if(!ds->bitfield.empty()) {
- entryDict->put(KEY_BITFIELD, util::toHex(ds->bitfield));
- }
- }
- if(requested_key(keys, KEY_DOWNLOAD_SPEED)) {
- entryDict->put(KEY_DOWNLOAD_SPEED, VLB_ZERO);
- }
- if(requested_key(keys, KEY_UPLOAD_SPEED)) {
- entryDict->put(KEY_UPLOAD_SPEED, VLB_ZERO);
- }
- if(!ds->infoHash.empty()) {
- if(requested_key(keys, KEY_INFO_HASH)) {
- entryDict->put(KEY_INFO_HASH, util::toHex(ds->infoHash));
- }
- if(requested_key(keys, KEY_NUM_SEEDERS)) {
- entryDict->put(KEY_NUM_SEEDERS, VLB_ZERO);
- }
- }
- if(requested_key(keys, KEY_PIECE_LENGTH)) {
- entryDict->put(KEY_PIECE_LENGTH, util::uitos(ds->pieceLength));
- }
- if(requested_key(keys, KEY_NUM_PIECES)) {
- entryDict->put(KEY_NUM_PIECES, util::uitos(ds->numPieces));
- }
- if(requested_key(keys, KEY_CONNECTIONS)) {
- entryDict->put(KEY_CONNECTIONS, VLB_ZERO);
- }
- if(requested_key(keys, KEY_DIR)) {
- entryDict->put(KEY_DIR, ds->dir);
- }
- }
- SharedHandle<ValueBase> GetFilesRpcMethod::process
- (const RpcRequest& req, DownloadEngine* e)
- {
- const String* gidParam = checkRequiredParam<String>(req, 0);
- a2_gid_t gid = str2Gid(gidParam);
- SharedHandle<List> files = List::g();
- SharedHandle<RequestGroup> group =
- findRequestGroup(e->getRequestGroupMan(), gid);
- if(!group) {
- SharedHandle<DownloadResult> dr =
- e->getRequestGroupMan()->findDownloadResult(gid);
- if(!dr) {
- throw DL_ABORT_EX
- (fmt("No file data is available for GID#%s",
- util::itos(gid).c_str()));
- } else {
- createFileEntry(files, dr->fileEntries.begin(), dr->fileEntries.end(),
- dr->totalLength, dr->pieceLength, dr->bitfield);
- }
- } else {
- const SharedHandle<PieceStorage>& ps = group->getPieceStorage();
- const SharedHandle<DownloadContext>& dctx = group->getDownloadContext();
- createFileEntry(files,
- group->getDownloadContext()->getFileEntries().begin(),
- group->getDownloadContext()->getFileEntries().end(),
- dctx->getTotalLength(),
- dctx->getPieceLength(),
- ps);
- }
- return files;
- }
- SharedHandle<ValueBase> GetUrisRpcMethod::process
- (const RpcRequest& req, DownloadEngine* e)
- {
- const String* gidParam = checkRequiredParam<String>(req, 0);
- a2_gid_t gid = str2Gid(gidParam);
- SharedHandle<RequestGroup> group =
- findRequestGroup(e->getRequestGroupMan(), gid);
- if(!group) {
- throw DL_ABORT_EX
- (fmt("No URI data is available for GID#%s",
- util::itos(gid).c_str()));
- }
- SharedHandle<List> uriList = List::g();
- // TODO Current implementation just returns first FileEntry's URIs.
- if(!group->getDownloadContext()->getFileEntries().empty()) {
- createUriEntry(uriList, group->getDownloadContext()->getFirstFileEntry());
- }
- return uriList;
- }
- #ifdef ENABLE_BITTORRENT
- SharedHandle<ValueBase> GetPeersRpcMethod::process
- (const RpcRequest& req, DownloadEngine* e)
- {
- const String* gidParam = checkRequiredParam<String>(req, 0);
- a2_gid_t gid = str2Gid(gidParam);
- SharedHandle<RequestGroup> group =
- findRequestGroup(e->getRequestGroupMan(), gid);
- if(!group) {
- throw DL_ABORT_EX
- (fmt("No peer data is available for GID#%s",
- util::itos(gid).c_str()));
- }
- SharedHandle<List> peers = List::g();
- const SharedHandle<BtObject>& btObject =
- e->getBtRegistry()->get(group->getGID());
- if(btObject) {
- assert(btObject->peerStorage);
- gatherPeer(peers, btObject->peerStorage);
- }
- return peers;
- }
- #endif // ENABLE_BITTORRENT
- SharedHandle<ValueBase> TellStatusRpcMethod::process
- (const RpcRequest& req, DownloadEngine* e)
- {
- const String* gidParam = checkRequiredParam<String>(req, 0);
- const List* keysParam = checkParam<List>(req, 1);
- a2_gid_t gid = str2Gid(gidParam);
- std::vector<std::string> keys;
- toStringList(std::back_inserter(keys), keysParam);
- SharedHandle<RequestGroup> group =
- e->getRequestGroupMan()->findRequestGroup(gid);
- SharedHandle<Dict> entryDict = Dict::g();
- if(!group) {
- group = e->getRequestGroupMan()->findReservedGroup(gid);
- if(!group) {
- SharedHandle<DownloadResult> ds =
- e->getRequestGroupMan()->findDownloadResult(gid);
- if(!ds) {
- throw DL_ABORT_EX
- (fmt("No such download for GID#%s",
- util::itos(gid).c_str()));
- }
- gatherStoppedDownload(entryDict, ds, keys);
- } else {
- if(requested_key(keys, KEY_STATUS)) {
- if(group->isPauseRequested()) {
- entryDict->put(KEY_STATUS, VLB_PAUSED);
- } else {
- entryDict->put(KEY_STATUS, VLB_WAITING);
- }
- }
- gatherProgress(entryDict, group, e, keys);
- }
- } else {
- if(requested_key(keys, KEY_STATUS)) {
- entryDict->put(KEY_STATUS, VLB_ACTIVE);
- }
- gatherProgress(entryDict, group, e, keys);
- }
- return entryDict;
- }
- SharedHandle<ValueBase> TellActiveRpcMethod::process
- (const RpcRequest& req, DownloadEngine* e)
- {
- const List* keysParam = checkParam<List>(req, 0);
- std::vector<std::string> keys;
- toStringList(std::back_inserter(keys), keysParam);
- SharedHandle<List> list = List::g();
- const std::deque<SharedHandle<RequestGroup> >& groups =
- e->getRequestGroupMan()->getRequestGroups();
- for(std::deque<SharedHandle<RequestGroup> >::const_iterator i =
- groups.begin(), eoi = groups.end(); i != eoi; ++i) {
- SharedHandle<Dict> entryDict = Dict::g();
- if(requested_key(keys, KEY_STATUS)) {
- entryDict->put(KEY_STATUS, VLB_ACTIVE);
- }
- gatherProgress(entryDict, *i, e, keys);
- list->append(entryDict);
- }
- return list;
- }
- const std::deque<SharedHandle<RequestGroup> >&
- TellWaitingRpcMethod::getItems(DownloadEngine* e) const
- {
- return e->getRequestGroupMan()->getReservedGroups();
- }
- void TellWaitingRpcMethod::createEntry
- (const SharedHandle<Dict>& entryDict,
- const SharedHandle<RequestGroup>& item,
- DownloadEngine* e,
- const std::vector<std::string>& keys) const
- {
- if(requested_key(keys, KEY_STATUS)) {
- if(item->isPauseRequested()) {
- entryDict->put(KEY_STATUS, VLB_PAUSED);
- } else {
- entryDict->put(KEY_STATUS, VLB_WAITING);
- }
- }
- gatherProgress(entryDict, item, e, keys);
- }
- const std::deque<SharedHandle<DownloadResult> >&
- TellStoppedRpcMethod::getItems(DownloadEngine* e) const
- {
- return e->getRequestGroupMan()->getDownloadResults();
- }
- void TellStoppedRpcMethod::createEntry
- (const SharedHandle<Dict>& entryDict,
- const SharedHandle<DownloadResult>& item,
- DownloadEngine* e,
- const std::vector<std::string>& keys) const
- {
- gatherStoppedDownload(entryDict, item, keys);
- }
- SharedHandle<ValueBase> PurgeDownloadResultRpcMethod::process
- (const RpcRequest& req, DownloadEngine* e)
- {
- e->getRequestGroupMan()->purgeDownloadResult();
- return VLB_OK;
- }
- SharedHandle<ValueBase> RemoveDownloadResultRpcMethod::process
- (const RpcRequest& req, DownloadEngine* e)
- {
- const String* gidParam = checkRequiredParam<String>(req, 0);
- a2_gid_t gid = str2Gid(gidParam);
- if(!e->getRequestGroupMan()->removeDownloadResult(gid)) {
- throw DL_ABORT_EX
- (fmt("Could not remove download result of GID#%s",
- util::itos(gid).c_str()));
- }
- return VLB_OK;
- }
- namespace {
- void changeOption
- (const SharedHandle<RequestGroup>& group,
- const Option& option,
- DownloadEngine* e)
- {
- const SharedHandle<DownloadContext>& dctx = group->getDownloadContext();
- const SharedHandle<Option>& grOption = group->getOption();
- grOption->merge(option);
- if(option.defined(PREF_CHECKSUM)) {
- const std::string& checksum = grOption->get(PREF_CHECKSUM);
- std::pair<Scip, Scip> p;
- util::divide(p, checksum.begin(), checksum.end(), '=');
- std::string hashType(p.first.first, p.first.second);
- util::lowercase(hashType);
- dctx->setDigest(hashType, util::fromHex(p.second.first, p.second.second));
- }
- if(option.defined(PREF_SELECT_FILE)) {
- SegList<int> sgl;
- util::parseIntSegments(sgl, grOption->get(PREF_SELECT_FILE));
- sgl.normalize();
- dctx->setFileFilter(sgl);
- }
- if(option.defined(PREF_SPLIT)) {
- group->setNumConcurrentCommand(grOption->getAsInt(PREF_SPLIT));
- }
- if(option.defined(PREF_MAX_CONNECTION_PER_SERVER)) {
- int maxConn = grOption->getAsInt(PREF_MAX_CONNECTION_PER_SERVER);
- const std::vector<SharedHandle<FileEntry> >& files = dctx->getFileEntries();
- for(std::vector<SharedHandle<FileEntry> >::const_iterator i = files.begin(),
- eoi = files.end(); i != eoi; ++i) {
- (*i)->setMaxConnectionPerServer(maxConn);
- }
- }
- if(option.defined(PREF_DIR) || option.defined(PREF_OUT)) {
- if(dctx->getFileEntries().size() == 1 &&
- !dctx->hasAttribute(bittorrent::BITTORRENT)) {
- dctx->getFirstFileEntry()->setPath
- (grOption->blank(PREF_OUT) ? A2STR::NIL :
- util::applyDir(grOption->get(PREF_DIR), grOption->get(PREF_OUT)));
- }
- }
- if(option.defined(PREF_DIR) || option.defined(PREF_INDEX_OUT)) {
- if(dctx->hasAttribute(bittorrent::BITTORRENT)) {
- std::istringstream indexOutIn(grOption->get(PREF_INDEX_OUT));
- std::vector<std::pair<size_t, std::string> > indexPaths =
- util::createIndexPaths(indexOutIn);
- for(std::vector<std::pair<size_t, std::string> >::const_iterator i =
- indexPaths.begin(), eoi = indexPaths.end(); i != eoi; ++i) {
- dctx->setFilePathWithIndex
- ((*i).first,
- util::applyDir(grOption->get(PREF_DIR), (*i).second));
- }
- }
- }
- if(option.defined(PREF_MAX_DOWNLOAD_LIMIT)) {
- group->setMaxDownloadSpeedLimit
- (grOption->getAsInt(PREF_MAX_DOWNLOAD_LIMIT));
- }
- if(option.defined(PREF_MAX_UPLOAD_LIMIT)) {
- group->setMaxUploadSpeedLimit(grOption->getAsInt(PREF_MAX_UPLOAD_LIMIT));
- }
- #ifdef ENABLE_BITTORRENT
- const SharedHandle<BtObject>& btObject =
- e->getBtRegistry()->get(group->getGID());
- if(btObject) {
- if(option.defined(PREF_BT_MAX_PEERS)) {
- btObject->btRuntime->setMaxPeers(grOption->getAsInt(PREF_BT_MAX_PEERS));
- }
- }
- #endif // ENABLE_BITTORRENT
- }
- } // namespace
- SharedHandle<ValueBase> ChangeOptionRpcMethod::process
- (const RpcRequest& req, DownloadEngine* e)
- {
- const String* gidParam = checkRequiredParam<String>(req, 0);
- const Dict* optsParam = checkRequiredParam<Dict>(req, 1);
- a2_gid_t gid = str2Gid(gidParam);
- SharedHandle<RequestGroup> group =
- e->getRequestGroupMan()->findRequestGroup(gid);
- Option option;
- if(group) {
- gatherChangeableOption(&option, optsParam);
- changeOption(group, option, e);
- } else {
- group = e->getRequestGroupMan()->findReservedGroup(gid);
- if(group) {
- gatherChangeableOptionForReserved(&option, optsParam);
- changeOption(group, option, e);
- } else {
- throw DL_ABORT_EX
- (fmt("Cannot change option for GID#%s", util::itos(gid).c_str()));
- }
- }
- return VLB_OK;
- }
- SharedHandle<ValueBase> ChangeGlobalOptionRpcMethod::process
- (const RpcRequest& req, DownloadEngine* e)
- {
- const Dict* optsParam = checkRequiredParam<Dict>(req, 0);
- Option option;
- gatherChangeableGlobalOption(&option, optsParam);
- e->getOption()->merge(option);
- if(option.defined(PREF_MAX_OVERALL_DOWNLOAD_LIMIT)) {
- e->getRequestGroupMan()->setMaxOverallDownloadSpeedLimit
- (option.getAsInt(PREF_MAX_OVERALL_DOWNLOAD_LIMIT));
- }
- if(option.defined(PREF_MAX_OVERALL_UPLOAD_LIMIT)) {
- e->getRequestGroupMan()->setMaxOverallUploadSpeedLimit
- (option.getAsInt(PREF_MAX_OVERALL_UPLOAD_LIMIT));
- }
- if(option.defined(PREF_MAX_CONCURRENT_DOWNLOADS)) {
- e->getRequestGroupMan()->setMaxSimultaneousDownloads
- (option.getAsInt(PREF_MAX_CONCURRENT_DOWNLOADS));
- e->getRequestGroupMan()->requestQueueCheck();
- }
- if(option.defined(PREF_MAX_DOWNLOAD_RESULT)) {
- e->getRequestGroupMan()->setMaxDownloadResult
- (option.getAsInt(PREF_MAX_DOWNLOAD_RESULT));
- }
- if(option.defined(PREF_LOG_LEVEL)) {
- LogFactory::setLogLevel(option.get(PREF_LOG_LEVEL));
- }
- if(option.defined(PREF_LOG)) {
- LogFactory::setLogFile(option.get(PREF_LOG));
- try {
- LogFactory::reconfigure();
- } catch(RecoverableException& e) {
- // TODO no exception handling
- }
- }
- return VLB_OK;
- }
- SharedHandle<ValueBase> GetVersionRpcMethod::process
- (const RpcRequest& req, DownloadEngine* e)
- {
- SharedHandle<Dict> result = Dict::g();
- result->put(KEY_VERSION, PACKAGE_VERSION);
- SharedHandle<List> featureList = List::g();
- const FeatureMap& features = FeatureConfig::getInstance()->getFeatures();
- for(FeatureMap::const_iterator i = features.begin(), eoi = features.end();
- i != eoi;++i){
- if((*i).second) {
- featureList->append((*i).first);
- }
- }
- result->put(KEY_ENABLED_FEATURES, featureList);
- return result;
- }
- namespace {
- void pushRequestOption
- (const SharedHandle<Dict>& dict,
- const SharedHandle<Option>& option,
- const SharedHandle<OptionParser>& oparser)
- {
- for(size_t i = 1, len = option::countOption(); i < len; ++i) {
- const Pref* pref = option::i2p(i);
- const SharedHandle<OptionHandler>& h = oparser->find(pref);
- if(h && h->getInitialOption() && option->defined(pref)) {
- dict->put(pref->k, option->get(pref));
- }
- }
- }
- } // namespace
- SharedHandle<ValueBase> GetOptionRpcMethod::process
- (const RpcRequest& req, DownloadEngine* e)
- {
- const String* gidParam = checkRequiredParam<String>(req, 0);
- a2_gid_t gid = str2Gid(gidParam);
- SharedHandle<RequestGroup> group =
- findRequestGroup(e->getRequestGroupMan(), gid);
- if(!group) {
- throw DL_ABORT_EX
- (fmt("Cannot get option for GID#%s",
- util::itos(gid).c_str()));
- }
- SharedHandle<Dict> result = Dict::g();
- SharedHandle<Option> option = group->getOption();
- pushRequestOption(result, option, getOptionParser());
- return result;
- }
- SharedHandle<ValueBase> GetGlobalOptionRpcMethod::process
- (const RpcRequest& req, DownloadEngine* e)
- {
- SharedHandle<Dict> result = Dict::g();
- for(size_t i = 0, len = e->getOption()->getTable().size(); i < len; ++i) {
- const Pref* pref = option::i2p(i);
- if(!e->getOption()->defined(pref)) {
- continue;
- }
- const SharedHandle<OptionHandler>& h = getOptionParser()->find(pref);
- if(h) {
- result->put(pref->k, e->getOption()->get(pref));
- }
- }
- return result;
- }
- SharedHandle<ValueBase> ChangePositionRpcMethod::process
- (const RpcRequest& req, DownloadEngine* e)
- {
- const String* gidParam = checkRequiredParam<String>(req, 0);
- const Integer* posParam = checkRequiredParam<Integer>(req, 1);
- const String* howParam = checkRequiredParam<String>(req, 2);
- a2_gid_t gid = str2Gid(gidParam);
- int pos = posParam->i();
- const std::string& howStr = howParam->s();
- RequestGroupMan::HOW how;
- if(howStr == "POS_SET") {
- how = RequestGroupMan::POS_SET;
- } else if(howStr == "POS_CUR") {
- how = RequestGroupMan::POS_CUR;
- } else if(howStr == "POS_END") {
- how = RequestGroupMan::POS_END;
- } else {
- throw DL_ABORT_EX("Illegal argument.");
- }
- size_t destPos =
- e->getRequestGroupMan()->changeReservedGroupPosition(gid, pos, how);
- SharedHandle<Integer> result = Integer::g(destPos);
- return result;
- }
- SharedHandle<ValueBase> GetSessionInfoRpcMethod::process
- (const RpcRequest& req, DownloadEngine* e)
- {
- SharedHandle<Dict> result = Dict::g();
- result->put(KEY_SESSION_ID, util::toHex(e->getSessionId()));
- return result;
- }
- SharedHandle<ValueBase> GetServersRpcMethod::process
- (const RpcRequest& req, DownloadEngine* e)
- {
- const String* gidParam = checkRequiredParam<String>(req, 0);
- a2_gid_t gid = str2Gid(gidParam);
- SharedHandle<RequestGroup> group =
- e->getRequestGroupMan()->findRequestGroup(gid);
- if(!group) {
- throw DL_ABORT_EX(fmt("No active download for GID#%s",
- util::itos(gid).c_str()));
- }
- const SharedHandle<DownloadContext>& dctx = group->getDownloadContext();
- const std::vector<SharedHandle<FileEntry> >& files = dctx->getFileEntries();
- SharedHandle<List> result = List::g();
- size_t index = 1;
- for(std::vector<SharedHandle<FileEntry> >::const_iterator fi = files.begin(),
- eoi = files.end(); fi != eoi; ++fi, ++index) {
- SharedHandle<Dict> fileEntry = Dict::g();
- fileEntry->put(KEY_INDEX, util::uitos(index));
- SharedHandle<List> servers = List::g();
- const std::deque<SharedHandle<Request> >& requests =
- (*fi)->getInFlightRequests();
- for(std::deque<SharedHandle<Request> >::const_iterator ri =requests.begin(),
- eoi = requests.end(); ri != eoi; ++ri) {
- SharedHandle<PeerStat> ps = (*ri)->getPeerStat();
- if(ps) {
- SharedHandle<Dict> serverEntry = Dict::g();
- serverEntry->put(KEY_URI, (*ri)->getUri());
- serverEntry->put(KEY_CURRENT_URI, (*ri)->getCurrentUri());
- serverEntry->put(KEY_DOWNLOAD_SPEED,
- util::uitos(ps->calculateDownloadSpeed()));
- servers->append(serverEntry);
- }
- }
- fileEntry->put(KEY_SERVERS, servers);
- result->append(fileEntry);
- }
- return result;
- }
- SharedHandle<ValueBase> ChangeUriRpcMethod::process
- (const RpcRequest& req, DownloadEngine* e)
- {
- const String* gidParam = checkRequiredParam<String>(req, 0);
- const Integer* indexParam = checkRequiredParam<Integer>(req, 1);
- const List* delUrisParam = checkRequiredParam<List>(req, 2);
- const List* addUrisParam = checkRequiredParam<List>(req, 3);
- const Integer* posParam = checkParam<Integer>(req, 4);
- a2_gid_t gid = str2Gid(gidParam);
- bool posGiven = checkPosParam(posParam);
- size_t pos = posGiven ? posParam->i() : 0;
- size_t index = indexParam->i()-1;
- SharedHandle<RequestGroup> group =
- findRequestGroup(e->getRequestGroupMan(), gid);
- if(!group) {
- throw DL_ABORT_EX
- (fmt("Cannot remove URIs from GID#%s",
- util::itos(gid).c_str()));
- }
- const SharedHandle<DownloadContext>& dctx = group->getDownloadContext();
- const std::vector<SharedHandle<FileEntry> >& files = dctx->getFileEntries();
- if(files.size() <= index) {
- throw DL_ABORT_EX(fmt("fileIndex is out of range"));
- }
- SharedHandle<FileEntry> s = files[index];
- size_t delcount = 0;
- for(List::ValueType::const_iterator i = delUrisParam->begin(),
- eoi = delUrisParam->end(); i != eoi; ++i) {
- const String* uri = downcast<String>(*i);
- if(uri && s->removeUri(uri->s())) {
- ++delcount;
- }
- }
- size_t addcount = 0;
- if(posGiven) {
- for(List::ValueType::const_iterator i = addUrisParam->begin(),
- eoi = addUrisParam->end(); i != eoi; ++i) {
- const String* uri = downcast<String>(*i);
- if(uri && s->insertUri(uri->s(), pos)) {
- ++addcount;
- ++pos;
- }
- }
- } else {
- for(List::ValueType::const_iterator i = addUrisParam->begin(),
- eoi = addUrisParam->end(); i != eoi; ++i) {
- const String* uri = downcast<String>(*i);
- if(uri && s->addUri(uri->s())) {
- ++addcount;
- }
- }
- }
- if(addcount && group->getPieceStorage()) {
- std::vector<Command*> commands;
- group->createNextCommand(commands, e);
- e->addCommand(commands);
- group->getSegmentMan()->recognizeSegmentFor(s);
- }
- SharedHandle<List> res = List::g();
- res->append(Integer::g(delcount));
- res->append(Integer::g(addcount));
- return res;
- }
- namespace {
- SharedHandle<ValueBase> goingShutdown
- (const RpcRequest& req, DownloadEngine* e, bool forceHalt)
- {
- // Schedule shutdown after 3seconds to give time to client to
- // receive RPC response.
- e->addRoutineCommand(new TimedHaltCommand(e->newCUID(), e, 3, forceHalt));
- A2_LOG_INFO("Scheduled shutdown in 3 seconds.");
- return VLB_OK;
- }
- } // namespace
- SharedHandle<ValueBase> ShutdownRpcMethod::process
- (const RpcRequest& req, DownloadEngine* e)
- {
- return goingShutdown(req, e, false);
- }
- SharedHandle<ValueBase> ForceShutdownRpcMethod::process
- (const RpcRequest& req, DownloadEngine* e)
- {
- return goingShutdown(req, e, true);
- }
- SharedHandle<ValueBase> GetGlobalStatRpcMethod::process
- (const RpcRequest& req, DownloadEngine* e)
- {
- const SharedHandle<RequestGroupMan>& rgman = e->getRequestGroupMan();
- TransferStat ts = rgman->calculateStat();
- SharedHandle<Dict> res = Dict::g();
- res->put(KEY_DOWNLOAD_SPEED, util::uitos(ts.downloadSpeed));
- res->put(KEY_UPLOAD_SPEED, util::uitos(ts.uploadSpeed));
- res->put(KEY_NUM_WAITING, util::uitos(rgman->getReservedGroups().size()));
- res->put(KEY_NUM_STOPPED, util::uitos(rgman->getDownloadResults().size()));
- res->put(KEY_NUM_ACTIVE, util::uitos(rgman->getRequestGroups().size()));
- return res;
- }
- SharedHandle<ValueBase> SystemMulticallRpcMethod::process
- (const RpcRequest& req, DownloadEngine* e)
- {
- const List* methodSpecs = checkRequiredParam<List>(req, 0);
- SharedHandle<List> list = List::g();
- for(List::ValueType::const_iterator i = methodSpecs->begin(),
- eoi = methodSpecs->end(); i != eoi; ++i) {
- const Dict* methodDict = downcast<Dict>(*i);
- if(!methodDict) {
- list->append(createErrorResponse
- (DL_ABORT_EX("system.multicall expected struct."), req));
- continue;
- }
- const String* methodName = downcast<String>(methodDict->get(KEY_METHOD_NAME));
- if(!methodName) {
- list->append(createErrorResponse
- (DL_ABORT_EX("Missing methodName."), req));
- continue;
- }
- if(methodName->s() == getMethodName()) {
- list->append(createErrorResponse
- (DL_ABORT_EX("Recursive system.multicall forbidden."), req));
- continue;
- }
- const SharedHandle<ValueBase>& tempParamsList = methodDict->get(KEY_PARAMS);
- SharedHandle<List> paramsList;
- if(downcast<List>(tempParamsList)) {
- paramsList = static_pointer_cast<List>(tempParamsList);
- } else {
- paramsList = List::g();
- }
- SharedHandle<RpcMethod> method = RpcMethodFactory::create(methodName->s());
- RpcRequest innerReq(methodName->s(), paramsList);
- innerReq.jsonRpc = req.jsonRpc;
- RpcResponse res = method->execute(innerReq, e);
- if(res.code == 0) {
- SharedHandle<List> l = List::g();
- l->append(res.param);
- list->append(l);
- } else {
- list->append(res.param);
- }
- }
- return list;
- }
- SharedHandle<ValueBase> NoSuchMethodRpcMethod::process
- (const RpcRequest& req, DownloadEngine* e)
- {
- throw DL_ABORT_EX(fmt("No such method: %s", req.methodName.c_str()));
- }
- } // namespace rpc
- } // namespace aria2
|