Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

main.cc

Go to the documentation of this file.
00001 //                            Package   : omniIFR
00002 //  main.cc                   Created   : 2004/02/22
00003 //                            Author    : Alex Tingle
00004 //
00005 //    Copyright (C) 2004 Alex Tingle.
00006 //
00007 //    This file is part of the omniIFR application.
00008 //
00009 //    omniIFR is free software; you can redistribute it and/or
00010 //    modify it under the terms of the GNU Lesser General Public
00011 //    License as published by the Free Software Foundation; either
00012 //    version 2.1 of the License, or (at your option) any later version.
00013 //
00014 //    omniIFR is distributed in the hope that it will be useful,
00015 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017 //    Lesser General Public License for more details.
00018 //
00019 //    You should have received a copy of the GNU Lesser General Public
00020 //    License along with this library; if not, write to the Free Software
00021 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 //
00023 
00024 #include "main.h"
00025 
00026 #ifdef HAVE_CONFIG_H
00027 #  include "config.h"
00028 #endif
00029 
00030 #if defined(HAVE_SIGNAL_H) && defined(HAVE_SIGSET)
00031 #  include <signal.h>
00032 #  define SIGSET(sig,func) ::sigset(sig,func)
00033 #elif defined(HAVE_SIGNAL_H)
00034 #  include <signal.h>
00035 #  define SIGSET(sig,func) ::signal(sig,func)
00036 #endif
00037 
00038 #include "Repository.h"
00039 #include "Persist.h"
00040 #include "daemon.h"
00041 
00042 CORBA::ORB_var orb; 
00043 bool readonly=false;  
00044 Omniifr::Persist* persist =NULL;
00045 int  checkpointPeriodSec=900; 
00046 omni_semaphore shutdownSemaphore(0);
00047 
00048 #ifdef HAVE_GETOPT
00049 #  include <unistd.h>
00050 extern char* optarg;
00051 extern int optind;
00052 
00053 void usage(int argc, char** argv);
00054 void version();
00055 
00056 void options(
00057   int               argc,
00058   char**            argv,
00059   Omniifr::Daemon&  daemon,
00060   Omniifr::Persist& persist
00061 )
00062 {
00063   int c;
00064   while ((c = getopt(argc,argv,"fl:P:rt:Vh")) != EOF)
00065   {
00066     switch (c)
00067     {
00068       case 'f': daemon.foreground(true);
00069                 break;
00070 
00071       case 'P': daemon.pidfile(optarg);
00072                 break;
00073 
00074       case 'l': persist.setdir(optarg);
00075                 break;
00076 
00077       case 'r': readonly=true;
00078                 break;
00079 
00080    // Informational options
00081       case 't': daemon.tracefile(optarg);
00082                 break;
00083 
00084       case 'V': version();
00085                 break;
00086 
00087       case 'h':
00088       default : usage(argc,argv);
00089                 break;
00090     }
00091   }
00092 }
00093 #endif
00094 
00095 int main(int argc, char* argv[])
00096 {
00097   Omniifr::Daemon daemon(argc,argv);
00098 
00099   const char* action=""; // Use this variable to help report errors.
00100   try
00101   {
00102     action="initialising ORB";
00103     orb=CORBA::ORB_init(argc,argv);
00104     
00105     action="constructing Persist";
00106     persist =new Omniifr::Persist(readonly? 0: checkpointPeriodSec);
00107 
00108 #ifdef HAVE_GETOPT
00109     action="processing command line options";
00110     options(argc,argv,daemon,*persist);
00111 #endif
00112 
00113     action="daemonizing";
00114     daemon.daemonize();
00115 
00116 
00117     action="initialising Repository";
00118     Omniifr::Repository_impl::inst().init( orb.in(), readonly, persist );
00119     if(readonly)
00120     {
00121       action="destroying Persist";
00122       delete persist;
00123       persist=NULL;
00124     }
00125 
00126 #ifdef HAVE_SIGNAL_H
00127     SIGSET(SIGINT , ::Omniifr_Orb_shutdown);
00128     SIGSET(SIGTERM, ::Omniifr_Orb_shutdown);
00129 #  ifdef SIGUSR1
00130     SIGSET(SIGUSR1, ::Omniifr_Orb_bumpTraceLevel);
00131 #  endif
00132 #  ifdef SIGUSR2
00133     SIGSET(SIGUSR2, ::Omniifr_Ifr_checkpoint);
00134 #  endif
00135 #  ifdef SIGPIPE
00136     SIGSET(SIGPIPE, SIG_IGN); // Ignore broken pipes
00137 #  endif
00138 #endif
00139 
00140     action="closing parent process";
00141     daemon.runningOk();
00142 
00143     // Park the main thread.
00144     // Do this by waiting on a semaphore rather than calling ORB_run() - 
00145     // this makes it safe for a signal to wake us up.
00146     action="running ORB";
00147     cout<<action<<'.'<<endl;
00148     shutdownSemaphore.wait();
00149     
00150     // SIGTERM or whatever has triggered shutdown.
00151     DB(1,"Shutdown requested.")
00152     action="shutting down ORB";
00153     orb->shutdown(1); // Synchronous shutdown
00154     action="destroying ORB";
00155     orb->destroy(); // clean up
00156     if(persist)
00157     {
00158       action="destroying Persist";
00159       delete persist;
00160     }
00161     return 0;
00162 
00163   }
00164   catch(CORBA::SystemException& ex)
00165   {
00166     cerr<<"Failed while "<<action<<"."
00167       IFELSE_OMNIORB4(" "<<ex._name()<<" ("<<ex.NP_minorString()<<")",) <<endl;
00168   }
00169   catch(CORBA::Exception& ex)
00170   {
00171     cerr<<"Failed while "<<action<<"." IFELSE_OMNIORB4(" "<<ex._name(),) <<endl;
00172   }
00173   return 1;
00174 }
00175 
00176 //
00177 // Signal handlers.
00178 //  
00179     
00180 extern "C"
00181 {   
00182   void Omniifr_Orb_shutdown(int signum)
00183   {
00184     // Wake up the main thread.
00185     shutdownSemaphore.post();
00186   }
00187 
00188   void Omniifr_Orb_bumpTraceLevel(int signum)
00189   {
00190     omniORB::traceLevel=(omniORB::traceLevel+5)%45;
00191     DB(0,"TRACE LEVEL BUMPED TO "<<omniORB::traceLevel<<" BY SIGNAL "<<signum)
00192   }
00193 
00194   void Omniifr_Ifr_checkpoint(int signum)
00195   {
00196     if(readonly)
00197     {
00198       DB(1,"Checkpoint request ignored: Interface Repository is read only.")
00199     }
00200     else
00201     {
00202       DB(1,"Checkpoint requested.")
00203       assert(persist!=NULL);
00204       persist->checkpoint();
00205     }
00206   }
00207 }
00208 

Generated on Fri Mar 4 13:03:23 2005 for OmniIFR by  doxygen 1.4.1