S60 3rd Edition SDK for Symbian OS
Example Applications Guide

SIPExState.cpp

00001 /*
00002 * ==============================================================================
00003 *  Name        : SIPExState.cpp
00004 *  Part of     : SIPExEngine
00005 *  Interface   : 
00006 *  Description : 
00007 *  Version     : 
00008 *
00009 *  Copyright (c) 2004-2006 Nokia Corporation.
00010 *  This material, including documentation and any related 
00011 *  computer programs, is protected by copyright controlled by 
00012 *  Nokia Corporation.
00013 * ==============================================================================
00014 */
00015 
00016 #include    "SIPExState.h"
00017 #include    "SIPExGameEngine.h"
00018 #include    "SIPExSIPEngine.h"
00019 #include    "SIPExGameObserver.h"
00020 
00021 // -----------------------------------------------------------------------------
00022 // TSIPExState::ChangeState
00023 // Changes the active state through the context.
00024 // -----------------------------------------------------------------------------
00025 //
00026 void TSIPExState::ChangeState( CSIPExEngine* aContext, TSIPExState* aNewState )
00027     {
00028     aContext->ChangeState( *aNewState );
00029     }
00030 
00031 // -----------------------------------------------------------------------------
00032 // TSIPExState::Info
00033 // Traps the leaving functions. Ignores if leaves.
00034 // -----------------------------------------------------------------------------
00035 //
00036 void TSIPExState::Info( CSIPExEngine* aContext, const TDesC& aText )
00037     {
00038     TRAPD( ignore, aContext->InfoL( aText ) );
00039     }
00040 
00041 // -----------------------------------------------------------------------------
00042 // TSIPExState::StatusInfo
00043 // Traps the leaving functions. Ignores if leaves.
00044 // -----------------------------------------------------------------------------
00045 //
00046 void TSIPExState::StatusInfo( CSIPExEngine* aContext, const TDesC& aText )
00047     {
00048     TRAPD( ignore, aContext->StatusInfoL( aText ) );
00049     }
00050 
00051 // -----------------------------------------------------------------------------
00052 // TSIPExState::CombineAndShowInfoL
00053 // Combine desc and number value. E.g. aText="Hello", aNum=2, result="Hello 2".
00054 // -----------------------------------------------------------------------------
00055 //
00056 void TSIPExState::CombineAndShowInfoL( 
00057     CSIPExEngine* aContext, 
00058     const TDesC& aText, 
00059     TInt aNum )
00060     {
00061     HBufC* buf = HBufC::NewLC( 64 );
00062     buf->Des().Append( aText );
00063     buf->Des().AppendNum( aNum );
00064     Info( aContext, *buf );
00065     CleanupStack::PopAndDestroy( buf );
00066     }
00067 
00068 // -----------------------------------------------------------------------------
00069 // TSIPExState::InviteReceived
00070 // Default implementation for virtual function.
00071 // -----------------------------------------------------------------------------
00072 //
00073 void TSIPExState::InviteReceived( CSIPExEngine* /*aContext*/, 
00074                 const TDesC8& /*aFrom*/, const TUint32 /*aIapId*/ ) 
00075     { 
00076     }
00077 
00078 // -----------------------------------------------------------------------------
00079 // TSIPExState::InviteAcceptedByRemote
00080 // Default implementation for virtual function.
00081 // -----------------------------------------------------------------------------
00082 //
00083 void TSIPExState::InviteAcceptedByRemote( 
00084     CSIPExEngine* /*aContext*/, 
00085     const TInetAddr& /*aIPAddress*/, 
00086     const TUint32 /*aIapId*/ ) 
00087     { 
00088     }
00089 
00090 // -----------------------------------------------------------------------------
00091 // TSIPExState::InviteDeclinedByRemote
00092 // Default implementation for virtual function.
00093 // -----------------------------------------------------------------------------
00094 //
00095 void TSIPExState::InviteDeclinedByRemote( 
00096     CSIPExEngine* /*aContext*/, 
00097     const TInt /*aResponse*/ ) 
00098     { 
00099     }
00100 
00101 // -----------------------------------------------------------------------------
00102 // TSIPExState::InvitationCancelled
00103 // Default implementation for virtual function.
00104 // -----------------------------------------------------------------------------
00105 //
00106 void TSIPExState::InvitationCancelled( CSIPExEngine* /*aContext*/) 
00107     { 
00108     }
00109 
00110 // -----------------------------------------------------------------------------
00111 // TSIPExState::CancelFailed
00112 // Default implementation for virtual function.
00113 // -----------------------------------------------------------------------------
00114 //
00115 void TSIPExState::CancelFailed( CSIPExEngine* /*aContext*/ ) 
00116     { 
00117     }
00118 
00119 // -----------------------------------------------------------------------------
00120 // TSIPExState::ProfileEnabled
00121 // Default implementation for virtual function.
00122 // -----------------------------------------------------------------------------
00123 //
00124 void TSIPExState::ProfileEnabled( 
00125     CSIPExEngine* /*aContext*/, 
00126     TUint32 /*aSIPProfileId*/ ) 
00127     { 
00128     }
00129 
00130 // -----------------------------------------------------------------------------
00131 // TSIPExState::SocketState
00132 // Default implementation for virtual function.
00133 // -----------------------------------------------------------------------------
00134 //
00135 void TSIPExState::SocketState( CSIPExEngine* /*aContext*/, TInt /*aNewState*/ ) 
00136     { 
00137     }
00138 
00139 // -----------------------------------------------------------------------------
00140 // TSIPExState::SocketData
00141 // Default implementation for virtual function.
00142 // -----------------------------------------------------------------------------
00143 //
00144 void TSIPExState::SocketData( CSIPExEngine* /*aContext*/, TDesC8& /*aData*/ ) 
00145     { 
00146     }
00147 
00148 // -----------------------------------------------------------------------------
00149 // TSIPExState::EnableProfileL
00150 // Default implementation for virtual function.
00151 // -----------------------------------------------------------------------------
00152 //
00153 void TSIPExState::EnableProfileL( CSIPExEngine* /*aContext*/ ) 
00154     { 
00155     }
00156 
00157 // -----------------------------------------------------------------------------
00158 // TSIPExState::DisableProfileL
00159 // Default implementation for virtual function.
00160 // -----------------------------------------------------------------------------
00161 //
00162 void TSIPExState::DisableProfileL( CSIPExEngine* /*aContext*/ ) 
00163     { 
00164     }
00165 
00166 // -----------------------------------------------------------------------------
00167 // TSIPExState::CursorPressed
00168 // Default implementation for virtual function.
00169 // -----------------------------------------------------------------------------
00170 //
00171 void TSIPExState::CursorPressed( CSIPExEngine* /*aContext*/ ) 
00172     { 
00173     }
00174 
00175 // -----------------------------------------------------------------------------
00176 // TSIPExState::CursorRight
00177 // Default implementation for virtual function.
00178 // -----------------------------------------------------------------------------
00179 //
00180 void TSIPExState::CursorRight( CSIPExEngine* /*aContext*/ ) 
00181     { 
00182     }
00183 
00184 // -----------------------------------------------------------------------------
00185 // TSIPExState::CursorLeft
00186 // Default implementation for virtual function.
00187 // -----------------------------------------------------------------------------
00188 //
00189 void TSIPExState::CursorLeft(  CSIPExEngine* /*aContext*/ ) 
00190     { 
00191     }
00192 
00193 // -----------------------------------------------------------------------------
00194 // TSIPExState::MoveCursorL
00195 // Default implementation for virtual function.
00196 // -----------------------------------------------------------------------------
00197 //
00198 void TSIPExState::MoveCursorL( 
00199     CSIPExEngine* /*aContext*/, 
00200     const TInt /*aNewPosition*/ ) 
00201     { 
00202     }
00203 
00204 // -----------------------------------------------------------------------------
00205 // TSIPExState::SendInstantMsgL
00206 // Default implementation for virtual function.
00207 // -----------------------------------------------------------------------------
00208 //
00209 void TSIPExState::SendInstantMsgL( 
00210     CSIPExEngine* /*aContext*/, 
00211     const TDesC& /*aAddress*/, 
00212     const TDesC& /*aMsg*/ ) 
00213     { 
00214     }
00215 
00216 // -----------------------------------------------------------------------------
00217 // TSIPExState::EndGameL
00218 // Default implementation for virtual function.
00219 // -----------------------------------------------------------------------------
00220 //
00221 void TSIPExState::EndGameL( CSIPExEngine* /*aContext*/ ) 
00222     { 
00223     }
00224 
00225 // -----------------------------------------------------------------------------
00226 // TSIPExState::InviteL
00227 // Default implementation for virtual function.
00228 // -----------------------------------------------------------------------------
00229 //
00230 void TSIPExState::InviteL( CSIPExEngine* /*aContext*/, const TDesC& /*aAddress*/ )
00231     {
00232     }
00233 
00234 // -----------------------------------------------------------------------------
00235 // TSIPExState::DrawCursor
00236 // Returns always EFalse.
00237 // -----------------------------------------------------------------------------
00238 //
00239 TBool TSIPExState::DrawCursor()
00240     {
00241     return EFalse;
00242     }
00243 
00244 // -----------------------------------------------------------------------------
00245 // TSIPExState::DrawBoard
00246 // Returns always ETrue.
00247 // -----------------------------------------------------------------------------
00248 //
00249 TBool TSIPExState::DrawBoard() 
00250     { 
00251     return ETrue; 
00252     }
00253 
00254 // -----------------------------------------------------------------------------
00255 // TSIPExState::CalculatePos
00256 // Calculates the position next free position in the cursor's column.
00257 // -----------------------------------------------------------------------------
00258 //
00259 TInt TSIPExState::CalculatePos( CSIPExEngine* aContext )
00260     {
00261     TInt y( KErrNotFound );
00262 
00263         for ( TInt i = KBoxCountX - 1; i >= 0; i-- ) 
00264         {
00265                 if ( aContext->BoardValue( aContext->Cursor(), i ) == -1 ) 
00266             {
00267                         y = i;
00268                         return y;
00269                     }
00270             }
00271 
00272         return y;
00273     }
00274 
00275 // -----------------------------------------------------------------------------
00276 // TSIPExState::IsWin
00277 // Checks that if the specified move is the win move.
00278 // -----------------------------------------------------------------------------
00279 //
00280 TInt TSIPExState::IsWin( CSIPExEngine* aContext, const TInt aX, const TInt aY )
00281     {
00282         TInt ret = -1;
00283 
00284         TInt player = aContext->BoardValue( aX, aY );
00285         TInt line1 = 1;
00286         TInt line2 = 1;
00287         TInt line3 = 1;
00288         TInt line4 = 1;
00289 
00290         TInt i = 0;
00291         TInt j = 0;
00292 
00293         for (i = aX + 1; i < 8; i ++) 
00294         {
00295                 if ( aContext->BoardValue( i, aY ) == player )
00296                         line1 ++;
00297                 else
00298                         break;
00299             }
00300 
00301         for (i = aX - 1; i >= 0; i --) 
00302         {
00303                 if ( aContext->BoardValue( i, aY ) == player )
00304                         line1 ++;
00305                 else
00306                         break;
00307             }
00308 
00309         if (line1 >= 4)
00310                 return player;
00311 
00312         for (i = aY + 1; i < 8; i ++) 
00313         {
00314                 if ( aContext->BoardValue( aX, i ) == player )
00315                         line2 ++;
00316                 else
00317                         break;
00318             }
00319 
00320         for (i = aY - 1; i >= 0; i --)
00321         {
00322                 if ( aContext->BoardValue( aX, i ) == player )
00323                         line2 ++;
00324                 else
00325                         break;
00326             }
00327 
00328         if (line2 >= 4)
00329                 return player;
00330 
00331 
00332         j = aY;
00333         for (i = aX + 1; i < 8; i ++) 
00334         {
00335                 j = j + 1;
00336                 if (j > 7)
00337                         break;
00338 
00339                 if ( aContext->BoardValue( i, j ) == player )
00340                         line3 ++;
00341                 else
00342                         break;
00343             }
00344 
00345         j = aY;
00346         for (i = aX - 1; i >= 0; i --) 
00347         {
00348                 j = j - 1;
00349                 if (j < 0)
00350                         break;
00351 
00352                 if ( aContext->BoardValue( i, j ) == player )
00353                         line3 ++;
00354                 else
00355                         break;
00356             }
00357 
00358         if (line3 >= 4)
00359                 return player;
00360 
00361         j = aY;
00362         for (i = aX + 1; i < 8; i ++) 
00363         {
00364                 j = j - 1;
00365                 if (j < 0)
00366                         break;
00367 
00368                 if ( aContext->BoardValue( i, j ) == player )
00369                         line4 ++;
00370                 else
00371                         break;
00372             }
00373 
00374         j = aY;
00375         for (i = aX - 1; i >= 0; i --) 
00376         {
00377                 j = j + 1;
00378                 if (j > 7)
00379                         break;
00380 
00381                 if ( aContext->BoardValue( i, j ) == player )
00382                         line4 ++;
00383                 else
00384                         break;
00385             }
00386 
00387         if (line4 >= 4)
00388                 return player;
00389 
00390         return ret;
00391     }
00392 
00393 // -----------------------------------------------------------------------------
00394 // TSIPExState::SessionEnded
00395 // The remote peer has ended the session. 
00396 // The state is changed to Idle and UI is notified.
00397 // -----------------------------------------------------------------------------
00398 //
00399 void TSIPExState::SessionEnded( CSIPExEngine* aContext )
00400     {
00401     ChangeState( aContext, aContext->iStateRegistered );
00402     Info( aContext, KSessionEnded() );
00403     StatusInfo( aContext, KGameOver() );
00404     TRAPD( ignore, aContext->SocketEngineL()->Stop() );
00405     aContext->DestroySocketEngine();
00406     }
00407 
00408 // -----------------------------------------------------------------------------
00409 // TSIPExState::ConnectionLost
00410 // The connection has lost. 
00411 // The state is changed to Idle and UI is notified.
00412 // -----------------------------------------------------------------------------
00413 //
00414 void TSIPExState::ConnectionLost( CSIPExEngine* aContext )
00415     {
00416     ChangeState( aContext, aContext->iStateIdle );
00417     Info( aContext, KConnectionLost() );
00418     StatusInfo( aContext, KGameOver() );
00419     }
00420 
00421 // -----------------------------------------------------------------------------
00422 // TSIPExState::ProfileError
00423 // The error in profile operation
00424 // The state is changed to Idle and UI is notified.
00425 // -----------------------------------------------------------------------------
00426 //
00427 void TSIPExState::ProfileError( CSIPExEngine* aContext, TInt aError )
00428     {
00429     ChangeState( aContext, aContext->iStateIdle );
00430     TRAPD( ignore, CombineAndShowInfoL( aContext, KProfileError(), aError ) );
00431     StatusInfo( aContext, KGameOver() );
00432     }
00433 
00434 // -----------------------------------------------------------------------------
00435 // TSIPExState::IMReceived
00436 // The error in profile operation
00437 // The state is changed to Idle and UI is notified.
00438 // -----------------------------------------------------------------------------
00439 //
00440 void TSIPExState::IMReceived( 
00441     CSIPExEngine* aContext, 
00442     const TDesC8& aFrom, 
00443     const TDesC8& aMessage )
00444     {
00445     TRAPD( ignore, aContext->GameObserver().IMReceivedL( aFrom, aMessage ) );
00446     }
00447 
00448 // -----------------------------------------------------------------------------
00449 // TSIPExState::EngineError
00450 // The error in SIP Engine
00451 // The state is changed to Idle and UI is notified.
00452 // -----------------------------------------------------------------------------
00453 //
00454 void TSIPExState::EngineError( CSIPExEngine* aContext, TInt aError )
00455     {
00456     ChangeState( aContext, aContext->iStateIdle );
00457     TRAPD( ignore, CombineAndShowInfoL( aContext, KErrorInSIPEngine(), aError ) );
00458     StatusInfo( aContext, KGameOver() );
00459     }
00460 
00461 // -----------------------------------------------------------------------------
00462 // TSIPExState::InviteReceivedByRemote
00463 // Callback from SIP engine observer.
00464 // The UI is notified.
00465 // -----------------------------------------------------------------------------
00466 //
00467 void TSIPExState::InviteReceivedByRemote( CSIPExEngine* aContext, 
00468                                           const TInt aResponse )
00469     {
00470     TRAPD( ignore, CombineAndShowInfoL( aContext, KProvResponse(), aResponse ) );
00471     }
00472 
00473 // -----------------------------------------------------------------------------
00474 // TSIPExState::InviteAcceptedByUs
00475 // Callback from SIP engine observer.
00476 // The UI is notified.
00477 // -----------------------------------------------------------------------------
00478 //
00479 void TSIPExState::InviteAcceptedByUs( CSIPExEngine* aContext ) 
00480     { 
00481     Info( aContext, KAckReceived() ); 
00482     }
00483 
00484 // End of file
00485 

© Nokia 2006

Back to top