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