tdaq-develop-2025-02-12
SOAPMessenger.cc
1 #include "otsdaq/SOAPUtilities/SOAPMessenger.h"
2 #include "otsdaq/Macros/CoutMacros.h"
3 #include "otsdaq/MessageFacility/MessageFacility.h"
4 #include "otsdaq/SOAPUtilities/SOAPCommand.h"
5 #include "otsdaq/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 //==============================================================================
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  std::string msgStr;
64  try
65  {
66  msgStr = send(d, message);
67  }
68  catch(...)
69  {
70  __COUT__ << "send failed?!" << __E__;
71  __COUT__ << SOAPUtilities::translate(message) << __E__;
72  throw;
73  }
74 
75  return msgStr;
76 }
77 
78 //==============================================================================
79 std::string SOAPMessenger::send(XDAQ_CONST_CALL xdaq::ApplicationDescriptor* ind,
80  std::string cmd,
81  SOAPParameters parameters)
82 
83 {
84  return SOAPUtilities::receive(sendWithSOAPReply(ind, cmd, parameters));
85 }
86 
87 //==============================================================================
88 xoap::MessageReference SOAPMessenger::sendWithSOAPReply(
89  XDAQ_CONST_CALL xdaq::ApplicationDescriptor* ind, std::string cmd)
90 
91 {
92  return sendWithSOAPReply(ind, SOAPUtilities::makeSOAPMessageReference(cmd));
93 }
94 
95 //==============================================================================
96 xoap::MessageReference SOAPMessenger::sendWithSOAPReply(
97  XDAQ_CONST_CALL xdaq::ApplicationDescriptor* ind, xoap::MessageReference message)
98 
99 {
100  // const_cast away the const
101  // so that this line is compatible with slf6 and slf7 versions of xdaq
102  // where they changed to XDAQ_CONST_CALL xdaq::ApplicationDescriptor* in slf7
103  XDAQ_CONST_CALL xdaq::ApplicationDescriptor* d =
104  const_cast<xdaq::ApplicationDescriptor*>(ind);
105  try
106  {
107  message->getMimeHeaders()->setHeader("Content-Location", d->getURN());
108  message->getMimeHeaders()->setHeader("xdaq-pthttp-connection-timeout",
109  "300" /*seconds*/);
110 
111  //__COUT__ << d->getURN() << __E__;
112  //__COUT__ << SOAPUtilities::translate(message) << __E__;
113  // std::string mystring;
114  // message->writeTo(mystring);
115  //__COUT__<< mystring << std::endl;
116 
117  xoap::MessageReference reply = theApplication_->getApplicationContext()->postSOAP(
118  message, *(theApplication_->getApplicationDescriptor()), *d);
119  return reply;
120  }
121  catch(xdaq::exception::Exception& e)
122  {
123  //__COUT_ERR__ << "This application failed to send a SOAP message to " << d->getClassName() << " instance " << d->getInstance()
124  // << " re-throwing exception = " << xcept::stdformat_exception_history(e);
125  // std::string mystring;
126  // message->writeTo(mystring);
127  //__COUT_ERR__ << mystring << std::endl;
128  XCEPT_RETHROW(xdaq::exception::Exception, "Failed to send SOAP command.", e);
129  }
130 }
131 
132 //==============================================================================
133 xoap::MessageReference SOAPMessenger::sendWithSOAPReply(
134  XDAQ_CONST_CALL xdaq::ApplicationDescriptor* ind,
135  std::string cmd,
136  SOAPParameters parameters)
137 
138 {
139  return sendWithSOAPReply(ind,
140  SOAPUtilities::makeSOAPMessageReference(cmd, parameters));
141 }
142 
143 //==============================================================================
144 std::string SOAPMessenger::sendStatus(XDAQ_CONST_CALL xdaq::ApplicationDescriptor* ind,
145  std::string message)
146 
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 XDAQ_CONST_CALL xdaq::ApplicationDescriptor* in slf7
151  XDAQ_CONST_CALL xdaq::ApplicationDescriptor* d =
152  const_cast<xdaq::ApplicationDescriptor*>(ind);
153 
154  std::string cmd = "StatusNotification";
155  try
156  {
157  timeval tv; // keep track of when the message comes
158  gettimeofday(&tv, NULL);
159 
160  std::stringstream ss;
161  SOAPParameters parameters;
162  parameters.addParameter("Description", message);
163  ss.str("");
164  ss << tv.tv_sec;
165  parameters.addParameter("Time", ss.str());
166  ss.str("");
167  ss << tv.tv_usec;
168  parameters.addParameter("usec", ss.str());
169  return send(d, cmd, parameters);
170  }
171  catch(xdaq::exception::Exception& e)
172  {
173  __COUT__ << "This application failed to send a SOAP error message to "
174  << d->getClassName() << " instance " << d->getInstance()
175  << " with command = " << cmd
176  << " re-throwing exception = " << xcept::stdformat_exception_history(e)
177  << std::endl;
178  XCEPT_RETHROW(xdaq::exception::Exception, "Failed to send SOAP command.", e);
179  }
180 }
const std::string & getCommand(void) const
Getters.
Definition: SOAPCommand.cc:44
std::string send(XDAQ_CONST_CALL xdaq::ApplicationDescriptor *d, xoap::MessageReference message)