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 std::cout << __COUT_HDR_FL__ << "replyString " << replyString << std::endl;
00110 return replyString;
00111 }
00112 catch (xdaq::exception::Exception& e)
00113 {
00114 std::cout << __COUT_HDR_FL__ << "This application failed to send a SOAP message to "
00115 << d->getClassName() << " instance " << d->getInstance()
00116 << " re-throwing exception = " << xcept::stdformat_exception_history(e);
00117 std::string mystring;
00118 message->writeTo(mystring);
00119 std::cout << __COUT_HDR_FL__<< mystring << std::endl;
00120 XCEPT_RETHROW(xdaq::exception::Exception,"Failed to send SOAP command.",e);
00121 }
00122 }
00123
00124
00125 std::string SOAPMessenger::send(const xdaq::ApplicationDescriptor* d, SOAPCommand soapCommand)
00126 throw (xdaq::exception::Exception)
00127 {
00128 if(soapCommand.hasParameters())
00129 return send(d, soapCommand.getCommand(), soapCommand.getParameters());
00130 else
00131 return send(d, soapCommand.getCommand());
00132 }
00133
00134
00135 std::string SOAPMessenger::send(const xdaq::ApplicationDescriptor* d, std::string command)
00136 throw (xdaq::exception::Exception)
00137 {
00138 xoap::MessageReference message;
00139 message = SOAPUtilities::makeSOAPMessageReference(command);
00140 return send(d, message);
00141 }
00142
00143
00144 std::string SOAPMessenger::send(const xdaq::ApplicationDescriptor* ind, std::string cmd,
00145 SOAPParameters parameters)
00146 throw (xdaq::exception::Exception)
00147 {
00148
00149
00150
00151 XDAQ_CONST_CALL xdaq::ApplicationDescriptor* d =
00152 const_cast<xdaq::ApplicationDescriptor*>(ind);
00153
00154 xoap::MessageReference message;
00155 try
00156 {
00157 message = SOAPUtilities::makeSOAPMessageReference(cmd, parameters);
00158 message->getMimeHeaders()->setHeader("Content-Location", d->getURN());
00159 xoap::MessageReference reply = theApplication_->getApplicationContext()->postSOAP(message, *(theApplication_->getApplicationDescriptor()), *d);
00160 std::string replyString = receive(reply);
00161 return replyString;
00162 }
00163 catch (xdaq::exception::Exception& e)
00164 {
00165 std::cout << __COUT_HDR_FL__ << "This application failed to send a SOAP message to "
00166 << d->getClassName() << " instance " << d->getInstance()
00167 << " with command = " << cmd
00168 << " re-throwing exception = " << xcept::stdformat_exception_history(e)<<std::endl;
00169
00170 std::string mystring;
00171 message->writeTo(mystring);
00172 mf::LogError(__FILE__) << mystring;
00173 XCEPT_RETHROW(xdaq::exception::Exception,"Failed to send SOAP command.",e);
00174 }
00175
00176 }
00177
00178
00179 xoap::MessageReference SOAPMessenger::sendWithSOAPReply(const xdaq::ApplicationDescriptor* ind,
00180 std::string cmd)
00181 throw (xdaq::exception::Exception)
00182 {
00183
00184
00185
00186 XDAQ_CONST_CALL xdaq::ApplicationDescriptor* d =
00187 const_cast<xdaq::ApplicationDescriptor*>(ind);
00188 try
00189 {
00190 xoap::MessageReference message = SOAPUtilities::makeSOAPMessageReference(cmd);
00191 message->getMimeHeaders()->setHeader("Content-Location", d->getURN());
00192 xoap::MessageReference reply = theApplication_->getApplicationContext()->postSOAP(message, *(theApplication_->getApplicationDescriptor()), *d);
00193 return reply;
00194 }
00195 catch (xdaq::exception::Exception& e)
00196 {
00197 std::cout << __COUT_HDR_FL__ << "This application failed to send a SOAP message to "
00198 << d->getClassName() << " instance " << d->getInstance()
00199 << " with command = " << cmd
00200 << " re-throwing exception = " << xcept::stdformat_exception_history(e)<<std::endl;
00201 XCEPT_RETHROW(xdaq::exception::Exception,"Failed to send SOAP command.",e);
00202 }
00203
00204 }
00205
00206
00207 xoap::MessageReference SOAPMessenger::sendWithSOAPReply(const xdaq::ApplicationDescriptor *ind,
00208 xoap::MessageReference message)
00209 throw (xdaq::exception::Exception)
00210 {
00211
00212
00213
00214 XDAQ_CONST_CALL xdaq::ApplicationDescriptor* d =
00215 const_cast<xdaq::ApplicationDescriptor*>(ind);
00216 try
00217 {
00218 message->getMimeHeaders()->setHeader("Content-Location", d->getURN());
00219 xoap::MessageReference reply = theApplication_->getApplicationContext()->postSOAP(message, *(theApplication_->getApplicationDescriptor()), *d);
00220 return reply;
00221 }
00222 catch (xdaq::exception::Exception& e)
00223 {
00224 std::cout << __COUT_HDR_FL__ << "This application failed to send a SOAP message to "
00225 << d->getClassName() << " instance " << d->getInstance()
00226 << " re-throwing exception = " << xcept::stdformat_exception_history(e);
00227 std::string mystring;
00228 message->writeTo(mystring);
00229 mf::LogError(__FILE__)<< mystring << std::endl;
00230 XCEPT_RETHROW(xdaq::exception::Exception,"Failed to send SOAP command.",e);
00231 }
00232 }
00233
00234
00235 xoap::MessageReference SOAPMessenger::sendWithSOAPReply(const xdaq::ApplicationDescriptor* ind,
00236 std::string cmd, SOAPParameters parameters)
00237 throw (xdaq::exception::Exception)
00238 {
00239
00240
00241
00242 XDAQ_CONST_CALL xdaq::ApplicationDescriptor* d =
00243 const_cast<xdaq::ApplicationDescriptor*>(ind);
00244 try
00245 {
00246 xoap::MessageReference message = SOAPUtilities::makeSOAPMessageReference(cmd, parameters);
00247 message->getMimeHeaders()->setHeader("Content-Location", d->getURN());
00248 xoap::MessageReference reply = theApplication_->getApplicationContext()->postSOAP(message, *(theApplication_->getApplicationDescriptor()), *d);
00249 return reply;
00250 }
00251 catch (xdaq::exception::Exception& e)
00252 {
00253 std::cout << __COUT_HDR_FL__ << "This application failed to send a SOAP message to "
00254 << d->getClassName() << " instance " << d->getInstance()
00255 << " with command = " << cmd
00256 << " re-throwing exception = " << xcept::stdformat_exception_history(e);
00257 XCEPT_RETHROW(xdaq::exception::Exception,"Failed to send SOAP command.",e);
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
00299 std::string SOAPMessenger::sendStatus(const xdaq::ApplicationDescriptor* ind,
00300 std::string message)
00301 throw (xdaq::exception::Exception)
00302 {
00303
00304
00305
00306 XDAQ_CONST_CALL xdaq::ApplicationDescriptor* d =
00307 const_cast<xdaq::ApplicationDescriptor*>(ind);
00308
00309 std::string cmd = "StatusNotification";
00310 try
00311 {
00312 timeval tv;
00313 gettimeofday(&tv,NULL);
00314
00315 std::stringstream ss;
00316 SOAPParameters parameters;
00317 parameters.addParameter("Description",message);
00318 ss.str(""); ss << tv.tv_sec;
00319 parameters.addParameter("Time",ss.str());
00320 ss.str(""); ss << tv.tv_usec;
00321 parameters.addParameter("usec",ss.str());
00322 return send(d, cmd, parameters);
00323 }
00324 catch (xdaq::exception::Exception& e)
00325 {
00326 std::cout << __COUT_HDR_FL__ << "This application failed to send a SOAP error message to "
00327 << d->getClassName() << " instance " << d->getInstance()
00328 << " with command = " << cmd
00329 << " re-throwing exception = " << xcept::stdformat_exception_history(e)<<std::endl;
00330 XCEPT_RETHROW(xdaq::exception::Exception,"Failed to send SOAP command.",e);
00331 }
00332 }