2009-05-15 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>

Added changeOption command. Currently, max-download-limit,
	max-upload-limit option are available in this command.
	* src/RequestGroup.h
	* src/XmlRpcMethod.cc
	* src/XmlRpcMethod.h
	* src/XmlRpcMethodFactory.cc
	* src/XmlRpcMethodImpl.cc
	* src/XmlRpcMethodImpl.h
	* test/XmlRpcMethodTest.cc
pull/1/head
Tatsuhiro Tsujikawa 2009-05-14 15:23:50 +00:00
parent 692c4eaf3e
commit 850458f7b1
8 changed files with 126 additions and 9 deletions

View File

@ -1,3 +1,15 @@
2009-05-15 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
Added changeOption command. Currently, max-download-limit,
max-upload-limit option are available in this command.
* src/RequestGroup.h
* src/XmlRpcMethod.cc
* src/XmlRpcMethod.h
* src/XmlRpcMethodFactory.cc
* src/XmlRpcMethodImpl.cc
* src/XmlRpcMethodImpl.h
* test/XmlRpcMethodTest.cc
2009-05-14 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net> 2009-05-14 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>
Added purgeDownloadResult command. Added purgeDownloadResult command.

View File

@ -473,10 +473,22 @@ public:
return _maxDownloadSpeedLimit; return _maxDownloadSpeedLimit;
} }
void setMaxDownloadSpeedLimit(unsigned int speed)
{
_maxDownloadSpeedLimit = speed;
}
unsigned int getMaxUploadSpeedLimit() const unsigned int getMaxUploadSpeedLimit() const
{ {
return _maxUploadSpeedLimit; return _maxUploadSpeedLimit;
} }
void setMaxUploadSpeedLimit(unsigned int speed)
{
_maxUploadSpeedLimit = speed;
}
static void resetGIDCounter() { _gidCounter = 0; }
}; };
typedef SharedHandle<RequestGroup> RequestGroupHandle; typedef SharedHandle<RequestGroup> RequestGroupHandle;

View File

@ -74,16 +74,20 @@ XmlRpcResponse XmlRpcMethod::execute
} }
} }
void XmlRpcMethod::gatherRequestOption
(const SharedHandle<Option>& option, const BDE& optionsDict) template<typename InputIterator>
static void gatherOption
(InputIterator first, InputIterator last,
const SharedHandle<Option>& option, const BDE& optionsDict,
const SharedHandle<OptionParser>& optionParser)
{ {
for(std::vector<std::string>::const_iterator i = listRequestOptions().begin(); for(; first != last; ++first) {
i != listRequestOptions().end(); ++i) { if(optionsDict.containsKey(*first)) {
if(optionsDict.containsKey(*i)) { const BDE& value = optionsDict[*first];
const BDE& value = optionsDict[*i]; SharedHandle<OptionHandler> optionHandler =
SharedHandle<OptionHandler> optionHandler = _optionParser->findByName(*i); optionParser->findByName(*first);
// header and index-out option can take array as value // header and index-out option can take array as value
if((*i == PREF_HEADER || *i == PREF_INDEX_OUT) && value.isList()) { if((*first == PREF_HEADER || *first == PREF_INDEX_OUT) && value.isList()){
for(BDE::List::const_iterator argiter = value.listBegin(); for(BDE::List::const_iterator argiter = value.listBegin();
argiter != value.listEnd(); ++argiter) { argiter != value.listEnd(); ++argiter) {
if((*argiter).isString()) { if((*argiter).isString()) {
@ -94,7 +98,32 @@ void XmlRpcMethod::gatherRequestOption
optionHandler->parse(*option.get(), value.s()); optionHandler->parse(*option.get(), value.s());
} }
} }
} }
}
void XmlRpcMethod::gatherRequestOption
(const SharedHandle<Option>& option, const BDE& optionsDict)
{
gatherOption(listRequestOptions().begin(), listRequestOptions().end(),
option, optionsDict, _optionParser);
}
const std::vector<std::string>& listChangeableOptions()
{
static const std::string OPTIONS[] = {
PREF_MAX_UPLOAD_LIMIT,
PREF_MAX_DOWNLOAD_LIMIT,
};
static std::vector<std::string> options
(&OPTIONS[0], &OPTIONS[arrayLength(OPTIONS)]);;
return options;
}
void XmlRpcMethod::gatherChangeableOption
(const SharedHandle<Option>& option, const BDE& optionsDict)
{
gatherOption(listChangeableOptions().begin(), listChangeableOptions().end(),
option, optionsDict, _optionParser);
} }
} // namespace xmlrpc } // namespace xmlrpc

View File

@ -64,6 +64,9 @@ protected:
void gatherRequestOption(const SharedHandle<Option>& option, void gatherRequestOption(const SharedHandle<Option>& option,
const BDE& optionsDict); const BDE& optionsDict);
void gatherChangeableOption(const SharedHandle<Option>& option,
const BDE& optionDict);
public: public:
XmlRpcMethod(); XmlRpcMethod();

View File

@ -62,6 +62,8 @@ XmlRpcMethodFactory::create(const std::string& methodName)
return SharedHandle<XmlRpcMethod>(new GetPeersXmlRpcMethod()); return SharedHandle<XmlRpcMethod>(new GetPeersXmlRpcMethod());
} else if(methodName == "aria2.tellActive") { } else if(methodName == "aria2.tellActive") {
return SharedHandle<XmlRpcMethod>(new TellActiveXmlRpcMethod()); return SharedHandle<XmlRpcMethod>(new TellActiveXmlRpcMethod());
} else if(methodName == "aria2.changeOption") {
return SharedHandle<XmlRpcMethod>(new ChangeOptionXmlRpcMethod());
} else if(methodName == "aria2.purgeDownloadResult") { } else if(methodName == "aria2.purgeDownloadResult") {
return SharedHandle<XmlRpcMethod>(new PurgeDownloadResultXmlRpcMethod()); return SharedHandle<XmlRpcMethod>(new PurgeDownloadResultXmlRpcMethod());
} else { } else {

View File

@ -60,6 +60,7 @@
#include "BtProgressInfoFile.h" #include "BtProgressInfoFile.h"
#include "BtRuntime.h" #include "BtRuntime.h"
#include "BtAnnounce.h" #include "BtAnnounce.h"
#include "prefs.h"
namespace aria2 { namespace aria2 {
@ -459,6 +460,34 @@ BDE PurgeDownloadResultXmlRpcMethod::process
return BDE("OK"); return BDE("OK");
} }
BDE ChangeOptionXmlRpcMethod::process
(const XmlRpcRequest& req, DownloadEngine* e)
{
const BDE& params = req._params;
assert(params.isList());
if(params.empty() || !params[0].isString()) {
throw DlAbortEx("GID is not provided.");
}
int32_t gid = Util::parseInt(params[0].s());
SharedHandle<RequestGroup> group = findRequestGroup(e->_requestGroupMan, gid);
if(group.isNull()) {
throw DlAbortEx
(StringFormat("Cannot change option for GID#%d", gid).str());
}
SharedHandle<Option> option(new Option(*group->getOption().get()));
if(params.size() > 1 && params[1].isDict()) {
gatherChangeableOption(option, params[1]);
}
if(option->defined(PREF_MAX_DOWNLOAD_LIMIT)) {
group->setMaxDownloadSpeedLimit(option->getAsInt(PREF_MAX_DOWNLOAD_LIMIT));
}
if(option->defined(PREF_MAX_UPLOAD_LIMIT)) {
group->setMaxUploadSpeedLimit(option->getAsInt(PREF_MAX_UPLOAD_LIMIT));
}
return BDE("OK");
}
BDE NoSuchMethodXmlRpcMethod::process BDE NoSuchMethodXmlRpcMethod::process
(const XmlRpcRequest& req, DownloadEngine* e) (const XmlRpcRequest& req, DownloadEngine* e)
{ {

View File

@ -91,6 +91,11 @@ protected:
virtual BDE process(const XmlRpcRequest& req, DownloadEngine* e); virtual BDE process(const XmlRpcRequest& req, DownloadEngine* e);
}; };
class ChangeOptionXmlRpcMethod:public XmlRpcMethod {
protected:
virtual BDE process(const XmlRpcRequest& req, DownloadEngine* e);
};
class NoSuchMethodXmlRpcMethod:public XmlRpcMethod { class NoSuchMethodXmlRpcMethod:public XmlRpcMethod {
protected: protected:
virtual BDE process(const XmlRpcRequest& req, DownloadEngine* e); virtual BDE process(const XmlRpcRequest& req, DownloadEngine* e);

View File

@ -14,6 +14,7 @@
#include "OptionHandler.h" #include "OptionHandler.h"
#include "XmlRpcRequest.h" #include "XmlRpcRequest.h"
#include "XmlRpcResponse.h" #include "XmlRpcResponse.h"
#include "prefs.h"
namespace aria2 { namespace aria2 {
@ -23,6 +24,7 @@ class XmlRpcMethodTest:public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE(XmlRpcMethodTest); CPPUNIT_TEST_SUITE(XmlRpcMethodTest);
CPPUNIT_TEST(testAddUri); CPPUNIT_TEST(testAddUri);
CPPUNIT_TEST(testChangeOption);
CPPUNIT_TEST(testNoSuchMethod); CPPUNIT_TEST(testNoSuchMethod);
CPPUNIT_TEST_SUITE_END(); CPPUNIT_TEST_SUITE_END();
private: private:
@ -31,6 +33,7 @@ private:
public: public:
void setUp() void setUp()
{ {
RequestGroup::resetGIDCounter();
_option.reset(new Option()); _option.reset(new Option());
_e.reset(new DownloadEngine(SharedHandle<EventPoll>(new SelectEventPoll()))); _e.reset(new DownloadEngine(SharedHandle<EventPoll>(new SelectEventPoll())));
_e->option = _option.get(); _e->option = _option.get();
@ -42,6 +45,7 @@ public:
void tearDown() {} void tearDown() {}
void testAddUri(); void testAddUri();
void testChangeOption();
void testNoSuchMethod(); void testNoSuchMethod();
}; };
@ -63,6 +67,27 @@ void XmlRpcMethodTest::testAddUri()
rgs.front()->getRemainingUris().front()); rgs.front()->getRemainingUris().front());
} }
void XmlRpcMethodTest::testChangeOption()
{
SharedHandle<RequestGroup> group
(new RequestGroup(_option, std::deque<std::string>()));
_e->_requestGroupMan->addReservedGroup(group);
ChangeOptionXmlRpcMethod m;
XmlRpcRequest req("aria2.changeOption", BDE::list());
req._params << BDE("1");
BDE opt = BDE::dict();
opt[PREF_MAX_DOWNLOAD_LIMIT] = BDE("100K");
opt[PREF_MAX_UPLOAD_LIMIT] = BDE("50K");
req._params << opt;
XmlRpcResponse res = m.execute(req, _e.get());
CPPUNIT_ASSERT_EQUAL(0, res._code);
CPPUNIT_ASSERT_EQUAL((unsigned int)100*1024,
group->getMaxDownloadSpeedLimit());
CPPUNIT_ASSERT_EQUAL((unsigned int)50*1024, group->getMaxUploadSpeedLimit());
}
void XmlRpcMethodTest::testNoSuchMethod() void XmlRpcMethodTest::testNoSuchMethod()
{ {
NoSuchMethodXmlRpcMethod m; NoSuchMethodXmlRpcMethod m;