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>
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 ="http://";
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,"http://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;
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
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);
00071 curl_easy_cleanup(han);
00072 han = 0;
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 han = curl_easy_init();
00083 if(han==0)
00084 {
00085 XCEPT_RAISE(evf::Exception,"could not create handle for web ParameterSet");
00086 }
00087 headers = NULL;
00088 headers = curl_slist_append(headers, "Pragma:");
00089 curl_easy_setopt(han, CURLOPT_HTTPHEADER, headers);
00090 if(sn.check())
00091 curl_easy_setopt(han, CURLOPT_PROXY, "localhost:3128");
00092 hostname = getHostString(in,"path");
00093 curl_easy_setopt(han, CURLOPT_URL, hostname.c_str());
00094 curl_easy_setopt(han, CURLOPT_VERBOSE,"");
00095 curl_easy_setopt(han, CURLOPT_NOSIGNAL,"");
00096
00097
00098 curl_easy_setopt(han, CURLOPT_WRITEFUNCTION, &write_data);
00099 curl_easy_setopt(han, CURLOPT_WRITEDATA, &pathIndexTable);
00100 curl_easy_setopt(han, CURLOPT_ERRORBUFFER, error);
00101 success = curl_easy_perform(han);
00102 curl_slist_free_all(headers);
00103 curl_easy_cleanup(han);
00104
00105 if(success != 0)
00106 {
00107 ostringstream msg;
00108 msg << "could not get pathindex table from url " << in << " error #"
00109 << success << " " << error;
00110 XCEPT_RAISE(evf::Exception,msg.str().c_str());
00111 }
00112
00113
00114 }
00115 else if (dbheading==in.substr(0,dbheading.size()))
00116 {
00117 XCEPT_RAISE(evf::Exception,"db access for ParameterSet not yet implemented");
00118 }
00119 else
00120 {
00121 edm::LogInfo("psetRetriever")<<"Using string cfg from RunControl or XML";
00122 pset = in;
00123 }
00124 }
00125
00126
00127
00128 std::string ParameterSetRetriever::getAsString() const
00129 {
00130 return pset;
00131 }
00132
00133 std::string ParameterSetRetriever::getPathTableAsString() const
00134 {
00135 return pathIndexTable;
00136 }
00137
00138
00139 std::string ParameterSetRetriever::getHostString(const std::string &in, std::string modifier) const
00140 {
00141 using std::string;
00142 string innohttp = in.substr(webheading.size());
00143 string::size_type pos = innohttp.find(':',0);
00144 string host = innohttp.substr(0,pos);
00145 string path = innohttp.substr(pos);
00146 if(modifier != "")
00147 {
00148 pos = path.find_last_of('.');
00149 if(pos != string::npos)
00150 {
00151 string upath = path.substr(0,pos);
00152 path = upath + '.' + modifier;
00153 }
00154 }
00155 struct hostent *heb = gethostbyname(host.c_str());
00156 struct hostent *he = gethostbyaddr(heb->h_addr_list[0], heb->h_length, heb->h_addrtype);
00157 string completehost = "http://";
00158 completehost += he->h_name;
00159 completehost += path;
00160 return completehost;
00161 }
00162 }