otsdaq  3.09.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 <set>
15 #include <thread>
16 
17 #include "artdaq/ExternalComms/CommanderInterface.hh"
18 #include "otsdaq/CoreSupervisors/CoreSupervisorBase.h"
19 #include "otsdaq/TablePlugins/ARTDAQTableBase/ARTDAQTableBase.h"
20 
21 namespace ots
22 {
26 {
27  public:
28  XDAQ_INSTANTIATOR();
29 
31  {
32  std::string label;
33  std::string host;
34  int port;
35  int subsystem;
36  int rank;
37  std::string state;
38  };
39 
40  ARTDAQSupervisor(xdaq::ApplicationStub* s);
41  virtual ~ARTDAQSupervisor(void);
42 
43  void init(void);
44  void destroy(void);
45 
46  virtual void transitionConfiguring(toolbox::Event::Reference event) override;
47  virtual void transitionHalting(toolbox::Event::Reference event) override;
48  virtual void transitionInitializing(toolbox::Event::Reference event) override;
49  virtual void transitionPausing(toolbox::Event::Reference event) override;
50  virtual void transitionResuming(toolbox::Event::Reference event) override;
51  virtual void transitionStarting(toolbox::Event::Reference event) override;
52  virtual void transitionStopping(toolbox::Event::Reference event) override;
53  virtual void enteringError(toolbox::Event::Reference event) override;
54 
55  virtual std::vector<SupervisorInfo::SubappInfo> getSubappInfo(void) override;
56  virtual std::string getStatusProgressDetail(void) override
57  {
58  if(!theStateMachine_.isInTransition() &&
59  (theStateMachine_.getCurrentStateName() ==
60  RunControlStateMachine::HALTED_STATE_NAME ||
61  theStateMachine_.getCurrentStateName() ==
62  RunControlStateMachine::INITIAL_STATE_NAME))
64 
65  std::lock_guard<std::mutex> lk(thread_mutex_);
66  __COUTVS__(20, thread_progress_message_);
67  return thread_progress_message_;
68  } //end getStatusProgressDetail()
69 
70  std::list<
71  std::pair<DAQInterfaceProcessInfo, std::unique_ptr<artdaq::CommanderInterface>>>
72  makeCommandersFromProcessInfo();
73 
74  // Hostnames of all enabled artdaq processes from the active configuration.
75  // Reflects the configuration's intended deployment (works even when
76  // DAQInterface is not running). Note: config does NOT carry the runtime
77  // xmlrpc commander ports -- this is for host discovery only.
78  std::set<std::string> getConfiguredArtdaqHosts(void);
79 
80  static std::list<std::string> tokenize_(std::string const& input);
81 
82  private:
83  void configuringThread(void);
84  void startingThread(void);
85 
87  struct PyObjectGuard
88  {
89  PyObject* obj;
90  explicit PyObjectGuard(PyObject* o)
91  : obj(o) {}
92  ~PyObjectGuard()
93  {
94  if(obj)
95  Py_DECREF(obj);
96  }
97  PyObjectGuard(const PyObjectGuard&) = delete;
98  PyObjectGuard& operator=(const PyObjectGuard&) = delete;
99  PyObject* get() const { return obj; }
100  };
101 
102  PyObject *daqinterface_ptr_, *stringIO_out_,
103  *stringIO_err_; //stringIO_err_ not needed with new Tee Buffer solution
104  std::recursive_mutex daqinterface_pythonMutex_;
105  std::mutex daqinterface_statusMutex_;
106  std::string daqinterface_status_;
107  int partition_;
108  std::string daqinterface_state_;
109  std::unique_ptr<std::thread> runner_thread_;
110  std::atomic<bool> runner_running_;
111 
112  std::mutex thread_mutex_;
113  ProgressBar thread_progress_bar_;
114  std::string thread_progress_message_;
115  std::string thread_error_message_;
116  int last_thread_progress_read_;
117  time_t last_thread_progress_update_;
118  std::map<std::string, std::string> label_to_proc_type_map_;
119 
120  std::string capturePyErr(std::string label = "");
121  bool checkPythonError(
122  PyObject* result); // Check if Python call failed (returns true on error)
123  std::string captureStderrAndStdout_(std::string label = "");
124  void getDAQState_(void);
125  std::string getProcessInfo_(void);
126  std::string artdaqStateToOtsState(std::string state);
127  std::string labelToProcType_(std::string label);
128  std::list<DAQInterfaceProcessInfo> getAndParseProcessInfo_(void);
129  void daqinterfaceRunner_(void);
130  void stop_runner_(void);
131  void start_runner_(void);
132  void set_thread_message_(std::string msg)
133  {
134  std::lock_guard<std::mutex> lk(thread_mutex_);
135  thread_progress_message_ = msg;
136  }
137 };
138 
139 } // namespace ots
140 
141 #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