realtimenetprots/sipfw/SampleApp/gameengine/Inc/SIPExGameEngine.h
changeset 0 307788aac0a8
equal deleted inserted replaced
-1:000000000000 0:307788aac0a8
       
     1 
       
     2 /*
       
     3 * Copyright (c) 2004-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 * All rights reserved.
       
     5 * This component and the accompanying materials are made available
       
     6 * under the terms of "Eclipse Public License v1.0"
       
     7 * which accompanies this distribution, and is available
       
     8 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     9 *
       
    10 * Initial Contributors:
       
    11 * Nokia Corporation - initial contribution.
       
    12 *
       
    13 * Contributors:
       
    14 *
       
    15 * Description:
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 
       
    21 #ifndef __CSIPEXENGINE_H__
       
    22 #define __CSIPEXENGINE_H__
       
    23 
       
    24 // INCLUDES
       
    25 #include    "SIPExGameConstants.h"
       
    26 #include    "SIPExSocketEngineObserver.h"
       
    27 #include    "SIPExSIPEngineObserver.h"
       
    28 
       
    29 
       
    30 // Remove imports in unit test build
       
    31 #ifdef CPPUNIT_TEST
       
    32 #undef IMPORT_C
       
    33 #define IMPORT_C
       
    34 #endif
       
    35 
       
    36 // DATA TYPES
       
    37 
       
    38 // FORWARD DECLARATIONS
       
    39 class MSIPExStateViewNotifier;
       
    40 class TSIPExState;
       
    41 class MSIPExGameObserver;
       
    42 class CSIPExSIPEngine;
       
    43 class CSIPExSocketEngine;
       
    44 class TInetAddr;
       
    45 
       
    46 // CLASS DECLARATIONS
       
    47 /**
       
    48 * The engine class for SIP Example application.
       
    49 * the networking and SIP messaging.
       
    50 */
       
    51 class CSIPExEngine 
       
    52 :   public CBase, 
       
    53     public MSIPExSIPEngineObserver,
       
    54     public MSIPExSocketEngineObserver
       
    55     {
       
    56     public:
       
    57 
       
    58 		/**
       
    59 		* Create new instance of GameEngine.
       
    60 		* @param aGameObserver Reference to observing class.
       
    61 		* @returns Pointer to new CSIPExEngine instance.
       
    62 		*/
       
    63         IMPORT_C static CSIPExEngine* NewL( MSIPExGameObserver& aGameObserver );
       
    64 
       
    65 		/**
       
    66 		* Create new instance of GameEngine.
       
    67         * The GameEngine pointer is left to the CleanupStack 
       
    68         * returned.
       
    69 		* @param aGameObserver Reference to observing class.
       
    70 		* @returns Pointer to new CSIPExEngine instance.
       
    71 		*/
       
    72         IMPORT_C static CSIPExEngine* NewLC( MSIPExGameObserver& aGameObserver );
       
    73 
       
    74 		/**
       
    75 		* Destructor.
       
    76 		*/
       
    77         IMPORT_C ~CSIPExEngine();
       
    78 
       
    79     public: // public data
       
    80 
       
    81 		/**
       
    82 		* Enumerations for the application role.
       
    83         * The inviting peer acts as a client and invited peer as a server.
       
    84 		*/        
       
    85         enum TPeer 
       
    86             {
       
    87             EUnknown,
       
    88             EClient,
       
    89             EServer
       
    90             };
       
    91 
       
    92         /**
       
    93         * Game engine states for UI.
       
    94         */
       
    95         enum TEngineState
       
    96             {
       
    97             EIdle,
       
    98             EEnabled,
       
    99             EActivating,
       
   100             EActive
       
   101             };
       
   102 
       
   103     public: // New functions
       
   104 
       
   105         /**
       
   106         * The instant message is send to the remote user.
       
   107 		* Redirects the call to the active State object.
       
   108         * @param aAddress The address of the recipient.
       
   109         * @param aMsg The message.
       
   110 		*/
       
   111         IMPORT_C void SendInstantMsgL( const TDesC& aAddress, const TDesC& aMsg );
       
   112 
       
   113         /**
       
   114 		* Player invites the remote peer to the game.
       
   115         * Redirectes the call to the active State object.
       
   116         * @param aAddress The address of the invited peer.
       
   117 		*/
       
   118         IMPORT_C void InviteL( const TDesC& aAddress );
       
   119 
       
   120         /**
       
   121 		* The SIP profile is enabled.
       
   122         * Redirectes the call to the active State object.
       
   123 		*/
       
   124         IMPORT_C void EnableProfileL();
       
   125 
       
   126         /**
       
   127 		* The SIP profile is disabled.
       
   128         * Redirectes the call to the active State object.
       
   129 		*/
       
   130         IMPORT_C void DisableProfileL();
       
   131 
       
   132         /**
       
   133 		* The user ends the game.
       
   134         * Redirectes the call to the active State object.
       
   135 		*/
       
   136         IMPORT_C void EndGameL();
       
   137 
       
   138         /**
       
   139         * Resolves whether we should draw cursor or not.
       
   140         * @return Returns ETrue if the game is in state where we 
       
   141         * should draw cursor (if it is our turn). Otherwise
       
   142         * EFalse is returned.
       
   143         */
       
   144         IMPORT_C TBool DrawCursor();
       
   145 
       
   146         /**
       
   147         * Resolves whether we should draw board or not.
       
   148         * @return Returns ETrue if the game is in state where we 
       
   149         * should draw board (if profile is enabled). Otherwise
       
   150         * EFalse is returned.
       
   151         */
       
   152         IMPORT_C TBool DrawBoard();
       
   153 
       
   154         /**
       
   155         * Updates the game state. User has pressed left key.
       
   156         */
       
   157         IMPORT_C void CursorLeft();
       
   158 
       
   159         /**
       
   160         * Updates the game state. User has pressed right key.
       
   161         */
       
   162         IMPORT_C void CursorRight();
       
   163 
       
   164         /**
       
   165         * Updates the game state. User has pressed enter key.
       
   166         */
       
   167         IMPORT_C void CursorPressed();
       
   168 
       
   169         /**
       
   170         * Updatas the game state. User moves the cursor with pointer.
       
   171         * @param aNewCursorPosition A new cursor column position.
       
   172         */
       
   173         IMPORT_C void MoveCursorL( const TInt aNewCursorPosition );
       
   174 
       
   175         /**
       
   176         * Returns value in specified place in the board.
       
   177         * @param aX The place's x coordinate.
       
   178         * @param aY The place's y coordinate.
       
   179         * @return The value in specified place.
       
   180         */
       
   181         IMPORT_C TInt BoardValue( TInt aX, TInt aY );
       
   182 
       
   183         /**
       
   184         * Returns the cursor position.
       
   185         * @return The cursor's position
       
   186         */
       
   187         IMPORT_C TInt Cursor();
       
   188 
       
   189         /**
       
   190         * Sets the iNotifier.
       
   191         * @param aNotifier Reference to the view notifier.
       
   192         */
       
   193         IMPORT_C void SetViewNotifier( MSIPExStateViewNotifier& aNotifier );
       
   194 
       
   195     public: // From socket observer
       
   196 
       
   197         /**
       
   198 		* Callback from socket observer.
       
   199         * Called when the state changes in the socket engine.
       
   200         * @param aNewState A new state of the socket engine.
       
   201         */
       
   202         void SocketState( TInt aNewState );
       
   203 
       
   204         /**
       
   205 		* Callback from socket observer.
       
   206         * Called when the data is received from the socket.
       
   207         * @param aData The data received from the socket.
       
   208         */
       
   209         void SocketData( TDesC8& aData );
       
   210         
       
   211     private: // From SIP observer. See SIPExObserver.h
       
   212         void InviteReceived( const TDesC8& aFrom, const TUint32 aIapId );
       
   213         void InviteReceivedByRemote( const TInt aResponse );
       
   214         void InviteDeclinedByRemote( const TInt aResponse );
       
   215         void InviteAcceptedByRemote( const TInetAddr& aIPAddress, const TUint32 aIapId );
       
   216         void InviteAcceptedByUs();
       
   217         void InvitationCancelled();
       
   218         void EngineError( TInt aError );
       
   219         void CancelFailed();
       
   220         void SessionEnded();
       
   221         void ConnectionLost();
       
   222         void ProfileEnabled( TUint32 aSIPProfileId );
       
   223         void ProfileError( TInt aError );
       
   224         void IMReceived( const TDesC8& aFrom,
       
   225 						 const TDesC8& aMessage );
       
   226         void WriteLog( const TDesC8& aLog );
       
   227 
       
   228     private: // New functions
       
   229 
       
   230         /**
       
   231         * Initilizes the game state.
       
   232         */
       
   233         void ResetGame();
       
   234 
       
   235         /**
       
   236         * Destroys the iSocketEngine.
       
   237         */
       
   238         void DestroySocketEngine();
       
   239 
       
   240         /**
       
   241         * Changes the active state.
       
   242         * @param aNewState A reference the to new active state.
       
   243         */
       
   244         void ChangeState( TSIPExState& aNewState );
       
   245 
       
   246         /**
       
   247         * Send the move to the remote peer.
       
   248         * @param aX The x coordinate of the move.
       
   249         * @param aY The y coordinate of the move.
       
   250         */
       
   251         void SendMessage( const TInt aX, const TInt aY );
       
   252 
       
   253         /**
       
   254         * Shows the text in the status info area. The call is
       
   255         * redirected to the view notifier.
       
   256         * @param aTxt The text shown in the status info area.
       
   257         */
       
   258         void StatusInfoL( const TDesC& aTxt );
       
   259 
       
   260         /**
       
   261         * Shows the text in the info area. The call is redirected 
       
   262         * to the view notifier.
       
   263         * @param aTxt The text shown in the info area.
       
   264         */
       
   265         void InfoL( const TDesC& aInfoTxt );
       
   266 
       
   267         /**
       
   268         * Calculates the next free place in cursor's column.
       
   269         * @return The next free position on the board.
       
   270         */
       
   271         TInt CalculatePos();
       
   272 
       
   273         /**
       
   274         * Checks if the move is win move.
       
   275         * @param aX The x coordinate of the move.
       
   276         * @param aY The y coordinate of the move.
       
   277         * @return 1 if you won, 
       
   278         *         2 if the remote player has won,
       
   279         *         -1 if not win move.
       
   280         */
       
   281         TInt IsWin( const TInt aX, const TInt aY );
       
   282 
       
   283         /**
       
   284         * Returns the count of moves in this game.
       
   285         * @return The count of moves.
       
   286         */
       
   287         TInt Moves();
       
   288 
       
   289         /**
       
   290         * Sets the specified value in to the specified place in
       
   291         * the board.
       
   292         * @param aX The x coordinate value.
       
   293         * @param aY The y coordinate value.
       
   294         * @param aValue The value to be set to the (x, y) position.
       
   295         */
       
   296         void SetBoard( const TInt aX, const TInt aY, const TInt aValue );
       
   297 
       
   298         /**
       
   299         * Increases the moves value by specified amount.
       
   300         * @param aAmount The amount of the increased moves.
       
   301         */
       
   302         void IncreaseMovesBy( const TInt aAmount );
       
   303 
       
   304         /**
       
   305         * Set cursor the specified position.
       
   306         * @param aNewValue The new position value for the cursor.
       
   307         */
       
   308         void SetCursor( const TInt aNewValue );
       
   309         
       
   310         /**
       
   311         * Returns the peer value.
       
   312         * @return The iPeer value.
       
   313         */
       
   314         TPeer Peer();
       
   315         
       
   316         /**
       
   317         * Sets the iPeer's value
       
   318         * @param aPeer A new value for the iPeer.
       
   319         */
       
   320         void SetPeer( TPeer aPeer );
       
   321         
       
   322         /**
       
   323         * Sets remote peer's move to the board.
       
   324         * @param aX The x coordinate value.
       
   325         * @param aY The y coordinate value.
       
   326         */
       
   327         void SetRemote( const TInt aX, const TInt aY );
       
   328 
       
   329         /**
       
   330 		* The acceptance is asked from the user
       
   331         * Redirectes the call to the game observer.
       
   332         * @param aFrom The summoner's address
       
   333         * @return Whether we accept the invitation or not.
       
   334 		*/
       
   335         TBool AcceptInvitationL( const TDesC8& aFrom );
       
   336 
       
   337         /**
       
   338         * Returns the SIP Engine pointer.
       
   339         * @return The pointer to the CSIPExSIPEngine. The ownership
       
   340         *         is NOT transferred.
       
   341         */
       
   342         CSIPExSIPEngine*    SIPEngine();
       
   343 
       
   344         /**
       
   345         * Returns the socket engine. If the socket engine (iSocketEngine) is 
       
   346         * NULL it will be created.
       
   347         * @return The pointer to the CSIPExSocketEngine. The ownership
       
   348         *         is NOT transferred.
       
   349         */        
       
   350         CSIPExSocketEngine* SocketEngineL();
       
   351 
       
   352         /**
       
   353         * Returns the game observer reference.
       
   354         * @return The reference to the MSIPExGameObserver.
       
   355         */        
       
   356         MSIPExGameObserver& GameObserver();
       
   357 
       
   358     private:
       
   359 
       
   360         /**
       
   361         * Constructor
       
   362         * @param aGameObserver The observer reference.
       
   363         */        
       
   364         CSIPExEngine( MSIPExGameObserver& aGameObserver );
       
   365 
       
   366         /**
       
   367         * 2nd phase constructor
       
   368         */        
       
   369         void ConstructL();
       
   370 
       
   371     private: // Member variables
       
   372         // Owned: States
       
   373         TSIPExState*    iStateIdle;
       
   374         TSIPExState*    iStateRegistering;
       
   375         TSIPExState*    iStateRegistered;
       
   376         TSIPExState*    iStateInviting;
       
   377         TSIPExState*    iStateConnecting;
       
   378         TSIPExState*    iStateLocal;
       
   379         TSIPExState*    iStateRemote;
       
   380         TSIPExState*    iStateAcceptingSIP;
       
   381         
       
   382         // Not owned: ui notifier
       
   383         MSIPExStateViewNotifier*     iNotifier;
       
   384 
       
   385         // Owned: The networking engine
       
   386         CSIPExSocketEngine*     iSocketEngine;
       
   387 
       
   388         // Owned: The SIP signaling engine.
       
   389         CSIPExSIPEngine*        iSIPEngine;
       
   390         
       
   391         // Observer for game events
       
   392         MSIPExGameObserver&     iGameObserver;
       
   393 
       
   394         // Game data
       
   395         TInt iMoves;
       
   396 	    TInt iBoard[ KBoxCountX ][ KBoxCountY ];
       
   397 	    TInt iCursor;
       
   398         TPeer iPeer;
       
   399 
       
   400         // Not owned: Reference to active state
       
   401         TSIPExState*    iActiveState;
       
   402 
       
   403        // State classes are friend classes because they need access 
       
   404         // to the engine.
       
   405         
       
   406         friend class TSIPExState;
       
   407         friend class TSIPExStateAcceptingSIP;
       
   408         friend class TSIPExStateConnecting;
       
   409         friend class TSIPExStateIdle;
       
   410         friend class TSIPExStateInviting;
       
   411         friend class TSIPExStateLocal;
       
   412         friend class TSIPExStateRegistered;
       
   413         friend class TSIPExStateRegistering;
       
   414         friend class TSIPExStateRemote;
       
   415         
       
   416         // In unit tests following friend class definitions are needed
       
   417         #ifdef CPPUNIT_TEST
       
   418         friend class CGameEngineTest;
       
   419         friend class CBaseStateTest;
       
   420         friend class CStateAcceptingSIPTest;
       
   421         #endif
       
   422     };
       
   423 
       
   424 #endif // __CSIPEXENGINE_H__
       
   425