realtimenetprots/sipfw/SampleApp/gameengine/Src/SIPExState.cpp
changeset 0 307788aac0a8
equal deleted inserted replaced
-1:000000000000 0:307788aac0a8
       
     1 
       
     2 // Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 // All rights reserved.
       
     4 // This component and the accompanying materials are made available
       
     5 // under the terms of "Eclipse Public License v1.0"
       
     6 // which accompanies this distribution, and is available
       
     7 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 //
       
     9 // Initial Contributors:
       
    10 // Nokia Corporation - initial contribution.
       
    11 //
       
    12 // Contributors:
       
    13 //
       
    14 // Description:
       
    15 //
       
    16 
       
    17 
       
    18 
       
    19 #include    "SIPExState.h"
       
    20 #include    "SIPExGameEngine.h"
       
    21 #include    "SIPExSIPEngine.h"
       
    22 #include    "SIPExGameObserver.h"
       
    23 
       
    24 // -----------------------------------------------------------------------------
       
    25 // TSIPExState::ChangeState
       
    26 // Changes the active state through the context.
       
    27 // -----------------------------------------------------------------------------
       
    28 //
       
    29 void TSIPExState::ChangeState( CSIPExEngine* aContext, TSIPExState* aNewState )
       
    30     {
       
    31     aContext->ChangeState( *aNewState );
       
    32     }
       
    33 
       
    34 // -----------------------------------------------------------------------------
       
    35 // TSIPExState::Info
       
    36 // Traps the leaving functions. Ignores if leaves.
       
    37 // -----------------------------------------------------------------------------
       
    38 //
       
    39 void TSIPExState::Info( CSIPExEngine* aContext, const TDesC& aText )
       
    40     {
       
    41     TRAPD( ignore, aContext->InfoL( aText ) );
       
    42     }
       
    43 
       
    44 // -----------------------------------------------------------------------------
       
    45 // TSIPExState::StatusInfo
       
    46 // Traps the leaving functions. Ignores if leaves.
       
    47 // -----------------------------------------------------------------------------
       
    48 //
       
    49 void TSIPExState::StatusInfo( CSIPExEngine* aContext, const TDesC& aText )
       
    50     {
       
    51     TRAPD( ignore, aContext->StatusInfoL( aText ) );
       
    52     }
       
    53 
       
    54 // -----------------------------------------------------------------------------
       
    55 // TSIPExState::CombineAndShowInfoL
       
    56 // Combine desc and number value. E.g. aText="Hello", aNum=2, result="Hello 2".
       
    57 // -----------------------------------------------------------------------------
       
    58 //
       
    59 void TSIPExState::CombineAndShowInfoL( 
       
    60     CSIPExEngine* aContext, 
       
    61     const TDesC& aText, 
       
    62     TInt aNum )
       
    63     {
       
    64     HBufC* buf = HBufC::NewLC( 64 );
       
    65     buf->Des().Append( aText );
       
    66     buf->Des().AppendNum( aNum );
       
    67     Info( aContext, *buf );
       
    68     CleanupStack::PopAndDestroy( buf );
       
    69     }
       
    70 
       
    71 // -----------------------------------------------------------------------------
       
    72 // TSIPExState::InviteReceived
       
    73 // Default implementation for virtual function.
       
    74 // -----------------------------------------------------------------------------
       
    75 //
       
    76 void TSIPExState::InviteReceived( CSIPExEngine* /*aContext*/, 
       
    77                 const TDesC8& /*aFrom*/, const TUint32 /*aIapId*/ ) 
       
    78     { 
       
    79     }
       
    80 
       
    81 // -----------------------------------------------------------------------------
       
    82 // TSIPExState::InviteAcceptedByRemote
       
    83 // Default implementation for virtual function.
       
    84 // -----------------------------------------------------------------------------
       
    85 //
       
    86 void TSIPExState::InviteAcceptedByRemote( 
       
    87     CSIPExEngine* /*aContext*/, 
       
    88     const TInetAddr& /*aIPAddress*/, 
       
    89     const TUint32 /*aIapId*/ ) 
       
    90     { 
       
    91     }
       
    92 
       
    93 // -----------------------------------------------------------------------------
       
    94 // TSIPExState::InviteDeclinedByRemote
       
    95 // Default implementation for virtual function.
       
    96 // -----------------------------------------------------------------------------
       
    97 //
       
    98 void TSIPExState::InviteDeclinedByRemote( 
       
    99     CSIPExEngine* /*aContext*/, 
       
   100     const TInt /*aResponse*/ ) 
       
   101     { 
       
   102     }
       
   103 
       
   104 // -----------------------------------------------------------------------------
       
   105 // TSIPExState::InvitationCancelled
       
   106 // Default implementation for virtual function.
       
   107 // -----------------------------------------------------------------------------
       
   108 //
       
   109 void TSIPExState::InvitationCancelled( CSIPExEngine* /*aContext*/) 
       
   110     { 
       
   111     }
       
   112 
       
   113 // -----------------------------------------------------------------------------
       
   114 // TSIPExState::CancelFailed
       
   115 // Default implementation for virtual function.
       
   116 // -----------------------------------------------------------------------------
       
   117 //
       
   118 void TSIPExState::CancelFailed( CSIPExEngine* /*aContext*/ ) 
       
   119     { 
       
   120     }
       
   121 
       
   122 // -----------------------------------------------------------------------------
       
   123 // TSIPExState::ProfileEnabled
       
   124 // Default implementation for virtual function.
       
   125 // -----------------------------------------------------------------------------
       
   126 //
       
   127 void TSIPExState::ProfileEnabled( 
       
   128     CSIPExEngine* /*aContext*/, 
       
   129     TUint32 /*aSIPProfileId*/ ) 
       
   130     { 
       
   131     }
       
   132 
       
   133 // -----------------------------------------------------------------------------
       
   134 // TSIPExState::SocketState
       
   135 // Default implementation for virtual function.
       
   136 // -----------------------------------------------------------------------------
       
   137 //
       
   138 void TSIPExState::SocketState( CSIPExEngine* /*aContext*/, TInt /*aNewState*/ ) 
       
   139     { 
       
   140     }
       
   141 
       
   142 // -----------------------------------------------------------------------------
       
   143 // TSIPExState::SocketData
       
   144 // Default implementation for virtual function.
       
   145 // -----------------------------------------------------------------------------
       
   146 //
       
   147 void TSIPExState::SocketData( CSIPExEngine* /*aContext*/, TDesC8& /*aData*/ ) 
       
   148     { 
       
   149     }
       
   150 
       
   151 // -----------------------------------------------------------------------------
       
   152 // TSIPExState::EnableProfileL
       
   153 // Default implementation for virtual function.
       
   154 // -----------------------------------------------------------------------------
       
   155 //
       
   156 void TSIPExState::EnableProfileL( CSIPExEngine* /*aContext*/ ) 
       
   157     { 
       
   158     }
       
   159 
       
   160 // -----------------------------------------------------------------------------
       
   161 // TSIPExState::DisableProfileL
       
   162 // Default implementation for virtual function.
       
   163 // -----------------------------------------------------------------------------
       
   164 //
       
   165 void TSIPExState::DisableProfileL( CSIPExEngine* /*aContext*/ ) 
       
   166     { 
       
   167     }
       
   168 
       
   169 // -----------------------------------------------------------------------------
       
   170 // TSIPExState::CursorPressed
       
   171 // Default implementation for virtual function.
       
   172 // -----------------------------------------------------------------------------
       
   173 //
       
   174 void TSIPExState::CursorPressed( CSIPExEngine* /*aContext*/ ) 
       
   175     { 
       
   176     }
       
   177 
       
   178 // -----------------------------------------------------------------------------
       
   179 // TSIPExState::CursorRight
       
   180 // Default implementation for virtual function.
       
   181 // -----------------------------------------------------------------------------
       
   182 //
       
   183 void TSIPExState::CursorRight( CSIPExEngine* /*aContext*/ ) 
       
   184     { 
       
   185     }
       
   186 
       
   187 // -----------------------------------------------------------------------------
       
   188 // TSIPExState::CursorLeft
       
   189 // Default implementation for virtual function.
       
   190 // -----------------------------------------------------------------------------
       
   191 //
       
   192 void TSIPExState::CursorLeft(  CSIPExEngine* /*aContext*/ ) 
       
   193     { 
       
   194     }
       
   195 
       
   196 // -----------------------------------------------------------------------------
       
   197 // TSIPExState::MoveCursorL
       
   198 // Default implementation for virtual function.
       
   199 // -----------------------------------------------------------------------------
       
   200 //
       
   201 void TSIPExState::MoveCursorL( 
       
   202     CSIPExEngine* /*aContext*/, 
       
   203     const TInt /*aNewPosition*/ ) 
       
   204     { 
       
   205     }
       
   206 
       
   207 // -----------------------------------------------------------------------------
       
   208 // TSIPExState::SendInstantMsgL
       
   209 // Default implementation for virtual function.
       
   210 // -----------------------------------------------------------------------------
       
   211 //
       
   212 void TSIPExState::SendInstantMsgL( 
       
   213     CSIPExEngine* /*aContext*/, 
       
   214     const TDesC& /*aAddress*/, 
       
   215     const TDesC& /*aMsg*/ ) 
       
   216     { 
       
   217     }
       
   218 
       
   219 // -----------------------------------------------------------------------------
       
   220 // TSIPExState::EndGameL
       
   221 // Default implementation for virtual function.
       
   222 // -----------------------------------------------------------------------------
       
   223 //
       
   224 void TSIPExState::EndGameL( CSIPExEngine* /*aContext*/ ) 
       
   225     { 
       
   226     }
       
   227 
       
   228 // -----------------------------------------------------------------------------
       
   229 // TSIPExState::InviteL
       
   230 // Default implementation for virtual function.
       
   231 // -----------------------------------------------------------------------------
       
   232 //
       
   233 void TSIPExState::InviteL( CSIPExEngine* /*aContext*/, const TDesC& /*aAddress*/ )
       
   234     {
       
   235     }
       
   236 
       
   237 // -----------------------------------------------------------------------------
       
   238 // TSIPExState::DrawCursor
       
   239 // Returns always EFalse.
       
   240 // -----------------------------------------------------------------------------
       
   241 //
       
   242 TBool TSIPExState::DrawCursor()
       
   243     {
       
   244     return EFalse;
       
   245     }
       
   246 
       
   247 // -----------------------------------------------------------------------------
       
   248 // TSIPExState::DrawBoard
       
   249 // Returns always ETrue.
       
   250 // -----------------------------------------------------------------------------
       
   251 //
       
   252 TBool TSIPExState::DrawBoard() 
       
   253     { 
       
   254     return ETrue; 
       
   255     }
       
   256 
       
   257 // -----------------------------------------------------------------------------
       
   258 // TSIPExState::CalculatePos
       
   259 // Calculates the position next free position in the cursor's column.
       
   260 // -----------------------------------------------------------------------------
       
   261 //
       
   262 TInt TSIPExState::CalculatePos( CSIPExEngine* aContext )
       
   263     {
       
   264     TInt y( KErrNotFound );
       
   265 
       
   266 	for ( TInt i = KBoxCountX - 1; i >= 0; i-- ) 
       
   267         {
       
   268 		if ( aContext->BoardValue( aContext->Cursor(), i ) == -1 ) 
       
   269             {
       
   270 			y = i;
       
   271 			return y;
       
   272 		    }
       
   273 	    }
       
   274 
       
   275 	return y;
       
   276     }
       
   277 
       
   278 // -----------------------------------------------------------------------------
       
   279 // TSIPExState::IsWin
       
   280 // Checks that if the specified move is the win move.
       
   281 // -----------------------------------------------------------------------------
       
   282 //
       
   283 TInt TSIPExState::IsWin( CSIPExEngine* aContext, const TInt aX, const TInt aY )
       
   284     {
       
   285 	TInt ret = -1;
       
   286 
       
   287 	TInt player = aContext->BoardValue( aX, aY );
       
   288 	TInt line1 = 1;
       
   289 	TInt line2 = 1;
       
   290 	TInt line3 = 1;
       
   291 	TInt line4 = 1;
       
   292 
       
   293 	TInt i = 0;
       
   294 	TInt j = 0;
       
   295 
       
   296 	for (i = aX + 1; i < 8; i ++) 
       
   297         {
       
   298 		if ( aContext->BoardValue( i, aY ) == player )
       
   299 			line1 ++;
       
   300 		else
       
   301 			break;
       
   302 	    }
       
   303 
       
   304 	for (i = aX - 1; i >= 0; i --) 
       
   305         {
       
   306 		if ( aContext->BoardValue( i, aY ) == player )
       
   307 			line1 ++;
       
   308 		else
       
   309 			break;
       
   310 	    }
       
   311 
       
   312 	if (line1 >= 4)
       
   313 		return player;
       
   314 
       
   315 	for (i = aY + 1; i < 8; i ++) 
       
   316         {
       
   317 		if ( aContext->BoardValue( aX, i ) == player )
       
   318 			line2 ++;
       
   319 		else
       
   320 			break;
       
   321 	    }
       
   322 
       
   323 	for (i = aY - 1; i >= 0; i --)
       
   324         {
       
   325 		if ( aContext->BoardValue( aX, i ) == player )
       
   326 			line2 ++;
       
   327 		else
       
   328 			break;
       
   329 	    }
       
   330 
       
   331 	if (line2 >= 4)
       
   332 		return player;
       
   333 
       
   334 
       
   335 	j = aY;
       
   336 	for (i = aX + 1; i < 8; i ++) 
       
   337         {
       
   338 		j = j + 1;
       
   339 		if (j > 7)
       
   340 			break;
       
   341 
       
   342 		if ( aContext->BoardValue( i, j ) == player )
       
   343 			line3 ++;
       
   344 		else
       
   345 			break;
       
   346 	    }
       
   347 
       
   348 	j = aY;
       
   349 	for (i = aX - 1; i >= 0; i --) 
       
   350         {
       
   351 		j = j - 1;
       
   352 		if (j < 0)
       
   353 			break;
       
   354 
       
   355 		if ( aContext->BoardValue( i, j ) == player )
       
   356 			line3 ++;
       
   357 		else
       
   358 			break;
       
   359 	    }
       
   360 
       
   361 	if (line3 >= 4)
       
   362 		return player;
       
   363 
       
   364 	j = aY;
       
   365 	for (i = aX + 1; i < 8; i ++) 
       
   366         {
       
   367 		j = j - 1;
       
   368 		if (j < 0)
       
   369 			break;
       
   370 
       
   371 		if ( aContext->BoardValue( i, j ) == player )
       
   372 			line4 ++;
       
   373 		else
       
   374 			break;
       
   375 	    }
       
   376 
       
   377 	j = aY;
       
   378 	for (i = aX - 1; i >= 0; i --) 
       
   379         {
       
   380 		j = j + 1;
       
   381 		if (j > 7)
       
   382 			break;
       
   383 
       
   384 		if ( aContext->BoardValue( i, j ) == player )
       
   385 			line4 ++;
       
   386 		else
       
   387 			break;
       
   388 	    }
       
   389 
       
   390 	if (line4 >= 4)
       
   391 		return player;
       
   392 
       
   393 	return ret;
       
   394     }
       
   395 
       
   396 // -----------------------------------------------------------------------------
       
   397 // TSIPExState::SessionEnded
       
   398 // The remote peer has ended the session. 
       
   399 // The state is changed to Idle and UI is notified.
       
   400 // -----------------------------------------------------------------------------
       
   401 //
       
   402 void TSIPExState::SessionEnded( CSIPExEngine* aContext )
       
   403     {
       
   404     ChangeState( aContext, aContext->iStateRegistered );
       
   405     Info( aContext, KSessionEnded() );
       
   406     StatusInfo( aContext, KGameOver() );
       
   407     TRAPD( ignore, aContext->SocketEngineL()->Stop() );
       
   408     aContext->DestroySocketEngine();
       
   409     }
       
   410 
       
   411 // -----------------------------------------------------------------------------
       
   412 // TSIPExState::ConnectionLost
       
   413 // The connection has lost. 
       
   414 // The state is changed to Idle and UI is notified.
       
   415 // -----------------------------------------------------------------------------
       
   416 //
       
   417 void TSIPExState::ConnectionLost( CSIPExEngine* aContext )
       
   418     {
       
   419     ChangeState( aContext, aContext->iStateIdle );
       
   420     Info( aContext, KConnectionLost() );
       
   421     StatusInfo( aContext, KGameOver() );
       
   422     }
       
   423 
       
   424 // -----------------------------------------------------------------------------
       
   425 // TSIPExState::ProfileError
       
   426 // The error in profile operation
       
   427 // The state is changed to Idle and UI is notified.
       
   428 // -----------------------------------------------------------------------------
       
   429 //
       
   430 void TSIPExState::ProfileError( CSIPExEngine* aContext, TInt aError )
       
   431     {
       
   432     ChangeState( aContext, aContext->iStateIdle );
       
   433     TRAPD( ignore, CombineAndShowInfoL( aContext, KProfileError(), aError ) );
       
   434     StatusInfo( aContext, KGameOver() );
       
   435     }
       
   436 
       
   437 // -----------------------------------------------------------------------------
       
   438 // TSIPExState::IMReceived
       
   439 // The error in profile operation
       
   440 // The state is changed to Idle and UI is notified.
       
   441 // -----------------------------------------------------------------------------
       
   442 //
       
   443 void TSIPExState::IMReceived( 
       
   444     CSIPExEngine* aContext, 
       
   445     const TDesC8& aFrom, 
       
   446     const TDesC8& aMessage )
       
   447     {
       
   448     TRAPD( ignore, aContext->GameObserver().IMReceivedL( aFrom, aMessage ) );
       
   449     }
       
   450 
       
   451 // -----------------------------------------------------------------------------
       
   452 // TSIPExState::EngineError
       
   453 // The error in SIP Engine
       
   454 // The state is changed to Idle and UI is notified.
       
   455 // -----------------------------------------------------------------------------
       
   456 //
       
   457 void TSIPExState::EngineError( CSIPExEngine* aContext, TInt aError )
       
   458     {
       
   459     ChangeState( aContext, aContext->iStateIdle );
       
   460     TRAPD( ignore, CombineAndShowInfoL( aContext, KErrorInSIPEngine(), aError ) );
       
   461     StatusInfo( aContext, KGameOver() );
       
   462     }
       
   463 
       
   464 // -----------------------------------------------------------------------------
       
   465 // TSIPExState::InviteReceivedByRemote
       
   466 // Callback from SIP engine observer.
       
   467 // The UI is notified.
       
   468 // -----------------------------------------------------------------------------
       
   469 //
       
   470 void TSIPExState::InviteReceivedByRemote( CSIPExEngine* aContext, 
       
   471                                           const TInt aResponse )
       
   472     {
       
   473     TRAPD( ignore, CombineAndShowInfoL( aContext, KProvResponse(), aResponse ) );
       
   474     }
       
   475 
       
   476 // -----------------------------------------------------------------------------
       
   477 // TSIPExState::InviteAcceptedByUs
       
   478 // Callback from SIP engine observer.
       
   479 // The UI is notified.
       
   480 // -----------------------------------------------------------------------------
       
   481 //
       
   482 void TSIPExState::InviteAcceptedByUs( CSIPExEngine* aContext ) 
       
   483     { 
       
   484     Info( aContext, KAckReceived() ); 
       
   485     }
       
   486 
       
   487 // End of file