otsdaq  v1_01_02
 All Classes Namespaces Functions
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 "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  __MOUT__ << "*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*" << std::endl;
70 
71  __MOUT__ << configManager->__SELF_NODE__ << std::endl;
72 
73 
74  bool status,enableFwd,useWeb,useQT;
75  int fwdPort;
76  std::string fwdIP;
77 
78  auto childrenMap = configManager->__SELF_NODE__.getChildren();
79 
80 
81  //generate MF_CFG_FILE file
82  std::fstream fs;
83  fs.open(MF_CFG_FILE, std::fstream::out | std::fstream::trunc);
84  if(fs.fail())
85  {
86  __SS__ << "Failed to open Message Facility configuration file: " << MF_CFG_FILE << std::endl;
87  throw std::runtime_error(ss.str());
88  }
89 
90  //loop through all children just to be same as other configurations
91  // exit loop after first active one
92  for(auto &child:childrenMap)
93  {
94  child.second.getNode(COL_STATUS ).getValue(status);
95 
96  if(!status) continue; //skip inactive rows
97 
98  child.second.getNode(COL_ENABLE_FWD ).getValue(enableFwd);
99 
100  child.second.getNode(COL_USE_WEB ).getValue(useWeb);
101  child.second.getNode(COL_USE_QT ).getValue(useQT);
102 
103  if(useWeb && useQT)
104  {
105  fs.close();
106  __SS__ << "Illegal Message Facility configuration: " <<
107  "Can only enable Web Console or QT Viewer, not both." << std::endl;
108  throw std::runtime_error(ss.str());
109  }
110 
111  std::fstream bfs;
112  //output use web bool for StartOTS.sh
113  bfs.open(USE_WEB_BOOL_FILE, std::fstream::out | std::fstream::trunc);
114  if(bfs.fail())
115  {
116  fs.close();
117  __SS__ << "Failed to open boolean Use of Web Console configuration file: " <<
118  USE_WEB_BOOL_FILE << std::endl;
119  throw std::runtime_error(ss.str());
120  }
121  bfs << (useWeb?1:0);
122  bfs.close();
123 
124  //output use web bool for StartOTS.sh
125  bfs.open(USE_QT_BOOL_FILE, std::fstream::out | std::fstream::trunc);
126  if(bfs.fail())
127  {
128  fs.close();
129  __SS__ << "Failed to open boolean Use of QT Viewer configuration file: " <<
130  USE_QT_BOOL_FILE << std::endl;
131  throw std::runtime_error(ss.str());
132  }
133  bfs << (useQT?1:0);
134  bfs.close();
135 
136  if(enableFwd) //write udp forward config
137  {
138 
139  //handle using web gui
140  if(useWeb)
141  {
142  __MOUT__ << "Forwarding to Web GUI with UDP forward MesageFacility configuration." << std::endl;
143 
144  child.second.getNode(COL_WEB_PORT0 ).getValue(fwdPort);
145  child.second.getNode(COL_WEB_IP ).getValue(fwdIP);
146 
147  fs << "udp: {\n";
148  fs << "\t" << "type: UDP\n";
149  fs << "\t" << "threshold: DEBUG\n";
150  fs << "\t" << "port: " << fwdPort << "\n";
151  fs << "\t" << "host: \"" << fwdIP << "\"\n";
152  fs << "}\n";
153 
154  //output quiet forwarder config file
155  std::fstream qtfs;
156  qtfs.open(QUIET_CFG_FILE, std::fstream::out | std::fstream::trunc);
157  if(qtfs.fail())
158  {
159  fs.close();
160  __SS__ << "Failed to open Web Console's 'Quiet Forwarder' configuration file: " <<
161  QUIET_CFG_FILE << std::endl;
162  throw std::runtime_error(ss.str());
163  }
164  qtfs << "RECEIVE_PORT \t " << fwdPort << "\n";
165  child.second.getNode(COL_WEB_PORT1 ).getValue(fwdPort);
166  qtfs << "DESTINATION_PORT \t " << fwdPort << "\n";
167  qtfs.close();
168  }
169 
170  //handle using qt viewer
171  if(useQT)
172  {
173  __MOUT__ << "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  __MOUT__ << "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)