00001 #ifdef linux
00002 #undef linux
00003 #endif
00004
00005 #include "SctApiCall.h"
00006 #include "RunController.h"
00007 #include "SctApiAccessException.h"
00008 #include "CalibrationControllerImpl.h"
00009 #include "ipc/CalibrationController.h"
00010 #include "Sct/SctNames.h"
00011 #include "Sct/StringStreamer.h"
00012 #include "Sct/Exception.h"
00013 #include "Sct/LogicErrors.h"
00014 #include <rc/RunParams.h>
00015 #include <rc/Controller.h>
00016 #include <ipc/partition.h>
00017 #include <is/isinfo.h>
00018 #include <is/isinfotmpl.h>
00019 #include <pmg/pmg_initSync.h>
00020 #include <iostream>
00021 #include <fstream>
00022
00023 #include "cmdl/cmdargs.h"
00024 #include "dal/implementation.h"
00025 #include "config/Configuration.h"
00026 #include "dal/util.h"
00027
00028 using namespace std;
00029 using namespace Sct;
00030
00031
00032 inline std::string str_lkuvysjhgfdksjhd(const char * word)
00033 {
00034 return word ? word : "";
00035 }
00036
00037 int main(int argc, char** argv) {
00038 using namespace SctCalibrationController;
00039
00040 cout << "Starting Calibration controller" << endl;
00041 setExceptionHandlers(argv[0]);
00042
00043 IPCCore::init(argc,argv);
00044 SctNames::Mrs() << "CCRC" << MRS_TEXT("CCRC::main done IPCCore::init") << MRS_DIAGNOSTIC << ENDM;
00045
00046
00047
00048 CmdArgStr name ('n',"name", "controller", "Name of the controller");
00049 CmdArgStr data ('d',"data", "db-name", "Configuration DB name");
00050
00051
00052
00053
00054 CmdArgBool oks ('O',"oks" ,"Load Configuration from OKS");
00055
00056 CmdArgBool none ('N',"none" ,"Do not load Configuration");
00057
00058
00059
00060 CmdLine cmd(*argv,&name,&data,&oks,&none,NULL);
00061
00062
00063 cmd.description("The SCT Calibration Controller.");
00064
00065
00066 CmdArgvIter argvIter(--argc,++argv);
00067 if (cmd.parse(argvIter))
00068 {
00069 std::cerr << "ERROR in " << __FILE__ << " at line " << __LINE__ << " when parsing command line args." << std::endl ;
00070 return EXIT_FAILURE;
00071 }
00072
00073
00074 std::auto_ptr<ConfigurationImpl> configImpl
00075 (
00076 none ? 0 : dal::create_config_implementation
00077 (
00078 oks ? dal::OksDB_Implementation :
00079
00080 dal::DefaultDB_Implementation
00081 )
00082 );
00083
00084
00085 std::auto_ptr<Configuration> config(none ? 0 : new Configuration(str_lkuvysjhgfdksjhd(data),configImpl.get()));
00086 if (config.get())
00087 {
00088 config->register_convert_function(dal::SubstituteVariables::from_environment);
00089 }
00090
00091
00092 RunControl& user = RunControl::instance();
00093 cout << "Instance created" << endl;
00094
00095
00096 RC::Controller controller(SctNames::getPartition(),
00097 user,
00098 "CalibrationController",
00099 "",
00100 config.get(),
00101 "RunCtrl"
00102 );
00103
00104 pmg_initSync();
00105 controller.run();
00106 cout << "All over" << endl;
00107
00108 return 0;
00109 }
00110
00111 namespace SctCalibrationController {
00112
00113 RunControl::RunControl() : cc(CalibrationControllerImpl::initialize(*this)) {
00114 }
00115
00116 RunControl& RunControl::instance() {
00117 static RunControl rc;
00118 return rc;
00119 }
00120
00121 int RunControl::getRunNumber() {
00122 if (m_isInfoDictionary.get()) {
00123
00124 RunParams runParams;
00125 runParams.run_number = defaultRunNumber;
00126 SctNames::Mrs() << "RC_DEBUG" << MRS_TEXT(Sct::StringStreamer<< "Before I make any attempt to see the run number my runParams.run_number holds defaultRunNumber="<< runParams.run_number)<< MRS_INFORMATION << ENDM;
00127 const ISInfo::Status isStatus = m_isInfoDictionary->getValue("RunParams.RunParams",runParams);
00128 if (isStatus!=ISInfo::Success){
00129 SctNames::Mrs() << "RC_DEBUG" << MRS_TEXT("The attempt to get the run params was unsuccessful. Could not determine run number. Using default one (probably wrong!)")<< MRS_ERROR << ENDM;
00130 return defaultRunNumber;
00131 }
00132 SctNames::Mrs() << "RC_DEBUG" << MRS_TEXT(Sct::StringStreamer<<"After I make any attempt to see the run number my runParams.run_number holds "<< runParams.run_number)<< MRS_INFORMATION << ENDM;
00133 return runParams.run_number;
00134 } else {
00135 SctNames::Mrs() << "RC_getRunNumber" << MRS_TEXT("The CalibrationController's RunController failed to find the Information Service so a default run-number is being returned in place of the correct one!") << MRS_INFORMATION << ENDM;
00136 return defaultRunNumber;
00137 };
00138 };
00139
00140 bool RunControl::loadAction () {
00141 api = SctNames::getPartition().lookup<Sct_SctApi::SctApiIPC>(Sct_SctApi::SctApiIPC::instanceName);
00142 if (CORBA::is_nil(api)) {
00143 SctNames::Mrs() << "RC_INIT" << MRS_TEXT("RunController failed to find SctApi") << MRS_ERROR << ENDM;
00144 return false;
00145 }
00146 cc.setApi(api);
00147
00148 SctNames::Mrs() << "RC_loadAction" << MRS_TEXT("Trying to get isInfoDictionary ...")<< MRS_INFORMATION << ENDM;
00149 m_isInfoDictionary = boost::shared_ptr<ISInfoDictionary>(new ISInfoDictionary(SctNames::getPartition()));
00150 if (!m_isInfoDictionary.get()) {
00151 SctNames::Mrs() << "RC_INIT" << MRS_TEXT(Sct::StringStreamer<< "RunController failed to find Information Server in partition "<< SctNames::getPartition().name()<<". This means numbers like the run number will be set to default (probably incorrect) values!") << MRS_ERROR << ENDM;
00152 } else {
00153 SctNames::Mrs() << "RC_INIT" << MRS_TEXT(Sct::StringStreamer<< "RunController succeeded in finding Information Server in partition "<< SctNames::getPartition().name()<< ". This means numbers like the run number should be correct.") << MRS_INFORMATION << ENDM;
00154 };
00155
00156 try {
00157 if (CORBA::is_nil(api)) { throw InvalidArgumentError("Null pointer to SctApi", __FILE__, __LINE__); }
00158 try {
00159 const int rn=getRunNumber();
00160 SctNames::Mrs() << "RC_INIT" << MRS_TEXT(Sct::StringStreamer<< "RunController found runNumber="<<rn)
00161 << MRS_INFORMATION << ENDM;
00162 (api)->initialiseAll(rn);
00163 } catch (const CORBA::Exception & exe) {
00164 throw raiseSctApiException(exe, "SctApi failed to initialiseAll", __FILE__, __LINE__);
00165 }
00166 } catch(SctApiAccessException& e) {
00167 e.sendToMrs(MRS_ERROR);
00168 return false;
00169 }
00170 SctNames::Mrs() << "RC_INIT" << MRS_TEXT("CC RunController initialised SctApi") << MRS_DIAGNOSTIC << ENDM;
00171
00172 return true;
00173 }
00174
00175 bool RunControl::configureAction() {
00176 cout << "CC configureAction" << endl;
00177 try {
00178
00179 APICALL(api, configureAllModules(), "SctApi failed to configureAllModules")
00180 } catch(SctApiAccessException& e) {
00181 e.sendToMrs(MRS_ERROR);
00182 return false;
00183 }
00184 SctNames::Mrs() << "RC_CONFIG" << MRS_TEXT("CC RunController configured SctApi") << MRS_DIAGNOSTIC << ENDM;
00185 return true;
00186 }
00187
00189 bool RunControl::prepareAction() {
00190 cout << "CC prepare action" << endl;
00191 if (CORBA::is_nil(api)) {
00192 cout << "Can't enter PREPARE as we don't have the api" << endl;
00193 return false;
00194 }
00195
00196 RunParams runParams;
00197 ISInfoDictionary& is = SctNames::getISDictionary();
00198 ISInfo::Status isStatus;
00199
00200 isStatus = is.findValue("RunParams.RunParams", runParams);
00201
00202 if (isStatus!=ISInfo::Success){
00203 SctNames::Mrs() << "RC_PREPARE" << MRS_TEXT("CalibrationController failed to find RunParameters") << MRS_ERROR << ENDM;
00204 return false;
00205 }
00206
00207 try {
00208 APICALL(api, setRunNumber(runParams.run_number), "Failed to set run number")
00209 APICALL(api, setScanNumber(0), "Failed to set scan number")
00210 cout << "New Run, setting run number and scan number: " << runParams.run_number << " " << 0 << endl;
00211 cc.takeControl(runParams.run_number);
00212 } catch (SctApiAccessException& e) {
00213 e.sendToMrs(MRS_ERROR);
00214 return false;
00215 }
00216 return true;
00217 }
00218
00220 bool RunControl::stopEFAction() {
00221 cout << "CC exit stopFE action" << endl;
00222 cc.giveupControl();
00223 return true;
00224 }
00225
00226 bool RunControl::unloadAction () {
00227 cout << "CC unloadaction" << endl;
00228
00229 try {
00230 APICALL(api, shutdownAll(), "Could not shutdown all")
00231 } catch (SctApiAccessException& e) {
00232 e.sendToMrs(MRS_ERROR);
00233 return false;
00234 }
00235 cc.reset();
00236
00237 return true;
00238 }
00239
00240 bool RunControl::resetAction () {
00241 cout << "CC resetAction" << endl;
00242 cc.reset();
00243 return true;
00244 }
00245
00246 void RunControl::sctApiError(const string& reason) {
00247 SctNames::Mrs() << "CC_API_ERROR" << MRS_PARAM<const char*>("reason",reason.c_str())
00248 << MRS_TEXT("Calibration Controller found error with SctApi") << MRS_WARNING << ENDM;
00249 this->raiseError(reason);
00250 cc.giveupControl();
00251 }
00252
00253 }