Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members

/usr/src/redhat/BUILD/fmdfeeserver-0.1/fmdfeeserver/server.cc

Go to the documentation of this file.
00001 #include <feeserver/fee_main.hh>
00002 #include <feeserver/fee_scmd.hh>
00003 #include <rcuce/rcuce_ce.hh>
00004 #include <rcuce/rcuce_rcu.hh>
00005 #include <rcuce/rcuce_dcsc.hh>
00006 #include <rcuce/rcuce_msg.hh>
00007 #include <fmdfeeserver/fec.hh>
00008 #include <rcuce/rcuce_sig.hh>
00009 #include <iostream>
00010 #include <iomanip>
00011 #include <string>
00012 DINIT(false);
00013 
00014 //____________________________________________________________________  
00015 template <typename T>
00016 T&
00017 str2val(const char* str, T& v)
00018 {
00019   std::stringstream s(str);
00020   s >> v;
00021 }
00022 
00023 //____________________________________________________________________  
00024 struct option_base 
00025 {
00026   option_base(char opt, const char* help, const char* arg="") 
00027     : _opt(opt), _help(help), _arg(arg)
00028   {}
00029   virtual ~option_base() {}
00030   virtual void print_help(std::ostream& out) const
00031   { 
00032     out << "\t-" << _opt << " " << std::left << std::setw(10) << _arg 
00033         << " " << std::setw(40) << _help;
00034   }
00035   virtual bool handle() = 0;
00036   virtual bool handle(const char* arg) = 0;
00037   virtual bool need_arg() const { return !_arg.empty(); }
00038   virtual char opt() const { return _opt; }
00039 protected:
00040   char _opt;
00041   std::string _help;
00042   std::string _arg;
00043 };
00044 
00045 //____________________________________________________________________  
00046 template <typename T>
00047 struct option : public option_base
00048 {
00049   option(char opt, const char* help, T def=T(), const char* arg="")
00050     : option_base(opt, help, arg), _value(def)
00051   {}
00052   operator T() { return _value; }
00053   operator const T() const { return _value; }
00054   virtual void print_help(std::ostream& out) const { 
00055     option_base::print_help(out);
00056     out << "[" << _value << "]" << std::endl;
00057   }
00058   virtual bool handle() { return toggle(_value); }
00059   virtual bool handle(const char* arg) { 
00060     if (_arg.empty()) return false; 
00061     if (!arg)         return false;
00062     std::stringstream s(arg);
00063     s >> _value;
00064     return true;
00065   }
00066   bool toggle(T& v) { return false; } 
00067 protected:
00068   T _value;
00069 };
00070 
00071 //____________________________________________________________________  
00072 template <>
00073 bool 
00074 option<bool>::toggle(bool& v) { v = !v; return true; }
00075 //____________________________________________________________________  
00076 template <>
00077 bool 
00078 option<unsigned int>::handle(const char* arg) 
00079 { 
00080   std::stringstream s(arg);
00081   if (arg[0] == '0') { 
00082     if (arg[1] == 'x' || arg[1] == 'X') s << std::hex;
00083     else                                s << std::oct;
00084   }
00085   s >> _value;
00086   return true; 
00087 }
00088 //____________________________________________________________________  
00089 struct command_line
00090 {
00091   command_line(const char* name) 
00092     : _options(), 
00093       _name(name),
00094       _help_opt('h', "Show this help", false)
00095   {
00096     add_option(_help_opt);
00097   }
00098   ~command_line() {}
00099   void print_help() { 
00100     std::cout << "Usage: " << _name << " [OPTIONS]\n\n"
00101               << "Options:" << std::endl;
00102     for (option_map::const_iterator i = _options.begin(); 
00103          i != _options.end(); ++i) 
00104       i->second->print_help(std::cout);
00105   }
00106   void add_option(option_base& o) { 
00107     char opt = o.opt();
00108     if (find_option(opt)) { 
00109       std::cerr << "Option " << opt << " already defined" << std::endl;
00110       return;
00111     }
00112     _options[opt] = &o;
00113   }
00114   option_base* find_option(char o) const { 
00115     option_map::const_iterator i = _options.find(o);
00116     if (i == _options.end()) return 0;
00117     return i->second;
00118   }
00119   bool handle(int argc, char** argv) { 
00120     for (int i = 1; i < argc; i++) { 
00121       if (argv[i][0] == '-') { 
00122         char o = argv[i][1];
00123         option_base* opt = find_option(o);
00124         if (!opt) { 
00125           std::cerr << _name << ": unknown option '" << argv[i] << "'" 
00126                     << std::endl;
00127           return false;
00128         }
00129         if (opt->need_arg()) {
00130           if (!opt->handle(argv[++i])) return false;
00131         }
00132         else { 
00133           if (!opt->handle()) return false;
00134         }
00135       }
00136     }
00137     if (_help_opt) print_help();
00138     return true;
00139   }
00140   bool got_help() const { return _help_opt; }
00141 protected:
00142   typedef std::map<char,option_base*> option_map;
00143   option_map _options;
00144   std::string _name;
00145   option<bool> _help_opt;
00146 };
00147   
00148     
00149      
00150 //____________________________________________________________________
00151 int 
00152 main(int argc, char** argv) 
00153 {
00154   command_line cl(argv[0]);
00155   option<std::string>  opt_n('n', "Set name of server", "FMD-FEE_0_0_0","NAME");
00156   option<std::string>  opt_D('D', "Set name of detector", "FMD", "DETECTOR");
00157   option<std::string>  opt_N('N', "Set DIM DNS host", "localhost", "HOST");
00158   option<bool>         opt_e('e', "Enable emulation mode", false);
00159   option<bool>         opt_d('d', "Enable debug output", false);
00160   option<bool>         opt_v('v', "Enable verbose output", false);
00161   option<bool>         opt_t('t', "Enable trace output", false);
00162   option<bool>         opt_i('i', "Enable informative output", false);
00163   option<unsigned int> opt_w('w', "Wait between updates", 2000, "MSECS");
00164   option<unsigned int> opt_T('T', "Watchdog timeout", 20000, "MSECS");
00165   option<unsigned int> opt_V('V',"Firmware version for emulation",8,"VERS");
00166   cl.add_option(opt_n);
00167   cl.add_option(opt_D);
00168   cl.add_option(opt_N);
00169   cl.add_option(opt_e);
00170   cl.add_option(opt_d);
00171   cl.add_option(opt_v);
00172   cl.add_option(opt_t);
00173   cl.add_option(opt_i);
00174   cl.add_option(opt_w);
00175   cl.add_option(opt_T);
00176   cl.add_option(opt_V);
00177   if (!cl.handle(argc, argv)) return 1;
00178   if (cl.got_help()) return 0;
00179   
00180   std::string  name     = opt_n;
00181   std::string  detector = opt_D;
00182   std::string  dns      = opt_N;
00183   bool         emul     = opt_e;
00184   bool         debug    = opt_d;
00185   bool         verbose  = opt_v;
00186   bool         trace    = opt_t;
00187   bool         info     = opt_i;
00188   unsigned int wait     = opt_w;// In milliseconds
00189   unsigned int wdto     = opt_T; // in milliseconds
00190   unsigned int fwvers   = opt_V;
00191 
00192   install_signal_handler();
00193   DTRACE(trace);
00194   //EndOptions
00195 
00196   std::cout << "Creating feeserver" << std::endl;
00197   FeeServer::Main m(name, detector, dns);
00198   unsigned int mask = (FeeServer::Message::Warning | 
00199                        FeeServer::Message::Error | 
00200                        FeeServer::Message::AuditFailure | 
00201                        FeeServer::Message::AuditSuccess |
00202                        FeeServer::Message::Alarm);
00203   // m.GetLogLevel();
00204   if (debug) mask |= FeeServer::Message::Debug;
00205   if (info)  mask |= FeeServer::Message::Info;
00206   m.SetLogLevel(mask);
00207   m.SetWatchdogTimeout(wdto);
00208 
00209   // m.AddCommand(new UpdateServer(m));
00210   m.AddCommand(new FeeServer::RestartServer(m));
00211   // m.AddCommand(new FeeServer::RebootMachine(m));
00212   // m.AddCommand(new FeeServer::ShutdownMachine(m));
00213   m.AddCommand(new FeeServer::ExitServer(m));
00214   m.AddCommand(new FeeServer::SetDeadband(m));
00215   m.AddCommand(new FeeServer::GetDeadband(m));
00216   m.AddCommand(new FeeServer::SetIssueTimeout(m));
00217   m.AddCommand(new FeeServer::GetIssueTimeout(m));
00218   m.AddCommand(new FeeServer::SetUpdateRate(m));
00219   m.AddCommand(new FeeServer::GetUpdateRate(m));
00220   m.AddCommand(new FeeServer::SetLogLevel(m));
00221   m.AddCommand(new FeeServer::GetLogLevel(m));
00222   m.AddCommand(new FeeServer::ShowServices(m));
00223   //EndFee
00224 
00225   RcuCE::ControlEngine ce(m, wait);
00226   m.SetCtrlEngine(ce);
00227   
00228   RcuCE::Dcsc* dcsc = 0;
00229   if (emul) dcsc = new RcuCE::DcscDummy(fwvers);
00230   else      dcsc = new RcuCE::Dcsc("",verbose);
00231 
00232   RcuCE::Rcu rcu(*dcsc, m);
00233   FecFactory f(m, rcu);
00234   rcu.SetFecFactory(f);
00235 
00236   ce.AddProvider(rcu);
00237   ce.AddHandler(rcu);
00238   ce.AddFsm(rcu);
00239   //EndRCU
00240   
00241   std::cout << "Running fee server " << std::endl;
00242   return m.Run();
00243 }
00244 //
00245 // EOF
00246 //

Generated on Thu Apr 17 14:32:44 2008 for FMD FeeServer by doxygen 1.3.5