apengine/apeng/src/ActiveApDb.cpp
changeset 66 ed07dcc72692
parent 0 5a93021fdf25
equal deleted inserted replaced
64:84c6623982f6 66:ed07dcc72692
    32 // CActiveApDb::NewL
    32 // CActiveApDb::NewL
    33 // ---------------------------------------------------------
    33 // ---------------------------------------------------------
    34 //
    34 //
    35 EXPORT_C CActiveApDb* CActiveApDb::NewL( TCommDbDatabaseType /*aType*/ )
    35 EXPORT_C CActiveApDb* CActiveApDb::NewL( TCommDbDatabaseType /*aType*/ )
    36     {
    36     {
    37     CLOG( ( EActiveDb, 0, _L( "-> CActiveApDb::NewL" ) ) );
    37     return NULL;
    38 
       
    39     CActiveApDb* db = new ( ELeave ) CActiveApDb;
       
    40     CleanupStack::PushL( db );
       
    41     db->ConstructL();
       
    42     CleanupStack::Pop( db ); // db
       
    43 
       
    44     CLOG( ( EActiveDb, 1, _L( "<- CActiveApDb::NewL" ) ) );
       
    45     return db;
       
    46     }
    38     }
    47 
    39 
    48 // ---------------------------------------------------------
    40 // ---------------------------------------------------------
    49 // CActiveApDb::~CActiveApDb
    41 // CActiveApDb::~CActiveApDb
    50 // ---------------------------------------------------------
    42 // ---------------------------------------------------------
    51 //
    43 //
    52 EXPORT_C CActiveApDb::~CActiveApDb()
    44 EXPORT_C CActiveApDb::~CActiveApDb()
    53     {
    45     {
    54     CLOG( ( EActiveDb, 0, _L( "-> CActiveApDb::~CActiveApDb" ) ) );
       
    55 
       
    56     delete iDbNotifier;
       
    57     delete iObservers;
       
    58     delete iDb;
       
    59 
       
    60     CLOG( ( EActiveDb, 1, _L( "<- CActiveApDb::~CActiveApDb" ) ) );
       
    61     }
    46     }
    62 
    47 
    63 
    48 
    64 // ---------------------------------------------------------
    49 // ---------------------------------------------------------
    65 // CActiveApDb::CActiveApDb
    50 // CActiveApDb::CActiveApDb
    74 // CActiveApDb::ConstructL
    59 // CActiveApDb::ConstructL
    75 // ---------------------------------------------------------
    60 // ---------------------------------------------------------
    76 //
    61 //
    77 void CActiveApDb::ConstructL()
    62 void CActiveApDb::ConstructL()
    78     {
    63     {
    79     iDb = CCommsDatabase::NewL( );
       
    80     iObservers = new ( ELeave ) CArrayPtrFlat<MActiveApDbObserver>
       
    81                         ( KObserverArrayGranularity );
       
    82     iDbNotifier = new ( ELeave ) CActiveApDbNotifier( *this );
       
    83 
       
    84     iState = EReady;
       
    85     }
    64     }
    86 
    65 
    87 
    66 
    88 // ---------------------------------------------------------
    67 // ---------------------------------------------------------
    89 // CActiveApDb::AddObserverL
    68 // CActiveApDb::AddObserverL
    90 // ---------------------------------------------------------
    69 // ---------------------------------------------------------
    91 //
    70 //
    92 EXPORT_C void CActiveApDb::AddObserverL( MActiveApDbObserver* anObserver )
    71 EXPORT_C void CActiveApDb::AddObserverL( MActiveApDbObserver* anObserver )
    93     {
    72     {
    94     CLOG( ( EActiveDb, 0, _L( "-> CActiveApDb::AddObserverL" ) ) );
       
    95 
       
    96     if ( !anObserver )
       
    97         {
       
    98         User::Leave( KErrNullPointerPassed );
       
    99         }
       
   100     iObservers->AppendL( anObserver );
       
   101     iDbNotifier->Start();
       
   102 
       
   103     CLOG( ( EActiveDb, 1, _L( "<- CActiveApDb::AddObserverL" ) ) );
       
   104     }
    73     }
   105 
    74 
   106 
    75 
   107 // ---------------------------------------------------------
    76 // ---------------------------------------------------------
   108 // CActiveApDb::RemoveObserver
    77 // CActiveApDb::RemoveObserver
   109 // ---------------------------------------------------------
    78 // ---------------------------------------------------------
   110 //
    79 //
   111 EXPORT_C void CActiveApDb::RemoveObserver( MActiveApDbObserver* anObserver )
    80 EXPORT_C void CActiveApDb::RemoveObserver( MActiveApDbObserver* anObserver )
   112     {
    81     {
   113     CLOG( ( EActiveDb, 0, _L( "-> CActiveApDb::RemoveObserver" ) ) );
       
   114 
       
   115     __ASSERT_DEBUG( anObserver, ApCommons::Panic( ENullPointer ) );
       
   116     TInt count = iObservers->Count();
       
   117     for ( TInt i = 0; i < count; i++ )
       
   118         {
       
   119         if ( iObservers->At( i ) == anObserver )
       
   120             {
       
   121             iObservers->Delete( i );
       
   122             // leave cycle for faster processing, can not return because
       
   123             // we may have to stop() if it was the last one...
       
   124             break;
       
   125             }
       
   126         }
       
   127     if ( !iObservers->Count() )
       
   128         { // no more observers waiting so stop notification...
       
   129         iDbNotifier->Stop();
       
   130         }
       
   131     // for some reason, observer was not found->
       
   132     // Someone has already removed it
       
   133     // simply ignore it
       
   134     CLOG( ( EActiveDb, 1, _L( "<- CActiveApDb::RemoveObserver" ) ) );
       
   135 
       
   136     }
    82     }
   137 
    83 
   138 
    84 
   139 // ---------------------------------------------------------
    85 // ---------------------------------------------------------
   140 // CActiveApDb::Database
    86 // CActiveApDb::Database
   141 // ---------------------------------------------------------
    87 // ---------------------------------------------------------
   142 //
    88 //
   143 EXPORT_C CCommsDatabase*  CActiveApDb::Database()
    89 EXPORT_C CCommsDatabase*  CActiveApDb::Database()
   144     {
    90     {
   145     CLOG( ( EActiveDb, 0, _L( "<-> CActiveApDb::Database" ) ) );
    91     return NULL;
   146 
       
   147     return iDb;
       
   148     }
    92     }
   149 
    93 
   150 // ---------------------------------------------------------
    94 // ---------------------------------------------------------
   151 // CActiveApDb::HandleDbEventL
    95 // CActiveApDb::HandleDbEventL
   152 // ---------------------------------------------------------
    96 // ---------------------------------------------------------
   153 //
    97 //
   154 void CActiveApDb::HandleDbEventL( TInt anEvent )
    98 void CActiveApDb::HandleDbEventL( TInt anEvent )
   155     {
    99     {
   156     CLOG( ( EActiveDb, 0, _L( "-> CActiveApDb::HandleDbEventL" ) ) );
       
   157 
       
   158     // possible events: EClose,EUnlock,ECommit,ERollback,ERecover
       
   159     switch ( anEvent )
       
   160         {
       
   161         case RDbNotifier::EUnlock:
       
   162             {
       
   163             CLOG( ( EActiveDb, 2, _L( "Notifier Unlock" ) ) );
       
   164             NotifyObserversL( MActiveApDbObserver::EDbAvailable );
       
   165             break;
       
   166             }
       
   167         case RDbNotifier::ECommit:
       
   168             {
       
   169             CLOG( ( EActiveDb, 2, _L( "Notifier Commit" ) ) );
       
   170             NotifyObserversL( MActiveApDbObserver::EDbChanged );
       
   171             break;
       
   172             }
       
   173         case RDbNotifier::EClose:
       
   174             {
       
   175             CLOG( ( EActiveDb, 2, _L( "Notifier Close/Rollbac/Recover" ) ) );
       
   176             NotifyObserversL( MActiveApDbObserver::EDbClosing );
       
   177             // Use base class' method to keep the notifier alive.
       
   178             // We need to know when all clients has released the db.
       
   179             break;
       
   180             }
       
   181         case RDbNotifier::ERollback:            
       
   182             {
       
   183             CLOG( ( EActiveDb, 2, _L( "Notifier Close/Rollbac/Recover" ) ) );
       
   184             NotifyObserversL( MActiveApDbObserver::EDbAvailable );
       
   185             break;
       
   186             }
       
   187         case RDbNotifier::ERecover:
       
   188             {
       
   189             CLOG( ( EActiveDb, 2, _L( "Notifier Close/Rollbac/Recover" ) ) );
       
   190             NotifyObserversL( MActiveApDbObserver::EDbClosing );
       
   191             // Use base class' method to keep the notifier alive.
       
   192             // We need to know when all clients has released the db.
       
   193             break;
       
   194             }
       
   195         default:
       
   196             {
       
   197             // Don't know what's happened, but instead of
       
   198             // Panicking, it's better to 'simulate' a change.
       
   199             // 'Defensive' programming.
       
   200             CLOG( ( EActiveDb, 2, _L( "Notifier UNKNOWN" ) ) );
       
   201             NotifyObserversL( MActiveApDbObserver::EDbChanged );
       
   202             break;
       
   203             }
       
   204         }
       
   205     CLOG( ( EActiveDb, 1, _L( "<- CActiveApDb::HandleDbEventL" ) ) );
       
   206 
   100 
   207     }
   101     }
   208 
   102 
   209 
   103 
   210 
   104 
   214 // ---------------------------------------------------------
   108 // ---------------------------------------------------------
   215 //
   109 //
   216 void CActiveApDb::NotifyObserversL( MActiveApDbObserver::TEvent anEvent )
   110 void CActiveApDb::NotifyObserversL( MActiveApDbObserver::TEvent anEvent )
   217     {
   111     {
   218 
   112 
   219     TInt i;
       
   220     TInt count = iObservers->Count();
       
   221     // one hazard is that if one client leaves,
       
   222     // the rest of the clients will not be notified this time...
       
   223     for ( i = 0; i < count; i++ )
       
   224         {
       
   225         TRAP_IGNORE( iObservers->At( i )->HandleApDbEventL( anEvent ) );
       
   226         }
       
   227     }
   113     }
   228 
   114 
   229 // End of File
   115 // End of File