otsdaq  v2_04_00
MessageFacilityTable_table.cc
1 #include "otsdaq-core/ConfigurationInterface/ConfigurationManager.h"
2 #include "otsdaq-core/Macros/TablePluginMacros.h"
3 #include "otsdaq-core/TablePluginDataFormats/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")) + "/MessageFacilityConfigurations/" \
25  "UseQTViewer.bool"
26 
27 // MessageFacilityTable Column names
28 #define COL_NAME "UID"
29 #define COL_STATUS TableViewColumnInfo::COL_NAME_STATUS
30 #define COL_ENABLE_FWD "EnableUDPForwarding"
31 
32 #define COL_USE_WEB "ForwardToWebConsoleGUI"
33 #define COL_WEB_IP "WebConsoleForwardingIPAddress"
34 #define COL_WEB_PORT0 "WebConsoleForwardingPort0"
35 #define COL_WEB_PORT1 "WebConsoleForwardingPort1"
36 
37 #define COL_USE_QT "ForwardToQTViewerGUI"
38 #define COL_QT_IP "QTViewerForwardingIPAddress"
39 #define COL_QT_PORT "QTViewerForwardingPort"
40 
41 MessageFacilityTable::MessageFacilityTable(void) : TableBase("MessageFacilityTable")
42 {
44  // WARNING: the names used in C++ MUST match the Table INFO //
46 
47  // <?xml version="1.0" encoding="UTF-8" standalone="no" ?>
48  // <ROOT xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
49  // xsi:noNamespaceSchemaLocation="TableInfo.xsd"> <TABLE
50  // Name="MessageFacilityTable">
51  // <VIEW Name="MESSAGE_FACILITY_TABLE"
52  // Type="File,Database,DatabaseTest">
53  // <COLUMN Type="UID" Name="UID" StorageName="UID"
54  // DataType="VARCHAR2"/> <COLUMN Type="OnOff" Name="Status"
55  // StorageName="STATUS" DataType="VARCHAR2"/>
56  // <COLUMN Type="TrueFalse" Name="EnableUDPForwarding"
57  // StorageName="ENABLE_UDP_FORWARDING" DataType="VARCHAR2"/>
58  // <COLUMN Type="YesNo" Name="ForwardToWebConsoleGUI"
59  // StorageName="FORWARD_TO_WEB_CONSOLE_GUI" DataType="VARCHAR2"/>
60  // <COLUMN Type="Data" Name="WebConsoleForwardingIPAddress"
61  // StorageName="WEB_CONSOLE_FORWARDING_IP_ADDRESS" DataType="VARCHAR2"/>
62  // <COLUMN Type="Data" Name="WebConsoleForwardingPort0"
63  // StorageName="WEB_CONSOLE_FORWARDING_PORT0" DataType="NUMBER"/>
64  // <COLUMN Type="Data" Name="WebConsoleForwardingPort1"
65  // StorageName="WEB_CONSOLE_FORWARDING_PORT1" DataType="NUMBER"/>
66  // <COLUMN Type="YesNo" Name="ForwardToQTViewerGUI"
67  // StorageName="FORWARD_TO_QT_VIEWER_GUI" DataType="VARCHAR2"/>
68  // <COLUMN Type="Data" Name="QTViewerForwardingIPAddress"
69  // StorageName="QT_VIEWER_FORWARDING_IP_ADDRESS" DataType="VARCHAR2"/>
70  // <COLUMN Type="Data" Name="QTViewerForwardingPort"
71  // StorageName="QT_VIEWER_FORWARDING_PORT" DataType="NUMBER"/>
72  // <COLUMN Type="Comment" Name="CommentDescription"
73  // StorageName="COMMENT_DESCRIPTION" DataType="VARCHAR2"/>
74  // <COLUMN Type="Author" Name="Author" StorageName="AUTHOR"
75  // DataType="VARCHAR2"/> <COLUMN Type="Timestamp"
76  // Name="RecordInsertionTime" StorageName="RECORD_INSERTION_TIME"
77  // DataType="TIMESTAMP WITH TIMEZONE"/>
78  // </VIEW>
79  // </TABLE>
80  // </ROOT>
81 }
82 
83 MessageFacilityTable::~MessageFacilityTable(void) {}
84 
85 void MessageFacilityTable::init(ConfigurationManager* configManager)
86 {
87  // __COUT__ << "*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*" << std::endl;
88  // __COUT__ << configManager->__SELF_NODE__ << std::endl;
89 
90  bool status, enableFwd, useWeb, useQT;
91  int fwdPort;
92  std::string fwdIP;
93 
94  auto childrenMap = configManager->__SELF_NODE__.getChildren();
95 
96  // generate MF_CFG_FILE file
97  std::fstream fs;
98  fs.open(MF_CFG_FILE, std::fstream::out | std::fstream::trunc);
99  if(fs.fail())
100  {
101  __SS__ << "Failed to open Message Facility table file: " << MF_CFG_FILE
102  << std::endl;
103  __SS_THROW__;
104  }
105  else
106  __COUT__ << "Opened.. " << MF_CFG_FILE << __E__;
107 
108  // loop through all children just to be same as other tables
109  // exit loop after first active one
110  for(auto& child : childrenMap)
111  {
112  child.second.getNode(COL_STATUS).getValue(status);
113 
114  if(!status)
115  continue; // skip inactive rows
116 
117  child.second.getNode(COL_ENABLE_FWD).getValue(enableFwd);
118 
119  child.second.getNode(COL_USE_WEB).getValue(useWeb);
120  child.second.getNode(COL_USE_QT).getValue(useQT);
121 
122  if(useWeb && useQT)
123  {
124  fs.close();
125  __SS__ << "Illegal Message Facility table: "
126  << "Can only enable Web Console or QT Viewer, not both." << std::endl;
127  __SS_THROW__;
128  }
129 
130  std::fstream bfs;
131  // output use web bool for StartOTS.sh
132  bfs.open(USE_WEB_BOOL_FILE, std::fstream::out | std::fstream::trunc);
133  if(bfs.fail())
134  {
135  fs.close();
136  __SS__ << "Failed to open boolean Use of Web Console table file: "
137  << USE_WEB_BOOL_FILE << std::endl;
138  __SS_THROW__;
139  }
140  bfs << (useWeb ? 1 : 0);
141  bfs.close();
142 
143  // output use web bool for StartOTS.sh
144  bfs.open(USE_QT_BOOL_FILE, std::fstream::out | std::fstream::trunc);
145  if(bfs.fail())
146  {
147  fs.close();
148  __SS__ << "Failed to open boolean Use of QT Viewer table file: "
149  << USE_QT_BOOL_FILE << std::endl;
150  __SS_THROW__;
151  }
152  bfs << (useQT ? 1 : 0);
153  bfs.close();
154 
155  if(enableFwd) // write udp forward config
156  {
157  // handle using web gui
158  if(useWeb)
159  {
160  __COUT__ << "Forwarding to Web GUI with UDP forward MesageFacility "
161  "table."
162  << std::endl;
163 
164  child.second.getNode(COL_WEB_PORT0).getValue(fwdPort);
165  child.second.getNode(COL_WEB_IP).getValue(fwdIP);
166 
167  fs << "udp: {\n";
168  fs << "\t"
169  << "type: UDP\n";
170  fs << "\t"
171  << "threshold: DEBUG\n";
172  fs << "\t"
173  << "port: " << fwdPort << "\n";
174  fs << "\t"
175  << "host: \"" << fwdIP << "\"\n";
176  fs << "}\n";
177 
178  // output quiet forwarder config file
179  std::fstream qtfs;
180  qtfs.open(QUIET_CFG_FILE, std::fstream::out | std::fstream::trunc);
181  if(qtfs.fail())
182  {
183  fs.close();
184  __SS__ << "Failed to open Web Console's 'Quiet Forwarder' "
185  "table file: "
186  << QUIET_CFG_FILE << std::endl;
187  __SS_THROW__;
188  }
189  qtfs << "RECEIVE_PORT \t " << fwdPort << "\n";
190  child.second.getNode(COL_WEB_PORT1).getValue(fwdPort);
191  qtfs << "DESTINATION_PORT \t " << fwdPort << "\n";
192  qtfs << "DESTINATION_IP \t " << fwdIP << "\n";
193  qtfs.close();
194  }
195 
196  // handle using qt viewer
197  if(useQT)
198  {
199  __COUT__ << "Forwarding to Web GUI with UDP forward MesageFacility "
200  "table."
201  << std::endl;
202 
203  child.second.getNode(COL_QT_PORT).getValue(fwdPort);
204  child.second.getNode(COL_QT_IP).getValue(fwdIP);
205 
206  fs << "udp: {\n";
207  fs << "\t"
208  << "type: UDP\n";
209  fs << "\t"
210  << "threshold: DEBUG\n";
211  fs << "\t"
212  << "port: " << fwdPort << "\n";
213  fs << "\t"
214  << "host: \"" << fwdIP << "\"\n";
215  fs << "}\n";
216 
217  // output QT Viewer config file
218  std::fstream qtfs;
219  qtfs.open(QT_CFG_FILE, std::fstream::out | std::fstream::trunc);
220  if(qtfs.fail())
221  {
222  fs.close();
223  __SS__ << "Failed to open QT Message Viewer table file: "
224  << QT_CFG_FILE << std::endl;
225  __SS_THROW__;
226  }
227  qtfs << "receivers: \n{\n";
228  qtfs << "\t"
229  << "syslog: \n{\n";
230  qtfs << "\t\t"
231  << "receiverType: "
232  << "\"UDP\""
233  << "\n";
234  qtfs << "\t\t"
235  << "port: " << fwdPort << "\n";
236  qtfs << "\t}\n"; // close syslog
237  qtfs << "}\n"; // close receivers
238  qtfs << "\n";
239  qtfs << "threshold: "
240  << "DEBUG"
241  << "\n";
242  qtfs.close();
243  }
244  }
245  else // write cout config (not forwarding to external process)
246  {
247  __COUT__ << "Using cout-only MesageFacility table." << std::endl;
248  fs << "console: {\n";
249  fs << "\t"
250  << "type: \"cout\"\n";
251  fs << "\t"
252  << "threshold: \"DEBUG\"\n";
253  fs << "\t"
254  << "noTimeStamps: true\n";
255  fs << "\t"
256  << "noLineBreaks: true\n";
257  fs << "}\n";
258  }
259 
260  break; // take first enable row only!
261  }
262 
263  // close MF config file
264  fs.close();
265 }
266 
267 DEFINE_OTS_TABLE(MessageFacilityTable)