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

Generated on Thu Dec 15 21:14:22 2005 for SCT DAQ/DCS Software - C++ by doxygen 1.3.5