Back to index

nordugrid-arc-nox  1.1.0~rc6
test.cpp
Go to the documentation of this file.
00001 #ifdef HAVE_CONFIG_H
00002 #include <config.h>
00003 #endif
00004 
00005 #include <string>
00006 #include <iostream>
00007 #include <fstream>
00008 #include <signal.h>
00009 #include <unistd.h>
00010 
00011 #include <arc/ArcConfig.h>
00012 #include <arc/Logger.h>
00013 #include <arc/XMLNode.h>
00014 #include <arc/message/MCCLoader.h>
00015 #include <arc/message/SOAPEnvelope.h>
00016 #include <arc/message/PayloadSOAP.h>
00017 #include <arc/delegation/DelegationInterface.h>
00018 #include <arc/infosys/InformationInterface.h>
00019 #include <arc/StringConv.h>
00020 #include <arc/XMLNode.h>
00021 
00022 #include <glibmm.h>
00023 
00024 int main(void) {
00025   signal(SIGTTOU,SIG_IGN);
00026   signal(SIGTTIN,SIG_IGN);
00027   signal(SIGPIPE,SIG_IGN);
00028 
00029   Arc::Logger logger(Arc::Logger::rootLogger, "Test");
00030   Arc::LogStream logcerr(std::cerr);
00031   Arc::Logger::rootLogger.addDestination(logcerr);
00032 
00033   // Load service chain
00034   logger.msg(Arc::INFO, "Creating service side chain");
00035   Arc::Config service_config("service.xml");
00036   if(!service_config) {
00037     logger.msg(Arc::ERROR, "Failed to load service configuration");
00038     return -1;
00039   };
00040   Arc::MCCLoader service_loader(service_config);
00041   logger.msg(Arc::INFO, "Service side MCCs are loaded");
00042 //  for(;;) sleep(10);
00043   logger.msg(Arc::INFO, "Creating client side chain");
00044 
00045 
00046   // Create client chain
00047   Arc::Config client_config("client.xml");
00048   if(!client_config) {
00049     logger.msg(Arc::ERROR, "Failed to load client configuration");
00050     return -1;
00051   };
00052   Arc::MCCLoader client_loader(client_config);
00053   logger.msg(Arc::INFO, "Client side MCCs are loaded");
00054   Arc::MCC* client_entry = client_loader["soap"];
00055   if(!client_entry) {
00056     logger.msg(Arc::ERROR, "Client chain does not have entry point");
00057     return -1;
00058   };
00059 
00060   Arc::MessageContext context;
00061 
00062   // -------------------------------------------------------
00063   //    Preparing delegation
00064   // -------------------------------------------------------
00065   std::string credentials;
00066   {
00067     std::ifstream ic("./cert.pem");
00068     for(;!ic.eof();) {
00069       char buf[256];
00070       ic.get(buf,sizeof(buf),0);
00071       if(ic.gcount() <= 0) break;
00072       credentials.append(buf,ic.gcount());
00073     };
00074   };
00075   {
00076     std::ifstream ic("key.pem");
00077     for(;!ic.eof();) {
00078       char buf[256];
00079       ic.get(buf,sizeof(buf),0);
00080       if(ic.gcount() <= 0) break;
00081       credentials.append(buf,ic.gcount());
00082     };
00083   };
00084   Arc::DelegationProviderSOAP deleg(credentials);
00085   if(!credentials.empty()) {
00086     logger.msg(Arc::INFO, "Initiating delegation procedure");
00087     if(!deleg.DelegateCredentialsInit(*client_entry,&context)) {
00088       logger.msg(Arc::ERROR, "Failed to initiate delegation");
00089       return -1;
00090     };
00091   };
00092 
00093 
00094   // -------------------------------------------------------
00095   //    Requesting information about service
00096   // -------------------------------------------------------
00097   {
00098     Arc::NS ns;
00099     Arc::InformationRequest inforeq;
00100     Arc::PayloadSOAP req(*(inforeq.SOAP()));
00101     Arc::Message reqmsg;
00102     Arc::Message repmsg;
00103     Arc::MessageAttributes attributes_in;
00104     Arc::MessageAttributes attributes_out;
00105     reqmsg.Payload(&req);
00106     reqmsg.Attributes(&attributes_in);
00107     reqmsg.Context(&context);
00108     repmsg.Attributes(&attributes_out);
00109     repmsg.Context(&context);
00110     Arc::MCC_Status status = client_entry->process(reqmsg,repmsg);
00111     if(!status) {
00112       logger.msg(Arc::ERROR, "Request failed");
00113       return -1;
00114     };
00115     logger.msg(Arc::INFO, "Request succeed!!!");
00116     if(repmsg.Payload() == NULL) {
00117       logger.msg(Arc::ERROR, "There is no response");
00118       return -1;
00119     };
00120     Arc::PayloadSOAP* resp = NULL;
00121     try {
00122       resp = dynamic_cast<Arc::PayloadSOAP*>(repmsg.Payload());
00123     } catch(std::exception&) { };
00124     if(resp == NULL) {
00125       logger.msg(Arc::ERROR, "Response is not SOAP");
00126       delete repmsg.Payload();
00127       return -1;
00128     };
00129     {
00130       std::string str;
00131       resp->GetXML(str);
00132       std::cout << "Response: " << str << std::endl;
00133     };
00134     Arc::InformationResponse inforesp(*resp);
00135     if(!inforesp) {
00136       logger.msg(Arc::ERROR, "Response is not expected WS-RP");
00137       delete repmsg.Payload();
00138       return -1;
00139     };
00140     std::list<Arc::XMLNode> results = inforesp.Result();
00141     int n = 0;
00142     for(std::list<Arc::XMLNode>::iterator i = results.begin();i!=results.end();++i) {
00143       std::string str;
00144       i->GetXML(str);
00145       std::cout << "Response("<<n<<"): " << str << std::endl;
00146     };
00147   };
00148 
00149   for(int n = 0;n<1;n++) {
00150 
00151     // -------------------------------------------------------
00152     //    Sending job request to service
00153     // -------------------------------------------------------
00154     logger.msg(Arc::INFO, "Creating and sending request");
00155 
00156     // Create job request
00157     /*
00158       bes-factory:CreateActivity
00159         bes-factory:ActivityDocument
00160           jsdl:JobDefinition
00161     */
00162     Arc::NS arex_ns;
00163     arex_ns["a-rex"]="http://www.nordugrid.org/schemas/a-rex";
00164     arex_ns["bes-factory"]="http://schemas.ggf.org/bes/2006/08/bes-factory";
00165     arex_ns["wsa"]="http://www.w3.org/2005/08/addressing";
00166     arex_ns["jsdl"]="http://schemas.ggf.org/jsdl/2005/11/jsdl";
00167     Arc::XMLNode id;
00168     {
00169       Arc::PayloadSOAP req(arex_ns);
00170       Arc::XMLNode op = req.NewChild("bes-factory:CreateActivity");
00171       Arc::XMLNode act_doc = op.NewChild("bes-factory:ActivityDocument");
00172       std::ifstream jsdl_file("jsdl.xml");
00173       std::string jsdl_str; 
00174       std::getline<char>(jsdl_file,jsdl_str,0);
00175       act_doc.NewChild(Arc::XMLNode(jsdl_str));
00176       deleg.DelegatedToken(op);
00177       req.GetXML(jsdl_str);
00178 
00179       // Send job request
00180       Arc::Message reqmsg;
00181       Arc::Message repmsg;
00182       Arc::MessageAttributes attributes_in;
00183       Arc::MessageAttributes attributes_out;
00184       reqmsg.Payload(&req);
00185       reqmsg.Attributes(&attributes_in);
00186       reqmsg.Context(&context);
00187       repmsg.Attributes(&attributes_out);
00188       repmsg.Context(&context);
00189       Arc::MCC_Status status = client_entry->process(reqmsg,repmsg);
00190       if(!status) {
00191         logger.msg(Arc::ERROR, "Request failed");
00192         return -1;
00193       };
00194       logger.msg(Arc::INFO, "Request succeed!!!");
00195       if(repmsg.Payload() == NULL) {
00196         logger.msg(Arc::ERROR, "There is no response");
00197         return -1;
00198       };
00199       Arc::PayloadSOAP* resp = NULL;
00200       try {
00201         resp = dynamic_cast<Arc::PayloadSOAP*>(repmsg.Payload());
00202       } catch(std::exception&) { };
00203       if(resp == NULL) {
00204         logger.msg(Arc::ERROR, "Response is not SOAP");
00205         delete repmsg.Payload();
00206         return -1;
00207       };
00208       {
00209         std::string str;
00210         resp->GetXML(str);
00211         std::cout << "Response: " << str << std::endl;
00212       };
00213       (*resp)["CreateActivityResponse"]["ActivityIdentifier"].New(id);
00214       {
00215         std::string str;
00216         id.GetDoc(str);
00217         std::cout << "Job ID: " << std::endl << str << std::endl;
00218       };
00219       delete repmsg.Payload();
00220     };
00221 
00222     // -------------------------------------------------------
00223     //    Requesting job's JSDL from service
00224     // -------------------------------------------------------
00225     {
00226       std::string str;
00227       logger.msg(Arc::INFO, "Creating and sending request");
00228 
00229       Arc::PayloadSOAP req(arex_ns);
00230       Arc::XMLNode jobref = req.NewChild("bes-factory:GetActivityDocuments").NewChild(id);
00231 
00232       // Send job request
00233       Arc::Message reqmsg;
00234       Arc::Message repmsg;
00235       Arc::MessageAttributes attributes_in;
00236       Arc::MessageAttributes attributes_out;
00237       Arc::MessageContext context;
00238       reqmsg.Payload(&req);
00239       reqmsg.Attributes(&attributes_in);
00240       reqmsg.Context(&context);
00241       repmsg.Attributes(&attributes_out);
00242       repmsg.Context(&context);
00243 
00244       req.GetXML(str);
00245       std::cout << "REQUEST: " << str << std::endl;
00246       Arc::MCC_Status status = client_entry->process(reqmsg,repmsg);
00247       if(!status) {
00248         logger.msg(Arc::ERROR, "Request failed");
00249         return -1;
00250       };
00251       logger.msg(Arc::INFO, "Request succeed!!!");
00252       if(repmsg.Payload() == NULL) {
00253         logger.msg(Arc::ERROR, "There is no response");
00254         return -1;
00255       };
00256       Arc::PayloadSOAP* resp = NULL;
00257       try {
00258         resp = dynamic_cast<Arc::PayloadSOAP*>(repmsg.Payload());
00259       } catch(std::exception&) { };
00260       if(resp == NULL) {
00261         logger.msg(Arc::ERROR, "Response is not SOAP");
00262         delete repmsg.Payload();
00263         return -1;
00264       };
00265       resp->GetXML(str);
00266       std::cout << "Response: " << str << std::endl;
00267       delete resp;
00268     };
00269 
00270     // -------------------------------------------------------
00271     //    Requesting job's status from service
00272     // -------------------------------------------------------
00273     {
00274       std::string str;
00275       logger.msg(Arc::INFO, "Creating and sending request");
00276 
00277       Arc::PayloadSOAP req(arex_ns);
00278       Arc::XMLNode jobref = req.NewChild("bes-factory:GetActivityStatuses").NewChild(id);
00279 
00280       // Send job request
00281       Arc::Message reqmsg;
00282       Arc::Message repmsg;
00283       Arc::MessageAttributes attributes_in;
00284       Arc::MessageAttributes attributes_out;
00285       Arc::MessageContext context;
00286       reqmsg.Payload(&req);
00287       reqmsg.Attributes(&attributes_in);
00288       reqmsg.Context(&context);
00289       repmsg.Attributes(&attributes_out);
00290       repmsg.Context(&context);
00291 
00292       req.GetXML(str);
00293       std::cout << "REQUEST: " << str << std::endl;
00294       Arc::MCC_Status status = client_entry->process(reqmsg,repmsg);
00295       if(!status) {
00296         logger.msg(Arc::ERROR, "Request failed");
00297         return -1;
00298       };
00299       logger.msg(Arc::INFO, "Request succeed!!!");
00300       if(repmsg.Payload() == NULL) {
00301         logger.msg(Arc::ERROR, "There is no response");
00302         return -1;
00303       };
00304       Arc::PayloadSOAP* resp = NULL;
00305       try {
00306         resp = dynamic_cast<Arc::PayloadSOAP*>(repmsg.Payload());
00307       } catch(std::exception&) { };
00308       if(resp == NULL) {
00309         logger.msg(Arc::ERROR, "Response is not SOAP");
00310         delete repmsg.Payload();
00311         return -1;
00312       };
00313       resp->GetXML(str);
00314       std::cout << "Response: " << str << std::endl;
00315       delete resp;
00316     };
00317 
00318     // -------------------------------------------------------
00319     //    Requesting job's termination
00320     // -------------------------------------------------------
00321     {
00322       std::string str;
00323       logger.msg(Arc::INFO, "Creating and sending request");
00324 
00325       Arc::PayloadSOAP req(arex_ns);
00326       Arc::XMLNode jobref = req.NewChild("bes-factory:TerminateActivities").NewChild(id);
00327 
00328       // Send job request
00329       Arc::Message reqmsg;
00330       Arc::Message repmsg;
00331       Arc::MessageAttributes attributes_in;
00332       Arc::MessageAttributes attributes_out;
00333       Arc::MessageContext context;
00334       reqmsg.Payload(&req);
00335       reqmsg.Attributes(&attributes_in);
00336       reqmsg.Context(&context);
00337       repmsg.Attributes(&attributes_out);
00338       repmsg.Context(&context);
00339 
00340       req.GetXML(str);
00341       std::cout << "REQUEST: " << str << std::endl;
00342       Arc::MCC_Status status = client_entry->process(reqmsg,repmsg);
00343       if(!status) {
00344         logger.msg(Arc::ERROR, "Request failed");
00345         return -1;
00346       };
00347       logger.msg(Arc::INFO, "Request succeed!!!");
00348       if(repmsg.Payload() == NULL) {
00349         logger.msg(Arc::ERROR, "There is no response");
00350         return -1;
00351       };
00352       Arc::PayloadSOAP* resp = NULL;
00353       try {
00354         resp = dynamic_cast<Arc::PayloadSOAP*>(repmsg.Payload());
00355       } catch(std::exception&) { };
00356       if(resp == NULL) {
00357         logger.msg(Arc::ERROR, "Response is not SOAP");
00358         delete repmsg.Payload();
00359         return -1;
00360       };
00361       resp->GetXML(str);
00362       std::cout << "Response: " << str << std::endl;
00363       delete resp;
00364     };
00365 
00366     // -------------------------------------------------------
00367     //    Requesting service's attributes
00368     // -------------------------------------------------------
00369     {
00370       std::string str;
00371       logger.msg(Arc::INFO, "Creating and sending request");
00372 
00373       Arc::PayloadSOAP req(arex_ns);
00374       req.NewChild("bes-factory:GetFactoryAttributesDocument");
00375 
00376       // Send job request
00377       Arc::Message reqmsg;
00378       Arc::Message repmsg;
00379       Arc::MessageAttributes attributes_in;
00380       Arc::MessageAttributes attributes_out;
00381       Arc::MessageContext context;
00382       reqmsg.Payload(&req);
00383       reqmsg.Attributes(&attributes_in);
00384       reqmsg.Context(&context);
00385       repmsg.Attributes(&attributes_out);
00386       repmsg.Context(&context);
00387 
00388       req.GetXML(str);
00389       std::cout << "REQUEST: " << str << std::endl;
00390       Arc::MCC_Status status = client_entry->process(reqmsg,repmsg);
00391       if(!status) {
00392         logger.msg(Arc::ERROR, "Request failed");
00393         return -1;
00394       };
00395       logger.msg(Arc::INFO, "Request succeed!!!");
00396       if(repmsg.Payload() == NULL) {
00397         logger.msg(Arc::ERROR, "There is no response");
00398         return -1;
00399       };
00400       Arc::PayloadSOAP* resp = NULL;
00401       try {
00402         resp = dynamic_cast<Arc::PayloadSOAP*>(repmsg.Payload());
00403       } catch(std::exception&) { };
00404       if(resp == NULL) {
00405         logger.msg(Arc::ERROR, "Response is not SOAP");
00406         delete repmsg.Payload();
00407         return -1;
00408       };
00409       resp->GetXML(str);
00410       std::cout << "Response: " << str << std::endl;
00411       delete resp;
00412     };
00413 
00414   };
00415 
00416   for(;;) sleep(10);
00417 
00418   return 0;
00419 }