Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Data Structures | File List | Namespace Members | Data Fields | Related Pages

AnalysisService.cpp

00001 #include "AnalysisService.h"
00002 #include "AnalysisAlgorithmMap.h"
00003 #include "AnalysisAlgorithm.h"
00004 #include "AnalysisWorkerGroup.h"
00005 #include "AnalysisArguments.h"
00006 #include "DcsInterface.h"
00007 #include "ConfigurationInterface.h"
00008 
00009 #include "Sct/SctNames.h"
00010 #include "Sct/StdExceptionWrapper.h"
00011 #include "Sct/IpcObjectException.h"
00012 #include "Sct/IS/IOManagerIS.h"
00013 #include "Sct/IS/IONameIS.h"
00014 #include "Sct/ConfigurationException.h"
00015 #include "Sct/DestroyingDeleter.h"
00016 #include "Sct/UnsupportedOperationError.h"
00017 #include "sctConf/configuration.h"
00018 #include "SctData/TestResult.h"
00019 #include "sctConfIPC/configipc.h"
00020 #include "CalibrationController/IS/TestData.h"
00021 
00022 #include <ipc/core.h>
00023 #include <boost/scoped_ptr.hpp>
00024 
00025 using namespace Sct;
00026 using namespace Sct::IS;
00027 using namespace SctData;
00028 using boost::scoped_ptr;
00029 
00030 namespace SctAnalysis {
00031 
00032 AnalysisService::~AnalysisService(){
00033 }
00034 
00035 void AnalysisService::shutdown(){
00036   if (workergroup) workergroup->stop();
00037   getServer().stop();
00038 }
00039 
00040 AnalysisService::AnalysisService(AnalysisArguments args) : 
00041       IPCNamedObject<POA_AnalysisServiceI::AnalysisServiceInterface,
00042              ipc::multi_thread>(
00043                     SctNames::getPartition(),
00044                     args.instanceName()),
00045       dcsinterface(0), infoReceiver(new ISInfoReceiver(SctNames::getPartition())), m_args(args) {
00046   if (!infoReceiver.get())
00047     throw ConfigurationException("AnalysisService::AnalysisService can't make infoReceiver ", __FILE__, __LINE__) ;
00048   std::cout << "Making DcsInterface ... " << std::endl;
00049     try{
00050       boost::shared_ptr<SctConfiguration::Configuration> configipc(new SctConfiguration::ConfigIPC);
00051       s_configuration=configipc;
00052       boost::shared_ptr<ConfigurationInterface> config_local (new ConfigurationInterface(configipc));
00053       dcsinterface = new DcsInterface(config_local);
00054       config_local->update();
00055     }catch(std::exception &e){
00056       SctNames::Mrs() << MRS_TEXT("I wont be able to save DCS information as I dont know the configuration (std::exception)")
00057               << "ANALYSIS_NO_DCS" << MRS_WARNING << ENDM;
00058     }catch(CORBA::Exception &e){
00059       SctNames::Mrs() << MRS_TEXT("I wont be able to save DCS information as I dont know the configuration (CORBA::Exception)")
00060               << "ANALYSIS_NO_DCS" << MRS_WARNING << ENDM;
00061     }catch(...){
00062       SctNames::Mrs() << MRS_TEXT("I wont be able to save DCS information as I dont know the configuration (unknown exception)")
00063               << "ANALYSIS_NO_DCS" << MRS_WARNING << ENDM;
00064     }
00065     try{
00066       setFitStrategy(m_args.getStrategyName());
00067     }catch(Throwable& e){
00068       e.sendToMrs();
00069       setFitStrategy("RootFitStrategy");
00070     }
00071     getFitStrategy().setOptions(m_args.getFitOptions());
00072     workergroup = new AnalysisWorkerGroup();
00073     workergroup->reportTo(m_args.getISStatusName());
00074 }
00075 
00076 IPCServer& AnalysisService::getServer() throw() {
00077     static IPCServer server;
00078     return server;
00079 }
00080 
00081 const std::string AnalysisService::getOutputISServerName(){
00082   return m_args.getOutputISServer();
00083 }
00084 
00085 boost::shared_ptr<SctConfiguration::Configuration> AnalysisService::getConfiguration(){
00086   return s_configuration;
00087 }
00088 
00089 void AnalysisService::run() {
00090     // start up worker thread.
00091     workergroup->go(m_args.getNWorkers());
00092     
00093     if (m_args.recoveryMode()) recover();
00094     const list<SctService::Arguments::Subscription>& theList=m_args.getInputISServers();
00095 
00096     // subscribe to various IS stuff.
00097     for (list<SctService::Arguments::Subscription>::const_iterator i=theList.begin(); 
00098      i!=theList.end(); ++i){
00099       ISInfo::Status s = infoReceiver->subscribe((*i).server.c_str(), 
00100                          (*i).regexp.c_str(), generalCallback);
00101       if (s!=ISInfo::Success){
00102     std::ostringstream oss;
00103     oss << "Analysis service could not subscribed to [" << (*i).server 
00104         << "] to receive [" << (*i).regexp << "]";
00105     SctNames::Mrs() << "ANALYSIS_SUBSCRIBE" << MRS_TEXT(oss.str()) << MRS_ERROR << ENDM;
00106       }else{
00107     std::ostringstream oss;
00108     oss << "Analysis service subscribed to [" << (*i).server 
00109         << "] to receive [" << (*i).regexp << "]";
00110     SctNames::Mrs() << "ANALYSIS_SUBSCRIBE" << MRS_TEXT(oss.str()) << MRS_INFORMATION << ENDM;
00111       }
00112     }
00113 }
00114 
00115   CORBA::Short AnalysisService::busy() {
00116     boost::recursive_mutex::scoped_lock lock(m_status_access);
00117     return workergroup->busy();
00118   }
00119 
00120   CORBA::Short AnalysisService::queueLength() {
00121   boost::recursive_mutex::scoped_lock lock(m_status_access);
00122     return workergroup->busy();
00123   }
00124 
00125 char* AnalysisService::status() throw() {
00126   boost::recursive_mutex::scoped_lock lock(m_status_access);
00127     ostringstream os;
00128     workergroup->printStatus(os);
00129     os << "\n" << AnalysisAlgorithmMap::instance().getAllStatus() << endl;
00130     if (dcsinterface){
00131       os << "AnalysisService : DCS INFORMATION" << endl;
00132       dcsinterface->printStatus(os);
00133     }
00134 
00135     const unsigned length = os.str().length()+1;
00136     char *statusMessage = new char[length];
00137     // Have to make a new char* because IPC wants to delete it after use!
00138     strcpy(statusMessage, os.str().c_str());
00139     return statusMessage;
00140 }
00141 
00142 void AnalysisService::analyzeModule(const char* testname, const char* modulename) throw() {
00143     try {
00144         shared_ptr<TestData> testdata( new TestData() );
00145         ISInfoDictionary& id = SctNames::getISDictionary();
00146         ISInfo::Status result = id.findValue(testname, *testdata);
00147 
00148         if (result != ISInfo::Success) {
00149             string os = "Error reading from IS server.  Couldn't get: ";
00150             os += testname;
00151             throw IsException(result, os, __FILE__, __LINE__);
00152         }
00153         //cout << "read " << testname << " which has #scans=" <<  testdata->nScans << endl;
00154         if (workergroup->findTest(*testdata).get() == 0 ) {
00155             workergroup->addTest(testdata);
00156         }
00157 
00158         for (unsigned iscan=testdata->startScanNumber;
00159                 iscan<(testdata->startScanNumber + testdata->nScans);
00160                 ++iscan ) {
00161             {
00162                 ostringstream name_raw;
00164                 name_raw << "SctData::RawScanResult." <<  testdata->runNumber << "." << iscan << "." << modulename;
00165                 //cout << "Looking for " << name_raw.str() << endl;
00166                 ISInfoIterator rawIter(SctNames::getPartition(), SctNames::getEventDataName().c_str(), name_raw.str().c_str());
00167                 while ( rawIter() ) {
00168                     try {
00169                         //cout << "trying to add " << rawIter.name() << endl;
00170                         workergroup->push(shared_ptr<IOName>(new IONameIS(rawIter.name())));
00171                     } catch ( Sct::Throwable& e) {
00172                         e.sendToMrs(MRS_ERROR);
00173                     }
00174                 }
00175             } {
00176                 ostringstream name_fit;
00178                 name_fit << "SctData::FitScanResult." <<  testdata->runNumber << "." << iscan << "." << modulename;
00179                 //cout << "Looking for " << name_fit.str() << endl;
00180                 ISInfoIterator fitIter(SctNames::getPartition(), SctNames::getFittedDataName().c_str(), name_fit.str().c_str());
00181                 while ( fitIter() ) {
00182                     try {
00183                         //cout << "trying to add " << fitIter.name() << endl;
00184                         workergroup->push(shared_ptr<IOName>(new IONameIS(fitIter.name())));
00185                     } catch ( Sct::Throwable& e) {
00186                         e.sendToMrs(MRS_ERROR);
00187                     }
00188                 }
00189             }
00190         }
00191     } catch (Throwable& e) {
00192         e.sendToMrs(MRS_ERROR);
00193         return;
00194     }
00195 }
00196 
00197 void AnalysisService::purge() throw() {
00198     try {
00199         cout << "Doing purge" << endl;
00200         workergroup->purge();
00201         cout << "Memory purge completed" << endl;
00202     } catch (Throwable& e) {
00203         e.sendToMrs(MRS_ERROR);
00204         return;
00205     }
00206 }
00207 
00208 void AnalysisService::analyze(const char* name) throw() {
00209     analyzeModule(name, "*");
00210 }
00211 
00212 void AnalysisService::generalCallback(ISCallbackInfo * isc){
00213   try{
00214     IONameIS name(isc->name());
00215     if (name.getClassName()=="TestData"){
00216       testDataCallback(isc);
00217     }else if (name.getClassName()=="SctData::RawScanResult"
00218           || name.getClassName()=="SctData::FitScanResult") {
00219       scanResultCallback(isc);
00220     }
00221   }catch(Sct::Throwable& e){
00222     e.sendToMrs();
00223   }
00224 }
00225 
00226 void AnalysisService::scanResultCallback(ISCallbackInfo * isc) {
00227     try {
00228         if (isc->reason() != ISInfoCreated )
00229             return;
00230         instance().workergroup->push(shared_ptr<IOName>(new IONameIS(isc->name())));
00231     } catch(Sct::Throwable& e) {
00232         e.sendToMrs(MRS_ERROR);
00233     }
00234 }
00235 
00236 void AnalysisService::testDataCallback(ISCallbackInfo * isc) {
00237     try {
00238         shared_ptr<TestData> testdata( new TestData() );
00239         isc->value(*testdata);
00240 
00241         if (isc->reason() == ISInfoCreated ) {
00242             instance().workergroup->push(shared_ptr<IOName>(new IONameIS(isc->name())));
00243     } else if (isc->reason() == ISInfoUpdated && testdata->status == TestData::ABORTED) {
00244         instance().workergroup->removeTestsUpTo(testdata);
00245         
00246     } else if (isc->reason() == ISInfoDeleted ) {
00247             instance().workergroup->removeTestsUpTo(testdata);
00248         }
00249     } catch (Sct::Throwable& e) {
00250         e.sendToMrs(MRS_ERROR);
00251     }
00252 }
00253 
00254 void AnalysisService::setFitStrategy(const string& name) throw(LogicError) {
00255     SctNames::Mrs() << "ANALYSIS_FIT_STRATEGY"  << MRS_PARAM<const char*>("Strategy", name.c_str()) 
00256             << MRS_INFORMATION << ENDM;
00257     fitStrategy = SctFitter::FitStrategyFactory::instance().getStrategy(name);
00258 }
00259 
00260 SctFitter::FitStrategy& AnalysisService::getFitStrategy() const throw(LogicError) {
00261     if (!fitStrategy)
00262         throw InvariantViolatedError("Fitter::getStrategy() no fit strategy defined", __FILE__, __LINE__);
00263     return *fitStrategy;
00264 }
00265 
00266 AnalysisService& AnalysisService::instance() {
00267     if (!service) throw Sct::IllegalStateError("Attempt to use uninitialised analysis", __FILE__, __LINE__);
00268     return *service;
00269 }
00270 
00271 bool AnalysisService::isFifo(){
00272   return instance().workergroup->isFifo();
00273 }
00274 
00275 void AnalysisService::setFifo(bool val){
00276   instance().workergroup->setFifo(val);
00277 }
00278 
00279 AnalysisService& AnalysisService::initialize(AnalysisArguments args) {
00280     service = new AnalysisService(args);
00281     return *service;
00282 }
00283 
00284 DcsInterface& AnalysisService::getDcsInterface(){
00285   if (!dcsinterface){
00286     throw ConfigurationException("Cant return dcsinterface",__FILE__, __LINE__);
00287   }else{
00288     return *dcsinterface;
00289   }
00290 }
00291 
00292 void AnalysisService::recover(){
00293   SctNames::Mrs() << "ANALYSIS_RECOVERY" << MRS_TEXT("Analysis recovery started in recovery mode") << ENDM;
00294   Sct::UnsupportedOperationError("Recovery not implimented", __FILE__, __LINE__).sendToMrs(MRS_DIAGNOSTIC);
00295 }
00296 
00297 
00298 AnalysisService* AnalysisService::service=0;
00299 
00300 }// end of namespace SctAnalysis

Generated on Fri Sep 16 18:01:48 2005 for SCT DAQ/DCS Software - C++ by doxygen 1.3.5