📄 main.c
字号:
/******************************************************* main.c :the main process zhoushijie 2000/3/9********************************************************/#include <pthread.h>#include "main.h" #include "tcpdtc.h"/* -----------------------------------------------------main():the main function of the middleware-----------------------------------------------------*/void main(){ WSLOGDATA m_log; int WaitReturnVal; int WaitReturnStatus; int rt; int Max; char m_temsour[13],m_userid[11],m_pwd[9]; int m_mythread; pthread_t thread[10]; char ProcessErr[100]; /******************************************** start to init the system *********************************************/ m_log=SetLogContent(L_Initialize,"middleware start","The main thread begin to run"); writeLog(&m_log); /* -------------------------------------------------- Step 1:Clear all the old shm and sempahore -------------------------------------------------- */ DelAllSemAndShm(); /* -------------------------------------------------- step 2:init the output shared memeory:for the server -------------------------------------------------- */ if(InitOutputSharedMem()<0) if(OpenOutputSharedMem()<0) { printf("initialize the output shared memory failed\n"); m_log=SetLogContent(L_Error,"init the input shared memory error","in the main function ,when call the initOutputShareMem,an error occured. Maybe no enough RAM"); writeLog(&m_log); return; } else { printf("init the output shared memory successfully\n"); m_log=SetLogContent(L_Success,"init the output shared memory success","in the main function,the InitOutputShareMem call success"); writeLog(&m_log); } /* -------------------------------------------------- step 3:init the input shared memeory:for the server -------------------------------------------------- */ if(InitInputSharedMem()<0) if(OpenInputSharedMem()<0) { printf("initialize the input shared memory failed\n"); m_log=SetLogContent(L_Error,"init the input shared memory error","in the main function ,when call the initOutputShareMem,an error occured. Maybe no enough RAM"); writeLog(&m_log); return; } else { printf("init the input shared memory successfully\n"); m_log=SetLogContent(L_Success,"init the output shared memory success","in the main function,the InitOutputShareMem call success"); writeLog(&m_log); } /* -------------------------------------------------- Step 4:init the output and input semaphore -------------------------------------------------- */ if(InitSemaphore()<0) { printf("Init semaphore failed,But Contine to open the semaphore\n"); m_log=SetLogContent(L_Error,"initialize input and output semaphore failed","cannot initailze the semaphore"); writeLog(&m_log); if(OpenSemaphore()<0) { printf("cannot open the semaphore.The preogram exit\n"); m_log=SetLogContent(L_Error,"open input and output semaphore failed","cannot init an open the input and output semaphore"); writeLog(&m_log); return; } else { printf("Init semaphore success\n"); m_log=SetLogContent(L_Success,"init the input and output semaphore success","in the main function,the Initsemaphore call success"); writeLog(&m_log); } } /* ----------------------------------------- Step 5:init other info ----------------------------------------- */ if(!Init()) { printf("initinalize system fail!"); m_log=SetLogContent(L_Error,"initialize failed","cannot initailze the middleware"); writeLog(&m_log); return; } else { printf("initinalize system successful!\n"); m_log=SetLogContent(L_Success,"initialize successful"," initailze the middleware successfully"); writeLog(&m_log); } /* ------------------------------------------------- step 6:transactionID set to 0000000000 ------------------------------------------------- */ TransactionID=0; /* ------------------------------ step 7:init the MaxSN data structure ------------------------------ */ for(Max=0;Max<MAXCLIENTCOUNT;Max++) { MaxSN[Max].MaxSN =0; memset(MaxSN[Max].Destination ,'\0',sizeof(MaxSN[Max].Destination)); } /* -------------------------------------------------------------- step 8:init the message queue structure:UsedQueue restore the application's request message and ACK message,FreeQueue restore the free room in the RAM -------------------------------------------------------------- */ rt=InitMessageQueue(); if(rt) { printf("initialize the queue failed\n"); m_log=SetLogContent(L_Error,"init the queue error","in the main function ,when call the InitMessageQueue,an error occured .Maybe no enough RAM"); writeLog(&m_log); return; } else { printf("init the queue data structure successfully\n"); m_log=SetLogContent(L_Success,"init the queue success","in the main function,the initMessageQueue call success"); writeLog(&m_log); } /* ------------------------------------------- step 9:init the user information ------------------------------------------- */ memset(m_temsour,0,sizeof(m_temsour)); memset(m_userid,0,sizeof(m_userid)); memset(m_pwd,0,sizeof(m_pwd)); memset(m_tablename,0,sizeof(m_tablename)); /* -------------------------------------------------------------- step 10:set the database informatio -------------------------------------------------------------- */ sprintf(m_temsour,"%s",Readprofile(INIFILE,"DATABASE","DATASOURCE")); sprintf(m_userid,"%s",Readprofile(INIFILE,"DATABASE","USERID")); sprintf(m_pwd,"%s",Readprofile(INIFILE,"DATABASE","USERPWD")); sprintf(m_tablename,"%s",Readprofile(INIFILE,"DATABASE","TABLENAME")); /*the following open the database */#ifdef aixall m_dbstring.m_servername=(SQLCHAR *)m_temsour; m_dbstring.m_uid=(SQLCHAR *)m_userid; m_dbstring.m_pwd=(SQLCHAR *)m_pwd; if(OpenDBS(&m_DBID,m_dbstring)) { m_log=SetLogContent(L_Error,"open db error","cannot open the database maybe the database is corrupt"); writeLog(&m_log); } else { rt=InitUsedQueueFromDB(&USEDQUEUE,MAXQUEUELEN/2+1); if(rt) { printf("initialize the USEDQUEUE failed\n"); m_log=SetLogContent(L_Error,"init the USEDQUEUE error","in the main function ,when call the InitUsedQueueFromDB,an error occured .Maybe read from DB error"); writeLog(&m_log); } else { printf("init the USEDQUEUE data structure successfully\n"); m_log=SetLogContent(L_Success,"init the USEDQUEUE success","in the main function,the initUsedQueueFromDB call success,the data in DB write to the queue success"); writeLog(&m_log); } }#endif/* -----------------------------------------------------step 11:start the local timer thread and listen at the Msock-----------------------------------------------------*/m_mythread=pthread_create(&thread[0],NULL,Timer,NULL);if(m_mythread) { m_log=SetLogContent(L_Error,"second thread not create","cannot create the second thread"); writeLog(&m_log); printf("Second_Thread not be created success! with error NO. : %d",GetLastError()); return; }else { m_log=SetLogContent(L_Success,"timer thread create successfully","create the timer thread successfully"); writeLog(&m_log); printf("timer Thread be created successfully\n"); }RecordThreadStatus(Tread,0,"TimerThread","Active",m_mythread);/* -----------------------------------------------step 12:start the local thread and listen at the Msock----------------------------------------------*/m_mythread=pthread_create(&thread[1],NULL,LocalThread,NULL);if(m_mythread) { m_log=SetLogContent(L_Error,"Local thread not create","cannot \ create the local thread"); writeLog(&m_log); printf("local thread not be created success! with error NO. : %d",GetLastError()); return; }else { m_log=SetLogContent(L_Success,"local thread create successfully","create \ the thread successfully"); writeLog(&m_log); printf("local Thread be created successfully\n"); }RecordThreadStatus(Tread,1,"LocalThread","Active",m_mythread);/*---------------------------------------------step 13:start the network thread---------------------------------------------*/m_mythread=pthread_create(&thread[2],NULL,NetworkThread,NULL);if(m_mythread) { m_log=SetLogContent(L_Error,"network thread not create","cannot create the network thread"); writeLog(&m_log); printf(" Network Thread not be created success! with error NO. : %d",GetLastError()); return; }else { m_log=SetLogContent(L_Success,"network thread create successfully","create the thread successfully"); writeLog(&m_log); printf("network Thread be created successfully\n"); }RecordThreadStatus(Tread,2,"NetworkThread","Active",m_mythread);/*--------------------------------------------------------step 14:start the queuethread to disposal the queued message--------------------------------------------------------*/m_mythread=pthread_create(&thread[3],NULL,QueueThread,NULL);if(m_mythread) { m_log=SetLogContent(L_Error,"queue thread not create","cannot create the queue thread"); writeLog(&m_log); printf(" queue Thread not be created success! with error NO. : %d",GetLastError()); return; }else { m_log=SetLogContent(L_Success,"queue thread create successfully","create the thread successfully"); writeLog(&m_log); printf("queue Thread be created successfully\n"); }RecordThreadStatus(Tread,3,"QueueThread","Active",m_mythread);/* -----------------------------------------------------------------step 15:start the shared memory local thread and listen at the Msock-----------------------------------------------------------------*/m_mythread=pthread_create(&thread[4],NULL,SM_LocalThread,NULL);if(m_mythread) { m_log=SetLogContent(L_Error,"shared memory Local thread not create","cannot \ create the shared memory local thread"); writeLog(&m_log); printf(" shared memorylocal thread not be created success! with error NO. : %d",GetLastError()); return; }else { m_log=SetLogContent(L_Success,"shared memory local thread create successfully","create \ the shared memory local thread successfully"); writeLog(&m_log); printf("shared memeory local Thread be created successfully\n"); }RecordThreadStatus(Tread,4,"SM_LocalThread","Active",m_mythread);RecordThreadStatus(Tread,5,"MainThread","sleep",m_mythread);/**************************************************step 16:start the application process***************************************************/if(InitProcess(PROCESSINIFILE)<0) { printf("cannot start all the application process>>\n"); m_log=SetLogContent(L_Error,"start application process failed","cannot start all the application process"); writeLog(&m_log); return; }else { printf("all the application process started success\n"); m_log=SetLogContent(L_Success,"application process start success","in the main function,the Initprocess call success"); writeLog(&m_log); }for(;;){ sleep(100); WaitReturnVal=wait(&WaitReturnStatus); if(WaitReturnVal==-1) { m_log=SetLogContent(L_Error,"parent process wait no child","Parent process warit error"); writeLog(&m_log); } else if(!WaitReturnStatus) { sprintf(ProcessErr,"child process exit:[%ld]",(long)WaitReturnVal); m_log=SetLogContent(L_Error,"child process exit",ProcessErr); writeLog(&m_log); } else if(WIFEXITED(WaitReturnStatus)) { sprintf(ProcessErr,"child process :[%ld] terminate nomally stauts is :[%d]",(long)WaitReturnVal,WEXITSTATUS(WaitReturnStatus)); m_log=SetLogContent(L_Error,"parent process wait child process exit","Parent process warit an child process terminate"); writeLog(&m_log); } else if(WIFSIGNALED(WaitReturnStatus)) { sprintf(ProcessErr,"child process :[%ld] terminate due to singal",(long)WaitReturnVal); m_log=SetLogContent(L_Error,"parent process wait child process exit","Parent process warit an child process terminate"); writeLog(&m_log); } }/* sleep(INFINITE); CloseDBS(&m_DBID);*/m_log=SetLogContent(L_Success,"the middleare exit","the middleware success exit");writeLog(&m_log);return;}/*---------------------------------------------------------initmessagequeue():set the max RAM to FREEQUEUE and clear the USEDQUEUE----------------------------------------------------------*/int InitMessageQueue(){ int i; QueueEntry x; CreateQueue(&USEDQUEUE); CreateQueue(&FREEQUEUE); x.flag =0; memset(x.MessageQueue.destination ,'\0',sizeof(x.MessageQueue.destination)); memset(x.MessageQueue.source ,'\0',sizeof(x.MessageQueue.source)); memset(x.MessageQueue.sn ,'\0',sizeof(x.MessageQueue.sn)); memset(x.MessageQueue.oprcode ,'\0',sizeof(x.MessageQueue.oprcode)); memset(x.MessageQueue.requesttype ,'\0',sizeof(x.MessageQueue.requesttype)); memset(x.MessageQueue.packettype ,'\0',sizeof(x.MessageQueue.packettype)); memset(x.MessageQueue.message ,'\0',sizeof(x.MessageQueue.message)); for(i=0;i<MAXQUEUELEN;i++) { QueueNode *NewNode; if(NewNode=MakeQueueNode(x)) AppendNode(NewNode,&FREEQUEUE); else return 1; } return 0;}/* --------------------------------------------------------------------SetLogContent():this functin set the content of the log data structure--------------------------------------------------------------------*/WSLOGDATA SetLogContent(int Log_Level,char *Log_Envent,char *Log_Cause){ WSLOGDATA m_log; m_log.log_level=Log_Level; strcpy(m_log.log_event,Log_Envent); strcpy(m_log.log_cause,Log_Cause); return m_log;}/* --------------------------------------------------init(): function initialize the system and set thelocal host information--------------------------------------------------*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -