Skip to content
Snippets Groups Projects
Commit eeaa0d69 authored by zwart's avatar zwart
Browse files

BugID: 673

Added general main for all ACC controlled applications
parent 0b15d857
No related branches found
No related tags found
No related merge requests found
//# 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
...@@ -2,7 +2,8 @@ pkginclude_HEADERS = \ ...@@ -2,7 +2,8 @@ pkginclude_HEADERS = \
DH_ProcControl.h \ DH_ProcControl.h \
ProcControlComm.h \ ProcControlComm.h \
ProcControlServer.h \ ProcControlServer.h \
ProcessControl.h ProcessControl.h \
ACCmain.h
noinst_HEADERS = noinst_HEADERS =
......
//# 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
...@@ -2,7 +2,8 @@ lib_LTLIBRARIES = libplc.la ...@@ -2,7 +2,8 @@ lib_LTLIBRARIES = libplc.la
libplc_la_SOURCES = DH_ProcControl.cc \ libplc_la_SOURCES = DH_ProcControl.cc \
ProcControlComm.cc \ ProcControlComm.cc \
ProcControlServer.cc ProcControlServer.cc \
ACCmain.cc
include $(top_srcdir)/Makefile.common include $(top_srcdir)/Makefile.common
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment