00001 #include "otsdaq-core/FECore/FEVInterfacesManager.h"
00002 #include "otsdaq-core/MessageFacility/MessageFacility.h"
00003 #include "otsdaq-core/Macros/CoutHeaderMacros.h"
00004 #include "otsdaq-core/FECore/FEVInterface.h"
00005 #include "otsdaq-core/PluginMakers/MakeInterface.h"
00006 #include "otsdaq-core/ConfigurationInterface/ConfigurationManager.h"
00007
00008 #include "messagefacility/MessageLogger/MessageLogger.h"
00009 #include "artdaq/DAQdata/configureMessageFacility.hh"
00010 #include "artdaq/BuildInfo/GetPackageBuildInfo.hh"
00011 #include "fhiclcpp/make_ParameterSet.h"
00012
00013 #include <iostream>
00014 #include <sstream>
00015
00016 using namespace ots;
00017
00018
00019 FEVInterfacesManager::FEVInterfacesManager(const ConfigurationTree& theXDAQContextConfigTree, const std::string& supervisorConfigurationPath)
00020 : Configurable(theXDAQContextConfigTree, supervisorConfigurationPath)
00021 {
00022 init();
00023 }
00024
00025
00026 FEVInterfacesManager::~FEVInterfacesManager(void)
00027 {
00028 destroy();
00029 }
00030
00031
00032 void FEVInterfacesManager::init(void)
00033 {
00034 }
00035
00036
00037 void FEVInterfacesManager::destroy(void)
00038 {
00039 for(auto& it : theFEInterfaces_)
00040 it.second.reset();
00041
00042 theFEInterfaces_.clear();
00043 }
00044
00045
00046 void FEVInterfacesManager::createInterfaces(void)
00047 {
00048 destroy();
00049
00050 __COUT__ << "Path: "<< theConfigurationPath_+"/LinkToFEInterfaceConfiguration" << std::endl;
00051 for(const auto& interface: theXDAQContextConfigTree_.getNode(theConfigurationPath_+"/LinkToFEInterfaceConfiguration").getChildren())
00052 {
00053 try
00054 {
00055 if(!interface.second.getNode(ViewColumnInfo::COL_NAME_STATUS).getValue<bool>()) continue;
00056 }
00057 catch(...)
00058 {
00059 __COUT_INFO__ << "Ignoring FE Status since Status column is missing!" << std::endl;
00060 }
00061
00062 __COUT__ << "Interface Plugin Name: "<< interface.second.getNode("FEInterfacePluginName").getValue<std::string>() << std::endl;
00063 __COUT__ << "Interface Name: "<< interface.first << std::endl;
00064 __COUT__ << "XDAQContext Node: "<< theXDAQContextConfigTree_ << std::endl;
00065 __COUT__ << "Path to configuration: "<< (theConfigurationPath_ + "/LinkToFEInterfaceConfiguration/" + interface.first + "/LinkToFETypeConfiguration") << std::endl;
00066 theFEInterfaces_[interface.first] = makeInterface(
00067 interface.second.getNode("FEInterfacePluginName").getValue<std::string>(),
00068 interface.first,
00069 theXDAQContextConfigTree_,
00070 (theConfigurationPath_ + "/LinkToFEInterfaceConfiguration/" + interface.first + "/LinkToFETypeConfiguration")
00071 );
00072 }
00073 __COUT__ << "Done creating interfaces" << std::endl;
00074 }
00075
00076
00077
00078 int FEVInterfacesManager::universalRead(const std::string &interfaceID, char* address, char* returnValue)
00079 {
00080 __COUT__ << "interfaceID: " << interfaceID << " and size: " << theFEInterfaces_.size() << std::endl;
00081
00082 if (theFEInterfaces_[interfaceID]->universalRead(address, returnValue) < 0) return -1;
00083 return 0;
00084 }
00085
00086
00087
00088
00089 unsigned int FEVInterfacesManager::getInterfaceUniversalAddressSize(const std::string &interfaceID)
00090 { return theFEInterfaces_[interfaceID]->getUniversalAddressSize(); }
00091
00092
00093
00094 unsigned int FEVInterfacesManager::getInterfaceUniversalDataSize(const std::string &interfaceID)
00095 { return theFEInterfaces_[interfaceID]->getUniversalDataSize(); }
00096
00097
00098
00099 void FEVInterfacesManager::universalWrite(const std::string &interfaceID, char* address, char* writeValue)
00100 {
00101
00102 __COUT__ << "interfaceID: " << interfaceID << " and size: " << theFEInterfaces_.size() << std::endl;
00103
00104 theFEInterfaces_[interfaceID]->universalWrite(address, writeValue);
00105
00106
00107
00108
00109
00110
00111
00112
00113 }
00114
00115
00116
00117
00118
00119
00120 std::string FEVInterfacesManager::getFEListString(const std::string &supervisorLid)
00121 {
00122 std::string retList = "";
00123
00124 for(const auto& it : theFEInterfaces_)
00125 {
00126 __COUT__ << "Just curious: it.first is " << it.first << std::endl;
00127
00128 retList += it.second->getInterfaceType() +
00129 ":" + supervisorLid + ":" +
00130 it.second->getInterfaceUID() + "\n";
00131 }
00132 return retList;
00133 }
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146 void FEVInterfacesManager::runFEMacro(const std::string &interfaceID,
00147 const std::string &feMacroName, const std::string &inputArgs, std::string &outputArgs)
00148 {
00149
00150 auto FEVInterfaceIt = theFEInterfaces_.find(interfaceID);
00151 if(FEVInterfaceIt == theFEInterfaces_.end())
00152 {
00153 __SS__ << "interfaceID '" << interfaceID << "' was not found!" << std::endl;
00154 __COUT_ERR__ << "\n" << ss.str();
00155 throw std::runtime_error(ss.str());
00156 }
00157
00158
00159 auto FEMacroIt = FEVInterfaceIt->second->getMapOfFEMacroFunctions().find(feMacroName);
00160 if(FEMacroIt == FEVInterfaceIt->second->getMapOfFEMacroFunctions().end())
00161 {
00162 __SS__ << "FE Macro '" << feMacroName << "' of interfaceID '" <<
00163 interfaceID << "' was not found!" << std::endl;
00164 __COUT_ERR__ << "\n" << ss.str();
00165 throw std::runtime_error(ss.str());
00166 }
00167
00168
00169
00170 std::vector<FEVInterface::frontEndMacroInArg_t> argsIn;
00171 {
00172 std::istringstream inputStream(inputArgs);
00173 std::string splitVal, argName, argValue;
00174 while (getline(inputStream, splitVal, ';'))
00175 {
00176 std::istringstream pairInputStream(splitVal);
00177 getline(pairInputStream, argName, ',');
00178 getline(pairInputStream, argValue, ',');
00179 argsIn.push_back(std::pair<std::string,std::string>(argName,argValue));
00180 }
00181 }
00182
00183
00184 if(FEMacroIt->second.namesOfInputArguments_.size() != argsIn.size())
00185 {
00186 __SS__ << "FE Macro '" << feMacroName << "' of interfaceID '" <<
00187 interfaceID << "' was attempted with a mismatch in" <<
00188 " number of input arguments. " << argsIn.size() <<
00189 " were given. " << FEMacroIt->second.namesOfInputArguments_.size() <<
00190 " expected." << std::endl;
00191 __COUT_ERR__ << "\n" << ss.str();
00192 throw std::runtime_error(ss.str());
00193 }
00194 for(unsigned int i=0;i<argsIn.size();++i)
00195 if(argsIn[i].first != FEMacroIt->second.namesOfInputArguments_[i])
00196 {
00197 __SS__ << "FE Macro '" << feMacroName << "' of interfaceID '" <<
00198 interfaceID << "' was attempted with a mismatch in" <<
00199 " a name of an input argument. " <<
00200 argsIn[i].first << " were given. " <<
00201 FEMacroIt->second.namesOfInputArguments_[i] <<
00202 " expected." << std::endl;
00203 __COUT_ERR__ << "\n" << ss.str();
00204 throw std::runtime_error(ss.str());
00205 }
00206
00207
00208
00209
00210 std::vector<std::string> returnStrings;
00211 std::vector<FEVInterface::frontEndMacroOutArg_t> argsOut;
00212
00213 {
00214 std::istringstream inputStream(outputArgs);
00215 std::string argName;
00216 while (getline(inputStream, argName, ','))
00217 {
00218 __COUT__ << "argName " << argName << std::endl;
00219
00220 returnStrings.push_back( "valueLore" );
00221 argsOut.push_back(FEVInterface::frontEndMacroOutArg_t(
00222 argName,
00223 returnStrings[returnStrings.size()-1]));
00224
00225
00226 __COUT__ << (uint64_t) &(returnStrings[returnStrings.size()-1]) << std::endl;
00227 }
00228 }
00229
00230
00231 if(FEMacroIt->second.namesOfOutputArguments_.size() != argsOut.size())
00232 {
00233 __SS__ << "FE Macro '" << feMacroName << "' of interfaceID '" <<
00234 interfaceID << "' was attempted with a mismatch in" <<
00235 " number of output arguments. " << argsOut.size() <<
00236 " were given. " << FEMacroIt->second.namesOfOutputArguments_.size() <<
00237 " expected." << std::endl;
00238 __COUT_ERR__ << "\n" << ss.str();
00239 throw std::runtime_error(ss.str());
00240 }
00241 for(unsigned int i=0;i<argsOut.size();++i)
00242 if(argsOut[i].first != FEMacroIt->second.namesOfOutputArguments_[i])
00243 {
00244 __SS__ << "FE Macro '" << feMacroName << "' of interfaceID '" <<
00245 interfaceID << "' was attempted with a mismatch in" <<
00246 " a name of an output argument. " <<
00247 argsOut[i].first << " were given. " <<
00248 FEMacroIt->second.namesOfOutputArguments_[i] <<
00249 " expected." << std::endl;
00250 __COUT_ERR__ << "\n" << ss.str();
00251 throw std::runtime_error(ss.str());
00252 }
00253
00254
00255
00256
00257
00258
00259 __COUT__ << "# of input args = " << argsIn.size() << std::endl;
00260 for(auto &argIn:argsIn)
00261 __COUT__ << argIn.first << ": " << argIn.second << std::endl;
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272 __COUT__ << "Trying it " << std::endl;
00273
00274
00275 (FEVInterfaceIt->second.get()->*(FEMacroIt->second.macroFunction_))(argsIn,argsOut);
00276
00277 __COUT__ << "Made it " << std::endl;
00278
00279 __COUT__ << "# of output args = " << argsOut.size() << std::endl;
00280 for(const auto &arg:argsOut)
00281 __COUT__ << arg.first << ": " << arg.second << std::endl;
00282
00283
00284
00285
00286
00287
00288 if(FEMacroIt->second.namesOfOutputArguments_.size() != argsOut.size())
00289 {
00290 __SS__ << "FE Macro '" << feMacroName << "' of interfaceID '" <<
00291 interfaceID << "' was attempted but the FE macro "
00292 "manipulated the output arguments vector. It is illegal "
00293 "to add or remove output vector name/value pairs." << std::endl;
00294 __COUT_ERR__ << "\n" << ss.str();
00295 throw std::runtime_error(ss.str());
00296 }
00297
00298
00299
00300
00301 outputArgs = "";
00302 for(unsigned int i=0; i<argsOut.size(); ++i)
00303 {
00304 if(i) outputArgs += ";";
00305 outputArgs += argsOut[i].first + "," + argsOut[i].second;
00306 }
00307
00308 __COUT__ << "outputArgs = " << outputArgs << std::endl;
00309
00310 }
00311
00312
00313
00314
00315
00316
00317
00318
00319 std::string FEVInterfacesManager::getFEMacrosString(const std::string &supervisorLid)
00320 {
00321 std::string retList = "";
00322
00323 for(const auto& it : theFEInterfaces_)
00324 {
00325 __COUT__ << "FE interface UID = " << it.first << std::endl;
00326
00327 retList += it.second->getInterfaceType() +
00328 ":" + supervisorLid + ":" +
00329 it.second->getInterfaceUID();
00330
00331 for(const auto& macroPair : it.second->getMapOfFEMacroFunctions())
00332 {
00333 __COUT__ << "FE Macro name = " << macroPair.first << std::endl;
00334 retList +=
00335 ":" + macroPair.first +
00336 ":" + std::to_string(macroPair.second.requiredUserPermissions_) +
00337 ":" + std::to_string(macroPair.second.namesOfInputArguments_.size());
00338 for(const auto& name:macroPair.second.namesOfInputArguments_)
00339 retList += ":" + name;
00340
00341 retList +=
00342 ":" + std::to_string(macroPair.second.namesOfOutputArguments_.size());
00343 for(const auto& name:macroPair.second.namesOfOutputArguments_)
00344 retList += ":" + name;
00345 }
00346
00347 retList += "\n";
00348 }
00349 return retList;
00350 }
00351
00352
00353 bool FEVInterfacesManager::allFEWorkloopsAreDone(void)
00354 {
00355 bool allFEWorkloopsAreDone = true;
00356 bool isActive;
00357
00358 for(const auto& FEInterface: theFEInterfaces_)
00359 {
00360 isActive = FEInterface.second->isActive();
00361 __COUT__ << FEInterface.second->getInterfaceUID() << " of type " <<
00362 FEInterface.second->getInterfaceType() << ": \t" <<
00363 "workLoop_->isActive() " <<
00364 (isActive?"yes":"no") << std::endl;
00365
00366 if(isActive)
00367 {
00368 allFEWorkloopsAreDone = false;
00369 break;
00370 }
00371 }
00372
00373 return allFEWorkloopsAreDone;
00374 }
00375
00376
00377 void FEVInterfacesManager::configure(void)
00378 {
00379 createInterfaces();
00380 for(const auto& it : theFEInterfaces_)
00381 {
00382
00383
00384
00385 __COUT__ << "Configuring interface " << it.first << std::endl;
00386 __COUT__ << "Configuring interface " << it.first << std::endl;
00387 __COUT__ << "Configuring interface " << it.first << std::endl;
00388 it.second->configure();
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398 __COUT__ << "Done configuring interface " << it.first << std::endl;
00399 __COUT__ << "Done configuring interface " << it.first << std::endl;
00400 __COUT__ << "Done configuring interface " << it.first << std::endl;
00401
00402
00403
00404
00405 }
00406
00407
00408 __COUT__ << "Done Configure" << std::endl;
00409 }
00410
00411
00412 void FEVInterfacesManager::halt(void)
00413 {
00414 for(const auto& it : theFEInterfaces_)
00415 {
00416 it.second->halt();
00417 it.second->stopWorkLoop();
00418
00419 }
00420
00421 destroy();
00422 }
00423
00424
00425 void FEVInterfacesManager::pause(void)
00426 {
00427 for(const auto& it : theFEInterfaces_)
00428 {
00429 it.second->pause();
00430 it.second->stopWorkLoop();
00431 }
00432 }
00433
00434
00435 void FEVInterfacesManager::resume(void)
00436 {
00437 for(const auto& it : theFEInterfaces_)
00438 {
00439 it.second->resume();
00440 it.second->startWorkLoop();
00441 }
00442 }
00443
00444
00445 void FEVInterfacesManager::start(std::string runNumber)
00446 {
00447 for(const auto& it : theFEInterfaces_)
00448 {
00449 it.second->start(runNumber);
00450 it.second->startWorkLoop();
00451 }
00452 }
00453
00454 void FEVInterfacesManager::stop(void)
00455 {
00456 for(const auto& it : theFEInterfaces_)
00457 {
00458 it.second->stopWorkLoop();
00459 it.second->stop();
00460 }
00461 }