From 8cba9bc24ce69884df603d6f825c279695c62c87 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Tue, 13 Nov 2007 13:49:10 +0000 Subject: [PATCH] 2007-11-13 Tatsuhiro Tsujikawa Now --metalink-location accepts a comma-deliminated list of locations. * src/MetalinkEntry.{h, cc} * test/MetalinkEntryTest.cc * src/version_usage.cc * src/Metalink2RequestGroup.cc * src/Xml2MetalinkProcessor.cc * test/Xml2MetalinkProcessorTest.cc * doc/aria2c.1.txt * doc/aria2c.1 * src/Util.cc (toUpper)(toLower): Rewritten. --- ChangeLog | 14 ++++++++++++ TODO | 6 +++-- doc/aria2c.1 | 8 +++---- doc/aria2c.1.txt | 7 +++--- src/Metalink2RequestGroup.cc | 4 +++- src/MetalinkEntry.cc | 22 +++++++++++------- src/MetalinkEntry.h | 2 +- src/Util.cc | 18 ++------------- src/Xml2MetalinkProcessor.cc | 2 +- src/version_usage.cc | 4 +++- test/MetalinkEntryTest.cc | 38 +++++++++++++++++++++++++------ test/Xml2MetalinkProcessorTest.cc | 4 ++-- 12 files changed, 83 insertions(+), 46 deletions(-) diff --git a/ChangeLog b/ChangeLog index 4df3afcb..90ee6313 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,17 @@ +2007-11-13 Tatsuhiro Tsujikawa + + Now --metalink-location accepts a comma-deliminated list of locations. + * src/MetalinkEntry.{h, cc} + * test/MetalinkEntryTest.cc + * src/version_usage.cc + * src/Metalink2RequestGroup.cc + * src/Xml2MetalinkProcessor.cc + * test/Xml2MetalinkProcessorTest.cc + * doc/aria2c.1.txt + * doc/aria2c.1 + + * src/Util.cc (toUpper)(toLower): Rewritten. + 2007-11-13 Tatsuhiro Tsujikawa Added the ability to detect duplicate download entry which is about to diff --git a/TODO b/TODO index 063579d1..f6c8a6a7 100644 --- a/TODO +++ b/TODO @@ -55,5 +55,7 @@ DownloadFailureException .... RequestGroup should halt. FatalException .... Program should abort. --- remaining features to be implemented for 0.12.0 release -* improve --metalink-location field +-- remaining issues to be implemented for 0.12.0 release +* Update man page +* Update translation +* Test configuration(without torrent/messagedigest/ssh etc) diff --git a/doc/aria2c.1 b/doc/aria2c.1 index bd2d1273..b674faf0 100644 --- a/doc/aria2c.1 +++ b/doc/aria2c.1 @@ -1,11 +1,11 @@ .\" Title: aria2c .\" Author: .\" Generator: DocBook XSL Stylesheets v1.73.1 -.\" Date: 10/29/2007 +.\" Date: 11/13/2007 .\" Manual: .\" Source: .\" -.TH "ARIA2C" "1" "10/29/2007" "" "" +.TH "ARIA2C" "1" "11/13/2007" "" "" .\" disable hyphenation .nh .\" disable justification (adjust text to left margin only) @@ -382,9 +382,9 @@ The language of the file to download\. The operating system of the file to download\. .RE .PP -\-\-metalink\-location=LOCATION +\-\-metalink\-location=LOCATION[,\&...] .RS 4 -The location of the prefered server\. +The location of the preferred server\. A comma\-deliminated list of locations is acceptable\. .RE .PP \-\-follow\-metalink=true|false diff --git a/doc/aria2c.1.txt b/doc/aria2c.1.txt index dab168d5..2c6e8332 100644 --- a/doc/aria2c.1.txt +++ b/doc/aria2c.1.txt @@ -292,9 +292,10 @@ http://host/image[000-100:2].img --metalink-os=OS:: The operating system of the file to download. - --metalink-location=LOCATION:: - The location of the prefered server. - + --metalink-location=LOCATION[,...]:: + The location of the preferred server. + A comma-deliminated list of locations is acceptable. + --follow-metalink=true|false:: Set to false to prevent aria2 from entering Metalink mode even if the filename of diff --git a/src/Metalink2RequestGroup.cc b/src/Metalink2RequestGroup.cc index e62e5e38..cdd2cf72 100644 --- a/src/Metalink2RequestGroup.cc +++ b/src/Metalink2RequestGroup.cc @@ -113,7 +113,9 @@ RequestGroups Metalink2RequestGroup::generate(const string& metalinkFile) itr++, ++count) { MetalinkEntryHandle& entry = *itr; if(_option->defined(PREF_METALINK_LOCATION)) { - entry->setLocationPreference(_option->get(PREF_METALINK_LOCATION), 100); + Strings locations; + Util::slice(locations, _option->get(PREF_METALINK_LOCATION), ',', true); + entry->setLocationPreference(locations, 100); } if(useIndex) { if(find(selectIndexes.begin(), selectIndexes.end(), count+1) == selectIndexes.end()) { diff --git a/src/MetalinkEntry.cc b/src/MetalinkEntry.cc index bfdbb5b2..bfd447f8 100644 --- a/src/MetalinkEntry.cc +++ b/src/MetalinkEntry.cc @@ -49,22 +49,28 @@ MetalinkEntry::~MetalinkEntry() {} class AddLocationPreference { private: - string location; - int32_t preferenceToAdd; + Strings _locations; + int32_t _preferenceToAdd; public: - AddLocationPreference(const string& location, int32_t preferenceToAdd): - location(location), preferenceToAdd(preferenceToAdd) {} + AddLocationPreference(const Strings& locations, int32_t preferenceToAdd): + _locations(locations), _preferenceToAdd(preferenceToAdd) + { + transform(_locations.begin(), _locations.end(), _locations.begin(), Util::toUpper); + sort(_locations.begin(), _locations.end()); + } void operator()(MetalinkResourceHandle& res) { - if(res->location == location) { - res->preference += preferenceToAdd; + if(binary_search(_locations.begin(), _locations.end(), res->location)) { + res->preference += _preferenceToAdd; } } }; -void MetalinkEntry::setLocationPreference(const string& location, int32_t preferenceToAdd) { +void MetalinkEntry::setLocationPreference(const Strings& locations, + int32_t preferenceToAdd) +{ for_each(resources.begin(), resources.end(), - AddLocationPreference(location, preferenceToAdd)); + AddLocationPreference(locations, preferenceToAdd)); } class PrefOrder { diff --git a/src/MetalinkEntry.h b/src/MetalinkEntry.h index a115194a..9111a676 100644 --- a/src/MetalinkEntry.h +++ b/src/MetalinkEntry.h @@ -95,7 +95,7 @@ public: void reorderResourcesByPreference(); - void setLocationPreference(const string& location, int32_t preferenceToAdd); + void setLocationPreference(const Strings& locations, int32_t preferenceToAdd); static FileEntries toFileEntry(const MetalinkEntries& metalinkEntries); }; diff --git a/src/Util.cc b/src/Util.cc index c7704605..525fbdcf 100644 --- a/src/Util.cc +++ b/src/Util.cc @@ -541,29 +541,15 @@ string Util::randomAlpha(int32_t length, const RandomizerHandle& randomizer) { return str; } -class UpperCase { -public: - void operator()(char& ch) { - ch = toupper(ch); - } -}; - string Util::toUpper(const string& src) { string temp = src; - for_each(temp.begin(), temp.end(), UpperCase()); + transform(temp.begin(), temp.end(), temp.begin(), ::toupper); return temp; } -class LowerCase { -public: - void operator()(char& ch) { - ch = tolower(ch); - } -}; - string Util::toLower(const string& src) { string temp = src; - for_each(temp.begin(), temp.end(), LowerCase()); + transform(temp.begin(), temp.end(), temp.begin(), ::tolower); return temp; } diff --git a/src/Xml2MetalinkProcessor.cc b/src/Xml2MetalinkProcessor.cc index 19cec2c8..87c34c33 100644 --- a/src/Xml2MetalinkProcessor.cc +++ b/src/Xml2MetalinkProcessor.cc @@ -224,7 +224,7 @@ MetalinkResourceHandle Xml2MetalinkProcessor::getResource(const string& xpath) { } else { resource->preference = STRTOLL(pref.c_str()); } - resource->location = Util::trim(xmlAttribute(node, "location")); + resource->location = Util::toUpper(Util::trim(xmlAttribute(node, "location"))); resource->url = Util::trim(xmlContent(node)); diff --git a/src/version_usage.cc b/src/version_usage.cc index 4be2832f..3e7bea6e 100644 --- a/src/version_usage.cc +++ b/src/version_usage.cc @@ -256,7 +256,9 @@ void showUsage() { cout << _(" --metalink-version=VERSION The version of the file to download.") << endl; cout << _(" --metalink-language=LANGUAGE The language of the file to download.") << endl; cout << _(" --metalink-os=OS The operating system of the file to download.") << endl; - cout << _(" --metalink-location=LOCATION The location of the prefered server.") << endl; + cout << _(" --metalink-location=LOCATION[,...] The location of the preferred server.\n" + " A comma-deliminated list of locations is\n" + " acceptable.") << endl; cout << _(" --follow-metalink=true|false Set to false to prevent aria2 from\n" " entering Metalink mode even if the filename of\n" " the downloaded file ends with .metalink.\n" diff --git a/test/MetalinkEntryTest.cc b/test/MetalinkEntryTest.cc index 3cde0d4e..a38189cc 100644 --- a/test/MetalinkEntryTest.cc +++ b/test/MetalinkEntryTest.cc @@ -8,6 +8,7 @@ class MetalinkEntryTest:public CppUnit::TestFixture { CPPUNIT_TEST_SUITE(MetalinkEntryTest); CPPUNIT_TEST(testDropUnsupportedResource); CPPUNIT_TEST(testReorderResourcesByPreference); + CPPUNIT_TEST(testSetLocationPreference); CPPUNIT_TEST_SUITE_END(); private: @@ -19,13 +20,14 @@ public: void testDropUnsupportedResource(); void testReorderResourcesByPreference(); + void testSetLocationPreference(); }; CPPUNIT_TEST_SUITE_REGISTRATION( MetalinkEntryTest ); -MetalinkEntry* createTestEntry() { - MetalinkEntry* entry = new MetalinkEntry(); +MetalinkEntryHandle createTestEntry() { + MetalinkEntryHandle entry = new MetalinkEntry(); MetalinkResource* res1 = new MetalinkResource(); res1->url = "ftp://myhost/aria2.tar.bz2"; res1->type = MetalinkResource::TYPE_FTP; @@ -39,17 +41,17 @@ MetalinkEntry* createTestEntry() { MetalinkResource* res3 = new MetalinkResource(); res3->url = "http://myhost/aria2.torrent"; res3->type = MetalinkResource::TYPE_BITTORRENT; - res3->location = "al"; + res3->location = "AL"; res3->preference = 60; MetalinkResource* res4 = new MetalinkResource(); res4->url = "http://myhost/aria2.ext"; res4->type = MetalinkResource::TYPE_NOT_SUPPORTED; - res4->location = "ad"; + res4->location = "AD"; res4->preference = 10; MetalinkResource* res5 = new MetalinkResource(); res5->url = "https://myhost/aria2.tar.bz2"; res5->type = MetalinkResource::TYPE_HTTPS; - res5->location = "jp"; + res5->location = "JP"; res5->preference = 90; entry->resources.push_back(res1); @@ -61,7 +63,7 @@ MetalinkEntry* createTestEntry() { } void MetalinkEntryTest::testDropUnsupportedResource() { - MetalinkEntry* entry = createTestEntry(); + MetalinkEntryHandle entry = createTestEntry(); entry->dropUnsupportedResource(); #if defined ENABLE_SSL && ENABLE_BITTORRENT @@ -88,7 +90,7 @@ void MetalinkEntryTest::testDropUnsupportedResource() { } void MetalinkEntryTest::testReorderResourcesByPreference() { - MetalinkEntry* entry = createTestEntry(); + MetalinkEntryHandle entry = createTestEntry(); entry->reorderResourcesByPreference(); @@ -98,3 +100,25 @@ void MetalinkEntryTest::testReorderResourcesByPreference() { CPPUNIT_ASSERT_EQUAL((int32_t)50, entry->resources.at(3)->preference); CPPUNIT_ASSERT_EQUAL((int32_t)10, entry->resources.at(4)->preference); } + +void MetalinkEntryTest::testSetLocationPreference() +{ + MetalinkEntryHandle entry = createTestEntry(); + + const char* locationsSrc[] = { "jp", "al", "RO" }; + + Strings locations(&locationsSrc[0], &locationsSrc[3]); + + entry->setLocationPreference(locations, 100); + + CPPUNIT_ASSERT_EQUAL(string("RO"), entry->resources[0]->location); + CPPUNIT_ASSERT_EQUAL((int32_t)150, entry->resources[0]->preference); + CPPUNIT_ASSERT_EQUAL(string("AT"), entry->resources[1]->location); + CPPUNIT_ASSERT_EQUAL((int32_t)100, entry->resources[1]->preference); + CPPUNIT_ASSERT_EQUAL(string("AL"), entry->resources[2]->location); + CPPUNIT_ASSERT_EQUAL((int32_t)160, entry->resources[2]->preference); + CPPUNIT_ASSERT_EQUAL(string("AD"), entry->resources[3]->location); + CPPUNIT_ASSERT_EQUAL((int32_t)10, entry->resources[3]->preference); + CPPUNIT_ASSERT_EQUAL(string("JP"), entry->resources[4]->location); + CPPUNIT_ASSERT_EQUAL((int32_t)190, entry->resources[4]->preference); +} diff --git a/test/Xml2MetalinkProcessorTest.cc b/test/Xml2MetalinkProcessorTest.cc index 27437668..ddafb861 100644 --- a/test/Xml2MetalinkProcessorTest.cc +++ b/test/Xml2MetalinkProcessorTest.cc @@ -46,7 +46,7 @@ void Xml2MetalinkProcessorTest::testParseFile() { MetalinkResources::iterator resourceItr1 = entry1->resources.begin(); MetalinkResourceHandle resource1 = *resourceItr1; CPPUNIT_ASSERT_EQUAL(MetalinkResource::TYPE_FTP, resource1->type); - CPPUNIT_ASSERT_EQUAL(string("jp"), resource1->location); + CPPUNIT_ASSERT_EQUAL(string("JP"), resource1->location); CPPUNIT_ASSERT_EQUAL((int32_t)100, resource1->preference); CPPUNIT_ASSERT_EQUAL(string("ftp://ftphost/aria2-0.5.2.tar.bz2"), resource1->url); @@ -55,7 +55,7 @@ void Xml2MetalinkProcessorTest::testParseFile() { resourceItr1++; MetalinkResourceHandle resource2 = *resourceItr1; CPPUNIT_ASSERT_EQUAL(MetalinkResource::TYPE_HTTP, resource2->type); - CPPUNIT_ASSERT_EQUAL(string("us"), resource2->location); + CPPUNIT_ASSERT_EQUAL(string("US"), resource2->location); CPPUNIT_ASSERT_EQUAL((int32_t)100, resource2->preference); CPPUNIT_ASSERT_EQUAL(string("http://httphost/aria2-0.5.2.tar.bz2"), resource2->url);