181
void getPosParam(const RpcRequest& req, size_t posParamIndex,
182
bool& posGiven, size_t& pos)
182
bool checkPosParam(const Integer* posParam)
184
const Integer* p = req.getIntegerParam(posParamIndex);
185
if(posParam->i() >= 0) {
191
188
throw DL_ABORT_EX("Position must be greater than or equal to 0.");
199
a2_gid_t getRequiredGidParam
200
(const RpcRequest& req, size_t posParamIndex)
196
a2_gid_t str2Gid(const String* str)
202
const String* gidParam = req.getStringParam(posParamIndex);
204
return util::parseLLInt(gidParam->s());
206
throw DL_ABORT_EX(MSG_GID_NOT_PROVIDED);
199
return util::parseLLInt(str->s());
227
219
SharedHandle<ValueBase> AddUriRpcMethod::process
228
220
(const RpcRequest& req, DownloadEngine* e)
222
const List* urisParam = checkRequiredParam<List>(req, 0);
223
const Dict* optsParam = checkParam<Dict>(req, 1);
224
const Integer* posParam = checkParam<Integer>(req, 2);
230
226
std::vector<std::string> uris;
231
extractUris(std::back_inserter(uris), req.getListParam(0));
227
extractUris(std::back_inserter(uris), urisParam);
232
228
if(uris.empty()) {
233
229
throw DL_ABORT_EX("URI is not provided.");
236
232
SharedHandle<Option> requestOption(new Option(*e->getOption()));
237
gatherRequestOption(requestOption, req.getDictParam(1));
233
gatherRequestOption(requestOption, optsParam);
240
bool posGiven = false;
241
getPosParam(req, 2, posGiven, pos);
235
bool posGiven = checkPosParam(posParam);
236
size_t pos = posGiven ? posParam->i() : 0;
243
238
std::vector<SharedHandle<RequestGroup> > result;
244
239
createRequestGroupForUri(result, requestOption, uris,
268
263
SharedHandle<ValueBase> AddTorrentRpcMethod::process
269
264
(const RpcRequest& req, DownloadEngine* e)
271
const String* torrentParam = req.getStringParam(0);
273
throw DL_ABORT_EX("Torrent data is not provided.");
266
const String* torrentParam = checkRequiredParam<String>(req, 0);
267
const List* urisParam = checkParam<List>(req, 1);
268
const Dict* optsParam = checkParam<Dict>(req, 2);
269
const Integer* posParam = checkParam<Integer>(req, 3);
275
271
SharedHandle<String> tempTorrentParam;
276
272
if(req.jsonRpc) {
277
273
tempTorrentParam = String::g(Base64::decode(torrentParam->s()));
278
274
torrentParam = tempTorrentParam.get();
280
276
std::vector<std::string> uris;
281
extractUris(std::back_inserter(uris), req.getListParam(1));
277
extractUris(std::back_inserter(uris), urisParam);
283
279
SharedHandle<Option> requestOption(new Option(*e->getOption()));
284
gatherRequestOption(requestOption, req.getDictParam(2));
280
gatherRequestOption(requestOption, optsParam);
287
bool posGiven = false;
288
getPosParam(req, 3, posGiven, pos);
282
bool posGiven = checkPosParam(posParam);
283
size_t pos = posGiven ? posParam->i() : 0;
290
285
std::string filename = util::applyDir
291
286
(requestOption->get(PREF_DIR), getHexSha1(torrentParam->s())+".torrent");
315
310
SharedHandle<ValueBase> AddMetalinkRpcMethod::process
316
311
(const RpcRequest& req, DownloadEngine* e)
318
const String* metalinkParam = req.getStringParam(0);
320
throw DL_ABORT_EX("Metalink data is not provided.");
313
const String* metalinkParam = checkRequiredParam<String>(req, 0);
314
const Dict* optsParam = checkParam<Dict>(req, 1);
315
const Integer* posParam = checkParam<Integer>(req, 2);
322
317
SharedHandle<String> tempMetalinkParam;
323
318
if(req.jsonRpc) {
324
319
tempMetalinkParam = String::g(Base64::decode(metalinkParam->s()));
325
320
metalinkParam = tempMetalinkParam.get();
327
322
SharedHandle<Option> requestOption(new Option(*e->getOption()));
328
gatherRequestOption(requestOption, req.getDictParam(1));
323
gatherRequestOption(requestOption, optsParam);
331
bool posGiven = false;
332
getPosParam(req, 2, posGiven, pos);
325
bool posGiven = checkPosParam(posParam);
326
size_t pos = posGiven ? posParam->i() : 0;
334
328
std::vector<SharedHandle<RequestGroup> > result;
335
329
#ifdef ENABLE_MESSAGE_DIGEST
374
368
SharedHandle<ValueBase> removeDownload
375
369
(const RpcRequest& req, DownloadEngine* e, bool forceRemove)
377
a2_gid_t gid = getRequiredGidParam(req, 0);
371
const String* gidParam = checkRequiredParam<String>(req, 0);
373
a2_gid_t gid = str2Gid(gidParam);
379
374
SharedHandle<RequestGroup> group =
380
375
e->getRequestGroupMan()->findRequestGroup(gid);
446
441
SharedHandle<ValueBase> pauseDownload
447
442
(const RpcRequest& req, DownloadEngine* e, bool forcePause)
449
a2_gid_t gid = getRequiredGidParam(req, 0);
444
const String* gidParam = checkRequiredParam<String>(req, 0);
446
a2_gid_t gid = str2Gid(gidParam);
451
447
bool reserved = false;
452
448
SharedHandle<RequestGroup> group =
453
449
e->getRequestGroupMan()->findRequestGroup(gid);
519
515
SharedHandle<ValueBase> UnpauseRpcMethod::process
520
516
(const RpcRequest& req, DownloadEngine* e)
522
a2_gid_t gid = getRequiredGidParam(req, 0);
518
const String* gidParam = checkRequiredParam<String>(req, 0);
520
a2_gid_t gid = str2Gid(gidParam);
523
521
SharedHandle<RequestGroup> group =
524
522
e->getRequestGroupMan()->findReservedGroup(gid);
525
523
if(!group || !group->isPauseRequested()) {
580
578
template<typename InputIterator>
581
579
void createFileEntry
582
(const SharedHandle<List>& files, InputIterator first, InputIterator last)
580
(const SharedHandle<List>& files,
581
InputIterator first, InputIterator last,
582
const BitfieldMan* bf)
584
584
size_t index = 1;
585
585
for(; first != last; ++first, ++index) {
588
588
entry->put(KEY_PATH, (*first)->getPath());
589
589
entry->put(KEY_SELECTED, (*first)->isRequested()?VLB_TRUE:VLB_FALSE);
590
590
entry->put(KEY_LENGTH, util::uitos((*first)->getLength()));
591
uint64_t completedLength = bf->getOffsetCompletedLength
592
((*first)->getOffset(), (*first)->getLength());
593
entry->put(KEY_COMPLETED_LENGTH, util::uitos(completedLength));
592
595
SharedHandle<List> uriList = List::g();
593
596
createUriEntry(uriList, *first);
604
template<typename InputIterator>
606
(const SharedHandle<List>& files,
607
InputIterator first, InputIterator last,
608
uint64_t totalLength,
610
const std::string& bitfieldStr)
612
std::string bitfield = util::fromHex(bitfieldStr);
613
BitfieldMan bf(pieceLength, totalLength);
614
bf.setBitfield(reinterpret_cast<const unsigned char*>(bitfield.data()),
616
createFileEntry(files, first, last, &bf);
621
template<typename InputIterator>
623
(const SharedHandle<List>& files,
624
InputIterator first, InputIterator last,
625
uint64_t totalLength,
627
const SharedHandle<PieceStorage>& ps)
629
BitfieldMan bf(pieceLength, totalLength);
631
bf.setBitfield(ps->getBitfield(), ps->getBitfieldLength());
633
createFileEntry(files, first, last, &bf);
601
638
bool requested_key
602
639
(const std::vector<std::string>& keys, const std::string& k)
610
647
const SharedHandle<RequestGroup>& group,
611
648
const std::vector<std::string>& keys)
650
const SharedHandle<PieceStorage>& ps = group->getPieceStorage();
613
651
if(requested_key(keys, KEY_GID)) {
614
652
entryDict->put(KEY_GID, util::itos(group->getGID()));
637
675
entryDict->put(KEY_CONNECTIONS, util::uitos(group->getNumConnection()));
639
677
if(requested_key(keys, KEY_BITFIELD)) {
640
SharedHandle<PieceStorage> ps = group->getPieceStorage();
642
679
if(ps->getBitfieldLength() > 0) {
643
680
entryDict->put(KEY_BITFIELD,
671
708
if(requested_key(keys, KEY_FILES)) {
672
709
SharedHandle<List> files = List::g();
674
(files, dctx->getFileEntries().begin(), dctx->getFileEntries().end());
711
(files, dctx->getFileEntries().begin(), dctx->getFileEntries().end(),
712
dctx->getTotalLength(), dctx->getPieceLength(), ps);
675
713
entryDict->put(KEY_FILES, files);
677
715
if(requested_key(keys, KEY_DIR)) {
830
868
if(requested_key(keys, KEY_FILES)) {
831
869
SharedHandle<List> files = List::g();
832
createFileEntry(files, ds->fileEntries.begin(), ds->fileEntries.end());
870
createFileEntry(files, ds->fileEntries.begin(), ds->fileEntries.end(),
871
ds->totalLength, ds->pieceLength, ds->bitfieldStr);
833
872
entryDict->put(KEY_FILES, files);
835
874
if(requested_key(keys, KEY_TOTAL_LENGTH)) {
877
916
SharedHandle<ValueBase> GetFilesRpcMethod::process
878
917
(const RpcRequest& req, DownloadEngine* e)
880
a2_gid_t gid = getRequiredGidParam(req, 0);
919
const String* gidParam = checkRequiredParam<String>(req, 0);
921
a2_gid_t gid = str2Gid(gidParam);
881
922
SharedHandle<List> files = List::g();
882
923
SharedHandle<RequestGroup> group =
883
924
findRequestGroup(e->getRequestGroupMan(), gid);
889
930
(fmt("No file data is available for GID#%s",
890
931
util::itos(gid).c_str()));
892
createFileEntry(files, dr->fileEntries.begin(), dr->fileEntries.end());
933
createFileEntry(files, dr->fileEntries.begin(), dr->fileEntries.end(),
934
dr->totalLength, dr->pieceLength, dr->bitfieldStr);
937
const SharedHandle<PieceStorage>& ps = group->getPieceStorage();
938
const SharedHandle<DownloadContext>& dctx = group->getDownloadContext();
895
939
createFileEntry(files,
896
940
group->getDownloadContext()->getFileEntries().begin(),
897
group->getDownloadContext()->getFileEntries().end());
941
group->getDownloadContext()->getFileEntries().end(),
942
dctx->getTotalLength(),
943
dctx->getPieceLength(),
902
949
SharedHandle<ValueBase> GetUrisRpcMethod::process
903
950
(const RpcRequest& req, DownloadEngine* e)
905
a2_gid_t gid = getRequiredGidParam(req, 0);
952
const String* gidParam = checkRequiredParam<String>(req, 0);
954
a2_gid_t gid = str2Gid(gidParam);
906
955
SharedHandle<RequestGroup> group =
907
956
findRequestGroup(e->getRequestGroupMan(), gid);
922
971
SharedHandle<ValueBase> GetPeersRpcMethod::process
923
972
(const RpcRequest& req, DownloadEngine* e)
925
a2_gid_t gid = getRequiredGidParam(req, 0);
974
const String* gidParam = checkRequiredParam<String>(req, 0);
976
a2_gid_t gid = str2Gid(gidParam);
927
977
SharedHandle<RequestGroup> group =
928
978
findRequestGroup(e->getRequestGroupMan(), gid);
944
994
SharedHandle<ValueBase> TellStatusRpcMethod::process
945
995
(const RpcRequest& req, DownloadEngine* e)
947
a2_gid_t gid = getRequiredGidParam(req, 0);
949
const List* keysParam = req.getListParam(1);
997
const String* gidParam = checkRequiredParam<String>(req, 0);
998
const List* keysParam = checkParam<List>(req, 1);
1000
a2_gid_t gid = str2Gid(gidParam);
950
1001
std::vector<std::string> keys;
951
1002
toStringList(std::back_inserter(keys), keysParam);
987
1038
SharedHandle<ValueBase> TellActiveRpcMethod::process
988
1039
(const RpcRequest& req, DownloadEngine* e)
990
const List* keysParam = req.getListParam(0);
1041
const List* keysParam = checkParam<List>(req, 0);
991
1042
std::vector<std::string> keys;
992
1043
toStringList(std::back_inserter(keys), keysParam);
993
1044
SharedHandle<List> list = List::g();
1052
1103
SharedHandle<ValueBase> RemoveDownloadResultRpcMethod::process
1053
1104
(const RpcRequest& req, DownloadEngine* e)
1055
a2_gid_t gid = getRequiredGidParam(req, 0);
1106
const String* gidParam = checkRequiredParam<String>(req, 0);
1108
a2_gid_t gid = str2Gid(gidParam);
1056
1109
if(!e->getRequestGroupMan()->removeDownloadResult(gid)) {
1057
1110
throw DL_ABORT_EX
1058
1111
(fmt("Could not remove download result of GID#%s",
1064
1117
SharedHandle<ValueBase> ChangeOptionRpcMethod::process
1065
1118
(const RpcRequest& req, DownloadEngine* e)
1067
a2_gid_t gid = getRequiredGidParam(req, 0);
1120
const String* gidParam = checkRequiredParam<String>(req, 0);
1121
const Dict* optsParam = checkRequiredParam<Dict>(req, 1);
1123
a2_gid_t gid = str2Gid(gidParam);
1069
1124
SharedHandle<RequestGroup> group =
1070
1125
findRequestGroup(e->getRequestGroupMan(), gid);
1074
1129
util::itos(gid).c_str()));
1076
1131
SharedHandle<Option> option(new Option());
1077
const Dict* optionsParam = req.getDictParam(1);
1079
gatherChangeableOption(option, optionsParam);
1080
applyChangeableOption(group->getOption().get(), option.get());
1081
if(option->defined(PREF_MAX_DOWNLOAD_LIMIT)) {
1082
group->setMaxDownloadSpeedLimit
1083
(option->getAsInt(PREF_MAX_DOWNLOAD_LIMIT));
1085
if(option->defined(PREF_MAX_UPLOAD_LIMIT)) {
1086
group->setMaxUploadSpeedLimit(option->getAsInt(PREF_MAX_UPLOAD_LIMIT));
1132
gatherChangeableOption(option, optsParam);
1133
applyChangeableOption(group->getOption().get(), option.get());
1134
if(option->defined(PREF_MAX_DOWNLOAD_LIMIT)) {
1135
group->setMaxDownloadSpeedLimit
1136
(option->getAsInt(PREF_MAX_DOWNLOAD_LIMIT));
1138
if(option->defined(PREF_MAX_UPLOAD_LIMIT)) {
1139
group->setMaxUploadSpeedLimit(option->getAsInt(PREF_MAX_UPLOAD_LIMIT));
1088
1141
#ifdef ENABLE_BITTORRENT
1089
BtObject btObject = e->getBtRegistry()->get(group->getGID());
1090
if(!btObject.isNull()) {
1091
if(option->defined(PREF_BT_MAX_PEERS)) {
1092
btObject.btRuntime_->setMaxPeers(option->getAsInt(PREF_BT_MAX_PEERS));
1142
BtObject btObject = e->getBtRegistry()->get(group->getGID());
1143
if(!btObject.isNull()) {
1144
if(option->defined(PREF_BT_MAX_PEERS)) {
1145
btObject.btRuntime_->setMaxPeers(option->getAsInt(PREF_BT_MAX_PEERS));
1095
1148
#endif // ENABLE_BITTORRENT
1100
1152
SharedHandle<ValueBase> ChangeGlobalOptionRpcMethod::process
1101
1153
(const RpcRequest& req, DownloadEngine* e)
1103
const Dict* optionsParam = req.getDictParam(0);
1155
const Dict* optsParam = checkRequiredParam<Dict>(req, 0);
1107
1157
SharedHandle<Option> option(new Option());
1108
gatherChangeableGlobalOption(option, optionsParam);
1158
gatherChangeableGlobalOption(option, optsParam);
1109
1159
applyChangeableGlobalOption(e->getOption(), option.get());
1111
1161
if(option->defined(PREF_MAX_OVERALL_DOWNLOAD_LIMIT)) {
1171
1221
SharedHandle<ValueBase> GetOptionRpcMethod::process
1172
1222
(const RpcRequest& req, DownloadEngine* e)
1174
a2_gid_t gid = getRequiredGidParam(req, 0);
1224
const String* gidParam = checkRequiredParam<String>(req, 0);
1226
a2_gid_t gid = str2Gid(gidParam);
1176
1227
SharedHandle<RequestGroup> group =
1177
1228
findRequestGroup(e->getRequestGroupMan(), gid);
1203
1254
SharedHandle<ValueBase> ChangePositionRpcMethod::process
1204
1255
(const RpcRequest& req, DownloadEngine* e)
1206
a2_gid_t gid = getRequiredGidParam(req, 0);
1207
const Integer* posParam = req.getIntegerParam(1);
1208
const String* howParam = req.getStringParam(2);
1257
const String* gidParam = checkRequiredParam<String>(req, 0);
1258
const Integer* posParam = checkRequiredParam<Integer>(req, 1);
1259
const String* howParam = checkRequiredParam<String>(req, 2);
1210
if(!posParam || !howParam) {
1211
throw DL_ABORT_EX("Illegal argument.");
1261
a2_gid_t gid = str2Gid(gidParam);
1213
1262
int pos = posParam->i();
1214
1263
const std::string& howStr = howParam->s();
1215
1264
RequestGroupMan::HOW how;
1239
1288
SharedHandle<ValueBase> GetServersRpcMethod::process
1240
1289
(const RpcRequest& req, DownloadEngine* e)
1242
a2_gid_t gid = getRequiredGidParam(req, 0);
1291
const String* gidParam = checkRequiredParam<String>(req, 0);
1293
a2_gid_t gid = str2Gid(gidParam);
1243
1294
SharedHandle<RequestGroup> group =
1244
1295
e->getRequestGroupMan()->findRequestGroup(gid);
1278
1329
SharedHandle<ValueBase> ChangeUriRpcMethod::process
1279
1330
(const RpcRequest& req, DownloadEngine* e)
1281
a2_gid_t gid = getRequiredGidParam(req, 0);
1282
const Integer* indexParam = req.getIntegerParam(1);
1283
const List* delUrisParam = req.getListParam(2);
1284
const List* addUrisParam = req.getListParam(3);
1285
if(!indexParam || !delUrisParam || ! addUrisParam) {
1286
throw DL_ABORT_EX("Bad request");
1289
bool posGiven = false;
1290
getPosParam(req, 4, posGiven, pos);
1332
const String* gidParam = checkRequiredParam<String>(req, 0);
1333
const Integer* indexParam = checkRequiredParam<Integer>(req, 1);
1334
const List* delUrisParam = checkRequiredParam<List>(req, 2);
1335
const List* addUrisParam = checkRequiredParam<List>(req, 3);
1336
const Integer* posParam = checkParam<Integer>(req, 4);
1338
a2_gid_t gid = str2Gid(gidParam);
1339
bool posGiven = checkPosParam(posParam);
1340
size_t pos = posGiven ? posParam->i() : 0;
1292
1341
size_t index = indexParam->i()-1;
1293
1342
SharedHandle<RequestGroup> group =
1294
1343
findRequestGroup(e->getRequestGroupMan(), gid);
1306
1355
size_t delcount = 0;
1307
1356
for(List::ValueType::const_iterator i = delUrisParam->begin(),
1308
1357
eoi = delUrisParam->end(); i != eoi; ++i) {
1309
const String* uri = asString(*i);
1358
const String* uri = downcast<String>(*i);
1310
1359
if(uri && s->removeUri(uri->s())) {
1316
1365
for(List::ValueType::const_iterator i = addUrisParam->begin(),
1317
1366
eoi = addUrisParam->end(); i != eoi; ++i) {
1318
const String* uri = asString(*i);
1367
const String* uri = downcast<String>(*i);
1319
1368
if(uri && s->insertUri(uri->s(), pos)) {
1325
1374
for(List::ValueType::const_iterator i = addUrisParam->begin(),
1326
1375
eoi = addUrisParam->end(); i != eoi; ++i) {
1327
const String* uri = asString(*i);
1376
const String* uri = downcast<String>(*i);
1328
1377
if(uri && s->addUri(uri->s())) {
1383
1432
SharedHandle<ValueBase> SystemMulticallRpcMethod::process
1384
1433
(const RpcRequest& req, DownloadEngine* e)
1386
const List* methodSpecs = req.getListParam(0);
1388
throw DL_ABORT_EX("Illegal argument. One item list is expected.");
1435
const List* methodSpecs = checkRequiredParam<List>(req, 0);
1390
1436
SharedHandle<List> list = List::g();
1391
1437
for(List::ValueType::const_iterator i = methodSpecs->begin(),
1392
1438
eoi = methodSpecs->end(); i != eoi; ++i) {
1393
const Dict* methodDict = asDict(*i);
1439
const Dict* methodDict = downcast<Dict>(*i);
1394
1440
if(!methodDict) {
1395
1441
list->append(createErrorResponse
1396
1442
(DL_ABORT_EX("system.multicall expected struct."), req));
1399
const String* methodName = asString(methodDict->get(KEY_METHOD_NAME));
1445
const String* methodName = downcast<String>(methodDict->get(KEY_METHOD_NAME));
1400
1446
if(!methodName) {
1401
1447
list->append(createErrorResponse
1402
1448
(DL_ABORT_EX("Missing methodName."), req));
1410
1456
const SharedHandle<ValueBase>& tempParamsList = methodDict->get(KEY_PARAMS);
1411
1457
SharedHandle<List> paramsList;
1412
if(asList(tempParamsList)) {
1458
if(downcast<List>(tempParamsList)) {
1413
1459
paramsList = static_pointer_cast<List>(tempParamsList);
1415
1461
paramsList = List::g();