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