CMS 3D CMS Logo

ParameterSetRetriever.cc

Go to the documentation of this file.
00001 #include "EventFilter/Utilities/interface/ParameterSetRetriever.h"
00002 #include "EventFilter/Utilities/interface/Exception.h"
00003 #include "EventFilter/Utilities/interface/CurlUtils.h"
00004 #include "EventFilter/Utilities/interface/SquidNet.h"
00005 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00006 
00007 #include "curl/curl.h"
00008 #include <netdb.h>
00009 #include <sys/socket.h>        /* for AF_INET */
00010 
00011 #include <fstream>
00012 
00013 
00014 
00015 namespace evf{
00016   //______________________________________________________________________________
00017 
00018 
00019   const std::string ParameterSetRetriever::fileheading="file:";
00020   const std::string ParameterSetRetriever::dbheading  ="db:";  
00021   const std::string ParameterSetRetriever::webheading ="https://";  
00022 
00023   ParameterSetRetriever::ParameterSetRetriever(const std::string& in)
00024   {
00025     using std::string;
00026     using std::ostringstream; 
00027     using std::ifstream; 
00028 
00029 
00030 
00031     if (fileheading==in.substr(0,fileheading.size()))
00032       { 
00033         string filename=in.substr(fileheading.size());
00034         edm::LogInfo("psetRetriever")<<"filename is --> "<<filename<<" <--";
00035       
00036         string   line;
00037         ifstream configFile(filename.c_str());
00038         while(std::getline(configFile,line)) {
00039           pset+=line;
00040           pset+="\n";
00041         }
00042       }
00043     else if (webheading==in.substr(0,webheading.size()))
00044       {
00045         string hostname = getHostString(in);
00046         SquidNet sn(3128,"https://localhost:8000/RELEASE-NOTES.txt");    
00047         edm::LogInfo("psetRetriever")<<"Using cfg from " << in;
00048         CURL* han = curl_easy_init();
00049         if(han==0)
00050           {
00051             XCEPT_RAISE(evf::Exception,"could not create handle for web ParameterSet");     
00052           }
00053 
00054         struct curl_slist *headers=NULL; /* init to NULL is important */
00055         headers = curl_slist_append(headers, "Pragma:");
00056         curl_easy_setopt(han, CURLOPT_HTTPHEADER, headers);
00057         char error[CURL_ERROR_SIZE];
00058         if(sn.check())
00059           curl_easy_setopt(han, CURLOPT_PROXY, "localhost:3128");
00060 
00061         curl_easy_setopt(han, CURLOPT_URL, hostname.c_str());
00062         curl_easy_setopt(han, CURLOPT_VERBOSE);
00063         curl_easy_setopt(han, CURLOPT_NOSIGNAL);
00064         //      curl_easy_setopt(han, CURLOPT_TIMEOUT, 60.0L);
00065 
00066         curl_easy_setopt(han, CURLOPT_WRITEFUNCTION, &write_data);
00067         curl_easy_setopt(han, CURLOPT_WRITEDATA, &pset);
00068         curl_easy_setopt(han, CURLOPT_ERRORBUFFER, error);
00069         int success = curl_easy_perform(han);
00070         curl_slist_free_all(headers); /* free the header list */
00071         curl_easy_cleanup(han);
00072 
00073         if(success != 0)
00074           {
00075             ostringstream msg;
00076             msg <<  "could not get config from url " << in << " error #" 
00077                 << success << " " << error;
00078             XCEPT_RAISE(evf::Exception,msg.str().c_str());
00079           }
00080 
00081       }
00082     else if (dbheading==in.substr(0,dbheading.size()))
00083       {
00084         XCEPT_RAISE(evf::Exception,"db access for ParameterSet not yet implemented");
00085       } 
00086     else
00087       {
00088         edm::LogInfo("psetRetriever")<<"Using string cfg from RunControl or XML";
00089         pset = in;
00090       }
00091   }
00092 
00093 
00094   //______________________________________________________________________________
00095   std::string ParameterSetRetriever::getAsString() const
00096   {
00097     return pset;
00098   }
00099   std::string ParameterSetRetriever::getHostString(const std::string &in) const
00100   {
00101     using std::string;
00102     string innohttps = in.substr(webheading.size());
00103     string::size_type pos = innohttps.find(':',0);
00104     string host = innohttps.substr(0,pos);
00105     string path = innohttps.substr(pos);
00106     struct hostent *heb = gethostbyname(host.c_str());    
00107     struct hostent *he = gethostbyaddr(heb->h_addr_list[0], heb->h_length, heb->h_addrtype);
00108     string completehost = "https://";
00109     completehost += he->h_name;
00110     completehost += path;
00111     return completehost;
00112   }
00113 } //end namespace evf

Generated on Tue Jun 9 17:34:59 2009 for CMSSW by  doxygen 1.5.4