diff --git a/LCS/ACC/PLC/include/PLC/ACCmain.h b/LCS/ACC/PLC/include/PLC/ACCmain.h new file mode 100644 index 0000000000000000000000000000000000000000..ca751a01a8691f0f838bcf4b22bc0eea892cc1de --- /dev/null +++ b/LCS/ACC/PLC/include/PLC/ACCmain.h @@ -0,0 +1,64 @@ +//# ACCmain.h: main loop that can be used by any ACC enabled program +//# +//# Copyright (C) 2006 +//# ASTRON (Netherlands Foundation for Research in Astronomy) +//# P.O.Box 2, 7990 AA Dwingeloo, The Netherlands, seg@astron.nl +//# +//# This program is free software; you can redistribute it and/or modify +//# it under the terms of the GNU General Public License as published by +//# the Free Software Foundation; either version 2 of the License, or +//# (at your option) any later version. +//# +//# This program is distributed in the hope that it will be useful, +//# but WITHOUT ANY WARRANTY; without even the implied warranty of +//# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +//# GNU General Public License for more details. +//# +//# You should have received a copy of the GNU General Public License +//# along with this program; if not, write to the Free Software +//# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +//# +//# $Id$ + +#ifndef LOFAR_PLC_ACCMAIN_H +#define LOFAR_PLC_ACCMAIN_H + +// \file +// main loop that can be used by any ACC enabled program + +//# Never #include <config.h> or #include <lofar_config.h> in a header file! + +//# Includes +#include <PLC/ProcessControl.h> + +namespace LOFAR +{ + namespace ACC + { + namespace PLC + { + + // \addtogroup PLC + // @{ + + // This function is the main loop that can (or should) be used by ACC controlled + // programs. To use it do the following: + // Subclass ProcessControl and implement all functions. + // Implement a real main: + // + // #include<PLC/ACCmain.cc> + // #include<SubClassOfProcessControl.h> + // + // int main(int argv, char* argv[]) { + // SubClassOfProcessControl myProcess; + // return LOFAR::ACC::PLC::ACCmain(argc, argv, &myProcess); + // } + int ACCmain (int argc, char* argv[], ProcessControl* theProcess); + + // @} + + } // namespace PLC + } // namespace ACC +} // namespace LOFAR + +#endif diff --git a/LCS/ACC/PLC/include/PLC/Makefile.am b/LCS/ACC/PLC/include/PLC/Makefile.am index 045f5cd42ed102ea691a291e6e7578900b1012f1..64da1fca2cc908244ef707e34fd7f054158094b6 100644 --- a/LCS/ACC/PLC/include/PLC/Makefile.am +++ b/LCS/ACC/PLC/include/PLC/Makefile.am @@ -2,7 +2,8 @@ pkginclude_HEADERS = \ DH_ProcControl.h \ ProcControlComm.h \ ProcControlServer.h \ - ProcessControl.h + ProcessControl.h \ + ACCmain.h noinst_HEADERS = diff --git a/LCS/ACC/PLC/src/ACCmain.cc b/LCS/ACC/PLC/src/ACCmain.cc new file mode 100644 index 0000000000000000000000000000000000000000..e3ac441da25898e1c6c24416dc7e29c56ea6183b --- /dev/null +++ b/LCS/ACC/PLC/src/ACCmain.cc @@ -0,0 +1,155 @@ +//# ACCmain.cc: main loop that can be used by any ACC enabled program +//# +//# Copyright (C) 2006 +//# ASTRON (Netherlands Foundation for Research in Astronomy) +//# P.O.Box 2, 7990 AA Dwingeloo, The Netherlands, seg@astron.nl +//# +//# This program is free software; you can redistribute it and/or modify +//# it under the terms of the GNU General Public License as published by +//# the Free Software Foundation; either version 2 of the License, or +//# (at your option) any later version. +//# +//# This program is distributed in the hope that it will be useful, +//# but WITHOUT ANY WARRANTY; without even the implied warranty of +//# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +//# GNU General Public License for more details. +//# +//# You should have received a copy of the GNU General Public License +//# along with this program; if not, write to the Free Software +//# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +//# +//# $Id$ + +//# Always #include <lofar_config.h> first! +#include <lofar_config.h> + +//# Includes +#include <libgen.h> +#include <PLC/ACCmain.h> +#include <Common/LofarLogger.h> +#include <APS/ParameterSet.h> +#include <PLC/ProcControlServer.h> + +namespace LOFAR { + namespace ACC { + namespace PLC { + + using APS::ParameterSet; + + int ACCmain (int argc, char* argv[], ProcessControl* theProcess) { + + string programName = basename(argv[0]); + INIT_LOGGER(programName.c_str()); + + try { + + // Check invocation syntax + if ((argc!=3) || (strncmp("ACC", argv[1], 3) != 0)) { + + // we were not called by ACC + LOG_TRACE_FLOW(programName + " not started by ACC"); + + // See if there is a parameterset available + vector<string> possibleNames; + + // try to find a parameterset + // start with program name + .parset + // also try parts of the program name (splitted with "_") + string pName = programName; + string::size_type pos; + do { + possibleNames.push_back(pName + ".parset"); + //possibleNames.push_back(pName + ".cfg"); + //possibleNames.push_back(pName + ".ps"); + + pos = pName.rfind('_'); + pName = pName.substr(0, pos); + } while (pos != string::npos); + + ParameterSet* ps = 0; + vector<string>::iterator i; + for (i = possibleNames.begin(); i != possibleNames.end(); i++) { + try { + LOG_TRACE_FLOW_STR("Trying to use "<<*i<<" as parameterSet"); + ps = new ParameterSet(*i); + LOG_INFO_STR("Using "<<*i<<" as parameter set."); + break; + } catch (...) { + LOG_TRACE_FLOW_STR(*i << " not found"); + ps = 0; + } + } + + if (ps == 0) { + LOG_INFO_STR("Could not find a parameter set."); + LOG_TRACE_FLOW(programName + " starting define"); + theProcess->define(); + } else { + LOG_TRACE_FLOW(programName + " starting define"); + theProcess->define(*ps); + delete ps; + } + LOG_TRACE_FLOW(programName + " initting"); + theProcess->init(); + LOG_TRACE_FLOW(programName + " running"); + theProcess->run(); + LOG_TRACE_FLOW(programName + " quitting"); + theProcess->quit(); + LOG_TRACE_FLOW(programName + " deleting process"); + + } else { + + LOG_TRACE_FLOW("Main program started by ACC"); + + // Now all ACC processes expect "ACC" as first argument + // So the parameter file is the second argument + + // Read in parameterfile and get my name + ParameterSet itsOrigParamSet(argv[2]); + string procID = itsOrigParamSet.getString("process.name"); + ParameterSet ParamSet = itsOrigParamSet.makeSubset(procID); + + ProcControlServer pcServer(ParamSet.getString("ACnode"), + ParamSet.getUint16("ACport"), + theProcess); + + + LOG_TRACE_FLOW("Registering at ApplController"); + pcServer.registerAtAC(procID); + LOG_TRACE_FLOW("Registered at ApplController"); + + // Main processing loop + while (1) { + LOG_TRACE_FLOW("Polling ApplController for message"); + if (pcServer.pollForMessage()) { + LOG_TRACE_FLOW("Message received from ApplController"); + + // get pointer to received data + DH_ProcControl* newMsg = pcServer.getDataHolder(); + + pcServer.handleMessage(newMsg); + + if (newMsg->getCommand() == ACC::PLC::PCCmdQuit) { + break; + } + } + } + + LOG_INFO_STR("Shutting down: ApplicationController"); + pcServer.unregisterAtAC(""); // send to AC before quiting + } + } catch (Exception& ex) { + LOG_FATAL_STR("Caught exception: " << ex << endl); + LOG_FATAL_STR(argv[0] << " terminated by exception!"); + return 1; + } catch (...) { + LOG_FATAL_STR("Caught unknown exception, exitting"); + return 1; + } + LOG_INFO_STR(programName << " terminated normally"); + return 0; + } + + } // namespace PLC + } // namespace ACC +} // namespace LOFAR diff --git a/LCS/ACC/PLC/src/Makefile.am b/LCS/ACC/PLC/src/Makefile.am index cde736056fa58b5005c9416593ba8ac754bf030d..75a3e75dbc599ee388ce78ac5ef608d797eeaf0d 100644 --- a/LCS/ACC/PLC/src/Makefile.am +++ b/LCS/ACC/PLC/src/Makefile.am @@ -2,7 +2,8 @@ lib_LTLIBRARIES = libplc.la libplc_la_SOURCES = DH_ProcControl.cc \ ProcControlComm.cc \ - ProcControlServer.cc + ProcControlServer.cc \ + ACCmain.cc include $(top_srcdir)/Makefile.common