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

/ps/src/dsc/co/mw/mwfwk/cmwfw_main.cc

Go to the documentation of this file.
00001 #ifndef CMWFW_WINDOWSNT
00002 #include <stdlib.h>            //itoa
00003 #include <unistd.h>            //gethostname
00004 #include <signal.h>            //sigaction, sigfillset()
00005 #endif
00006 #include <stdlib.h>            //atoi
00007 #include "cmwfwTypes.h"
00008 #include "cmwfwDeviceServer.h"
00009 #include "cmwfwDeviceAdapter.h"
00010 
00011 
00012 void signalHandler(int sig);
00013 extern cmwfwDeviceAdapter* initRoutine();
00014 
00015 
00016 
00017 #define FILE "source file:cmwfw_main.cc"
00018 #define ID "Contact Steen JENSEN, PS/CO"
00019 #define PROGRAM "Middleware server framework"
00020 #define T(a,b,c,d,e) TRACE(a,"%s(%s): %s %s %s %s\n",myname,FILE,b,c,d,e)
00021 #define E(a,b,c,d) ERROR("%s:%s(%s):ERROR: %s %s %s %s - %s\n",PROGRAM,myname,FILE,a,b,c,d,ID)
00022 //T(CMWFW_TRACE_,"","","","");
00023 //E("","","","");
00024 
00025 
00026 
00027 
00032 int main(int argc,char** argv){
00033 
00034   char * myname = "main";
00035   char srvName[256];
00036   int traceLevel = 0;
00037   cmwfwDeviceAdapter* theAdapter = 0;
00038   cmwfwDeviceServer* theServer   = 0;
00039 
00040 
00041   struct sigaction sa;                          // Set up signal handlers
00042 
00043 
00044   if (argc > 1)
00045     traceLevel = atoi(argv[1]);
00046   cmwfwMessageLogger::log()->setTraceLevel(traceLevel);
00047   T(CMWFW_TRACE_CALLS,"Entering","","","");
00048 
00049 
00050   sa.sa_handler = signalHandler;                // This line requires compiler
00051                                                                                                 // option -fno-strict-prototype
00052   sigfillset(&sa.sa_mask);                      // with the given signature of
00053                                                                                                 // the handler
00054   sa.sa_flags = 0 | SA_NOABORT;
00055    
00056   if ((sigaction(SIGINT,  &sa, (struct sigaction*) 0) == -1) ||
00057       (sigaction(SIGTERM, &sa, (struct sigaction*) 0) == -1) ||
00058       (sigaction(SIGHUP,  &sa, (struct sigaction*) 0) == -1)){
00059         E("Unable to install signal handlers","","","");
00060     return(-1);
00061   };
00062 
00063   if (gethostname(srvName, 255) != 0){          // For PS DSC servers the
00064         E("Can't get host name","","","");              // server name is the DSC name.
00065     return(-1);
00066   };
00067 
00068 
00069   try{
00070     JTCInitialize jtc(argc, argv);                // Initialize the JTC package.
00071     theAdapter = initRoutine();
00072 
00073     if(!theAdapter){
00074           E("No adapter registered - exiting","","","");
00075           return(-1);
00076         };
00077 
00078     printf("\nInitializing middleware server framework....");
00079                                                                                           // Create and run the server
00080     cmwfwDeviceServerBase::init(argc, argv);  // Initialize the RDA runtime
00081                                                                                           // Create  DeviceServer
00082     printf("done\n");
00083         theServer = new cmwfwDeviceServer(srvName,argc,argv);
00084     theServer->Start(); //returns at shutdown
00085 
00086     if(!theServer){
00087           E("FATAL ERROR - out of heap - terminating","","","");
00088           return(-1);
00089         };
00090 
00091         theAdapter->terminate();
00092         delete theServer;
00093         delete theAdapter;
00094   }
00095   catch(rdaInternalError& ie){
00096         int i = ie.getLine();
00097         char cp[30];
00098         itoa(i,cp);
00099         E(ie.getFile()," line ",cp,ie.getMessage());
00100         return(-1);
00101   }
00102   catch(rdaException& e){
00103         E("main: caught rdaException, saying ",e.getMessage(),"","");
00104         return(-1);
00105   }
00106   catch(JTCException& je){
00107         E("main: caught JTC exception",je.getType(),je.getMessage(),"");
00108         return(-1);
00109   }
00110   catch(...){
00111         E("main: caught unhandled exception","","","");
00112         return(-1);
00113   };
00114 
00115   T(CMWFW_TRACE_CALLS,"Returning","","","");
00116   return(0);
00117 };
00118 
00123 void signalHandler(int sig)      // This handler signature requires
00124 {                                // compiler option -fno-strict-prototype 
00125                                                                  // Ignore further signals.
00126                                                     
00127    struct sigaction ignore;      // This is useful to prevent signals that
00128    ignore.sa_handler = SIG_IGN;  // are sent to the process in rapid succession
00129    sigemptyset(&ignore.sa_mask); // from triggering the handler a second time.
00130    ignore.sa_flags = 0;
00131    if (sigaction(SIGINT,  &ignore, (struct sigaction*) 0) == -1) abort();
00132    if (sigaction(SIGTERM, &ignore, (struct sigaction*) 0) == -1) abort();
00133    if (sigaction(SIGHUP,  &ignore, (struct sigaction*) 0) == -1) abort();
00134 
00135    cmwfwDeviceServerBase::shutDown(sig);   // Terminate the RDA server
00136 };

Generated at Thu May 10 11:28:57 2001 for C.E.R.N.-PSControlsMiddlewareFramework by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001