Back to index

nordugrid-arc-nox  1.1.0~rc6
arcecho.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/UserConfig.h>
00017 #include <arc/client/ClientInterface.h>
00018 #include <arc/message/MCC.h>
00019 
00020 int main(int argc, char **argv) {
00021 
00022   setlocale(LC_ALL, "");
00023 
00024   Arc::Logger logger(Arc::Logger::getRootLogger(), "arcecho");
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 message"),
00033                             istring("The arcecho command is a client for "
00034                                     "the ARC echo service."),
00035                             istring("The service argument is a URL to an ARC "
00036                                     "echo service.\n"
00037                                     "The message argument is the message the "
00038                                     "service should return."));
00039 
00040   std::string conffile;
00041   options.AddOption('z', "conffile",
00042                     istring("configuration file (default ~/.arc/client.conf)"),
00043                     istring("filename"), conffile);
00044 
00045   std::string debug;
00046   options.AddOption('d', "debug",
00047                     istring("FATAL, ERROR, WARNING, INFO, VERBOSE or DEBUG"),
00048                     istring("debuglevel"), debug);
00049 
00050   bool version = false;
00051   options.AddOption('v', "version", istring("print version information"),
00052                     version);
00053 
00054   std::list<std::string> args = options.Parse(argc, argv);
00055 
00056   // If debug is specified as argument, it should be set before loading the configuration.
00057   if (!debug.empty())
00058     Arc::Logger::getRootLogger().setThreshold(Arc::string_to_level(debug));
00059 
00060   Arc::UserConfig usercfg(conffile);
00061   if (!usercfg) {
00062     logger.msg(Arc::ERROR, "Failed configuration initialization");
00063     return 1;
00064   }
00065 
00066   if (debug.empty() && !usercfg.Verbosity().empty())
00067     Arc::Logger::getRootLogger().setThreshold(Arc::string_to_level(usercfg.Verbosity()));
00068 
00069   if (version) {
00070     std::cout << Arc::IString("%s version %s", "arcecho", VERSION)
00071               << std::endl;
00072     return 0;
00073   }
00074 
00075   if (args.size() != 2) {
00076     logger.msg(Arc::ERROR, "Wrong number of arguments!");
00077     return 1;
00078   }
00079 
00080   std::list<std::string>::iterator it = args.begin();
00081 
00082   Arc::URL service = *it++;
00083   std::string message = *it++;
00084 
00085   Arc::MCCConfig cfg;
00086   usercfg.ApplyToConfig(cfg);
00087 
00088   Arc::ClientSOAP client(cfg, service, usercfg.Timeout());
00089 
00090   std::string xml;
00091 
00092   Arc::NS ns("echo", "http://www.nordugrid.org/schemas/echo");
00093   Arc::PayloadSOAP request(ns);
00094   request.NewChild("echo:echo").NewChild("echo:say") = message;
00095 
00096   request.GetXML(xml, true);
00097   logger.msg(Arc::INFO, "Request:\n%s", xml);
00098 
00099   Arc::PayloadSOAP *response = NULL;
00100 
00101   Arc::MCC_Status status = client.process(&request, &response);
00102 
00103   if (!status) {
00104     logger.msg(Arc::ERROR, (std::string)status);
00105     if (response)
00106       delete response;
00107     return 1;
00108   }
00109 
00110   if (!response) {
00111     logger.msg(Arc::ERROR, "No SOAP response");
00112     return 1;
00113   }
00114 
00115   response->GetXML(xml, true);
00116   logger.msg(Arc::INFO, "Response:\n%s", xml);
00117 
00118   std::string answer = (std::string)((*response)["echoResponse"]["hear"]);
00119 
00120   delete response;
00121 
00122   std::cout << answer << std::endl;
00123 
00124   return 0;
00125 }