examples/ForumNokia/ThreadAndActiveObjectsEx/src/threadaoengine.cpp

00001 /*
00002  * Copyright (c) 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 
00031 // INCLUDES
00032 #include <e32base.h> 
00033 #include <aknnotewrappers.h>
00034 
00035 #include "../inc/threadaoengine.h"
00036 #include "SharedIntermediator.h"
00037 #include "BluetoothRefreshTimer.h"
00038 #include "Devicelistcontainer.h"
00039 
00040 // CONSTANTS
00041 _LIT( KThreadOneName, "thread1" );
00042 const TInt KStackSize = 4096;
00043 
00044 // ----------------------------------------------------------------------------
00045 // CThreadAOEngine::CThreadAOEngine(CSharedIntermediator* aSMediator)
00046 //
00047 // Constructor.
00048 // ----------------------------------------------------------------------------
00049 CThreadAOEngine::CThreadAOEngine( CSharedIntermediator* aSMediator ) :
00050     iCreatedThreads(EFalse), 
00051     iSMediator(aSMediator), iListener(0)
00052         {
00053         }
00054 
00055 // ----------------------------------------------------------------------------
00056 // CThreadAOEngine::~CThreadAOEngine()
00057 //
00058 // Destructor.
00059 // ----------------------------------------------------------------------------
00060 CThreadAOEngine::~CThreadAOEngine()
00061         {
00062     // Thread should be killed allready, if thread is already killed this does
00063     // nothing
00064         iThreadOne.Kill(KErrNone);
00065         // Handles should be closed
00066         iThreadOne.Close();
00067 
00068         delete iListener;
00069         }
00070 
00071 CThreadAOEngine* CThreadAOEngine::NewL(CSharedIntermediator* aSMediator)
00072         {
00073         CThreadAOEngine* self = CThreadAOEngine::NewLC(aSMediator);
00074         CleanupStack::Pop(self);
00075         return self;
00076         }
00077 
00078 CThreadAOEngine* CThreadAOEngine::NewLC(CSharedIntermediator* aSMediator)
00079         {
00080         CThreadAOEngine* self = new (ELeave) CThreadAOEngine(aSMediator);
00081         CleanupStack::PushL(self);
00082         self->ConstructL();
00083         return self;
00084         }
00085 
00086 // Standard Symbian OS 2nd phase constructor
00087 void CThreadAOEngine::ConstructL()
00088         {
00089         }
00090 
00091 // ----------------------------------------------------------------------------
00092 // CThreadAOEngine::StartL()
00093 //
00094 // Create a new thread (= thread1).
00095 // ----------------------------------------------------------------------------
00096 void CThreadAOEngine::StartL()
00097         {
00098         
00099         // Create threads only once
00100         if ( iCreatedThreads == false )
00101                 {
00102                 CreateThreadsL();
00103                 }
00104         }
00105 
00106 // ----------------------------------------------------------------------------
00107 // CThreadAOEngine::Stop()
00108 //
00109 // Stop thread1. Stop thread1 ActiveScheduler.
00110 // ----------------------------------------------------------------------------
00111 void CThreadAOEngine::Stop()
00112         {
00113         // Notify that the BT search should be cancelled.
00114         iSMediator->SetStopSearching(ETrue);
00115         }
00116 
00117 // ----------------------------------------------------------------------------
00118 // CThreadAOEngine::ExecuteThread(TAny *aPtr)
00119 //
00120 // Threadfunction of threadOne. Executed only by threadOne.
00121 // ----------------------------------------------------------------------------
00122 TInt CThreadAOEngine::ExecuteThreadOne(TAny *aPtr)
00123         {
00124         CSharedIntermediator* sharedMediator = 
00125                              static_cast<CSharedIntermediator*>( aPtr );
00126         
00127         // Create cleanupstack  
00128         CTrapCleanup* cleanupStack = CTrapCleanup::New();
00129 
00130         // Test cleanup stack, additional cleanup stack must be prosessed under TRAP
00131         // We can't use low level cleanup stack handling
00132         TRAPD( error, CThreadAOEngine::CreateActiveSchedulerL( sharedMediator ) )
00133 
00134         delete cleanupStack;
00135         
00136         return 0;
00137         }
00138 
00139 
00140 // ----------------------------------------------------------------------------
00141 // CThreadAOEngine::CreateActiveScheduler(CSharedIntermediator* aSMediator)
00142 //
00143 // Create ActiveScheduler for thread1.
00144 // ----------------------------------------------------------------------------
00145 void CThreadAOEngine::CreateActiveSchedulerL(CSharedIntermediator* aSMediator)
00146         {
00147         // create a new active scheduler
00148         CActiveScheduler* activeScheduler = new (ELeave) CActiveScheduler;
00149         CleanupStack::PushL(activeScheduler);
00150 
00151         // use static function Install to install previously created scheduler
00152         CActiveScheduler::Install(activeScheduler);
00153 
00154         CActiveSchedulerWait* wait = new (ELeave) CActiveSchedulerWait;
00155         CleanupStack::PushL(wait);
00156         
00157         CBluetoothRefreshTimer* timer = CBluetoothRefreshTimer::NewL(
00158                                                          aSMediator, wait );
00159         CleanupStack::PushL(timer);
00160 
00161         CActiveScheduler::Add(timer);
00162 
00163         // Scheduler must have one outstanding request before it can be started.
00164         timer->StartL();
00165         wait->Start();  
00166 
00167     // Remove and delete scheduler and the rest.
00168         CleanupStack::PopAndDestroy(3);
00169         }
00170 
00171 
00172 // ----------------------------------------------------------------------------
00173 // CThreadAOEngine::CreateThreadsL()
00174 //
00175 // Create thread1 and resume it. Activate thread1 listener.
00176 // ----------------------------------------------------------------------------
00177 void CThreadAOEngine::CreateThreadsL()
00178         {
00179         // Create thread which uses the same heap as main program.
00180         iThreadOne.Create( KThreadOneName, ExecuteThreadOne, KStackSize, NULL, iSMediator);
00181         iThreadOne.Resume();
00182         
00183         // Listen to thread1 state. 
00184         iListener = CThreadListener::NewL(iThreadOne);
00185 
00186         iCreatedThreads = ETrue;
00187         }
00188 

Generated by  doxygen 1.6.2