otsdaq  v2_04_01
MessageFacilityTable_table.cc
1 #include "otsdaq-core/ConfigurationInterface/ConfigurationManager.h"
2 #include "otsdaq-core/Macros/TablePluginMacros.h"
3 #include "otsdaq-core/TablePlugins/MessageFacilityTable.h"
4 
5 #include <stdio.h>
6 #include <fstream> // std::fstream
7 #include <iostream>
8 using namespace ots;
9 
10 #define MF_CFG_FILE \
11  std::string(__ENV__("USER_DATA")) + \
12  "/MessageFacilityConfigurations/MessageFacilityGen.fcl"
13 #define QT_CFG_FILE \
14  std::string(__ENV__("USER_DATA")) + \
15  "/MessageFacilityConfigurations/QTMessageViewerGen.fcl"
16 #define QUIET_CFG_FILE \
17  std::string(__ENV__("USER_DATA")) + \
18  "/MessageFacilityConfigurations/QuietForwarderGen.cfg"
19 #define USE_WEB_BOOL_FILE \
20  std::string(__ENV__("USER_DATA")) + \
21  "/MessageFacilityConfigurations/" \
22  "UseWebConsole.bool"
23 #define USE_QT_BOOL_FILE \
24  std::string(__ENV__("USER_DATA")) + \
25  "/MessageFacilityConfigurations/" \
26  "UseQTViewer.bool"
27 
28 // MessageFacilityTable Column names
29 #define COL_NAME "UID"
30 #define COL_STATUS TableViewColumnInfo::COL_NAME_STATUS
31 #define COL_ENABLE_FWD "EnableUDPForwarding"
32 
33 #define COL_USE_WEB "ForwardToWebConsoleGUI"
34 #define COL_WEB_IP "WebConsoleForwardingIPAddress"
35 #define COL_WEB_PORT0 "WebConsoleForwardingPort0"
36 #define COL_WEB_PORT1 "WebConsoleForwardingPort1"
37 
38 #define COL_USE_QT "ForwardToQTViewerGUI"
39 #define COL_QT_IP "QTViewerForwardingIPAddress"
40 #define COL_QT_PORT "QTViewerForwardingPort"
41 
42 MessageFacilityTable::MessageFacilityTable(void) : TableBase("MessageFacilityTable")
43 {
45  // WARNING: the names used in C++ MUST match the Table INFO //
47 }
48 
49 MessageFacilityTable::~MessageFacilityTable(void) {}
50 
51 void MessageFacilityTable::init(ConfigurationManager* configManager)
52 {
53  // use isFirstAppInContext to only run once per context, for example to avoid
54  // generating files on local disk multiple times.
55  bool isFirstAppInContext = configManager->isOwnerFirstAppInContext();
56 
57  //__COUTV__(isFirstAppInContext);
58  if(!isFirstAppInContext)
59  return;
60 
61  // __COUT__ << "*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*" << std::endl;
62  // __COUT__ << configManager->__SELF_NODE__ << std::endl;
63 
64  bool status, enableFwd, useWeb, useQT;
65  int fwdPort;
66  std::string fwdIP;
67 
68  auto childrenMap = configManager->__SELF_NODE__.getChildren();
69 
70  // generate MF_CFG_FILE file
71  std::fstream fs;
72  fs.open(MF_CFG_FILE, std::fstream::out | std::fstream::trunc);
73  if(fs.fail())
74  {
75  __SS__ << "Failed to open Message Facility table file: " << MF_CFG_FILE
76  << std::endl;
77  __SS_THROW__;
78  }
79  else
80  __COUT__ << "Opened.. " << MF_CFG_FILE << __E__;
81 
82  // loop through all children just to be same as other tables
83  // exit loop after first active one
84  for(auto& child : childrenMap)
85  {
86  child.second.getNode(COL_STATUS).getValue(status);
87 
88  if(!status)
89  continue; // skip inactive rows
90 
91  child.second.getNode(COL_ENABLE_FWD).getValue(enableFwd);
92 
93  child.second.getNode(COL_USE_WEB).getValue(useWeb);
94  child.second.getNode(COL_USE_QT).getValue(useQT);
95 
96  if(useWeb && useQT)
97  {
98  fs.close();
99  __SS__ << "Illegal Message Facility table: "
100  << "Can only enable Web Console or QT Viewer, not both." << std::endl;
101  __SS_THROW__;
102  }
103 
104  std::fstream bfs;
105  // output use web bool for StartOTS.sh
106  bfs.open(USE_WEB_BOOL_FILE, std::fstream::out | std::fstream::trunc);
107  if(bfs.fail())
108  {
109  fs.close();
110  __SS__ << "Failed to open boolean Use of Web Console table file: "
111  << USE_WEB_BOOL_FILE << std::endl;
112  __SS_THROW__;
113  }
114  bfs << (useWeb ? 1 : 0);
115  bfs.close();
116 
117  // output use web bool for StartOTS.sh
118  bfs.open(USE_QT_BOOL_FILE, std::fstream::out | std::fstream::trunc);
119  if(bfs.fail())
120  {
121  fs.close();
122  __SS__ << "Failed to open boolean Use of QT Viewer table file: "
123  << USE_QT_BOOL_FILE << std::endl;
124  __SS_THROW__;
125  }
126  bfs << (useQT ? 1 : 0);
127  bfs.close();
128 
129  if(enableFwd) // write udp forward config
130  {
131  // handle using web gui
132  if(useWeb)
133  {
134  __COUT__ << "Forwarding to Web GUI with UDP forward MesageFacility "
135  "table."
136  << std::endl;
137 
138  child.second.getNode(COL_WEB_PORT0).getValue(fwdPort);
139  child.second.getNode(COL_WEB_IP).getValue(fwdIP);
140 
141  fs << "udp: {\n";
142  fs << "\t"
143  << "type: UDP\n";
144  fs << "\t"
145  << "threshold: DEBUG\n";
146  fs << "\t"
147  << "port: " << fwdPort << "\n";
148  fs << "\t"
149  << "host: \"" << fwdIP << "\"\n";
150  fs << "}\n";
151 
152  // output quiet forwarder config file
153  std::fstream qtfs;
154  qtfs.open(QUIET_CFG_FILE, std::fstream::out | std::fstream::trunc);
155  if(qtfs.fail())
156  {
157  fs.close();
158  __SS__ << "Failed to open Web Console's 'Quiet Forwarder' "
159  "table file: "
160  << QUIET_CFG_FILE << std::endl;
161  __SS_THROW__;
162  }
163  qtfs << "RECEIVE_PORT \t " << fwdPort << "\n";
164  child.second.getNode(COL_WEB_PORT1).getValue(fwdPort);
165  qtfs << "DESTINATION_PORT \t " << fwdPort << "\n";
166  qtfs << "DESTINATION_IP \t " << fwdIP << "\n";
167  qtfs.close();
168  }
169 
170  // handle using qt viewer
171  if(useQT)
172  {
173  __COUT__ << "Forwarding to Web GUI with UDP forward MesageFacility "
174  "table."
175  << std::endl;
176 
177  child.second.getNode(COL_QT_PORT).getValue(fwdPort);
178  child.second.getNode(COL_QT_IP).getValue(fwdIP);
179 
180  fs << "udp: {\n";
181  fs << "\t"
182  << "type: UDP\n";
183  fs << "\t"
184  << "threshold: DEBUG\n";
185  fs << "\t"
186  << "port: " << fwdPort << "\n";
187  fs << "\t"
188  << "host: \"" << fwdIP << "\"\n";
189  fs << "}\n";
190 
191  // output QT Viewer config file
192  std::fstream qtfs;
193  qtfs.open(QT_CFG_FILE, std::fstream::out | std::fstream::trunc);
194  if(qtfs.fail())
195  {
196  fs.close();
197  __SS__ << "Failed to open QT Message Viewer table file: "
198  << QT_CFG_FILE << std::endl;
199  __SS_THROW__;
200  }
201  qtfs << "receivers: \n{\n";
202  qtfs << "\t"
203  << "syslog: \n{\n";
204  qtfs << "\t\t"
205  << "receiverType: "
206  << "\"UDP\""
207  << "\n";
208  qtfs << "\t\t"
209  << "port: " << fwdPort << "\n";
210  qtfs << "\t}\n"; // close syslog
211  qtfs << "}\n"; // close receivers
212  qtfs << "\n";
213  qtfs << "threshold: "
214  << "DEBUG"
215  << "\n";
216  qtfs.close();
217  }
218  }
219  else // write cout config (not forwarding to external process)
220  {
221  __COUT__ << "Using cout-only MesageFacility table." << std::endl;
222  fs << "console: {\n";
223  fs << "\t"
224  << "type: \"cout\"\n";
225  fs << "\t"
226  << "threshold: \"DEBUG\"\n";
227  fs << "\t"
228  << "noTimeStamps: true\n";
229  fs << "\t"
230  << "noLineBreaks: true\n";
231  fs << "}\n";
232  }
233 
234  break; // take first enable row only!
235  }
236 
237  // close MF config file
238  fs.close();
239 }
240 
241 DEFINE_OTS_TABLE(MessageFacilityTable)