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