00001 #include "otsdaq-core/SOAPUtilities/SOAPMessenger.h"
00002 #include "otsdaq-core/SOAPUtilities/SOAPUtilities.h"
00003 #include "otsdaq-core/SOAPUtilities/SOAPCommand.h"
00004 #include "otsdaq-core/MessageFacility/MessageFacility.h"
00005 #include "otsdaq-core/Macros/CoutHeaderMacros.h"
00006
00007
00008 #include <xoap/Method.h>
00009 #include <xdaq/NamespaceURI.h>
00010 #include <xoap/MessageReference.h>
00011 #include <xoap/MessageFactory.h>
00012 #include <xoap/SOAPPart.h>
00013 #include <xoap/SOAPEnvelope.h>
00014 #include <xoap/SOAPBody.h>
00015 #include <xoap/domutils.h>
00016 #include <xoap/AttachmentPart.h>
00017
00018
00019
00020
00021
00022
00023
00024
00025 using namespace ots;
00026
00027
00028 SOAPMessenger::SOAPMessenger(xdaq::Application* application) :
00029 theApplication_(application)
00030 {
00031 }
00032
00033
00034 SOAPMessenger::SOAPMessenger(const SOAPMessenger& aSOAPMessenger) :
00035 theApplication_(aSOAPMessenger.theApplication_)
00036 {
00037 }
00038
00039
00040 std::string SOAPMessenger::receive(const xoap::MessageReference& message, SOAPCommand& soapCommand)
00041 {
00042 return receive(message, soapCommand.getParametersRef());
00043 }
00044
00045
00046 std::string SOAPMessenger::receive(const xoap::MessageReference& message)
00047 {
00048
00049 return (message->getSOAPPart().getEnvelope().getBody().getChildElements()).begin()->getElementName().getLocalName();
00050 }
00051
00052
00053 std::string SOAPMessenger::receive(const xoap::MessageReference& message, SOAPParameters& parameters)
00054 {
00055 xoap::SOAPEnvelope envelope = message->getSOAPPart().getEnvelope();
00056 std::vector<xoap::SOAPElement> bodyList = envelope.getBody().getChildElements();
00057 xoap::SOAPElement command = bodyList[0];
00058 std::string commandName = command.getElementName().getLocalName();
00059 xoap::SOAPName name = envelope.createName("Key");
00060
00061 for (SOAPParameters::iterator it=parameters.begin(); it!=parameters.end(); it++)
00062 {
00063 name = envelope.createName(it->first);
00064
00065 try
00066 {
00067 it->second = command.getAttributeValue(name);
00068
00069
00070
00071
00072
00073
00074
00075
00076 }
00077 catch (xoap::exception::Exception& e)
00078 {
00079 std::cout << __COUT_HDR_FL__ << "Parameter " << it->first << " does not exist in the list of incoming parameters!" << std::endl;
00080 XCEPT_RETHROW(xoap::exception::Exception,"Looking for parameter that does not exist!",e);
00081 }
00082
00083 }
00084
00085 return commandName;
00086 }
00087
00088
00089
00090
00091
00092
00093 std::string SOAPMessenger::send(const xdaq::ApplicationDescriptor* ind,
00094 xoap::MessageReference message)
00095 throw (xdaq::exception::Exception)
00096 {
00097
00098
00099
00100 XDAQ_CONST_CALL xdaq::ApplicationDescriptor* d =
00101 const_cast<xdaq::ApplicationDescriptor*>(ind);
00102 try
00103 {
00104 message->getMimeHeaders()->setHeader("Content-Location", d->getURN());
00105 xoap::MessageReference reply = theApplication_->getApplicationContext()->postSOAP(message,
00106 *(theApplication_->getApplicationDescriptor()),
00107 *d);
00108 std::string replyString = receive(reply);
00109 return replyString;
00110 }
00111 catch (xdaq::exception::Exception& e)
00112 {
00113 std::cout << __COUT_HDR_FL__ << "This application failed to send a SOAP message to "
00114 << d->getClassName() << " instance " << d->getInstance()
00115 << " re-throwing exception = " << xcept::stdformat_exception_history(e);
00116 std::string mystring;
00117 message->writeTo(mystring);
00118 std::cout << __COUT_HDR_FL__<< mystring << std::endl;
00119 XCEPT_RETHROW(xdaq::exception::Exception,"Failed to send SOAP command.",e);
00120 }
00121 }
00122
00123
00124 std::string SOAPMessenger::send(const xdaq::ApplicationDescriptor* d, SOAPCommand soapCommand)
00125 throw (xdaq::exception::Exception)
00126 {
00127 if(soapCommand.hasParameters())
00128 return send(d, soapCommand.getCommand(), soapCommand.getParameters());
00129 else
00130 return send(d, soapCommand.getCommand());
00131 }
00132
00133
00134 std::string SOAPMessenger::send(const xdaq::ApplicationDescriptor* d, std::string command)
00135 throw (xdaq::exception::Exception)
00136 {
00137 xoap::MessageReference message;
00138 message = SOAPUtilities::makeSOAPMessageReference(command);
00139 return send(d, message);
00140 }
00141
00142
00143 std::string SOAPMessenger::send(const xdaq::ApplicationDescriptor* ind, std::string cmd,
00144 SOAPParameters parameters)
00145 throw (xdaq::exception::Exception)
00146 {
00147
00148
00149
00150 XDAQ_CONST_CALL xdaq::ApplicationDescriptor* d =
00151 const_cast<xdaq::ApplicationDescriptor*>(ind);
00152
00153 xoap::MessageReference message;
00154 try
00155 {
00156 message = SOAPUtilities::makeSOAPMessageReference(cmd, parameters);
00157 message->getMimeHeaders()->setHeader("Content-Location", d->getURN());
00158 xoap::MessageReference reply = theApplication_->getApplicationContext()->postSOAP(message, *(theApplication_->getApplicationDescriptor()), *d);
00159 std::string replyString = receive(reply);
00160 return replyString;
00161 }
00162 catch (xdaq::exception::Exception& e)
00163 {
00164 std::cout << __COUT_HDR_FL__ << "This application failed to send a SOAP message to "
00165 << d->getClassName() << " instance " << d->getInstance()
00166 << " with command = " << cmd
00167 << " re-throwing exception = " << xcept::stdformat_exception_history(e)<<std::endl;
00168
00169 std::string mystring;
00170 message->writeTo(mystring);
00171 mf::LogError(__FILE__) << mystring;
00172 XCEPT_RETHROW(xdaq::exception::Exception,"Failed to send SOAP command.",e);
00173 }
00174
00175 }
00176
00177
00178 xoap::MessageReference SOAPMessenger::sendWithSOAPReply(const xdaq::ApplicationDescriptor* ind,
00179 std::string cmd)
00180 throw (xdaq::exception::Exception)
00181 {
00182
00183
00184
00185 XDAQ_CONST_CALL xdaq::ApplicationDescriptor* d =
00186 const_cast<xdaq::ApplicationDescriptor*>(ind);
00187 try
00188 {
00189 xoap::MessageReference message = SOAPUtilities::makeSOAPMessageReference(cmd);
00190 message->getMimeHeaders()->setHeader("Content-Location", d->getURN());
00191 xoap::MessageReference reply = theApplication_->getApplicationContext()->postSOAP(message, *(theApplication_->getApplicationDescriptor()), *d);
00192 return reply;
00193 }
00194 catch (xdaq::exception::Exception& e)
00195 {
00196 std::cout << __COUT_HDR_FL__ << "This application failed to send a SOAP message to "
00197 << d->getClassName() << " instance " << d->getInstance()
00198 << " with command = " << cmd
00199 << " re-throwing exception = " << xcept::stdformat_exception_history(e)<<std::endl;
00200 XCEPT_RETHROW(xdaq::exception::Exception,"Failed to send SOAP command.",e);
00201 }
00202
00203 }
00204
00205
00206 xoap::MessageReference SOAPMessenger::sendWithSOAPReply(const xdaq::ApplicationDescriptor *ind,
00207 xoap::MessageReference message)
00208 throw (xdaq::exception::Exception)
00209 {
00210
00211
00212
00213 XDAQ_CONST_CALL xdaq::ApplicationDescriptor* d =
00214 const_cast<xdaq::ApplicationDescriptor*>(ind);
00215 try
00216 {
00217 message->getMimeHeaders()->setHeader("Content-Location", d->getURN());
00218 xoap::MessageReference reply = theApplication_->getApplicationContext()->postSOAP(message, *(theApplication_->getApplicationDescriptor()), *d);
00219 return reply;
00220 }
00221 catch (xdaq::exception::Exception& e)
00222 {
00223 std::cout << __COUT_HDR_FL__ << "This application failed to send a SOAP message to "
00224 << d->getClassName() << " instance " << d->getInstance()
00225 << " re-throwing exception = " << xcept::stdformat_exception_history(e);
00226 std::string mystring;
00227 message->writeTo(mystring);
00228 mf::LogError(__FILE__)<< mystring << std::endl;
00229 XCEPT_RETHROW(xdaq::exception::Exception,"Failed to send SOAP command.",e);
00230 }
00231 }
00232
00233
00234 xoap::MessageReference SOAPMessenger::sendWithSOAPReply(const xdaq::ApplicationDescriptor* ind,
00235 std::string cmd, SOAPParameters parameters)
00236 throw (xdaq::exception::Exception)
00237 {
00238
00239
00240
00241 XDAQ_CONST_CALL xdaq::ApplicationDescriptor* d =
00242 const_cast<xdaq::ApplicationDescriptor*>(ind);
00243 try
00244 {
00245 xoap::MessageReference message = SOAPUtilities::makeSOAPMessageReference(cmd, parameters);
00246 message->getMimeHeaders()->setHeader("Content-Location", d->getURN());
00247 xoap::MessageReference reply = theApplication_->getApplicationContext()->postSOAP(message, *(theApplication_->getApplicationDescriptor()), *d);
00248 return reply;
00249 }
00250 catch (xdaq::exception::Exception& e)
00251 {
00252 std::cout << __COUT_HDR_FL__ << "This application failed to send a SOAP message to "
00253 << d->getClassName() << " instance " << d->getInstance()
00254 << " with command = " << cmd
00255 << " re-throwing exception = " << xcept::stdformat_exception_history(e);
00256 XCEPT_RETHROW(xdaq::exception::Exception,"Failed to send SOAP command.",e);
00257 }
00258
00259 }
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298 std::string SOAPMessenger::sendStatus(const xdaq::ApplicationDescriptor* ind,
00299 std::string message)
00300 throw (xdaq::exception::Exception)
00301 {
00302
00303
00304
00305 XDAQ_CONST_CALL xdaq::ApplicationDescriptor* d =
00306 const_cast<xdaq::ApplicationDescriptor*>(ind);
00307
00308 std::string cmd = "StatusNotification";
00309 try
00310 {
00311 timeval tv;
00312 gettimeofday(&tv,NULL);
00313
00314 std::stringstream ss;
00315 SOAPParameters parameters;
00316 parameters.addParameter("Description",message);
00317 ss.str(""); ss << tv.tv_sec;
00318 parameters.addParameter("Time",ss.str());
00319 ss.str(""); ss << tv.tv_usec;
00320 parameters.addParameter("usec",ss.str());
00321 return send(d, cmd, parameters);
00322 }
00323 catch (xdaq::exception::Exception& e)
00324 {
00325 std::cout << __COUT_HDR_FL__ << "This application failed to send a SOAP error message to "
00326 << d->getClassName() << " instance " << d->getInstance()
00327 << " with command = " << cmd
00328 << " re-throwing exception = " << xcept::stdformat_exception_history(e)<<std::endl;
00329 XCEPT_RETHROW(xdaq::exception::Exception,"Failed to send SOAP command.",e);
00330 }
00331 }