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
00158
00159 {
00160 if (CORBA::is_nil(api)) { throw InvalidArgumentError("Null pointer to SctApi", __FILE__, __LINE__); }
00161 try {
00162 const int rn=getRunNumber();
00163 SctNames::Mrs() << "RC_INIT" << MRS_TEXT(Sct::StringStreamer<< "RunController found runNumber="<<rn) << MRS_INFORMATION << ENDM;
00164 (api)->initialiseAll(rn);
00165 } catch (const CORBA::Exception & exe) {
00166 throw raiseSctApiException(exe, "SctApi failed to initialiseAll -- sometimes happens when a TIM needs a reset having failed to power on properly (top six leds lit == bad).", __FILE__, __LINE__);
00167 }
00168 }
00169
00170
00171 } catch(SctApiAccessException& e) {
00172 e.sendToMrs(MRS_ERROR);
00173 return false;
00174 }
00175 SctNames::Mrs() << "RC_INIT" << MRS_TEXT("CC RunController initialised SctApi") << MRS_DIAGNOSTIC << ENDM;
00176
00177 return true;
00178 }
00179
00180 bool RunControl::configureAction() {
00181 cout << "CC configureAction" << endl;
00182 try {
00183 APICALL(api, configureAllModules(), "SctApi failed to configureAllModules")
00184 } catch(SctApiAccessException& e) {
00185 e.sendToMrs(MRS_ERROR);
00186 return false;
00187 }
00188 SctNames::Mrs() << "RC_CONFIG" << MRS_TEXT("CC RunController configured SctApi") << MRS_DIAGNOSTIC << ENDM;
00189 return true;
00190 }
00191
00193 bool RunControl::prepareAction() {
00194 cout << "CC prepare action" << endl;
00195 if (CORBA::is_nil(api)) {
00196 cout << "Can't enter PREPARE as we don't have the api" << endl;
00197 return false;
00198 }
00199
00200 RunParams runParams;
00201 ISInfoDictionary& is = SctNames::getISDictionary();
00202 ISInfo::Status isStatus;
00203
00204 isStatus = is.findValue("RunParams.RunParams", runParams);
00205
00206 if (isStatus!=ISInfo::Success){
00207 SctNames::Mrs() << "RC_PREPARE" << MRS_TEXT("CalibrationController failed to find RunParameters") << MRS_ERROR << ENDM;
00208 return false;
00209 }
00210
00211 try {
00212 APICALL(api, setRunNumber(runParams.run_number), "Failed to set run number")
00213 APICALL(api, setScanNumber(0), "Failed to set scan number")
00214 cout << "New Run, setting run number and scan number: " << runParams.run_number << " " << 0 << endl;
00215 cc.takeControl(runParams.run_number);
00216 } catch (SctApiAccessException& e) {
00217 e.sendToMrs(MRS_ERROR);
00218 return false;
00219 }
00220 return true;
00221 }
00222
00224 bool RunControl::stopEFAction() {
00225 cout << "CC exit stopFE action" << endl;
00226 cc.giveupControl();
00227 return true;
00228 }
00229
00230 bool RunControl::unloadAction () {
00231 cout << "CC unloadaction" << endl;
00232
00233 try {
00234 APICALL(api, shutdownAll(), "Could not shutdown all")
00235 } catch (SctApiAccessException& e) {
00236 e.sendToMrs(MRS_ERROR);
00237 return false;
00238 }
00239 cc.reset();
00240
00241 return true;
00242 }
00243
00244 bool RunControl::resetAction () {
00245 cout << "CC resetAction" << endl;
00246 cc.reset();
00247 return true;
00248 }
00249
00250 void RunControl::sctApiError(const string& reason) {
00251 std::cerr << "CC RC received " << reason << endl << "CC Giving up control"<<endl;
00252 this->raiseError(reason);
00253 cc.giveupControl();
00254 }
00255
00256 }