otsdaq  v2_04_01
SOAPMessenger.cc
1 #include "otsdaq-core/SOAPUtilities/SOAPMessenger.h"
2 #include "otsdaq-core/Macros/CoutMacros.h"
3 #include "otsdaq-core/MessageFacility/MessageFacility.h"
4 #include "otsdaq-core/SOAPUtilities/SOAPCommand.h"
5 #include "otsdaq-core/SOAPUtilities/SOAPUtilities.h"
6 
7 #include <xdaq/NamespaceURI.h>
8 #include <xoap/MessageReference.h>
9 #include <xoap/Method.h>
10 #pragma GCC diagnostic push
11 #pragma GCC diagnostic ignored "-Wnon-virtual-dtor"
12 #include <xoap/MessageFactory.h>
13 #pragma GCC diagnostic pop
14 #include <xoap/AttachmentPart.h>
15 #include <xoap/SOAPBody.h>
16 #include <xoap/SOAPEnvelope.h>
17 #include <xoap/SOAPPart.h>
18 #include <xoap/domutils.h>
19 
20 using namespace ots;
21 
22 //========================================================================================================================
23 SOAPMessenger::SOAPMessenger(xdaq::Application* application)
24  : theApplication_(application)
25 {
26 }
27 
28 //========================================================================================================================
29 SOAPMessenger::SOAPMessenger(const SOAPMessenger& aSOAPMessenger)
30  : theApplication_(aSOAPMessenger.theApplication_)
31 {
32 }
33 
34 //========================================================================================================================
35 // in xdaq
36 // xdaq::ApplicationDescriptor* sourceptr;
37 // void getURN()
38 //
39 std::string SOAPMessenger::send(XDAQ_CONST_CALL xdaq::ApplicationDescriptor* ind,
40  xoap::MessageReference message)
41 
42 {
43  return SOAPUtilities::receive(sendWithSOAPReply(ind, message));
44 }
45 
46 //========================================================================================================================
47 std::string SOAPMessenger::send(XDAQ_CONST_CALL xdaq::ApplicationDescriptor* d,
48  SOAPCommand soapCommand)
49 
50 {
51  if(soapCommand.hasParameters())
52  return send(d, soapCommand.getCommand(), soapCommand.getParameters());
53  else
54  return send(d, soapCommand.getCommand());
55 }
56 
57 //========================================================================================================================
58 std::string SOAPMessenger::send(XDAQ_CONST_CALL xdaq::ApplicationDescriptor* d,
59  std::string command)
60 
61 {
62  xoap::MessageReference message = SOAPUtilities::makeSOAPMessageReference(command);
63  return send(d, message);
64 }
65 
66 //========================================================================================================================
67 std::string SOAPMessenger::send(XDAQ_CONST_CALL xdaq::ApplicationDescriptor* ind,
68  std::string cmd,
69  SOAPParameters parameters)
70 
71 {
72  return SOAPUtilities::receive(sendWithSOAPReply(ind, cmd, parameters));
73 }
74 
75 //========================================================================================================================
76 xoap::MessageReference SOAPMessenger::sendWithSOAPReply(
77  XDAQ_CONST_CALL xdaq::ApplicationDescriptor* ind, std::string cmd)
78 
79 {
80  return sendWithSOAPReply(ind, SOAPUtilities::makeSOAPMessageReference(cmd));
81 }
82 
83 //========================================================================================================================
84 xoap::MessageReference SOAPMessenger::sendWithSOAPReply(
85  XDAQ_CONST_CALL xdaq::ApplicationDescriptor* ind, xoap::MessageReference message)
86 
87 {
88  // const_cast away the const
89  // so that this line is compatible with slf6 and slf7 versions of xdaq
90  // where they changed to XDAQ_CONST_CALL xdaq::ApplicationDescriptor* in slf7
91  XDAQ_CONST_CALL xdaq::ApplicationDescriptor* d =
92  const_cast<xdaq::ApplicationDescriptor*>(ind);
93  try
94  {
95  message->getMimeHeaders()->setHeader("Content-Location", d->getURN());
96 
97  //__COUT__ << d->getURN() << __E__;
98  //__COUT__ << SOAPUtilities::translate(message) << __E__;
99  std::string mystring;
100  message->writeTo(mystring);
101  //__COUT__<< mystring << std::endl;
102 
103  xoap::MessageReference reply = theApplication_->getApplicationContext()->postSOAP(
104  message, *(theApplication_->getApplicationDescriptor()), *d);
105  return reply;
106  }
107  catch(xdaq::exception::Exception& e)
108  {
109  __COUT__ << "This application failed to send a SOAP message to "
110  << d->getClassName() << " instance " << d->getInstance()
111  << " re-throwing exception = " << xcept::stdformat_exception_history(e);
112  std::string mystring;
113  message->writeTo(mystring);
114  __COUT_ERR__ << mystring << std::endl;
115  XCEPT_RETHROW(xdaq::exception::Exception, "Failed to send SOAP command.", e);
116  }
117 }
118 
119 //========================================================================================================================
120 xoap::MessageReference SOAPMessenger::sendWithSOAPReply(
121  XDAQ_CONST_CALL xdaq::ApplicationDescriptor* ind,
122  std::string cmd,
123  SOAPParameters parameters)
124 
125 {
126  return sendWithSOAPReply(ind,
127  SOAPUtilities::makeSOAPMessageReference(cmd, parameters));
128 }
129 
130 //========================================================================================================================
131 std::string SOAPMessenger::sendStatus(XDAQ_CONST_CALL xdaq::ApplicationDescriptor* ind,
132  std::string message)
133 
134 {
135  // const_cast away the const
136  // so that this line is compatible with slf6 and slf7 versions of xdaq
137  // where they changed to XDAQ_CONST_CALL xdaq::ApplicationDescriptor* in slf7
138  XDAQ_CONST_CALL xdaq::ApplicationDescriptor* d =
139  const_cast<xdaq::ApplicationDescriptor*>(ind);
140 
141  std::string cmd = "StatusNotification";
142  try
143  {
144  timeval tv; // keep track of when the message comes
145  gettimeofday(&tv, NULL);
146 
147  std::stringstream ss;
148  SOAPParameters parameters;
149  parameters.addParameter("Description", message);
150  ss.str("");
151  ss << tv.tv_sec;
152  parameters.addParameter("Time", ss.str());
153  ss.str("");
154  ss << tv.tv_usec;
155  parameters.addParameter("usec", ss.str());
156  return send(d, cmd, parameters);
157  }
158  catch(xdaq::exception::Exception& e)
159  {
160  __COUT__ << "This application failed to send a SOAP error message to "
161  << d->getClassName() << " instance " << d->getInstance()
162  << " with command = " << cmd
163  << " re-throwing exception = " << xcept::stdformat_exception_history(e)
164  << std::endl;
165  XCEPT_RETHROW(xdaq::exception::Exception, "Failed to send SOAP command.", e);
166  }
167 }