otsdaq  v1_01_02
 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  return replyString;
110  }
111  catch (xdaq::exception::Exception& e)
112  {
113  std::cout << __COUT_HDR_FL__ << "This application failed to send a SOAP message to "
114  << d->getClassName() << " instance " << d->getInstance()
115  << " re-throwing exception = " << xcept::stdformat_exception_history(e);
116  std::string mystring;
117  message->writeTo(mystring);
118  std::cout << __COUT_HDR_FL__<< mystring << std::endl;
119  XCEPT_RETHROW(xdaq::exception::Exception,"Failed to send SOAP command.",e);
120  }
121 }
122 
123 //========================================================================================================================
124 std::string SOAPMessenger::send(const xdaq::ApplicationDescriptor* d, SOAPCommand soapCommand)
125 throw (xdaq::exception::Exception)
126 {
127  if(soapCommand.hasParameters())
128  return send(d, soapCommand.getCommand(), soapCommand.getParameters());
129  else
130  return send(d, soapCommand.getCommand());
131 }
132 
133 //========================================================================================================================
134 std::string SOAPMessenger::send(const xdaq::ApplicationDescriptor* d, std::string command)
135 throw (xdaq::exception::Exception)
136 {
137  xoap::MessageReference message;
138  message = SOAPUtilities::makeSOAPMessageReference(command);
139  return send(d, message);
140 }
141 
142 //========================================================================================================================
143 std::string SOAPMessenger::send(const xdaq::ApplicationDescriptor* ind, std::string cmd,
144  SOAPParameters parameters)
145 throw (xdaq::exception::Exception)
146 {
147  //const_cast away the const
148  // so that this line is compatible with slf6 and slf7 versions of xdaq
149  // where they changed to const xdaq::ApplicationDescriptor* in slf7
150  XDAQ_CONST_CALL xdaq::ApplicationDescriptor* d =
151  const_cast<xdaq::ApplicationDescriptor*>(ind);
152 
153  xoap::MessageReference message;
154  try
155  {
156  message = SOAPUtilities::makeSOAPMessageReference(cmd, parameters);
157  message->getMimeHeaders()->setHeader("Content-Location", d->getURN());
158  xoap::MessageReference reply = theApplication_->getApplicationContext()->postSOAP(message, *(theApplication_->getApplicationDescriptor()), *d);
159  std::string replyString = receive(reply);
160  return replyString;
161  }
162  catch (xdaq::exception::Exception& e)
163  {
164  std::cout << __COUT_HDR_FL__ << "This application failed to send a SOAP message to "
165  << d->getClassName() << " instance " << d->getInstance()
166  << " with command = " << cmd
167  << " re-throwing exception = " << xcept::stdformat_exception_history(e)<<std::endl;
168 
169  std::string mystring;
170  message->writeTo(mystring);
171  mf::LogError(__FILE__) << mystring;
172  XCEPT_RETHROW(xdaq::exception::Exception,"Failed to send SOAP command.",e);
173  }
174 
175 }
176 
177 //========================================================================================================================
178 xoap::MessageReference SOAPMessenger::sendWithSOAPReply(const xdaq::ApplicationDescriptor* ind,
179  std::string cmd)
180 throw (xdaq::exception::Exception)
181 {
182  //const_cast away the const
183  // so that this line is compatible with slf6 and slf7 versions of xdaq
184  // where they changed to const xdaq::ApplicationDescriptor* in slf7
185  XDAQ_CONST_CALL xdaq::ApplicationDescriptor* d =
186  const_cast<xdaq::ApplicationDescriptor*>(ind);
187  try
188  {
189  xoap::MessageReference message = SOAPUtilities::makeSOAPMessageReference(cmd);
190  message->getMimeHeaders()->setHeader("Content-Location", d->getURN());
191  xoap::MessageReference reply = theApplication_->getApplicationContext()->postSOAP(message, *(theApplication_->getApplicationDescriptor()), *d);
192  return reply;
193  }
194  catch (xdaq::exception::Exception& e)
195  {
196  std::cout << __COUT_HDR_FL__ << "This application failed to send a SOAP message to "
197  << d->getClassName() << " instance " << d->getInstance()
198  << " with command = " << cmd
199  << " re-throwing exception = " << xcept::stdformat_exception_history(e)<<std::endl;
200  XCEPT_RETHROW(xdaq::exception::Exception,"Failed to send SOAP command.",e);
201  }
202 
203 }
204 
205 //========================================================================================================================
206 xoap::MessageReference SOAPMessenger::sendWithSOAPReply(const xdaq::ApplicationDescriptor *ind,
207  xoap::MessageReference message)
208 throw (xdaq::exception::Exception)
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 const xdaq::ApplicationDescriptor* in slf7
213  XDAQ_CONST_CALL xdaq::ApplicationDescriptor* d =
214  const_cast<xdaq::ApplicationDescriptor*>(ind);
215  try
216  {
217  message->getMimeHeaders()->setHeader("Content-Location", d->getURN());
218  xoap::MessageReference reply = theApplication_->getApplicationContext()->postSOAP(message, *(theApplication_->getApplicationDescriptor()), *d);
219  return reply;
220  }
221  catch (xdaq::exception::Exception& e)
222  {
223  std::cout << __COUT_HDR_FL__ << "This application failed to send a SOAP message to "
224  << d->getClassName() << " instance " << d->getInstance()
225  << " re-throwing exception = " << xcept::stdformat_exception_history(e);
226  std::string mystring;
227  message->writeTo(mystring);
228  mf::LogError(__FILE__)<< mystring << std::endl;
229  XCEPT_RETHROW(xdaq::exception::Exception,"Failed to send SOAP command.",e);
230  }
231 }
232 
233 //========================================================================================================================
234 xoap::MessageReference SOAPMessenger::sendWithSOAPReply(const xdaq::ApplicationDescriptor* ind,
235  std::string cmd, SOAPParameters parameters)
236 throw (xdaq::exception::Exception)
237 {
238  //const_cast away the const
239  // so that this line is compatible with slf6 and slf7 versions of xdaq
240  // where they changed to const xdaq::ApplicationDescriptor* in slf7
241  XDAQ_CONST_CALL xdaq::ApplicationDescriptor* d =
242  const_cast<xdaq::ApplicationDescriptor*>(ind);
243  try
244  {
245  xoap::MessageReference message = SOAPUtilities::makeSOAPMessageReference(cmd, parameters);
246  message->getMimeHeaders()->setHeader("Content-Location", d->getURN());
247  xoap::MessageReference reply = theApplication_->getApplicationContext()->postSOAP(message, *(theApplication_->getApplicationDescriptor()), *d);
248  return reply;
249  }
250  catch (xdaq::exception::Exception& e)
251  {
252  std::cout << __COUT_HDR_FL__ << "This application failed to send a SOAP message to "
253  << d->getClassName() << " instance " << d->getInstance()
254  << " with command = " << cmd
255  << " re-throwing exception = " << xcept::stdformat_exception_history(e);
256  XCEPT_RETHROW(xdaq::exception::Exception,"Failed to send SOAP command.",e);
257  }
258 
259 }
260 
261 //========================================================================================================================
262 /*
263 std::string SOAPMessenger::send(XDAQ_CONST_CALL xdaq::ApplicationDescriptor* d, std::string cmd, std::string filepath) throw (xdaq::exception::Exception)
264 {
265 
266  try
267  {
268  std::cout << __COUT_HDR_FL__ << "SOAP XML file path : " << filepath << std::endl;
269  xoap::MessageReference message = xoap::createMessage();
270  xoap::SOAPPart soap = message->getSOAPPart();
271  xoap::SOAPEnvelope envelope = soap.getEnvelope();
272  xoap::AttachmentPart * attachment;
273  attachment = message->createAttachmentPart();
274  attachment->setContent(filepath);
275  attachment->setContentId("SOAPTEST1");
276  attachment->addMimeHeader("Content-Description", "This is a SOAP message with attachments");
277  message->addAttachmentPart(attachment);
278  xoap::SOAPName command = envelope.createName(cmd, "xdaq", XDAQ_NS_URI);
279  xoap::SOAPBody body= envelope.getBody();
280  body.addBodyElement(command);
281 #ifdef DEBUGMSG
282  std::string mystring;
283  message->writeTo(mystring);
284  std::cout << __COUT_HDR_FL__ << "SOAP Message : "<< mystring <<std::endl << std::endl;
285 #endif
286  message->getMimeHeaders()->setHeader("Content-Location", d->getURN());
287  xoap::MessageReference reply=theApplication_->getApplicationContext()->postSOAP(message, *(theApplication_->getApplicationDescriptor()), *d);
288  std::string replyString = receive(reply);
289  return replyString;
290  }
291  catch (xdaq::exception::Exception& e)
292  {
293  XCEPT_RETHROW(xdaq::exception::Exception,"Failed to send SOAP command.",e);
294  }
295 }
296  */
297 //========================================================================================================================
298 std::string SOAPMessenger::sendStatus(const xdaq::ApplicationDescriptor* ind,
299  std::string message)
300 throw (xdaq::exception::Exception)
301 {
302  //const_cast away the const
303  // so that this line is compatible with slf6 and slf7 versions of xdaq
304  // where they changed to const xdaq::ApplicationDescriptor* in slf7
305  XDAQ_CONST_CALL xdaq::ApplicationDescriptor* d =
306  const_cast<xdaq::ApplicationDescriptor*>(ind);
307 
308  std::string cmd = "StatusNotification";
309  try
310  {
311  timeval tv; //keep track of when the message comes
312  gettimeofday(&tv,NULL);
313 
314  std::stringstream ss;
315  SOAPParameters parameters;
316  parameters.addParameter("Description",message);
317  ss.str(""); ss << tv.tv_sec;
318  parameters.addParameter("Time",ss.str());
319  ss.str(""); ss << tv.tv_usec;
320  parameters.addParameter("usec",ss.str());
321  return send(d, cmd, parameters);
322  }
323  catch (xdaq::exception::Exception& e)
324  {
325  std::cout << __COUT_HDR_FL__ << "This application failed to send a SOAP error message to "
326  << d->getClassName() << " instance " << d->getInstance()
327  << " with command = " << cmd
328  << " re-throwing exception = " << xcept::stdformat_exception_history(e)<<std::endl;
329  XCEPT_RETHROW(xdaq::exception::Exception,"Failed to send SOAP command.",e);
330  }
331 }