/* */ #include "MetalinkEntry.h" #include "Util.h" #include MetalinkEntry::MetalinkEntry(): file(0), maxConnections(-1) #ifdef ENABLE_MESSAGE_DIGEST , checksum(0), chunkChecksum(0) #endif // ENABLE_MESSAGE_DIGEST {} MetalinkEntry::~MetalinkEntry() {} class AddLocationPreference { private: Strings _locations; int32_t _preferenceToAdd; public: 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(binary_search(_locations.begin(), _locations.end(), res->location)) { res->preference += _preferenceToAdd; } } }; void MetalinkEntry::setLocationPreference(const Strings& locations, int32_t preferenceToAdd) { for_each(resources.begin(), resources.end(), AddLocationPreference(locations, preferenceToAdd)); } class AddProtocolPreference { private: const string& _protocol; int32_t _preferenceToAdd; public: AddProtocolPreference(const string& protocol, int32_t prefToAdd): _protocol(protocol), _preferenceToAdd(prefToAdd) {} void operator()(const MetalinkResourceHandle& res) const { if(_protocol == MetalinkResource::getTypeString(res->type)) { res->preference += _preferenceToAdd; } } }; void MetalinkEntry::setProtocolPreference(const string& protocol, int32_t preferenceToAdd) { for_each(resources.begin(), resources.end(), AddProtocolPreference(protocol, preferenceToAdd)); } class PrefOrder { public: bool operator()(const MetalinkResourceHandle& res1, const MetalinkResourceHandle& res2) { return res1->preference > res2->preference; } }; void MetalinkEntry::reorderResourcesByPreference() { random_shuffle(resources.begin(), resources.end()); sort(resources.begin(), resources.end(), PrefOrder()); } class Supported { public: bool operator()(const MetalinkResourceHandle& res) { switch(res->type) { case MetalinkResource::TYPE_FTP: case MetalinkResource::TYPE_HTTP: #ifdef ENABLE_SSL case MetalinkResource::TYPE_HTTPS: #endif // ENABLE_SSL #ifdef ENABLE_BITTORRENT case MetalinkResource::TYPE_BITTORRENT: #endif // ENABLE_BITTORRENT return true; default: return false; } } }; void MetalinkEntry::dropUnsupportedResource() { MetalinkResources::iterator split = partition(resources.begin(), resources.end(), Supported()); resources.erase(split, resources.end()); } FileEntries MetalinkEntry::toFileEntry(const MetalinkEntries& metalinkEntries) { FileEntries entries; for(MetalinkEntries::const_iterator itr = metalinkEntries.begin(); itr != metalinkEntries.end(); ++itr) { entries.push_back((*itr)->file); } return entries; }