otsdaq  v2_01_00
SOAPMessenger.cc
1 #include "otsdaq-core/SOAPUtilities/SOAPMessenger.h"
2 #include "otsdaq-core/SOAPUtilities/SOAPUtilities.h"
3 #include "otsdaq-core/SOAPUtilities/SOAPCommand.h"
4 #include "otsdaq-core/MessageFacility/MessageFacility.h"
5 #include "otsdaq-core/Macros/CoutMacros.h"
6 //#include "otsdaq-core/DataTypes/DataStructs.h"
7 
8 #include <xoap/Method.h>
9 #include <xdaq/NamespaceURI.h>
10 #include <xoap/MessageReference.h>
11 #pragma GCC diagnostic push
12 #pragma GCC diagnostic ignored "-Wnon-virtual-dtor"
13 #include <xoap/MessageFactory.h>
14 #pragma GCC diagnostic pop
15 #include <xoap/SOAPPart.h>
16 #include <xoap/SOAPEnvelope.h>
17 #include <xoap/SOAPBody.h>
18 #include <xoap/domutils.h>
19 #include <xoap/AttachmentPart.h>
20 
21 //#include <iostream>
22 //#include <sys/time.h>
23 
24 
25 
26 
27 
28 using namespace ots;
29 
30 //========================================================================================================================
31 SOAPMessenger::SOAPMessenger(xdaq::Application* application) :
32  theApplication_(application)
33 {
34 }
35 
36 //========================================================================================================================
37 SOAPMessenger::SOAPMessenger(const SOAPMessenger& aSOAPMessenger) :
38  theApplication_(aSOAPMessenger.theApplication_)
39 {
40 }
41 
42 //========================================================================================================================
43 std::string SOAPMessenger::receive(const xoap::MessageReference& message, SOAPCommand& soapCommand)
44 {
45  return receive(message, soapCommand.getParametersRef());
46 }
47 
48 //========================================================================================================================
49 std::string SOAPMessenger::receive(const xoap::MessageReference& message)
50 {
51  //NOTE it is assumed that there is only 1 command for each message (that's why we use begin)
52  return (message->getSOAPPart().getEnvelope().getBody().getChildElements()).begin()->getElementName().getLocalName();
53 }
54 
55 //========================================================================================================================
56 std::string SOAPMessenger::receive(const xoap::MessageReference& message, SOAPParameters& parameters)
57 {
58  xoap::SOAPEnvelope envelope = message->getSOAPPart().getEnvelope();
59  std::vector<xoap::SOAPElement> bodyList = envelope.getBody().getChildElements();
60  xoap::SOAPElement command = bodyList[0];
61  std::string commandName = command.getElementName().getLocalName();
62  xoap::SOAPName name = envelope.createName("Key");
63 
64  for (SOAPParameters::iterator it=parameters.begin(); it!=parameters.end(); it++)
65  {
66  name = envelope.createName(it->first);
67 
68  try
69  {
70  it->second = command.getAttributeValue(name);
71  //if( parameters.getParameter(it->first).isEmpty() )
72  //{
73  // __COUT__ << "Complaint from " << (theApplication_->getApplicationDescriptor()->getClassName()) << std::endl;
74  // __COUT__ << " : Parameter "<< it->first
75  // << " does not exist in the list of incoming parameters!" << std::endl;
76  // __COUT__ << "It could also be because you passed an empty std::string" << std::endl;
77  // //assert(0);
78  //};
79  }
80  catch (xoap::exception::Exception& e)
81  {
82  __COUT__ << "Parameter " << it->first << " does not exist in the list of incoming parameters!" << std::endl;
83  XCEPT_RETHROW(xoap::exception::Exception,"Looking for parameter that does not exist!",e);
84  }
85 
86  }
87 
88  return commandName;
89 }
90 
91 //========================================================================================================================
92 // in xdaq
93 // xdaq::ApplicationDescriptor* sourceptr;
94 // void getURN()
95 //
96 std::string SOAPMessenger::send(XDAQ_CONST_CALL xdaq::ApplicationDescriptor* ind,
97  xoap::MessageReference message)
98 
99 {
100  return receive(sendWithSOAPReply(ind,message));
101 }
102 //{
103 // //const_cast away the const
104 // // so that this line is compatible with slf6 and slf7 versions of xdaq
105 // // where they changed to XDAQ_CONST_CALL xdaq::ApplicationDescriptor* in slf7
106 // XDAQ_CONST_CALL xdaq::ApplicationDescriptor* d =
107 // const_cast<xdaq::ApplicationDescriptor*>(ind);
108 // try
109 // {
110 // message->getMimeHeaders()->setHeader("Content-Location", d->getURN());
111 //
112 // __COUT__ << d->getURN() << __E__;
113 // __COUT__ << SOAPUtilities::translate(message) << __E__;
114 // std::string mystring;
115 // message->writeTo(mystring);
116 // __COUT__<< mystring << std::endl;
117 //
118 // xoap::MessageReference reply = theApplication_->getApplicationContext()->postSOAP(message,
119 // *(theApplication_->getApplicationDescriptor()),
120 // *d);
121 // std::string replyString = receive(reply);
122 // __COUT__ << "replyString " << replyString << std::endl;
123 // return replyString;
124 // }
125 // catch (xdaq::exception::Exception& e)
126 // {
127 // __COUT__ << "This application failed to send a SOAP message to "
128 // << d->getClassName() << " instance " << d->getInstance()
129 // << " re-throwing exception = " << xcept::stdformat_exception_history(e);
130 // std::string mystring;
131 // message->writeTo(mystring);
132 // __COUT__<< mystring << std::endl;
133 // XCEPT_RETHROW(xdaq::exception::Exception,"Failed to send SOAP command.",e);
134 // }
135 //}
136 
137 //========================================================================================================================
138 std::string SOAPMessenger::send(XDAQ_CONST_CALL xdaq::ApplicationDescriptor* d, SOAPCommand soapCommand)
139 
140 {
141  if(soapCommand.hasParameters())
142  return send(d, soapCommand.getCommand(), soapCommand.getParameters());
143  else
144  return send(d, soapCommand.getCommand());
145 }
146 
147 //========================================================================================================================
148 std::string SOAPMessenger::send(XDAQ_CONST_CALL xdaq::ApplicationDescriptor* d, std::string command)
149 
150 {
151  xoap::MessageReference message = SOAPUtilities::makeSOAPMessageReference(command);
152  return send(d, message);
153 }
154 
155 //========================================================================================================================
156 std::string SOAPMessenger::send(XDAQ_CONST_CALL xdaq::ApplicationDescriptor* ind, std::string cmd,
157  SOAPParameters parameters)
158 
159 {
160  return receive(sendWithSOAPReply(ind,cmd,parameters));
161 }
162 
163 //{
164 // //const_cast away the const
165 // // so that this line is compatible with slf6 and slf7 versions of xdaq
166 // // where they changed to XDAQ_CONST_CALL xdaq::ApplicationDescriptor* in slf7
167 // XDAQ_CONST_CALL xdaq::ApplicationDescriptor* d =
168 // const_cast<xdaq::ApplicationDescriptor*>(ind);
169 //
170 // xoap::MessageReference message;
171 // try
172 // {
173 // message = SOAPUtilities::makeSOAPMessageReference(cmd, parameters);
174 // message->getMimeHeaders()->setHeader("Content-Location", d->getURN());
175 //
176 // __COUT__ << d->getURN() << __E__;
177 // __COUT__ << SOAPUtilities::translate(message) << __E__;
178 // std::string mystring;
179 // message->writeTo(mystring);
180 // __COUT__<< mystring << std::endl;
181 //
182 // xoap::MessageReference reply = theApplication_->getApplicationContext()->postSOAP(message,
183 // *(theApplication_->getApplicationDescriptor()), *d);
184 // std::string replyString = receive(reply);
185 // return replyString;
186 // }
187 // catch (xdaq::exception::Exception& e)
188 // {
189 // __COUT__ << "This application failed to send a SOAP message to "
190 // << d->getClassName() << " instance " << d->getInstance()
191 // << " with command = " << cmd
192 // << " re-throwing exception = " << xcept::stdformat_exception_history(e)<<std::endl;
193 //
194 // std::string mystring;
195 // message->writeTo(mystring);
196 // __COUT_ERR__ << mystring;
197 // XCEPT_RETHROW(xdaq::exception::Exception,"Failed to send SOAP command.",e);
198 // }
199 //
200 //}
201 
202 //========================================================================================================================
203 xoap::MessageReference SOAPMessenger::sendWithSOAPReply(XDAQ_CONST_CALL xdaq::ApplicationDescriptor* ind,
204  std::string cmd)
205 
206 {
207  return sendWithSOAPReply(ind, SOAPUtilities::makeSOAPMessageReference(cmd));
208 }
209 //{
210 // //const_cast away the const
211 // // so that this line is compatible with slf6 and slf7 versions of xdaq
212 // // where they changed to XDAQ_CONST_CALL xdaq::ApplicationDescriptor* in slf7
213 // XDAQ_CONST_CALL xdaq::ApplicationDescriptor* d =
214 // const_cast<xdaq::ApplicationDescriptor*>(ind);
215 // try
216 // {
217 // xoap::MessageReference message = SOAPUtilities::makeSOAPMessageReference(cmd);
218 // message->getMimeHeaders()->setHeader("Content-Location", d->getURN());
219 //
220 // __COUT__ << d->getURN() << __E__;
221 // __COUT__ << SOAPUtilities::translate(message) << __E__;
222 // std::string mystring;
223 // message->writeTo(mystring);
224 // __COUT__<< mystring << std::endl;
225 //
226 // xoap::MessageReference reply = theApplication_->getApplicationContext()->postSOAP(message,
227 // *(theApplication_->getApplicationDescriptor()),
228 // *d);
229 // return reply;
230 // }
231 
232 // catch (xdaq::exception::Exception& e)
233 // {
234 // __COUT__ << "This application failed to send a SOAP message to "
235 // << d->getClassName() << " instance " << d->getInstance()
236 // << " with command = " << cmd
237 // << " re-throwing exception = " << xcept::stdformat_exception_history(e)<<std::endl;
238 // XCEPT_RETHROW(xdaq::exception::Exception,"Failed to send SOAP command.",e);
239 // }
240 //
241 //}
242 
243 //========================================================================================================================
244 xoap::MessageReference SOAPMessenger::sendWithSOAPReply(XDAQ_CONST_CALL xdaq::ApplicationDescriptor *ind,
245  xoap::MessageReference message)
246 
247 {
248  //const_cast away the const
249  // so that this line is compatible with slf6 and slf7 versions of xdaq
250  // where they changed to XDAQ_CONST_CALL xdaq::ApplicationDescriptor* in slf7
251  XDAQ_CONST_CALL xdaq::ApplicationDescriptor* d =
252  const_cast<xdaq::ApplicationDescriptor*>(ind);
253  try
254  {
255  message->getMimeHeaders()->setHeader("Content-Location", d->getURN());
256 
257  //__COUT__ << d->getURN() << __E__;
258  //__COUT__ << SOAPUtilities::translate(message) << __E__;
259  std::string mystring;
260  message->writeTo(mystring);
261  //__COUT__<< mystring << std::endl;
262 
263  xoap::MessageReference reply = theApplication_->getApplicationContext()->postSOAP(message,
264  *(theApplication_->getApplicationDescriptor()),
265  *d);
266  return reply;
267  }
268  catch (xdaq::exception::Exception& e)
269  {
270  __COUT__ << "This application failed to send a SOAP message to "
271  << d->getClassName() << " instance " << d->getInstance()
272  << " re-throwing exception = " << xcept::stdformat_exception_history(e);
273  std::string mystring;
274  message->writeTo(mystring);
275  __COUT_ERR__<< mystring << std::endl;
276  XCEPT_RETHROW(xdaq::exception::Exception,"Failed to send SOAP command.",e);
277  }
278 }
279 
280 //========================================================================================================================
281 xoap::MessageReference SOAPMessenger::sendWithSOAPReply(XDAQ_CONST_CALL xdaq::ApplicationDescriptor* ind,
282  std::string cmd, SOAPParameters parameters)
283 
284 {
285  return sendWithSOAPReply(ind, SOAPUtilities::makeSOAPMessageReference(cmd, parameters));
286 }
287 //{
288 // //const_cast away the const
289 // // so that this line is compatible with slf6 and slf7 versions of xdaq
290 // // where they changed to XDAQ_CONST_CALL xdaq::ApplicationDescriptor* in slf7
291 // XDAQ_CONST_CALL xdaq::ApplicationDescriptor* d =
292 // const_cast<xdaq::ApplicationDescriptor*>(ind);
293 // try
294 // {
295 // xoap::MessageReference message = SOAPUtilities::makeSOAPMessageReference(cmd, parameters);
296 // message->getMimeHeaders()->setHeader("Content-Location", d->getURN());
297 // xoap::MessageReference reply = theApplication_->getApplicationContext()->postSOAP(message,
298 // *(theApplication_->getApplicationDescriptor()), *d);
299 // return reply;
300 // }
301 // catch (xdaq::exception::Exception& e)
302 // {
303 // __COUT__ << "This application failed to send a SOAP message to "
304 // << d->getClassName() << " instance " << d->getInstance()
305 // << " with command = " << cmd
306 // << " re-throwing exception = " << xcept::stdformat_exception_history(e);
307 // XCEPT_RETHROW(xdaq::exception::Exception,"Failed to send SOAP command.",e);
308 // }
309 //
310 //}
311 
312 //========================================================================================================================
313 /*
314 std::string SOAPMessenger::send(XDAQ_CONST_CALL xdaq::ApplicationDescriptor* d, std::string cmd, std::string filepath)
315 {
316 
317  try
318  {
319  __COUT__ << "SOAP XML file path : " << filepath << std::endl;
320  xoap::MessageReference message = xoap::createMessage();
321  xoap::SOAPPart soap = message->getSOAPPart();
322  xoap::SOAPEnvelope envelope = soap.getEnvelope();
323  xoap::AttachmentPart * attachment;
324  attachment = message->createAttachmentPart();
325  attachment->setContent(filepath);
326  attachment->setContentId("SOAPTEST1");
327  attachment->addMimeHeader("Content-Description", "This is a SOAP message with attachments");
328  message->addAttachmentPart(attachment);
329  xoap::SOAPName command = envelope.createName(cmd, "xdaq", XDAQ_NS_URI);
330  xoap::SOAPBody body= envelope.getBody();
331  body.addBodyElement(command);
332 #ifdef DEBUGMSG
333  std::string mystring;
334  message->writeTo(mystring);
335  __COUT__ << "SOAP Message : "<< mystring <<std::endl << std::endl;
336 #endif
337  message->getMimeHeaders()->setHeader("Content-Location", d->getURN());
338  xoap::MessageReference reply=theApplication_->getApplicationContext()->postSOAP(message, *(theApplication_->getApplicationDescriptor()), *d);
339  std::string replyString = receive(reply);
340  return replyString;
341  }
342  catch (xdaq::exception::Exception& e)
343  {
344  XCEPT_RETHROW(xdaq::exception::Exception,"Failed to send SOAP command.",e);
345  }
346 }
347  */
348 //========================================================================================================================
349 std::string SOAPMessenger::sendStatus(XDAQ_CONST_CALL xdaq::ApplicationDescriptor* ind,
350  std::string message)
351 
352 {
353  //const_cast away the const
354  // so that this line is compatible with slf6 and slf7 versions of xdaq
355  // where they changed to XDAQ_CONST_CALL xdaq::ApplicationDescriptor* in slf7
356  XDAQ_CONST_CALL xdaq::ApplicationDescriptor* d =
357  const_cast<xdaq::ApplicationDescriptor*>(ind);
358 
359  std::string cmd = "StatusNotification";
360  try
361  {
362  timeval tv; //keep track of when the message comes
363  gettimeofday(&tv,NULL);
364 
365  std::stringstream ss;
366  SOAPParameters parameters;
367  parameters.addParameter("Description",message);
368  ss.str(""); ss << tv.tv_sec;
369  parameters.addParameter("Time",ss.str());
370  ss.str(""); ss << tv.tv_usec;
371  parameters.addParameter("usec",ss.str());
372  return send(d, cmd, parameters);
373  }
374  catch (xdaq::exception::Exception& e)
375  {
376  __COUT__ << "This application failed to send a SOAP error message to "
377  << d->getClassName() << " instance " << d->getInstance()
378  << " with command = " << cmd
379  << " re-throwing exception = " << xcept::stdformat_exception_history(e) << std::endl;
380  XCEPT_RETHROW(xdaq::exception::Exception,"Failed to send SOAP command.",e);
381  }
382 }