Back to index

nordugrid-arc-nox  1.1.0~rc6
arcsrmping.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 <iostream>
00008 #include <list>
00009 #include <string>
00010 
00011 #include <arc/ArcLocation.h>
00012 #include <arc/IString.h>
00013 #include <arc/Logger.h>
00014 #include <arc/OptionParser.h>
00015 #include <arc/StringConv.h>
00016 #include <arc/message/MCC.h>
00017 #include <arc/client/ClientInterface.h>
00018 #include <arc/UserConfig.h>
00019 
00020 int main(int argc, char **argv) {
00021 
00022   setlocale(LC_ALL, "");
00023 
00024   Arc::Logger logger(Arc::Logger::getRootLogger(), "srmping");
00025   Arc::LogStream logcerr(std::cerr);
00026   logcerr.setFormat(Arc::ShortFormat);
00027   Arc::Logger::getRootLogger().addDestination(logcerr);
00028   Arc::Logger::rootLogger.setThreshold(Arc::WARNING);
00029 
00030   Arc::ArcLocation::Init(argv[0]);
00031 
00032   Arc::OptionParser options(istring("service"),
00033                             istring("The arcsrmping command is a ping client "
00034                                     "for the SRM service."),
00035                             istring("The service argument is a URL to an SRM "
00036                                     "service."));
00037 
00038   int timeout = -1;
00039   options.AddOption('t', "timeout", istring("timeout in seconds (default 20)"),
00040                     istring("seconds"), timeout);
00041 
00042   std::string conffile;
00043   options.AddOption('z', "conffile",
00044                     istring("configuration file (default ~/.arc/client.conf)"),
00045                     istring("filename"), conffile);
00046 
00047   std::string debug;
00048   options.AddOption('d', "debug",
00049                     istring("FATAL, ERROR, WARNING, INFO, VERBOSE or DEBUG"),
00050                     istring("debuglevel"), debug);
00051 
00052   bool version = false;
00053   options.AddOption('v', "version", istring("print version information"),
00054                     version);
00055 
00056   std::list<std::string> args = options.Parse(argc, argv);
00057 
00058   // If debug is specified as argument, it should be set before loading the configuration.
00059   if (!debug.empty())
00060     Arc::Logger::getRootLogger().setThreshold(Arc::string_to_level(debug));
00061 
00062   Arc::UserConfig usercfg(conffile);
00063   if (!usercfg) {
00064     logger.msg(Arc::ERROR, "Failed configuration initialization");
00065     return 1;
00066   }
00067   
00068   if (debug.empty() && !usercfg.Verbosity().empty())
00069     Arc::Logger::getRootLogger().setThreshold(Arc::string_to_level(usercfg.Verbosity()));
00070   
00071   if (timeout > 0)
00072     usercfg.Timeout(timeout);
00073 
00074   if (version) {
00075     std::cout << Arc::IString("%s version %s", "arcsrmping", VERSION)
00076               << std::endl;
00077     return 0;
00078   }
00079 
00080   if (args.size() != 1) {
00081     logger.msg(Arc::ERROR, "Wrong number of arguments!");
00082     return 1;
00083   }
00084 
00085   std::list<std::string>::iterator it = args.begin();
00086 
00087   Arc::URL service = *it;
00088 
00089   if (service.Protocol() == "srm")
00090     service.ChangeProtocol("httpg");
00091 
00092   Arc::MCCConfig cfg;
00093   usercfg.ApplyToConfig(cfg);
00094 
00095   Arc::ClientSOAP client(cfg, service, usercfg.Timeout());
00096 
00097   std::string xml;
00098 
00099   Arc::NS ns("SRMv2", "http://srm.lbl.gov/StorageResourceManager");
00100   Arc::PayloadSOAP request(ns);
00101   request.NewChild("SRMv2:srmPing").NewChild("srmPingRequest");
00102 
00103   request.GetXML(xml, true);
00104   logger.msg(Arc::INFO, "Request:\n%s", xml);
00105 
00106   Arc::PayloadSOAP *response = NULL;
00107 
00108   Arc::MCC_Status status = client.process(&request, &response);
00109 
00110   if (!status) {
00111     logger.msg(Arc::ERROR, (std::string)status);
00112     if (response)
00113       delete response;
00114     return 1;
00115   }
00116 
00117   if (!response) {
00118     logger.msg(Arc::ERROR, "No SOAP response");
00119     return 1;
00120   }
00121 
00122   response->GetXML(xml, true);
00123   logger.msg(Arc::INFO, "Response:\n%s", xml);
00124 
00125   Arc::XMLNode pingresponse = (*response)["srmPingResponse"]["srmPingResponse"];
00126 
00127   std::string srmversion = (std::string)(pingresponse["versionInfo"]);
00128 
00129   std::cout << "SRM version: " << srmversion << std::endl;
00130 
00131   for (Arc::XMLNode n = pingresponse["otherInfo"]["extraInfoArray"]; n; ++n)
00132     std::cout << "  " << (std::string)n["key"]
00133               << ": " << (std::string)n["value"] << std::endl;
00134 
00135   delete response;
00136 
00137   return 0;
00138 }