00001 #ifndef CMWFW_WINDOWSNT
00002 #include <stdlib.h>
00003 #include <unistd.h>
00004 #include <signal.h>
00005 #endif
00006 #include <stdlib.h>
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
00023
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;
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;
00051
00052 sigfillset(&sa.sa_mask);
00053
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){
00064 E("Can't get host name","","","");
00065 return(-1);
00066 };
00067
00068
00069 try{
00070 JTCInitialize jtc(argc, argv);
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
00080 cmwfwDeviceServerBase::init(argc, argv);
00081
00082 printf("done\n");
00083 theServer = new cmwfwDeviceServer(srvName,argc,argv);
00084 theServer->Start();
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)
00124 {
00125
00126
00127 struct sigaction ignore;
00128 ignore.sa_handler = SIG_IGN;
00129 sigemptyset(&ignore.sa_mask);
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);
00136 };