examples/PIPS/opencproducerconsumerex/src/observer.cpp

Go to the documentation of this file.
00001 
00014 // INCLUDE FILES
00015 #include <e32std.h>
00016 #include <f32file.h>
00017 
00018 #include <stdlib.h>
00019 #include <stdio.h>
00020 #include <sys/types.h>
00021 #include <sys/ipc.h>
00022 #include <sys/msg.h>
00023 #include <pthread.h>
00024 
00025 #include "CommanHeader.h"
00026 
00027 _LIT(KObserverLogFile, "C:\\ObserverLogFile.txt");
00028 
00029 /*****************************************************************************
00030 *  ObserverThreadL
00031 *  Function: Observer Thread that does the logging of all the events sent by 
00032 *  Producer and Consumers
00033 *******************************************************************************/
00034 
00035 TInt ObserverThreadL( TInt aNoOfMsg ) 
00036         {
00037         TInt retVal = KErrNone;
00038 
00039         //Connect to Symbian File Server and open a file for logging
00040         RFs fileSession;
00041         User::LeaveIfError(fileSession.Connect());
00042         RFile logFile;
00043         retVal = logFile.Open(fileSession, KObserverLogFile, EFileWrite);
00044         if(retVal)
00045                 {
00046                 retVal = logFile.Create(fileSession, KObserverLogFile, EFileWrite);
00047                 }
00048         User::LeaveIfError(retVal);
00049 
00050         //Create the MRT STDLIBS Message Q
00051     key_t msgQFd = msgget(KMSGQKEY, IPC_CREAT);
00052     if (msgQFd == -1)
00053         {
00054         logFile.Write(_L8("Msg Q Creation Failed\r"));
00055         return -1;
00056         }
00057    logFile.Write(_L8("Observer is Up and Running\r"));
00058 
00059         //Construct the message to be send thru msg q
00060         struct msgbuf* recvMsg = (struct msgbuf*)malloc(KMAXSENDMSG);
00061         TBuf8<KMAXSENDMSG> logData;
00062         
00063         
00064         for(int msgCount = 0; msgCount<aNoOfMsg; msgCount++ )
00065                 {
00066                 retVal = msgrcv(msgQFd, recvMsg, KMAXSENDMSG, 0, 0);
00067                 if(retVal > 0 )
00068                         {
00069                         logData.Copy((const TUint8 *)recvMsg->mtext, retVal-4);
00070                         logFile.Write(logData);
00071                         //Also flush the info on to console
00072                         recvMsg->mtext[retVal-4] = '\0';
00073                         printf("Observer: %s\n", recvMsg->mtext);
00074                         }
00075                 }
00076         
00077         //Close the Message Q
00078         retVal = msgctl(msgQFd, IPC_RMID, NULL);
00079         free(recvMsg);
00080         
00081         logFile.Close();
00082         fileSession.Close();            
00083         return retVal;
00084         }
00085 
00086 /*****************************************************************************
00087 *  ObserverThreadEntryPoint
00088 *  Function: Observer Thread Entry Point
00089 *  As its Symbian Thread, it has to create a cleanup stack and should have
00090 *  TOP level TRAP
00091 *******************************************************************************/
00092 
00093 TInt ObserverThreadEntryPoint( TAny* aParam ) 
00094         {
00095         TInt retVal = KErrNone;
00096         
00097         // Create a Cleanup Stack for this Thread
00098         CTrapCleanup* cleanupStack = CTrapCleanup::New();
00099         if(cleanupStack)
00100                 {
00101                 //Have a top level TRAP
00102                 TRAP( retVal, retVal = ObserverThreadL( (int)aParam ));
00103                 delete cleanupStack;
00104                 }
00105         else
00106                 {
00107                 retVal = KErrNoMemory;
00108                 }
00109         return retVal;
00110         }
00111 
00112 extern "C" {
00113 
00114 /*****************************************************************************
00115 *  CreateObserverThread
00116 *  Function: Function that creats Observing Thread (Symbian Thread)
00117 *******************************************************************************/
00118 
00119 void CreateObserverThread( int aNoOfMsg )
00120         {
00121         RThread thread;
00122         TInt stackSize = 0x8000; //Set the stack size for this thread as 8K
00123         thread.Create(_L("ObserverThread"), ObserverThreadEntryPoint, stackSize, NULL, (TAny*)aNoOfMsg);
00124         TRequestStatus stat;
00125         thread.Logon(stat);
00126 
00127         //Start executing the thread.
00128         thread.Resume();
00129 
00130         //Wait for the thread to Terminate.
00131         User::WaitForRequest(stat);
00132         }
00133 
00134 } //extern "C"
00135 
00136 //  End of File

Generated by  doxygen 1.6.2