otsdaq  v2_01_00
MessageFacilityConfiguration_configuration.cc
1 #include "otsdaq-core/ConfigurationPluginDataFormats/MessageFacilityConfiguration.h"
2 #include "otsdaq-core/Macros/ConfigurationPluginMacros.h"
3 #include "otsdaq-core/ConfigurationInterface/ConfigurationManager.h"
4 
5 #include <iostream>
6 #include <fstream> // std::fstream
7 #include <stdio.h>
8 using namespace ots;
9 
10 #define MF_CFG_FILE std::string(getenv("USER_DATA")) + "/MessageFacilityConfigurations/MessageFacilityGen.fcl"
11 #define QT_CFG_FILE std::string(getenv("USER_DATA")) + "/MessageFacilityConfigurations/QTMessageViewerGen.fcl"
12 #define QUIET_CFG_FILE std::string(getenv("USER_DATA")) + "/MessageFacilityConfigurations/QuietForwarderGen.cfg"
13 #define USE_WEB_BOOL_FILE std::string(getenv("USER_DATA")) + "/MessageFacilityConfigurations/UseWebConsole.bool"
14 #define USE_QT_BOOL_FILE std::string(getenv("USER_DATA")) + "/MessageFacilityConfigurations/UseQTViewer.bool"
15 
16 //MessageFacilityConfiguration Column names
17 #define COL_NAME "UID"
18 #define COL_STATUS ViewColumnInfo::COL_NAME_STATUS
19 #define COL_ENABLE_FWD "EnableUDPForwarding"
20 
21 #define COL_USE_WEB "ForwardToWebConsoleGUI"
22 #define COL_WEB_IP "WebConsoleForwardingIPAddress"
23 #define COL_WEB_PORT0 "WebConsoleForwardingPort0"
24 #define COL_WEB_PORT1 "WebConsoleForwardingPort1"
25 
26 #define COL_USE_QT "ForwardToQTViewerGUI"
27 #define COL_QT_IP "QTViewerForwardingIPAddress"
28 #define COL_QT_PORT "QTViewerForwardingPort"
29 
30 
31 
32 MessageFacilityConfiguration::MessageFacilityConfiguration(void) :
33  ConfigurationBase("MessageFacilityConfiguration")
34 {
36  //WARNING: the names used in C++ MUST match the Configuration INFO //
38 
39 // <?xml version="1.0" encoding="UTF-8" standalone="no" ?>
40 // <ROOT xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ConfigurationInfo.xsd">
41 // <CONFIGURATION Name="MessageFacilityConfiguration">
42 // <VIEW Name="MESSAGE_FACILITY_CONFIGURATION" Type="File,Database,DatabaseTest">
43 // <COLUMN Type="UID" Name="UID" StorageName="UID" DataType="VARCHAR2"/>
44 // <COLUMN Type="OnOff" Name="Status" StorageName="STATUS" DataType="VARCHAR2"/>
45 // <COLUMN Type="TrueFalse" Name="EnableUDPForwarding" StorageName="ENABLE_UDP_FORWARDING" DataType="VARCHAR2"/>
46 // <COLUMN Type="YesNo" Name="ForwardToWebConsoleGUI" StorageName="FORWARD_TO_WEB_CONSOLE_GUI" DataType="VARCHAR2"/>
47 // <COLUMN Type="Data" Name="WebConsoleForwardingIPAddress" StorageName="WEB_CONSOLE_FORWARDING_IP_ADDRESS" DataType="VARCHAR2"/>
48 // <COLUMN Type="Data" Name="WebConsoleForwardingPort0" StorageName="WEB_CONSOLE_FORWARDING_PORT0" DataType="NUMBER"/>
49 // <COLUMN Type="Data" Name="WebConsoleForwardingPort1" StorageName="WEB_CONSOLE_FORWARDING_PORT1" DataType="NUMBER"/>
50 // <COLUMN Type="YesNo" Name="ForwardToQTViewerGUI" StorageName="FORWARD_TO_QT_VIEWER_GUI" DataType="VARCHAR2"/>
51 // <COLUMN Type="Data" Name="QTViewerForwardingIPAddress" StorageName="QT_VIEWER_FORWARDING_IP_ADDRESS" DataType="VARCHAR2"/>
52 // <COLUMN Type="Data" Name="QTViewerForwardingPort" StorageName="QT_VIEWER_FORWARDING_PORT" DataType="NUMBER"/>
53 // <COLUMN Type="Comment" Name="CommentDescription" StorageName="COMMENT_DESCRIPTION" DataType="VARCHAR2"/>
54 // <COLUMN Type="Author" Name="Author" StorageName="AUTHOR" DataType="VARCHAR2"/>
55 // <COLUMN Type="Timestamp" Name="RecordInsertionTime" StorageName="RECORD_INSERTION_TIME" DataType="TIMESTAMP WITH TIMEZONE"/>
56 // </VIEW>
57 // </CONFIGURATION>
58 // </ROOT>
59 
60 
61 
62 }
63 
64 MessageFacilityConfiguration::~MessageFacilityConfiguration(void)
65 {}
66 
67 void MessageFacilityConfiguration::init(ConfigurationManager *configManager)
68 {
69 // __COUT__ << "*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*" << std::endl;
70 // __COUT__ << configManager->__SELF_NODE__ << std::endl;
71 
72 
73  bool status,enableFwd,useWeb,useQT;
74  int fwdPort;
75  std::string fwdIP;
76 
77  auto childrenMap = configManager->__SELF_NODE__.getChildren();
78 
79 
80  //generate MF_CFG_FILE file
81  std::fstream fs;
82  fs.open(MF_CFG_FILE, std::fstream::out | std::fstream::trunc);
83  if(fs.fail())
84  {
85  __SS__ << "Failed to open Message Facility configuration file: " << MF_CFG_FILE << std::endl;
86  throw std::runtime_error(ss.str());
87  }
88 
89  //loop through all children just to be same as other configurations
90  // exit loop after first active one
91  for(auto &child:childrenMap)
92  {
93  child.second.getNode(COL_STATUS ).getValue(status);
94 
95  if(!status) continue; //skip inactive rows
96 
97  child.second.getNode(COL_ENABLE_FWD ).getValue(enableFwd);
98 
99  child.second.getNode(COL_USE_WEB ).getValue(useWeb);
100  child.second.getNode(COL_USE_QT ).getValue(useQT);
101 
102  if(useWeb && useQT)
103  {
104  fs.close();
105  __SS__ << "Illegal Message Facility configuration: " <<
106  "Can only enable Web Console or QT Viewer, not both." << std::endl;
107  throw std::runtime_error(ss.str());
108  }
109 
110  std::fstream bfs;
111  //output use web bool for StartOTS.sh
112  bfs.open(USE_WEB_BOOL_FILE, std::fstream::out | std::fstream::trunc);
113  if(bfs.fail())
114  {
115  fs.close();
116  __SS__ << "Failed to open boolean Use of Web Console configuration file: " <<
117  USE_WEB_BOOL_FILE << std::endl;
118  throw std::runtime_error(ss.str());
119  }
120  bfs << (useWeb?1:0);
121  bfs.close();
122 
123  //output use web bool for StartOTS.sh
124  bfs.open(USE_QT_BOOL_FILE, std::fstream::out | std::fstream::trunc);
125  if(bfs.fail())
126  {
127  fs.close();
128  __SS__ << "Failed to open boolean Use of QT Viewer configuration file: " <<
129  USE_QT_BOOL_FILE << std::endl;
130  throw std::runtime_error(ss.str());
131  }
132  bfs << (useQT?1:0);
133  bfs.close();
134 
135  if(enableFwd) //write udp forward config
136  {
137 
138  //handle using web gui
139  if(useWeb)
140  {
141  //__COUT__ << "Forwarding to Web GUI with UDP forward MesageFacility configuration." << std::endl;
142 
143  child.second.getNode(COL_WEB_PORT0 ).getValue(fwdPort);
144  child.second.getNode(COL_WEB_IP ).getValue(fwdIP);
145 
146  fs << "udp: {\n";
147  fs << "\t" << "type: UDP\n";
148  fs << "\t" << "threshold: DEBUG\n";
149  fs << "\t" << "port: " << fwdPort << "\n";
150  fs << "\t" << "host: \"" << fwdIP << "\"\n";
151  fs << "}\n";
152 
153  //output quiet forwarder config file
154  std::fstream qtfs;
155  qtfs.open(QUIET_CFG_FILE, std::fstream::out | std::fstream::trunc);
156  if(qtfs.fail())
157  {
158  fs.close();
159  __SS__ << "Failed to open Web Console's 'Quiet Forwarder' configuration file: " <<
160  QUIET_CFG_FILE << std::endl;
161  throw std::runtime_error(ss.str());
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 configuration." << std::endl;
174 
175  child.second.getNode(COL_QT_PORT ).getValue(fwdPort);
176  child.second.getNode(COL_QT_IP ).getValue(fwdIP);
177 
178  fs << "udp: {\n";
179  fs << "\t" << "type: UDP\n";
180  fs << "\t" << "threshold: DEBUG\n";
181  fs << "\t" << "port: " << fwdPort << "\n";
182  fs << "\t" << "host: \"" << fwdIP << "\"\n";
183  fs << "}\n";
184 
185  //output QT Viewer config file
186  std::fstream qtfs;
187  qtfs.open(QT_CFG_FILE, std::fstream::out | std::fstream::trunc);
188  if(qtfs.fail())
189  {
190  fs.close();
191  __SS__ << "Failed to open QT Message Viewer configuration file: " <<
192  QT_CFG_FILE << std::endl;
193  throw std::runtime_error(ss.str());
194  }
195  qtfs << "receivers: \n{\n";
196  qtfs << "\t" << "syslog: \n{\n";
197  qtfs << "\t\t" << "receiverType: " << "\"UDP\"" << "\n";
198  qtfs << "\t\t" << "port: " << fwdPort << "\n";
199  qtfs << "\t}\n"; //close syslog
200  qtfs << "}\n"; //close receivers
201  qtfs << "\n";
202  qtfs << "threshold: " << "DEBUG" << "\n";
203  qtfs.close();
204  }
205  }
206  else //write cout config (not forwarding to external process)
207  {
208  __COUT__ << "Using cout-only MesageFacility configuration." << std::endl;
209  fs << "console: {\n";
210  fs << "\t" << "type: \"cout\"\n";
211  fs << "\t" << "threshold: \"DEBUG\"\n";
212  fs << "\t" << "noTimeStamps: true\n";
213  fs << "\t" << "noLineBreaks: true\n";
214  fs << "}\n";
215  }
216 
217  break; //take first enable row only!
218  }
219 
220  //close MF config file
221  fs.close();
222 }
223 
224 DEFINE_OTS_CONFIGURATION(MessageFacilityConfiguration)