Back to index

nordugrid-arc-nox  1.1.0~rc6
DataPoint.cpp
Go to the documentation of this file.
00001 // -*- indent-tabs-mode: nil -*-
00002 
00003 #ifdef HAVE_CONFIG_H
00004 #include <config.h>
00005 #endif
00006 
00007 #include <list>
00008 
00009 #include <arc/Logger.h>
00010 #include <arc/UserConfig.h>
00011 #include <arc/XMLNode.h>
00012 #include <arc/data/DataPoint.h>
00013 #include <arc/data/DataHandle.h>
00014 #include <arc/loader/FinderLoader.h>
00015 
00016 namespace Arc {
00017 
00018   Logger DataPoint::logger(Logger::rootLogger, "DataPoint");
00019 
00020   DataPoint::DataPoint(const URL& url, const UserConfig& usercfg)
00021     : url(url),
00022       usercfg(usercfg),
00023       size((unsigned long long int)(-1)),
00024       created(-1),
00025       valid(-1),
00026       triesleft(1),
00027       failure_code(DataStatus::UnknownError),
00028       cache(url.Option("cache") != "no") {
00029     // add standard options applicable to all protocols
00030     valid_url_options.clear();
00031     valid_url_options.push_back("cache");
00032     valid_url_options.push_back("readonly");
00033     valid_url_options.push_back("blocksize");
00034     valid_url_options.push_back("checksum");
00035     valid_url_options.push_back("exec");
00036     valid_url_options.push_back("preserve");
00037     valid_url_options.push_back("overwrite");
00038     valid_url_options.push_back("threads");
00039     valid_url_options.push_back("secure");
00040     valid_url_options.push_back("autodir");
00041   }
00042 
00043   DataPoint::~DataPoint() {}
00044 
00045   const URL& DataPoint::GetURL() const {
00046     return url;
00047   }
00048 
00049   const UserConfig& DataPoint::GetUserConfig() const {
00050     return usercfg;
00051   }
00052 
00053   std::string DataPoint::str() const {
00054     return url.str();
00055   }
00056 
00057   DataPoint::operator bool() const {
00058 
00059     if (!url)
00060       return false;
00061       
00062     // URL option validation. Subclasses which do not want to validate
00063     // URL options should override this method.
00064     std::map<std::string, std::string> options = url.Options();
00065     for (std::map<std::string, std::string>::iterator i = options.begin(); i != options.end(); i++) {
00066       bool valid = false; 
00067       for (std::list<std::string>::const_iterator j = valid_url_options.begin(); j != valid_url_options.end(); j++) {
00068         if (i->first == *j) valid = true;
00069       }
00070       if (!valid) {
00071         logger.msg(ERROR, "Invalid URL option: %s", i->first);
00072         return false;
00073       }
00074     }
00075     return true;
00076   }
00077 
00078   bool DataPoint::operator!() const {
00079     return !((bool)*this);
00080   }
00081 
00082   DataStatus DataPoint::GetFailureReason() const {
00083     return failure_code;
00084   }
00085   
00086   bool DataPoint::Cache() const {
00087     return cache;
00088   }
00089   
00090   bool DataPoint::CheckSize() const {
00091     return (size != (unsigned long long int)(-1));
00092   }
00093 
00094   void DataPoint::SetSize(const unsigned long long int val) {
00095     size = val;
00096   }
00097 
00098   unsigned long long int DataPoint::GetSize() const {
00099     return size;
00100   }
00101 
00102   bool DataPoint::CheckCheckSum() const {
00103     return (!checksum.empty());
00104   }
00105 
00106   void DataPoint::SetCheckSum(const std::string& val) {
00107     checksum = val;
00108   }
00109 
00110   const std::string& DataPoint::GetCheckSum() const {
00111     return checksum;
00112   }
00113     
00114   const std::string DataPoint::DefaultCheckSum() const {
00115     return std::string("cksum");
00116   }
00117 
00118   bool DataPoint::CheckCreated() const {
00119     return (created != -1);
00120   }
00121 
00122   void DataPoint::SetCreated(const Time& val) {
00123     created = val;
00124   }
00125 
00126   const Time& DataPoint::GetCreated() const {
00127     return created;
00128   }
00129 
00130   bool DataPoint::CheckValid() const {
00131     return (valid != -1);
00132   }
00133 
00134   void DataPoint::SetValid(const Time& val) {
00135     valid = val;
00136   }
00137 
00138   const Time& DataPoint::GetValid() const {
00139     return valid;
00140   }
00141 
00142   int DataPoint::GetTries() const {
00143     return triesleft;
00144   }
00145 
00146   void DataPoint::SetTries(const int n) {
00147     triesleft = std::max(0, n);
00148   }
00149 
00150   void DataPoint::NextTry() {
00151     if(triesleft) --triesleft;
00152   }
00153 
00154   void DataPoint::SetMeta(const DataPoint& p) {
00155     if (!CheckSize())
00156       SetSize(p.GetSize());
00157     if (!CheckCheckSum())
00158       SetCheckSum(p.GetCheckSum());
00159     if (!CheckCreated())
00160       SetCreated(p.GetCreated());
00161     if (!CheckValid())
00162       SetValid(p.GetValid());
00163   }
00164 
00165   bool DataPoint::CompareMeta(const DataPoint& p) const {
00166     if (CheckSize() && p.CheckSize())
00167       if (GetSize() != p.GetSize())
00168         return false;
00169     if (CheckCheckSum() && p.CheckCheckSum())
00170       // TODO: compare checksums properly
00171       if (strcasecmp(GetCheckSum().c_str(), p.GetCheckSum().c_str()))
00172         return false;
00173     if (CheckValid() && p.CheckValid())
00174       if (GetValid() != p.GetValid())
00175         return false;
00176     return true;
00177   }
00178 
00179   DataPointLoader::DataPointLoader()
00180     : Loader(BaseConfig().MakeConfig(Config()).Parent()) {}
00181 
00182   DataPointLoader::~DataPointLoader() {}
00183 
00184   DataPoint* DataPointLoader::load(const URL& url, const UserConfig& usercfg) {
00185     DataPointPluginArgument arg(url, usercfg);
00186     factory_->load(FinderLoader::GetLibrariesList(), "HED:DMC");
00187     return factory_->GetInstance<DataPoint>("HED:DMC", &arg, false);
00188   }
00189 
00190   DataPointLoader DataHandle::loader;
00191 
00192 } // namespace Arc