Simplified RequestGroupMan::fillRequestGroupFromReserver

pull/43/head
Tatsuhiro Tsujikawa 2012-12-22 23:37:44 +09:00
parent b017e80438
commit 616cd9e75f
5 changed files with 93 additions and 25 deletions

View File

@ -43,7 +43,7 @@ class Dependency {
public: public:
virtual ~Dependency() {} virtual ~Dependency() {}
virtual bool resolve() = 0; virtual bool resolve() = 0; // throw()
}; };
} // namespace aria2 } // namespace aria2

View File

@ -208,7 +208,11 @@ namespace {
void notifyDownloadEvent void notifyDownloadEvent
(const std::string& event, const SharedHandle<RequestGroup>& group) (const std::string& event, const SharedHandle<RequestGroup>& group)
{ {
SingletonHolder<Notifier>::instance()->notifyDownloadEvent(event, group); // Check NULL to make unit test easier.
Notifier* notifier = SingletonHolder<Notifier>::instance();
if(notifier) {
notifier->notifyDownloadEvent(event, group);
}
} }
} // namespace } // namespace
@ -486,6 +490,7 @@ void RequestGroupMan::fillRequestGroupFromReserver(DownloadEngine* e)
while(count < num && (uriListParser_ || resitr != reservedGroups_.end())) { while(count < num && (uriListParser_ || resitr != reservedGroups_.end())) {
if(uriListParser_ && resitr == reservedGroups_.end()) { if(uriListParser_ && resitr == reservedGroups_.end()) {
std::vector<SharedHandle<RequestGroup> > groups; std::vector<SharedHandle<RequestGroup> > groups;
// May throw exception
bool ok = createRequestGroupFromUriListParser(groups, option_, bool ok = createRequestGroupFromUriListParser(groups, option_,
uriListParser_.get()); uriListParser_.get());
if(ok) { if(ok) {
@ -501,7 +506,6 @@ void RequestGroupMan::fillRequestGroupFromReserver(DownloadEngine* e)
} }
SharedHandle<RequestGroup> groupToAdd = (*resitr).second; SharedHandle<RequestGroup> groupToAdd = (*resitr).second;
std::vector<Command*> commands; std::vector<Command*> commands;
try {
if((rpc_ && groupToAdd->isPauseRequested()) || if((rpc_ && groupToAdd->isPauseRequested()) ||
!groupToAdd->isDependencyResolved()) { !groupToAdd->isDependencyResolved()) {
++resitr; ++resitr;
@ -514,15 +518,9 @@ void RequestGroupMan::fillRequestGroupFromReserver(DownloadEngine* e)
groupToAdd->dropPieceStorage(); groupToAdd->dropPieceStorage();
configureRequestGroup(groupToAdd); configureRequestGroup(groupToAdd);
groupToAdd->setRequestGroupMan(this); groupToAdd->setRequestGroupMan(this);
try {
createInitialCommand(groupToAdd, commands, e); createInitialCommand(groupToAdd, commands, e);
if(commands.empty()) {
requestQueueCheck();
}
groupToAdd->setState(RequestGroup::STATE_ACTIVE);
requestGroups_.push_back(groupToAdd->getGID(), groupToAdd);
++count; ++count;
e->addCommand(commands);
commands.clear();
} catch(RecoverableException& ex) { } catch(RecoverableException& ex) {
A2_LOG_ERROR_EX(EX_EXCEPTION_CAUGHT, ex); A2_LOG_ERROR_EX(EX_EXCEPTION_CAUGHT, ex);
A2_LOG_DEBUG("Deleting temporal commands."); A2_LOG_DEBUG("Deleting temporal commands.");
@ -530,12 +528,17 @@ void RequestGroupMan::fillRequestGroupFromReserver(DownloadEngine* e)
commands.clear(); commands.clear();
A2_LOG_DEBUG("Commands deleted"); A2_LOG_DEBUG("Commands deleted");
groupToAdd->setLastErrorCode(ex.getErrorCode()); groupToAdd->setLastErrorCode(ex.getErrorCode());
// We add groupToAdd to e in order to it is processed in // We add groupToAdd to e later in order to it is processed in
// removeStoppedGroup(). // removeStoppedGroup().
}
if(commands.empty()) {
requestQueueCheck();
} else {
e->addCommand(commands);
}
groupToAdd->setState(RequestGroup::STATE_ACTIVE); groupToAdd->setState(RequestGroup::STATE_ACTIVE);
requestGroups_.push_back(groupToAdd->getGID(), groupToAdd); requestGroups_.push_back(groupToAdd->getGID(), groupToAdd);
requestQueueCheck();
}
util::executeHookByOptName(groupToAdd, e->getOption(), util::executeHookByOptName(groupToAdd, e->getOption(),
PREF_ON_DOWNLOAD_START); PREF_ON_DOWNLOAD_START);
notifyDownloadEvent(Notifier::ON_DOWNLOAD_START, groupToAdd); notifyDownloadEvent(Notifier::ON_DOWNLOAD_START, groupToAdd);

View File

@ -4,6 +4,7 @@
#include <cppunit/extensions/HelperMacros.h> #include <cppunit/extensions/HelperMacros.h>
#include "TestUtil.h"
#include "prefs.h" #include "prefs.h"
#include "DownloadContext.h" #include "DownloadContext.h"
#include "RequestGroup.h" #include "RequestGroup.h"
@ -16,6 +17,8 @@
#include "array_fun.h" #include "array_fun.h"
#include "RecoverableException.h" #include "RecoverableException.h"
#include "util.h" #include "util.h"
#include "DownloadEngine.h"
#include "SelectEventPoll.h"
namespace aria2 { namespace aria2 {
@ -27,13 +30,27 @@ class RequestGroupManTest : public CppUnit::TestFixture {
CPPUNIT_TEST(testLoadServerStat); CPPUNIT_TEST(testLoadServerStat);
CPPUNIT_TEST(testSaveServerStat); CPPUNIT_TEST(testSaveServerStat);
CPPUNIT_TEST(testChangeReservedGroupPosition); CPPUNIT_TEST(testChangeReservedGroupPosition);
CPPUNIT_TEST(testFillRequestGroupFromReserver);
CPPUNIT_TEST_SUITE_END(); CPPUNIT_TEST_SUITE_END();
private: private:
SharedHandle<DownloadEngine> e_;
SharedHandle<Option> option_; SharedHandle<Option> option_;
SharedHandle<RequestGroupMan> rgman_;
public: public:
void setUp() void setUp()
{ {
option_.reset(new Option()); option_.reset(new Option());
option_->put(PREF_PIECE_LENGTH, "1048576");
// To enable paused RequestGroup
option_->put(PREF_ENABLE_RPC, A2_V_TRUE);
File(option_->get(PREF_DIR)).mkdirs();
e_.reset
(new DownloadEngine(SharedHandle<EventPoll>(new SelectEventPoll())));
e_->setOption(option_.get());
rgman_ = SharedHandle<RequestGroupMan>
(new RequestGroupMan(std::vector<SharedHandle<RequestGroup> >(),
3, option_.get()));
e_->setRequestGroupMan(rgman_);
} }
void testIsSameFileBeingDownloaded(); void testIsSameFileBeingDownloaded();
@ -41,6 +58,7 @@ public:
void testLoadServerStat(); void testLoadServerStat();
void testSaveServerStat(); void testSaveServerStat();
void testChangeReservedGroupPosition(); void testChangeReservedGroupPosition();
void testFillRequestGroupFromReserver();
}; };
@ -190,4 +208,25 @@ void RequestGroupManTest::testChangeReservedGroupPosition()
} }
} }
void RequestGroupManTest::testFillRequestGroupFromReserver()
{
SharedHandle<RequestGroup> rgs[] = {
createRequestGroup(0, 0, "foo1", "http://host/foo1", util::copy(option_)),
createRequestGroup(0, 0, "foo2", "http://host/foo2", util::copy(option_)),
createRequestGroup(0, 0, "foo3", "http://host/foo3", util::copy(option_)),
// Intentionally same path/URI for first RequestGroup and set
// length explicitly to do duplicate filename check.
createRequestGroup(0, 10, "foo1", "http://host/foo1", util::copy(option_)),
createRequestGroup(0, 0, "foo4", "http://host/foo4", util::copy(option_)),
createRequestGroup(0, 0, "foo5", "http://host/foo5", util::copy(option_))
};
rgs[1]->setPauseRequested(true);
for(SharedHandle<RequestGroup>* i = vbegin(rgs); i != vend(rgs); ++i) {
rgman_->addReservedGroup(*i);
}
rgman_->fillRequestGroupFromReserver(e_.get());
CPPUNIT_ASSERT_EQUAL((size_t)2, rgman_->getReservedGroups().size());
}
} // namespace aria2 } // namespace aria2

View File

@ -17,6 +17,9 @@
#include "util.h" #include "util.h"
#include "RequestGroupMan.h" #include "RequestGroupMan.h"
#include "RequestGroup.h" #include "RequestGroup.h"
#include "DownloadContext.h"
#include "Option.h"
#include "FileEntry.h"
#ifdef ENABLE_MESSAGE_DIGEST #ifdef ENABLE_MESSAGE_DIGEST
# include "message_digest_helper.h" # include "message_digest_helper.h"
#endif // ENABLE_MESSAGE_DIGEST #endif // ENABLE_MESSAGE_DIGEST
@ -124,4 +127,21 @@ SharedHandle<RequestGroup> getReservedGroup
return (*i).second; return (*i).second;
} }
SharedHandle<RequestGroup> createRequestGroup(int32_t pieceLength,
int64_t totalLength,
const std::string& path,
const std::string& uri,
const SharedHandle<Option>& opt)
{
SharedHandle<DownloadContext> dctx(new DownloadContext(pieceLength,
totalLength,
path));
std::vector<std::string> uris;
uris.push_back(uri);
dctx->getFirstFileEntry()->addUris(uris.begin(), uris.end());
SharedHandle<RequestGroup> group(new RequestGroup(GroupId::create(), opt));
group->setDownloadContext(dctx);
return group;
}
} // namespace aria2 } // namespace aria2

View File

@ -12,6 +12,7 @@ namespace aria2 {
class MessageDigest; class MessageDigest;
class RequestGroupMan; class RequestGroupMan;
class RequestGroup; class RequestGroup;
class Option;
void createFile(const std::string& filename, size_t length); void createFile(const std::string& filename, size_t length);
@ -64,4 +65,9 @@ SharedHandle<RequestGroup> findReservedGroup
SharedHandle<RequestGroup> getReservedGroup SharedHandle<RequestGroup> getReservedGroup
(const SharedHandle<RequestGroupMan>& rgman, size_t index); (const SharedHandle<RequestGroupMan>& rgman, size_t index);
SharedHandle<RequestGroup> createRequestGroup(int32_t pieceLength,
int64_t totalLength,
const std::string& path,
const std::string& uri,
const SharedHandle<Option>& opt);
} // namespace aria2 } // namespace aria2