otsdaq  v2_05_02_indev
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) : theApplication_(application) {}
24 
25 //==============================================================================
26 SOAPMessenger::SOAPMessenger(const SOAPMessenger& aSOAPMessenger) : theApplication_(aSOAPMessenger.theApplication_) {}
27 
28 //==============================================================================
29 // in xdaq
30 // xdaq::ApplicationDescriptor* sourceptr;
31 // void getURN()
32 //
33 std::string SOAPMessenger::send(XDAQ_CONST_CALL xdaq::ApplicationDescriptor* ind, xoap::MessageReference message)
34 
35 {
36  return SOAPUtilities::receive(sendWithSOAPReply(ind, message));
37 }
38 
39 //==============================================================================
40 std::string SOAPMessenger::send(XDAQ_CONST_CALL xdaq::ApplicationDescriptor* d, SOAPCommand soapCommand)
41 
42 {
43  if(soapCommand.hasParameters())
44  return send(d, soapCommand.getCommand(), soapCommand.getParameters());
45  else
46  return send(d, soapCommand.getCommand());
47 }
48 
49 //==============================================================================
50 std::string SOAPMessenger::send(XDAQ_CONST_CALL xdaq::ApplicationDescriptor* d, std::string command)
51 
52 {
53  xoap::MessageReference message = SOAPUtilities::makeSOAPMessageReference(command);
54  return send(d, message);
55 }
56 
57 //==============================================================================
58 std::string SOAPMessenger::send(XDAQ_CONST_CALL xdaq::ApplicationDescriptor* ind, std::string cmd, SOAPParameters parameters)
59 
60 {
61  return SOAPUtilities::receive(sendWithSOAPReply(ind, cmd, parameters));
62 }
63 
64 //==============================================================================
65 xoap::MessageReference SOAPMessenger::sendWithSOAPReply(XDAQ_CONST_CALL xdaq::ApplicationDescriptor* ind, std::string cmd)
66 
67 {
68  return sendWithSOAPReply(ind, SOAPUtilities::makeSOAPMessageReference(cmd));
69 }
70 
71 //==============================================================================
72 xoap::MessageReference SOAPMessenger::sendWithSOAPReply(XDAQ_CONST_CALL xdaq::ApplicationDescriptor* ind, xoap::MessageReference message)
73 
74 {
75  // const_cast away the const
76  // so that this line is compatible with slf6 and slf7 versions of xdaq
77  // where they changed to XDAQ_CONST_CALL xdaq::ApplicationDescriptor* in slf7
78  XDAQ_CONST_CALL xdaq::ApplicationDescriptor* d = const_cast<xdaq::ApplicationDescriptor*>(ind);
79  try
80  {
81  message->getMimeHeaders()->setHeader("Content-Location", d->getURN());
82 
83  //__COUT__ << d->getURN() << __E__;
84  //__COUT__ << SOAPUtilities::translate(message) << __E__;
85  std::string mystring;
86  message->writeTo(mystring);
87  //__COUT__<< mystring << std::endl;
88 
89  xoap::MessageReference reply = theApplication_->getApplicationContext()->postSOAP(message, *(theApplication_->getApplicationDescriptor()), *d);
90  return reply;
91  }
92  catch(xdaq::exception::Exception& e)
93  {
94  //__COUT__ << "This application failed to send a SOAP message to " << d->getClassName() << " instance " << d->getInstance()
95  // << " re-throwing exception = " << xcept::stdformat_exception_history(e);
96  std::string mystring;
97  message->writeTo(mystring);
98  //__COUT_ERR__ << mystring << std::endl;
99  XCEPT_RETHROW(xdaq::exception::Exception, "Failed to send SOAP command.", e);
100  }
101 }
102 
103 //==============================================================================
104 xoap::MessageReference SOAPMessenger::sendWithSOAPReply(XDAQ_CONST_CALL xdaq::ApplicationDescriptor* ind, std::string cmd, SOAPParameters parameters)
105 
106 {
107  return sendWithSOAPReply(ind, SOAPUtilities::makeSOAPMessageReference(cmd, parameters));
108 }
109 
110 //==============================================================================
111 std::string SOAPMessenger::sendStatus(XDAQ_CONST_CALL xdaq::ApplicationDescriptor* ind, std::string message)
112 
113 {
114  // const_cast away the const
115  // so that this line is compatible with slf6 and slf7 versions of xdaq
116  // where they changed to XDAQ_CONST_CALL xdaq::ApplicationDescriptor* in slf7
117  XDAQ_CONST_CALL xdaq::ApplicationDescriptor* d = const_cast<xdaq::ApplicationDescriptor*>(ind);
118 
119  std::string cmd = "StatusNotification";
120  try
121  {
122  timeval tv; // keep track of when the message comes
123  gettimeofday(&tv, NULL);
124 
125  std::stringstream ss;
126  SOAPParameters parameters;
127  parameters.addParameter("Description", message);
128  ss.str("");
129  ss << tv.tv_sec;
130  parameters.addParameter("Time", ss.str());
131  ss.str("");
132  ss << tv.tv_usec;
133  parameters.addParameter("usec", ss.str());
134  return send(d, cmd, parameters);
135  }
136  catch(xdaq::exception::Exception& e)
137  {
138  __COUT__ << "This application failed to send a SOAP error message to " << d->getClassName() << " instance " << d->getInstance()
139  << " with command = " << cmd << " re-throwing exception = " << xcept::stdformat_exception_history(e) << std::endl;
140  XCEPT_RETHROW(xdaq::exception::Exception, "Failed to send SOAP command.", e);
141  }
142 }