otsdaq  3.06.00
ARTDAQSupervisor.hh
1 #ifndef _ots_ARTDAQSupervisor_h
2 #define _ots_ARTDAQSupervisor_h
3 
4 #if __cplusplus > 201402L
5 #pragma GCC diagnostic push
6 #pragma GCC diagnostic ignored "-Wregister"
7 #include <Python.h>
8 #pragma GCC diagnostic pop
9 #else
10 #include <Python.h>
11 #endif
12 
13 #include <mutex>
14 #include <thread>
15 
16 #include "artdaq/ExternalComms/CommanderInterface.hh"
17 #include "otsdaq/CoreSupervisors/CoreSupervisorBase.h"
18 #include "otsdaq/TablePlugins/ARTDAQTableBase/ARTDAQTableBase.h"
19 
20 namespace ots
21 {
25 {
26  public:
27  XDAQ_INSTANTIATOR();
28 
30  {
31  std::string label;
32  std::string host;
33  int port;
34  int subsystem;
35  int rank;
36  std::string state;
37  };
38 
39  ARTDAQSupervisor(xdaq::ApplicationStub* s);
40  virtual ~ARTDAQSupervisor(void);
41 
42  void init(void);
43  void destroy(void);
44 
45  virtual void transitionConfiguring(toolbox::Event::Reference event) override;
46  virtual void transitionHalting(toolbox::Event::Reference event) override;
47  virtual void transitionInitializing(toolbox::Event::Reference event) override;
48  virtual void transitionPausing(toolbox::Event::Reference event) override;
49  virtual void transitionResuming(toolbox::Event::Reference event) override;
50  virtual void transitionStarting(toolbox::Event::Reference event) override;
51  virtual void transitionStopping(toolbox::Event::Reference event) override;
52  virtual void enteringError(toolbox::Event::Reference event) override;
53 
54  virtual std::vector<SupervisorInfo::SubappInfo> getSubappInfo(void) override;
55  virtual std::string getStatusProgressDetail(void) override
56  {
57  if(!theStateMachine_.isInTransition() &&
58  (theStateMachine_.getCurrentStateName() ==
59  RunControlStateMachine::HALTED_STATE_NAME ||
60  theStateMachine_.getCurrentStateName() ==
61  RunControlStateMachine::INITIAL_STATE_NAME))
63 
64  std::lock_guard<std::mutex> lk(thread_mutex_);
65  __COUTVS__(20, thread_progress_message_);
66  return thread_progress_message_;
67  } //end getStatusProgressDetail()
68 
69  std::list<
70  std::pair<DAQInterfaceProcessInfo, std::unique_ptr<artdaq::CommanderInterface>>>
71  makeCommandersFromProcessInfo();
72 
73  static std::list<std::string> tokenize_(std::string const& input);
74 
75  private:
76  void configuringThread(void);
77  void startingThread(void);
78 
80  struct PyObjectGuard
81  {
82  PyObject* obj;
83  explicit PyObjectGuard(PyObject* o)
84  : obj(o) {}
85  ~PyObjectGuard()
86  {
87  if(obj)
88  Py_DECREF(obj);
89  }
90  PyObjectGuard(const PyObjectGuard&) = delete;
91  PyObjectGuard& operator=(const PyObjectGuard&) = delete;
92  PyObject* get() const { return obj; }
93  };
94 
95  PyObject *daqinterface_ptr_, *stringIO_out_,
96  *stringIO_err_; //stringIO_err_ not needed with new Tee Buffer solution
97  std::recursive_mutex daqinterface_pythonMutex_;
98  std::mutex daqinterface_statusMutex_;
99  std::string daqinterface_status_;
100  int partition_;
101  std::string daqinterface_state_;
102  std::unique_ptr<std::thread> runner_thread_;
103  std::atomic<bool> runner_running_;
104 
105  std::mutex thread_mutex_;
106  ProgressBar thread_progress_bar_;
107  std::string thread_progress_message_;
108  std::string thread_error_message_;
109  int last_thread_progress_read_;
110  time_t last_thread_progress_update_;
111  std::map<std::string, std::string> label_to_proc_type_map_;
112 
113  std::string capturePyErr(std::string label = "");
114  bool checkPythonError(
115  PyObject* result); // Check if Python call failed (returns true on error)
116  std::string captureStderrAndStdout_(std::string label = "");
117  void getDAQState_(void);
118  std::string getProcessInfo_(void);
119  std::string artdaqStateToOtsState(std::string state);
120  std::string labelToProcType_(std::string label);
121  std::list<DAQInterfaceProcessInfo> getAndParseProcessInfo_(void);
122  void daqinterfaceRunner_(void);
123  void stop_runner_(void);
124  void start_runner_(void);
125  void set_thread_message_(std::string msg)
126  {
127  std::lock_guard<std::mutex> lk(thread_mutex_);
128  thread_progress_message_ = msg;
129  }
130 };
131 
132 } // namespace ots
133 
134 #endif
virtual void transitionHalting(toolbox::Event::Reference event) override
virtual void transitionInitializing(toolbox::Event::Reference event) override
virtual std::string getStatusProgressDetail(void) override
virtual std::string getStatusProgressDetail(void)
defines used also by OtsConfigurationWizardSupervisor