00001
00014
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
00031
00032
00033
00034
00035 TInt ObserverThreadL( TInt aNoOfMsg )
00036 {
00037 TInt retVal = KErrNone;
00038
00039
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
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
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
00072 recvMsg->mtext[retVal-4] = '\0';
00073 printf("Observer: %s\n", recvMsg->mtext);
00074 }
00075 }
00076
00077
00078 retVal = msgctl(msgQFd, IPC_RMID, NULL);
00079 free(recvMsg);
00080
00081 logFile.Close();
00082 fileSession.Close();
00083 return retVal;
00084 }
00085
00086
00087
00088
00089
00090
00091
00092
00093 TInt ObserverThreadEntryPoint( TAny* aParam )
00094 {
00095 TInt retVal = KErrNone;
00096
00097
00098 CTrapCleanup* cleanupStack = CTrapCleanup::New();
00099 if(cleanupStack)
00100 {
00101
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
00116
00117
00118
00119 void CreateObserverThread( int aNoOfMsg )
00120 {
00121 RThread thread;
00122 TInt stackSize = 0x8000;
00123 thread.Create(_L("ObserverThread"), ObserverThreadEntryPoint, stackSize, NULL, (TAny*)aNoOfMsg);
00124 TRequestStatus stat;
00125 thread.Logon(stat);
00126
00127
00128 thread.Resume();
00129
00130
00131 User::WaitForRequest(stat);
00132 }
00133
00134 }
00135
00136