Search:

SENF Extensible Network Framework

  • Home
  • Download
  • Wiki
  • BerliOS
  • ChangeLog
  • Browse SVN
  • Bug Tracker
  • Overview
  • Examples
  • HowTos
  • Glossary
  • PPI
  • Packets
  • Scheduler
  • Socket
  • Utils
  • Console
  • Daemon
  • Logger
  • Termlib
  • Main Page
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

Daemon.hh

Go to the documentation of this file.
00001 // $Id: Daemon.hh 1756 2011-01-06 10:10:07Z tho $
00002 //
00003 // Copyright (C) 2007
00004 // Fraunhofer (FOKUS)
00005 // Competence Center NETwork research (NET), St. Augustin, GERMANY
00006 //     Stefan Bund <g0dil@berlios.de>
00007 //
00008 // This program is free software; you can redistribute it and/or modify
00009 // it under the terms of the GNU General Public License as published by
00010 // the Free Software Foundation; either version 2 of the License, or
00011 // (at your option) any later version.
00012 //
00013 // This program is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 // GNU General Public License for more details.
00017 //
00018 // You should have received a copy of the GNU General Public License
00019 // along with this program; if not, write to the
00020 // Free Software Foundation, Inc.,
00021 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00022 
00026 #ifndef HH_SENF_Utils_Daemon_Daemon_
00027 #define HH_SENF_Utils_Daemon_Daemon_ 1
00028 
00029 // Custom includes
00030 #include <senf/Utils/Logger/SenfLog.hh>
00031 
00032 //#include "Daemon.mpp"
00033 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00034 
00035 namespace senf {
00036 
00103     class Daemon : boost::noncopyable
00104     {
00105     public:
00106         SENF_LOG_CLASS_AREA();
00107 
00108         //-/////////////////////////////////////////////////////////////////////////
00109         // Types
00110 
00112         enum StdStream {
00113             StdOut  
00114         ,   StdErr  
00115         ,   Both    
00116         };
00117 
00118         //-/////////////////////////////////////////////////////////////////////////
00120         //\{
00121 
00122         virtual ~Daemon();
00123 
00124         //\}
00126         //\{
00127 
00128         void daemonize(bool);           
00129         bool daemon();                  
00130 
00131         void consoleLog(std::string const &, StdStream which = Both);
00133 
00141         void pidFile(std::string const &); 
00142 
00146         //\}
00148         //\{
00149 
00150         void detach();                  
00151 
00158         int argc();                     
00159         char const ** argv();           
00160         void removeDaemonArgs();        
00161 
00162         static void exit(unsigned code=0); 
00163 
00164         void logReopen();               
00165 
00168         //\}
00169 
00170         int start(int argc, char const ** argv); 
00171 
00174         static Daemon & instance();     
00175 
00176     protected:
00177         Daemon();
00178 
00179         virtual void configure();       
00180 
00183 #   ifdef DOXYGEN
00184     protected:
00185 #   else
00186     private:
00187 #   endif
00188 
00189         virtual void main();            
00190 
00193         virtual void init();            
00194 
00200         virtual void run();             
00201 
00206     private:
00207         void openLog();
00208         void fork();
00209         bool pidfileCreate();
00210         void installSighandlers();
00211 
00212         int argc_;
00213         char const ** argv_;
00214 
00215         bool daemonize_;
00216         std::string stdoutLog_;
00217         std::string stderrLog_;
00218         int stdout_;
00219         int stderr_;
00220         std::string pidfile_;
00221         bool pidfileCreated_;
00222 
00223         bool detached_;
00224 
00225         static Daemon * instance_;
00226     };
00227 
00235 #   define SENF_DAEMON_MAIN(klass)                                                                \
00236         int main(int argc, char const ** argv)                                                    \
00237         {                                                                                         \
00238             klass instance;                                                                       \
00239             return instance.start(argc, argv);                                                    \
00240         }
00241 
00242 }
00243 
00244 //-/////////////////////////////////////////////////////////////////////////////////////////////////
00245 #include "Daemon.cci"
00246 //#include "Daemon.ct"
00247 //#include "Daemon.cti"
00248 #endif
00249 
00250 
00251 // Local Variables:
00252 // mode: c++
00253 // fill-column: 100
00254 // comment-column: 40
00255 // c-file-style: "senf"
00256 // indent-tabs-mode: nil
00257 // ispell-local-dictionary: "american"
00258 // compile-command: "scons -u test"
00259 // End:

Contact: senf-dev@lists.berlios.de | © 2006-2010 Fraunhofer Institute for Open Communication Systems, Network Research