Back to index

nordugrid-arc-nox  1.1.0~rc6
resource.cpp
Go to the documentation of this file.
00001 #ifdef HAVE_CONFIG_H
00002 #include <config.h>
00003 #endif
00004 
00005 #include "resource.h"
00006 #include <arc/URL.h>
00007 #include <arc/message/PayloadSOAP.h>
00008 #include <map>
00009 
00010 
00011 namespace GridScheduler {
00012 
00013 
00014 Resource::Resource() 
00015 {
00016 
00017 }
00018 
00019 Resource::Resource(const std::string &url_str, std::map<std::string, std::string> &cli_config) 
00020 {
00021     url = url_str;
00022     ns["a-rex"]="http://www.nordugrid.org/schemas/a-rex";
00023     ns["bes-factory"]="http://schemas.ggf.org/bes/2006/08/bes-factory";
00024     ns["deleg"]="http://www.nordugrid.org/schemas/delegation";
00025     ns["wsa"]="http://www.w3.org/2005/08/addressing";
00026     ns["jsdl"]="http://schemas.ggf.org/jsdl/2005/11/jsdl";
00027     ns["wsrf-bf"]="http://docs.oasis-open.org/wsrf/bf-2";
00028     ns["wsrf-r"]="http://docs.oasis-open.org/wsrf/r-2";
00029     ns["wsrf-rw"]="http://docs.oasis-open.org/wsrf/rw-2";
00030     ns["ibes"]="http://www.nordugrid.org/schemas/ibes";
00031     ns["sched"]="http://www.nordugrid.org/schemas/sched";
00032 
00033     Arc::URL url(url_str);
00034     
00035     if (url.Protocol() == "https") {
00036         cfg.AddPrivateKey(cli_config["PrivateKey"]);
00037         cfg.AddCertificate(cli_config["CertificatePath"]);
00038         cfg.AddCAFile(cli_config["CACertificatePath"]);
00039     }
00040     
00041     client = new Arc::ClientSOAP(cfg, url, 60);
00042 }
00043 
00044 
00045 Resource::~Resource(void) 
00046 {
00047     //if (client) delete client;
00048 }
00049 
00050 bool Resource::refresh(void) 
00051 {
00052     // TODO ClientSOAP refresh if the connection is wrong
00053     //
00054 
00055     if (client) delete client;
00056     Arc::URL u(url);
00057     client = new Arc::ClientSOAP(cfg, u, 60);
00058     std::cout << "Resource refreshed: " << url << std::endl;
00059 
00060     return true;
00061 }
00062 
00063 
00064 const std::string Resource::CreateActivity(const Arc::XMLNode &jsdl) 
00065 {
00066     std::string jobid, faultstring;
00067     Arc::PayloadSOAP request(ns);
00068     request.NewChild("bes-factory:CreateActivity").NewChild("bes-factory:ActivityDocument").NewChild(jsdl);
00069 
00070     Arc::PayloadSOAP* response;
00071         
00072     Arc::MCC_Status status = client->process(&request, &response);
00073 
00074     if (!status) {
00075         std::cerr << "Request failed" << std::endl;
00076         if(response) {
00077            std::string str;
00078            response->GetXML(str);
00079            std::cout << str << std::endl;
00080            delete response;
00081         }
00082         return "";
00083      };
00084 
00085      if (!response) {
00086          std::cerr << "No response" << std::endl;
00087          return "";
00088      };
00089 
00090     Arc::XMLNode id, fs;
00091     (*response)["CreateActivityResponse"]["ActivityIdentifier"].New(id);
00092     (*response)["Fault"]["faultstring"].New(fs);
00093     id.GetDoc(jobid);
00094     faultstring=(std::string)fs;
00095     if (faultstring=="")
00096       return jobid;
00097     return "";
00098 }
00099 
00100 const std::string Resource::GetActivityStatus(const std::string &arex_job_id) 
00101 {
00102     std::string state, substate, faultstring;
00103     Arc::PayloadSOAP* response;
00104 
00105     // TODO: better error handling
00106 
00107     try {
00108         Arc::PayloadSOAP request(ns);
00109         request.NewChild("bes-factory:GetActivityStatuses").NewChild(Arc::XMLNode(arex_job_id));
00110 
00111         Arc::MCC_Status status = client->process(&request, &response);
00112         if (!status || !response) {
00113             return "Unknown";
00114         }
00115     } catch (...) { 
00116         return "Unknown";
00117     }
00118 
00119     Arc::XMLNode st, fs;
00120     (*response)["GetActivityStatusesResponse"]["Response"]["ActivityStatus"].New(st);
00121     state = (std::string)st.Attribute("state");
00122     Arc::XMLNode sst;
00123     (*response)["GetActivityStatusesResponse"]["Response"]["ActivityStatus"]["state"].New(sst);
00124     substate = (std::string)sst;
00125 
00126     faultstring=(std::string)fs;
00127     if (faultstring!="")
00128       std::cerr << "ERROR" << std::endl;
00129     else if (state=="")
00130       std::cerr << "The job status could not be retrieved." << std::endl;
00131     else {
00132       return substate;
00133     }
00134     return "";
00135 }
00136 
00137 bool Resource::TerminateActivity(const std::string &arex_job_id) 
00138 {
00139     std::cout << "kill this job: " << arex_job_id << std::endl; 
00140     std::string state, substate, faultstring;
00141     Arc::PayloadSOAP* response;
00142       
00143     // TODO: better error handling
00144 
00145     try {
00146         Arc::PayloadSOAP request(ns);
00147         request.NewChild("bes-factory:TerminateActivities").NewChild(Arc::XMLNode(arex_job_id));
00148 
00149         Arc::MCC_Status status = client->process(&request, &response);
00150         if(!status || !response) {
00151             return false;
00152         }
00153     } catch (...) { 
00154         return false;
00155     }
00156 
00157     Arc::XMLNode cancelled, fs;
00158     (*response)["TerminateActivitiesResponse"]["Response"]["Terminated"].New(cancelled);
00159     std::string result = (std::string)cancelled;
00160     if (result=="true") {
00161         return true;
00162     }
00163     else {
00164         return false;
00165     }
00166 }
00167 
00168 
00169 Resource&  Resource::operator=( const  Resource& r )
00170 {
00171    if ( this != &r ) {
00172       id = r.id;
00173       url = r.url;
00174       client = r.client;
00175       ns = r.ns;
00176       cfg = r.cfg;
00177    }
00178 
00179    return *this;
00180 }
00181 
00182 Resource::Resource( const Resource& r)
00183 {
00184     id = r.id;
00185     url = r.url;
00186     client = r.client;
00187     ns = r.ns;
00188     cfg = r.cfg;
00189 }
00190 
00191 
00192 
00193 } // Namespace
00194 
00195 
00196 
00197