examples/PIPS/opencproducerconsumerex/src/observer.cpp

00001 /*
00002 Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00003 
00004 Redistribution and use in source and binary forms, with or without
00005 modification, are permitted provided that the following conditions are met:
00006 
00007 * Redistributions of source code must retain the above copyright notice, this
00008   list of conditions and the following disclaimer.
00009 * Redistributions in binary form must reproduce the above copyright notice,
00010   this list of conditions and the following disclaimer in the documentation
00011   and/or other materials provided with the distribution.
00012 * Neither the name of Nokia Corporation nor the names of its contributors
00013   may be used to endorse or promote products derived from this software
00014   without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00020 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00023 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00024 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 
00027 Description:  
00028 */
00029 
00030 // INCLUDE FILES
00031 #include <e32std.h>
00032 #include <f32file.h>
00033 
00034 #include <stdlib.h>
00035 #include <stdio.h>
00036 #include <sys/types.h>
00037 #include <sys/ipc.h>
00038 #include <sys/msg.h>
00039 #include <pthread.h>
00040 
00041 #include "CommanHeader.h"
00042 
00043 _LIT(KObserverLogFile, "C:\\ObserverLogFile.txt");
00044 
00045 /*****************************************************************************
00046 *  ObserverThreadL
00047 *  Function: Observer Thread that does the logging of all the events sent by 
00048 *  Producer and Consumers
00049 *******************************************************************************/
00050 
00051 TInt ObserverThreadL( TInt aNoOfMsg ) 
00052         {
00053         TInt retVal = KErrNone;
00054 
00055         //Connect to Symbian File Server and open a file for logging
00056         RFs fileSession;
00057         User::LeaveIfError(fileSession.Connect());
00058         RFile logFile;
00059         retVal = logFile.Open(fileSession, KObserverLogFile, EFileWrite);
00060         if(retVal)
00061                 {
00062                 retVal = logFile.Create(fileSession, KObserverLogFile, EFileWrite);
00063                 }
00064         User::LeaveIfError(retVal);
00065 
00066         //Create the MRT STDLIBS Message Q
00067     key_t msgQFd = msgget(KMSGQKEY, IPC_CREAT);
00068     if (msgQFd == -1)
00069         {
00070         logFile.Write(_L8("Msg Q Creation Failed\r"));
00071         return -1;
00072         }
00073    logFile.Write(_L8("Observer is Up and Running\r"));
00074 
00075         //Construct the message to be send thru msg q
00076         struct msgbuf* recvMsg = (struct msgbuf*)malloc(KMAXSENDMSG);
00077         TBuf8<KMAXSENDMSG> logData;
00078         
00079         
00080         for(int msgCount = 0; msgCount<aNoOfMsg; msgCount++ )
00081                 {
00082                 retVal = msgrcv(msgQFd, recvMsg, KMAXSENDMSG, 0, 0);
00083                 if(retVal > 0 )
00084                         {
00085                         logData.Copy((const TUint8 *)recvMsg->mtext, retVal-4);
00086                         logFile.Write(logData);
00087                         //Also flush the info on to console
00088                         recvMsg->mtext[retVal-4] = '\0';
00089                         printf("Observer: %s\n", recvMsg->mtext);
00090                         }
00091                 }
00092         
00093         //Close the Message Q
00094         retVal = msgctl(msgQFd, IPC_RMID, NULL);
00095         free(recvMsg);
00096         
00097         logFile.Close();
00098         fileSession.Close();            
00099         return retVal;
00100         }
00101 
00102 /*****************************************************************************
00103 *  ObserverThreadEntryPoint
00104 *  Function: Observer Thread Entry Point
00105 *  As its Symbian Thread, it has to create a cleanup stack and should have
00106 *  TOP level TRAP
00107 *******************************************************************************/
00108 
00109 TInt ObserverThreadEntryPoint( TAny* aParam ) 
00110         {
00111         TInt retVal = KErrNone;
00112         
00113         // Create a Cleanup Stack for this Thread
00114         CTrapCleanup* cleanupStack = CTrapCleanup::New();
00115         if(cleanupStack)
00116                 {
00117                 //Have a top level TRAP
00118                 TRAP( retVal, retVal = ObserverThreadL( (int)aParam ));
00119                 delete cleanupStack;
00120                 }
00121         else
00122                 {
00123                 retVal = KErrNoMemory;
00124                 }
00125         return retVal;
00126         }
00127 
00128 extern "C" {
00129 
00130 /*****************************************************************************
00131 *  CreateObserverThread
00132 *  Function: Function that creats Observing Thread (Symbian Thread)
00133 *******************************************************************************/
00134 
00135 void CreateObserverThread( int aNoOfMsg )
00136         {
00137         RThread thread;
00138         TInt stackSize = 0x8000; //Set the stack size for this thread as 8K
00139         thread.Create(_L("ObserverThread"), ObserverThreadEntryPoint, stackSize, NULL, (TAny*)aNoOfMsg);
00140         TRequestStatus stat;
00141         thread.Logon(stat);
00142 
00143         //Start executing the thread.
00144         thread.Resume();
00145 
00146         //Wait for the thread to Terminate.
00147         User::WaitForRequest(stat);
00148         }
00149 
00150 } //extern "C"
00151 
00152 //  End of File

Generated by  doxygen 1.6.2