tdaq-develop-2025-02-12
SimpleSoap.cc
1 /*--------------------------------------------------------------------
2  To compile: make
3  To run:
4 --------------------------------------------------------------------*/
5 
6 #include "Tests/SimpleSoap/include/SimpleSoap.h"
7 #include <xdaq/NamespaceURI.h>
8 #include <xoap/Method.h>
9 #include <iostream>
10 #include "Utilities/MacroUtilities/include/Macro.h"
11 #include "Utilities/SOAPUtilities/include/SOAPUtilities.h"
12 
13 using namespace ots;
14 
15 XDAQ_INSTANTIATOR_IMPL(SimpleSoap)
16 
17 //==============================================================================
18 SimpleSoap::SimpleSoap(xdaq::ApplicationStub* s)
19  : xdaq::Application(s), SOAPMessenger(this)
20 {
21  xgi::bind(this, &SimpleSoap::Default, "Default");
22  xgi::bind(this, &SimpleSoap::StateMachineXgiHandler, "StateMachineXgiHandler");
23 
24  xoap::bind(this,
25  &SimpleSoap::Start,
26  RunControlStateMachine::START_TRANSITION_NAME,
27  XDAQ_NS_URI);
28  fsm_.addState(
29  'I', RunControlStateMachine::INITIAL_STATE_NAME, this, &SimpleSoap::stateInitial);
30  fsm_.addState(
31  'H', RunControlStateMachine::HALTED_STATE_NAME, this, &SimpleSoap::stateHalted);
32  fsm_.addStateTransition('I', 'H', RunControlStateMachine::INIT_TRANSITION_NAME);
33 }
34 
35 //==============================================================================
36 void SimpleSoap::Default(xgi::Input* in, xgi::Output* out)
37 {
38  std::string url = "/" + getApplicationDescriptor()->getURN();
39  std::cout << __COUT_HDR_FL__ << url << std::endl;
40  // url = "http://131.225.82.72:1983";
41  *out << cgicc::HTMLDoctype(cgicc::HTMLDoctype::eStrict) << std::endl;
42  *out << cgicc::html().set("lang", "en").set("dir", "ltr") << std::endl;
43  *out << cgicc::title("Simple button page") << std::endl;
44  *out << "<body>" << std::endl;
45  *out << " <form name=\"input\" method=\"get\" action=\"" << url
46  << "/StateMachineXgiHandler"
47  << "\" enctype=\"multipart/form-data\">" << std::endl;
48  *out << " <p align=\"left\"><input type=\"submit\" name=\"StateInput\" "
49  "value=\"PushStart\"/></p>"
50  << std::endl;
51  *out << " </form>" << std::endl;
52  *out << " <form name=\"input\" method=\"get\" action=\"" << url << "/Start"
53  << "\" enctype=\"multipart/form-data\">" << std::endl;
54  *out << " <p align=\"left\"><input type=\"submit\" name=\"StateInput\" "
55  "value=\"Start\"/></p>"
56  << std::endl;
57  *out << " </form>" << std::endl;
58  *out << "</body>" << std::endl;
59 }
60 
61 //==============================================================================
62 void SimpleSoap::StateMachineXgiHandler(xgi::Input* in, xgi::Output* out)
63 {
64  cgicc::Cgicc cgi(in);
65  std::string Command = cgi.getElement("StateInput")->getValue();
66 
67  if(Command == "PushStart")
68  {
69  std::cout << __COUT_HDR_FL__ << "Got start" << std::endl;
70  xoap::MessageReference msg = SOAPUtilities::makeSOAPMessageReference("Start");
71  xoap::MessageReference reply = Start(msg);
72 
73  if(receive(reply) == "StartDone")
74  std::cout << __COUT_HDR_FL__ << "Everything started correctly!" << std::endl
75  << std::endl;
76  else
77  std::cout
78  << __COUT_HDR_FL__
79  << "All underlying Supervisors could not be started by browser button!"
80  << std::endl
81  << std::endl;
82  }
83  else if(Command == "Start")
84  {
85  // std::set<xdaq::ApplicationDescriptor*> set_SimpleSoap =
86  // getApplicationContext()->getDefaultZone()->getApplicationGroup("rivera")->getApplicationDescriptors("SimpleSoap::SimpleSoap");
87  std::set<xdaq::ApplicationDescriptor*> set_SimpleSoap =
88  getApplicationContext()
89  ->getDefaultZone()
90  ->getApplicationGroup("daq")
91  ->getApplicationDescriptors("SimpleSoap::SimpleSoap");
92 
93  for(std::set<xdaq::ApplicationDescriptor*>::iterator i_set_SimpleSoap =
94  set_SimpleSoap.begin();
95  i_set_SimpleSoap != set_SimpleSoap.end();
96  ++i_set_SimpleSoap)
97  {
98  try
99  {
100  std::string sReply = send(*i_set_SimpleSoap, Command.c_str());
101 
102  if(sReply == "StartDone")
103  std::cout << __COUT_HDR_FL__ << "Everything started correctly!"
104  << std::endl
105  << std::endl;
106  else
107  std::cout << __COUT_HDR_FL__
108  << "All underlying Supervisors could not be started by "
109  "browser button!"
110  << std::endl
111  << std::endl;
112  }
113  catch(xdaq::exception::Exception& e)
114  {
115  std::cout << __COUT_HDR_FL__
116  //<< std::stringF((*i_set_SimpleSoap)->getInstance())
117  << " Couldn't start sending a msg" << std::endl;
118  }
119  }
120  }
121  else
122  {
123  std::cout << __COUT_HDR_FL__ << "Don't understand the command: " << Command
124  << std::endl;
125  }
126 
127  this->Default(in, out);
128 }
129 
130 //==============================================================================
131 xoap::MessageReference SimpleSoap::Start(xoap::MessageReference msg)
132 {
133  std::cout << __COUT_HDR_FL__ << "Starting" << std::endl;
134  return SOAPUtilities::makeSOAPMessageReference("StartDone");
135 }
136 
137 //==============================================================================
138 void SimpleSoap::stateInitial(toolbox::fsm::FiniteStateMachine& fsm)
139 {
140  std::cout << __COUT_HDR_FL__ << "--- SimpleWeb is in its Initial state ---"
141  << std::endl;
142  state_ = fsm_.getStateName(fsm_.getCurrentState());
143 
144  // diagService_->reportError("PixelSupervisor::stateInitial: workloop active:
145  // "+stringF(calibWorkloop_->isActive())+", workloop type:
146  // "+calibWorkloop_->getType(),DIAGINFO);
147 }
148 
149 //==============================================================================
150 void SimpleSoap::stateHalted(toolbox::fsm::FiniteStateMachine& fsm)
151 {
152  std::cout << __COUT_HDR_FL__ << "--- SimpleWeb is in its Halted state ---"
153  << std::endl;
154  state_ = fsm_.getStateName(fsm_.getCurrentState());
155 }
std::string send(XDAQ_CONST_CALL xdaq::ApplicationDescriptor *d, xoap::MessageReference message)