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