Arguments.cpp

00001 #include "Arguments.h"
00002 #include "Sct/LogicErrors.h"
00003 #include "Sct/SctNames.h"
00004 #include <boost/lexical_cast.hpp>
00005 
00006 namespace SctService {
00007   using std::string;
00008   using std::list;
00009   using std::endl;
00010   using Sct::InvalidArgumentError;
00011   using Sct::SctNames;
00012 
00013   Arguments::Arguments(int argc, char** argv) : 
00014     m_nworkers(1), m_recovery(false){
00015     m_list = makeList(argc,argv);
00016   }
00017   
00018   void Arguments::parse(){
00019     for (list<string>::iterator i=m_list.begin(); i!=m_list.end(); ++i){
00020       if (*i == "-help"){
00021     SctNames::Mrs() << "HELP_MESSAGE" << MRS_TEXT(printHelp()) << MRS_INFORMATION << ENDM; exit(1);     
00022       }else if (*i == "-recover"){
00023     m_recovery=true;
00024       } else if (*i == "-input"){
00025     string server=next(i);
00026     string regexp=next(i);
00027     m_input.push_back(Subscription(server,regexp));
00028       } else if (*i == "-name"){
00029     m_instanceName=next(i);
00030       } else if (*i == "-status"){
00031     m_status_name=next(i);
00032       } else if (*i == "-output"){
00033     m_output=next(i);
00034       } else if (*i == "-nworkers"){
00035     unsigned requested_workers=boost::lexical_cast<int>(next(i));
00036     m_nworkers=requested_workers;
00037     if (m_nworkers>MAX_WORKERS) {
00038       m_nworkers=MAX_WORKERS;
00039       std::cerr << "Number of workers requested (" << requested_workers
00040             << ") too large - limited to 10" << std::endl;
00041     }
00042       }
00043     }
00044   }
00045   
00046   string Arguments::next(list<string>::iterator& i) const{
00047     if (++i==m_list.end()) throw InvalidArgumentError(printHelp(), __FILE__, __LINE__);
00048     return *i;
00049   }
00050 
00051   list<string> Arguments::makeList(int argc, char** argv){
00052     list<std::string> theList;
00053     for (int i=1; i<argc; ++i){
00054       theList.push_back(argv[i]);
00055     }
00056     return theList;
00057   }
00058 
00059   std::string Arguments::printHelp() const{
00060     std::ostringstream oss;
00061     printHelp(oss);
00062     return oss.str();
00063   }
00064 
00065   std::string Arguments::print() const{
00066     std::ostringstream oss;
00067     print(oss);
00068     return oss.str();
00069   }
00070   
00071   Arguments::~Arguments(){}
00072 
00073   void Arguments::printHelp(std::ostream& oss) const{
00074     oss << "ServiceArguments:" << endl
00075     << "  -name <name>                   : name (in IPC) for this service" << endl
00076     << "  -status <name>                 : IS name to publish status information to" << endl
00077     << "  -nworkers <nworkers>           : number of worker threads" << endl
00078     << "  -recover (switch)              : try to do auto-recovery" << endl
00079     << "  -input <server name> <regexp>  : add IS server from which to receive data (can be used many times)" << endl
00080     << "  -output <server name>          : set IS server to write output data to" << endl
00081     << "  -help                          : print this help message" << endl;
00082   }
00083 
00084   void Arguments::print(std::ostream& oss) const{
00085     oss << "Initial arguments were: ";
00086     for (list<string>::const_iterator i=m_list.begin(); i!=m_list.end(); ++i){
00087       oss << " " << *i;
00088     }
00089     oss << endl;
00090   }
00091   unsigned Arguments::getNWorkers() const{
00092     return m_nworkers;
00093   }
00094 
00095   std::string Arguments::getOutputISServer() const{
00096     return m_output;
00097   }
00098   
00099   std::list<Arguments::Subscription> Arguments::getInputISServers() const{
00100     return m_input;
00101   }
00102   
00103   bool Arguments::recoveryMode() const{
00104     return m_recovery;
00105   }
00106 
00107   std::string Arguments::instanceName() const{
00108     return m_instanceName;
00109   }
00110 
00111   std::string Arguments::getISStatusName() const{
00112     return m_status_name;
00113   }
00114 
00115   void Arguments::setInstanceName(const std::string& n){
00116     m_instanceName=n;
00117   }
00118 
00119   void Arguments::setNWorkers(const unsigned n){
00120     m_nworkers=n;
00121   }
00122 
00123   void Arguments::setOutputISServer(const std::string &s){
00124     m_output=s;
00125   }
00126 
00127   void Arguments::setISStatusName(const std::string& s){
00128     m_status_name=s;
00129   }
00130   
00131   void Arguments::setRecoveryMode(const bool b){
00132     m_recovery=b;
00133   }
00134 
00135   Arguments::Subscription::Subscription(string server, string regexp) : 
00136     server (server), regexp(regexp)
00137   {
00138   }
00139 }

Generated on Mon Feb 6 14:01:17 2006 for SCT DAQ/DCS Software - C++ by  doxygen 1.4.6