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 "DcsInterface.h"
00006 #include "ConfigurationInterface.h"
00007 
00008 #include "Sct/SctNames.h"
00009 #include "Sct/IpcObjectException.h"
00010 #include "Sct/IS/IOManagerIS.h"
00011 #include "Sct/IS/IONameIS.h"
00012 #include "Sct/ConfigurationException.h"
00013 #include "sctConf/configuration.h"
00014 
00015 #include "SctData/TestResult.h"
00016 #include "sctConf/configipc.h"
00017 #include "CalibrationController/IS/TestData.h"
00018 
00019 #include <boost/scoped_ptr.hpp>
00020 
00021 using namespace Sct;
00022 using namespace Sct::IS;
00023 using namespace SctData;
00024 using boost::scoped_ptr;
00025 
00026 namespace SctAnalysis {
00027 
00028 AnalysisService::AnalysisService(const string& fitStrategyName) : IPCObject(AnalysisServiceI_C_AnalysisServiceInterface_instanceName, &getServer()),
00029    dcsinterface(0),
00030    infoReceiver(new ISInfoReceiver(SctNames::getPartition()))
00031 {
00032     if (!infoReceiver.get())
00033         throw ConfigurationException("AnalysisService::AnalysisService can't make infoReceiver ", __FILE__, __LINE__) ;
00034     std::cout << "Making DcsInterface ... " << std::endl;
00035     boost::shared_ptr<SctConfiguration::Configuration> configipc(new SctConfiguration::ConfigIPC);
00036     boost::shared_ptr<ConfigurationInterface> config_local (new ConfigurationInterface(configipc));
00037     dcsinterface = new DcsInterface(config_local);
00038 
00039     try{
00040       config_local->update();
00041     }catch(Sct::Throwable& e){
00042       e.sendToMrs(MRS_INFORMATION);
00043     }catch(SctConfiguration::ConfigurationException &e){
00044       Sct::ConfigurationException(e.what(), __FILE__, __LINE__).sendToMrs(MRS_INFORMATION);
00045     }
00046     setFitStrategy(fitStrategyName);
00047     workergroup = new AnalysisWorkerGroup();
00048 }
00049 
00050 IPCServer& AnalysisService::getServer() throw() {
00051     static IPCServer server(AnalysisServiceI_C_AnalysisServiceInterface_serverName, SctNames::getPartition());
00052     return server;
00053 }
00054 
00055 void AnalysisService::run() {
00056     // start up worker thread.
00057     workergroup->go(1);
00058 
00059     // subscribe to various IS stuff.
00060     infoReceiver->subscribe(SctNames::getControlDataName().c_str(), ".*TestData.*", testDataCallback, this);
00061     infoReceiver->subscribe(SctNames::getFittedDataName().c_str(),  ".*FitScanResult.*", scanResultCallback, this);
00062     infoReceiver->subscribe(SctNames::getEventDataName().c_str(), ".*RawScanResult.*", scanResultCallback, this);
00063 }
00064 
00065   ilu_ShortInteger AnalysisService::busy(AnalysisServiceIStatus* status) {
00066     boost::recursive_mutex::scoped_lock lock(m_status_access);
00067      status->returnCode = AnalysisServiceIReply_Success;
00068     return workergroup->busy();
00069   }
00070 
00071   ilu_ShortInteger AnalysisService::queueLength(AnalysisServiceIStatus* status) {
00072   boost::recursive_mutex::scoped_lock lock(m_status_access);
00073      status->returnCode = AnalysisServiceIReply_Success;
00074     return workergroup->busy();
00075   }
00076 
00077 char* AnalysisService::status(AnalysisServiceIStatus* status) throw() {
00078   boost::recursive_mutex::scoped_lock lock(m_status_access);
00079     status->returnCode = AnalysisServiceIReply_Success;
00080     ostringstream os;
00081     workergroup->printStatus(os);
00082     os << "\n" << AnalysisAlgorithmMap::instance().getAllStatus() << endl;
00083     if (dcsinterface){
00084       os << "AnalysisService : DCS INFORMATION" << endl;
00085       dcsinterface->printStatus(os);
00086     }
00087 
00088     // Have to make a new char* because IPC wants to delete it after use!
00089     const unsigned length = os.str().length()+1;
00090     char *statusMessage = new char[length];
00091     std::cout << os.str();
00092     strcpy(statusMessage, os.str().c_str());
00093     return statusMessage;
00094 }
00095 
00096 void AnalysisService::analyzeModule(AnalysisServiceIStatus* status, char* testname, char* modulename) throw() {
00097     try {
00098         shared_ptr<TestData> testdata( new TestData() );
00099         ISInfoDictionary& id = SctNames::getISDictionary();
00100         ISInfo::Status result = id.findValue(testname, *testdata);
00101 
00102         if (result != ISInfo::Success) {
00103             string os = "Error reading from IS server.  Couldn't get: ";
00104             os += testname;
00105             throw IsException(result, os, __FILE__, __LINE__);
00106         }
00107         cout << "read " << testname << " which has #scans=" <<  testdata->nScans << endl;
00108         if (workergroup->findTest(*testdata).get() == 0 ) {
00109             workergroup->addTest(testdata);
00110         }
00111 
00112         for (unsigned iscan=testdata->startScanNumber;
00113                 iscan<(testdata->startScanNumber + testdata->nScans);
00114                 ++iscan ) {
00115             {
00116                 ostringstream name_raw;
00118                 name_raw << "SctData::RawScanResult." <<  testdata->runNumber << "." << iscan << "." << modulename;
00119                 cout << "Looking for " << name_raw.str() << endl;
00120                 ISInfoIterator rawIter(SctNames::getPartition(), SctNames::getEventDataName().c_str(), name_raw.str().c_str());
00121                 while ( rawIter() ) {
00122                     try {
00123                         cout << "trying to add " << rawIter.name() << endl;
00124                         workergroup->push(shared_ptr<IOName>(new IONameIS(rawIter.name())));
00125                     } catch ( Sct::Throwable& e) {
00126                         e.sendToMrs(MRS_ERROR);
00127                     }
00128                 }
00129             } {
00130                 ostringstream name_fit;
00132                 name_fit << "SctData::FitScanResult." <<  testdata->runNumber << "." << iscan << "." << modulename;
00133                 cout << "Looking for " << name_fit.str() << endl;
00134                 ISInfoIterator fitIter(SctNames::getPartition(), SctNames::getFittedDataName().c_str(), name_fit.str().c_str());
00135                 while ( fitIter() ) {
00136                     try {
00137                         cout << "trying to add " << fitIter.name() << endl;
00138                         workergroup->push(shared_ptr<IOName>(new IONameIS(fitIter.name())));
00139                     } catch ( Sct::Throwable& e) {
00140                         e.sendToMrs(MRS_ERROR);
00141                     }
00142                 }
00143             }
00144         }
00145         status->returnCode = AnalysisServiceIReply_Success;
00146     } catch (Throwable& e) {
00147         e.sendToMrs(MRS_ERROR);
00148         return;
00149     }
00150 }
00151 
00152 void AnalysisService::purge(AnalysisServiceIStatus* status) throw() {
00153     try {
00154         cout << "Doing purge" << endl;
00155         workergroup->purge();
00156         cout << "Memory purge completed" << endl;
00157         status->returnCode = AnalysisServiceIReply_Success;
00158     } catch (Throwable& e) {
00159         e.sendToMrs(MRS_ERROR);
00160         return;
00161     }
00162 }
00163 
00164 void AnalysisService::analyze(AnalysisServiceIStatus* status, char* name) throw() {
00165     analyzeModule(status, name, "*");
00166 }
00167 
00168 void AnalysisService::scanResultCallback(ISCallbackInfo * isc) {
00169     try {
00170         if (isc->reason() != ISInfoCreated )
00171             return;
00173         cout << "AnalysisService scanResultCallback on " << string(isc->name())<<endl;
00174         //cout << "AnalysisService scanResultCallback busy=" << AnalysisWorkerGroupFactory::pointer()->busy()
00175         //<< " Q=" << AnalysisWorkerGroupFactory::pointer()->queueSize()
00176         //<< " Nworker=" << AnalysisWorkerGroupFactory::pointer()->nWorkers() << endl;
00177         instance().workergroup->push(shared_ptr<IOName>(new IONameIS(isc->name())));
00178         //cout << " scanResultCallback done " << endl;
00179     } catch(Sct::Throwable& e) {
00180         e.sendToMrs(MRS_ERROR);
00181     }
00182 }
00183 
00184 void AnalysisService::testDataCallback(ISCallbackInfo * isc) {
00185     try {
00186         shared_ptr<TestData> testdata( new TestData() );
00187         isc->value(*testdata);
00188 
00189         if (isc->reason() == ISInfoCreated ) {
00190       cout << "Callback for new testdata object " << isc->name() << endl;
00191         instance().workergroup->push(shared_ptr<IOName>(new IONameIS(isc->name())));
00192     } else if (isc->reason() == ISInfoUpdated && testdata->status == TestData::ABORTED) {
00193         instance().workergroup->removeTestsUpTo(testdata);    
00194     } else if (isc->reason() == ISInfoDeleted ) {
00195             instance().workergroup->removeTestsUpTo(testdata);
00196         }
00197     } catch (Sct::Throwable& e) {
00198         e.sendToMrs(MRS_ERROR);
00199     }
00200 }
00201 
00202 void AnalysisService::setFitStrategy(const string& name) throw(LogicError) {
00203     fitStrategy = SctFitter::FitStrategyFactory::instance().getStrategy(name);
00204 }
00205 
00206 SctFitter::FitStrategy& AnalysisService::getFitStrategy() const throw(LogicError) {
00207     if (!fitStrategy)
00208         throw InvariantViolatedError("Fitter::getStrategy() no fit strategy defined", __FILE__, __LINE__);
00209     return *fitStrategy;
00210 }
00211 
00212 AnalysisService& AnalysisService::instance() {
00213     if (!service)
00214         return initialize();
00215     return *service;
00216 }
00217 
00218 AnalysisService& AnalysisService::initialize(const string& fitStrategyName) {
00219     if (service) {
00220         service->setFitStrategy(fitStrategyName);
00221     } else {
00222         service = new AnalysisService(fitStrategyName);
00223     }
00224     return *service;
00225 }
00226 
00227   DcsInterface& AnalysisService::getDcsInterface(){
00228     if (!dcsinterface){
00229       throw IllegalStateError("Cant return dcsinterface",__FILE__, __LINE__);
00230     }else{
00231       return *dcsinterface;
00232     }
00233   }
00234 
00235 AnalysisService* AnalysisService::service = 0;
00236 
00237 }// end of namespace SctAnalysis

Generated on Fri Jan 14 12:47:01 2005 for SCT DAQ/DCS Software - C++ by doxygen 1.3.5