examples/ForumNokia/Symbian_OS_End-to-End_Sockets_API_Example/SocketTaskManager/engine/TaskManagerEngine.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 // INCLUDE FILES
00032 #include "TimeOutTimer.h"
00033 #include "Response.h"
00034 #include "Request.h"
00035 #include "TaskManagerEngine.h"
00036 #include "TaskManagerEngineReader.h"
00037 #include "TaskManager.pan"
00038 
00039 #include <msvids.h>                             // KMsvGlobalInBoxIndexEntryId
00040 #include <txtrich.h>                    // CRichText
00041 #include <mtclreg.h>                    // CClientMtmRegistry
00042 #include <mtclbase.h>                   // CBaseMtm
00043 #include <commdb.h>                             // CCommsDatabase
00044 #include <commdbconnpref.h>             // TCommDbConnPref
00045 #include <es_enum.h>                    // TConnectionInfoV2Buf
00046 #include <in_sock.h>                    // TInetAddr
00047 #include <eikenv.h>
00048 #include <s32mem.h>                             // TDesBuf
00049 #include <e32std.h>                             // TLex
00050 #include <securesocket.h>       // CSecureSocket
00051 #include <SocketTaskManager.rsg>
00052 #include <ssl_internal.h>
00053 
00054 // CONSTANTS
00055 
00056 _LIT( KSSL3, "SSL3.0" );
00057 
00058 _LIT( KGPRSStartError, "Error starting GPRS service" );
00059 _LIT( KLookupError, "Error looking up server IP");
00060 _LIT( KConnectionError, "Error in forming the connection" );
00061 _LIT( KSSLConnError, "Error forming SSL link between client and server" );
00062 _LIT( KWriteError, "Error writing data to server" );
00063 _LIT( KErroneousPackage, "Package received was erroneous" );
00064 
00065 _LIT(KIapNotSet, "IAP not selected");
00066 _LIT(KServerNotSet, "Server name not set");
00067 _LIT(KSmsUpdateMessage, "TaskManagerUpdate");
00068 
00069 const TInt CTaskManagerEngine::KTimeOut = 30000000; // 30 seconds time-out
00070 
00071 // ================= MEMBER FUNCTIONS =======================
00072 
00073 // constructor
00074 CTaskManagerEngine::CTaskManagerEngine(MTransactionObserver& aObserver)
00075         : CActive( EPriorityStandard ), iState( ENotConnected ),
00076         iOperation( TRequest::EFetchTasks ), iMarkId( 0 ),
00077         iTransactionObserver( aObserver )
00078         
00079         {
00080         }
00081         
00082 // destructor
00083 CTaskManagerEngine::~CTaskManagerEngine()
00084         {
00085         Cancel(); // Closes the socket and resolver if engine is active
00086 
00087     if( iReader )
00088         {
00089         iReader->Cancel();
00090         }
00091         delete iReader;
00092         
00093         delete iTimer;
00094         delete iSecureSocket;
00095         
00096         iConnection.Close();
00097         iSocketServer.Close();
00098         
00099         delete iMsvEntry;
00100         delete iMsvSession;
00101         iIAPs.Close();
00102         }
00103 
00104 // ----------------------------------------------------
00105 // CTaskManagerEngine::NewL()
00106 // Two-phased constructor.
00107 // ----------------------------------------------------
00108 //      
00109 CTaskManagerEngine* CTaskManagerEngine::NewL(MTransactionObserver& aObserver)
00110         {
00111         CTaskManagerEngine* self = new (ELeave) CTaskManagerEngine(aObserver);
00112         CleanupStack::PushL(self);
00113         self->ConstructL();
00114         CleanupStack::Pop(self);
00115         return self;
00116         }
00117 
00118 // ----------------------------------------------------
00119 // CTaskManagerEngine::ConstructL()
00120 // Symbian OS default constructor can leave.
00121 // ----------------------------------------------------
00122 //      
00123 void CTaskManagerEngine::ConstructL()
00124         {
00125         User::LeaveIfError(iSocketServer.Connect());
00126         User::LeaveIfError(iConnection.Open(iSocketServer));
00127         
00128         // instantiate a timer for connection, socketread etc. timeouts 
00129         iTimer = CTimeOutTimer::NewL( EPriorityHigh, *this );
00130         
00131     iReader = CTaskManagerEngineReader::NewL( *this );
00132     
00133     CActiveScheduler::Add( this );
00134         
00135         iMsvSession = CMsvSession::OpenAsyncL(*this);
00136         
00137         LoadIapsL();
00138         
00139         // no IAPs defined in the device.
00140         if (iIAPs.Count() == 0)
00141                 {
00142                 CEikonEnv::Static()->LeaveWithInfoMsg(R_TASKMANAGER_NO_IAPS_DEFINED);
00143                 }
00144         
00145         }
00146 
00147 // ----------------------------------------------------
00148 // CTaskManagerEngine::SetConnectionSettings()
00149 // Sets all connections settings. 
00150 // ----------------------------------------------------
00151 //              
00152 void CTaskManagerEngine::SetConnectionSettings(const TDesC& aServerName, 
00153                                                                                                 const TInt& aPort,
00154                                                                                                 const TDesC& aUsername, 
00155                                                                                                 const TDesC& aPassword)
00156         {
00157         iUsername.Copy(aUsername);
00158         iPassword.Copy(aPassword);
00159         iServer.Copy(aServerName);
00160         iPort = aPort;
00161         }
00162 
00163 // ----------------------------------------------------
00164 // CTaskManagerEngine::SetIap()
00165 // Sets the IAP to be used in the connections.
00166 // ----------------------------------------------------
00167 //      
00168 void CTaskManagerEngine::SetIap(const TUint32& aId)
00169         {
00170         iIap = aId;
00171         }
00172         
00173 // ----------------------------------------------------
00174 // CTaskManagerEngine::IapSet()
00175 // Returns ETrue if the IAP is set, EFalse if not.
00176 // ----------------------------------------------------
00177 //      
00178 TBool CTaskManagerEngine::IapSet() const
00179         {
00180         if (iIap == 0)
00181                 {
00182                 return EFalse;
00183                 }
00184         else
00185                 {
00186                 return ETrue;
00187                 }
00188         }
00189 
00190 // ----------------------------------------------------
00191 // CTaskManagerEngine::CheckAndReportErrorsL()
00192 // Checks that all necessary connections settings are 
00193 // set and reports from missing data.
00194 // ----------------------------------------------------
00195 //      
00196 TBool CTaskManagerEngine::CheckAndReportErrorsL()
00197         {
00198         if (!IapSet())
00199                 {
00200                 iTransactionObserver.ErrorL(KIapNotSet);
00201                 return ETrue;
00202                 }
00203                 
00204         if (iServer.Length() == 0)
00205                 {
00206                 iTransactionObserver.ErrorL(KServerNotSet);
00207                 return ETrue;
00208                 }
00209 
00210         return EFalse;
00211         }
00212 
00213 // ----------------------------------------------------
00214 // CTaskManagerEngine::FetchTasksL()
00215 // Starts fetching tasks from the server.
00216 // Called from UI.
00217 // ----------------------------------------------------
00218 //              
00219 void CTaskManagerEngine::FetchTasksL()
00220         {
00221         iOperation = TRequest::EFetchTasks;
00222         
00223     iTransactionObserver.ConnectingToServerL( ETrue );
00224         
00225         iRunning = ETrue;
00226         GPRSConnectL();
00227         }
00228         
00229 // ----------------------------------------------------
00230 // CTaskManagerEngine::MarkTaskDoneL()
00231 // Informs the server that a task has been completed.
00232 // Server updates task info in the database.
00233 // ----------------------------------------------------
00234 //      
00235 void CTaskManagerEngine::MarkTaskDoneL(const TInt& aTaskId)
00236         {
00237         iOperation = TRequest::ETaskDone;
00238         iMarkId.Num( aTaskId );
00239         
00240         iRunning = ETrue;
00241         GPRSConnectL();
00242         }
00243 
00244 // ----------------------------------------------------
00245 // CTaskManagerEngine::CancelTransaction()
00246 // Can be used for cancelling a transaction at 
00247 // any time.
00248 // ----------------------------------------------------
00249 //              
00250 void CTaskManagerEngine::CancelTransaction()
00251         {
00252         Cancel();
00253         
00254         iRunning = EFalse;
00255         
00256         TRAPD( error, iTransactionObserver.CancelledL() );
00257         if( error != KErrNone )
00258             {
00259             Panic( error );
00260             }
00261         
00262         }
00263 
00264 // ----------------------------------------------------
00265 // CTaskManagerEngine::CheckRefreshL()
00266 // An SMS update message may have been received while 
00267 // a transaction was running. If so, tasks will 
00268 // be fetched.
00269 // ----------------------------------------------------
00270 //
00271 void CTaskManagerEngine::CheckRefreshL()
00272         {
00273         if (iRunning)
00274             {
00275             return;
00276             }
00277 
00278         if (iDoRefresh)
00279                 {
00280                 iDoRefresh = EFalse;
00281                 FetchTasksL();
00282                 }
00283         }
00284 
00285 // ----------------------------------------------------
00286 // CTaskManagerEngine::SetAutomaticUpdateL()
00287 // Defines whether tasks are downloaded automatically 
00288 // when an update SMS message arrives or not.
00289 // ----------------------------------------------------
00290 //
00291 void CTaskManagerEngine::SetAutomaticUpdateL(const TBool& aOn)
00292         {
00293         iAutomaticUpdate = aOn;
00294         if (iAutomaticUpdate)
00295                 {
00296                 CheckRefreshL();
00297                 }
00298         }
00299 
00300 // ----------------------------------------------------
00301 // CTaskManagerEngine::HandleSessionEventL()
00302 // Indicates an event in the message server has occurred. 
00303 // Used here for listening incoming SMS messages.
00304 // ----------------------------------------------------
00305 //      
00306 void CTaskManagerEngine::HandleSessionEventL(TMsvSessionEvent aEvent, TAny* aArg1, TAny* aArg2, TAny* /*aArg3*/)
00307         {
00308         switch (aEvent)
00309                 {
00310                 case EMsvServerReady:
00311                         if (!iMsvEntry)
00312                                 {
00313                                 iMsvEntry = CMsvEntry::NewL(*iMsvSession, KMsvGlobalInBoxIndexEntryId, TMsvSelectionOrdering());
00314                                 }
00315                         break;
00316                         
00317                 case EMsvEntriesCreated:
00318                         if (*(static_cast<TMsvId*>(aArg2)) == KMsvGlobalInBoxIndexEntryId)
00319                                 {
00320                                 CMsvEntrySelection* entries = static_cast<CMsvEntrySelection*>(aArg1);
00321 
00322                                 iMsvEntry->SetEntryL(entries->At(0));
00323                                 TMsvEntry msvEntry(iMsvEntry->Entry());
00324                                 msvEntry.SetVisible(EFalse);
00325 
00326                                 CClientMtmRegistry* mtmReg = CClientMtmRegistry::NewL(*iMsvSession);
00327                                 CleanupStack::PushL(mtmReg);
00328                                 CBaseMtm* smsMtm = mtmReg->NewMtmL(msvEntry.iMtm);
00329                                 smsMtm->SwitchCurrentEntryL(entries->At(0));
00330                                 smsMtm->LoadMessageL();
00331                                 TBool CorrectSms = EFalse;
00332                                 
00333                                 // received SMS message is a 'Task manager update' SMS.
00334                                 if (smsMtm->Body().Read(0,KSmsUpdateMessage().Length()).Compare(KSmsUpdateMessage)==0)
00335                                         {
00336                                         msvEntry.SetUnread(EFalse);
00337                                         CorrectSms = ETrue;
00338                                         }
00339                                 // not 'Task manager update' SMS, show it to user.
00340                                 else
00341                                         {
00342                                         msvEntry.SetVisible(ETrue);
00343                                         }
00344                                         
00345                                 iMsvEntry->ChangeL(msvEntry);
00346 
00347                                 CleanupStack::PopAndDestroy(smsMtm);
00348 
00349                                 // if received SMS message was a 'Task Manager update' SMS.
00350                                 if (CorrectSms)
00351                                         {
00352                                         // delete the received SMS message for it is not intended for the user to read.
00353                                         iMsvEntry->DeleteL(entries->At(0));
00354                                         
00355                                         // if a transaction is running or program is in the background (paused) 
00356                                         // don't fetch tasks yet.
00357                                         if (iRunning || iAutomaticUpdate)
00358                                                 {
00359                                                 iDoRefresh = ETrue;
00360                                                 }
00361                                         // Transaction is not running, fetch tasks.
00362                                         else
00363                                                 {
00364                                                 FetchTasksL();
00365                                                 }
00366                                         }
00367                                 }
00368                         break;
00369                         
00370                         default:
00371                         break;
00372                 }
00373         }
00374 
00375 // ----------------------------------------------------
00376 // CTaskManagerEngine::LoadIapsL()
00377 // Loads all IAPs of the device.
00378 // ----------------------------------------------------
00379 //      
00380 void CTaskManagerEngine::LoadIapsL()
00381         {
00382         // open commdb
00383         CCommsDatabase* commDb = CCommsDatabase::NewL(EDatabaseTypeIAP);
00384         CleanupStack::PushL(commDb);
00385 
00386         // open IAP table
00387         CCommsDbTableView* commView = commDb->OpenIAPTableViewMatchingBearerSetLC(ECommDbBearerCSD|ECommDbBearerGPRS,ECommDbConnectionDirectionOutgoing);
00388         
00389         // search all IAPs
00390         if (commView->GotoFirstRecord() == KErrNone)
00391                 {
00392                 do
00393                         {
00394                         TIap iap;
00395                         commView->ReadTextL(TPtrC(COMMDB_NAME), iap.iName);
00396                         commView->ReadUintL(TPtrC(COMMDB_ID), iap.iId);
00397                         User::LeaveIfError(iIAPs.Append(iap));
00398                         }
00399                 while (commView->GotoNextRecord() == KErrNone);
00400                 }
00401         CleanupStack::PopAndDestroy(commView);
00402         CleanupStack::PopAndDestroy(commDb);
00403         }
00404 
00405 // ----------------------------------------------------
00406 // CTaskManagerEngine::Iaps()
00407 // Returns all IAPs of the device.
00408 // ----------------------------------------------------
00409 //              
00410 RArray<TIap>& CTaskManagerEngine::Iaps() 
00411         {
00412         return iIAPs;
00413         }
00414 
00415         
00416 // ----------------------------------------------------
00417 // CTaskManagerEngine::ConnectL() (overloaded)
00418 // First phase of initiating a connection between sockets.
00419 // Handles DNS-lookup etc.
00420 // ----------------------------------------------------
00421 void CTaskManagerEngine::ConnectL()
00422         {
00423         if( iState == ENotConnected )
00424         {
00425                 TInetAddr addr;
00426                 
00427                 // determine if the server address is a valid ip address
00428                 if( addr.Input( iServer ) == KErrNone )
00429                 {
00430                         ConnectL( addr.Address() );
00431                 }
00432                 else // if not, we must try DNS-lookup
00433                 {
00434                         // open a dns-resolver session
00435                         User::LeaveIfError( iResolver.Open( iSocketServer, KAfInet, KProtocolInetUdp) );
00436                         
00437                         // DNS-lookup request
00438                         iResolver.GetByName( iServer, iNameEntry, iStatus );
00439                         
00440                         iState = ELookingUp;
00441                         iTimer->After( KTimeOut );
00442                         SetActive();
00443                 }
00444         }
00445 }
00446 
00447 
00448 // ----------------------------------------------------
00449 // CTaskManagerEngine::ConnectL() (overloaded)
00450 // True connection function. Takes a valid IP-address
00451 // as parameter (valid as in xxx.xxx.xxx.xxx form)
00452 // ----------------------------------------------------
00453 void CTaskManagerEngine::ConnectL( TUint32 aAddr )
00454         {
00455         if( iState == ENotConnected )
00456                 {
00457                 // open a new socket
00458 #ifdef __WINS__
00459                 // Emulator can't handle RConnections
00460                 User::LeaveIfError( iSocket.Open( iSocketServer,
00461                                                                                   KAfInet,
00462                                                                                   KSockStream,
00463                                                                                   KProtocolInetTcp ) );
00464 #else
00465                 User::LeaveIfError( iSocket.Open( iSocketServer,
00466                                                                                   KAfInet,
00467                                                                                   KSockStream,
00468                                                                                   KProtocolInetTcp,
00469                                                                                   iConnection ) );
00470 #endif
00471                 // set correct port and address
00472                 iAddress.SetPort( iPort );
00473                 iAddress.SetAddress( aAddr );
00474                 
00475                 // try to connect the socket
00476                 iSocket.Connect( iAddress, iStatus );
00477                 iState = EConnecting;
00478                 
00479                 iTimer->After( KTimeOut );
00480                 
00481                 SetActive();
00482                 }
00483         }
00484         
00485 // ----------------------------------------------------
00486 // CTaskManagerEngine::DoCancel()
00487 // Called when a cancel is done during activity.
00488 // ----------------------------------------------------
00489 //
00490 void CTaskManagerEngine::DoCancel()
00491         {
00492         iTimer->Cancel();
00493         
00494         switch( iState )
00495                 {
00496                 case EStartingGPRS:
00497                     {
00498                     iConnection.Stop();
00499                     break;
00500                     }
00501                 case ELookingUp:
00502                         {
00503                         iResolver.Cancel();
00504                         iResolver.Close();
00505                     iState = ENotConnected;
00506                         break;
00507                         }
00508                 case EConnecting:
00509                         {
00510                 iSocket.CancelConnect();
00511                         iSocket.Close();
00512                         iState = ENotConnected;
00513                         break;
00514                         }
00515                 case ESSLConnecting:
00516                     {
00517             iSecureSocket->CancelHandshake();
00518             
00519             EndConnection();
00520                     break;
00521                     }
00522                 case EWriting:
00523                     {
00524                     iSecureSocket->CancelSend();
00525                     iReader->Cancel();
00526                     
00527             EndConnection();
00528                     break;
00529                     }
00530                 default:
00531                     {
00532                     Panic( ETaskManagerInvalidState );
00533                     break;
00534                     }
00535                 };
00536         }
00537 
00538 // ----------------------------------------------------
00539 // CTaskManagerEngine::RunL()
00540 // Handles the states of the object and ensures everything
00541 // is done in right order.
00542 // ----------------------------------------------------
00543 //
00544 void CTaskManagerEngine::RunL()
00545         {
00546     iTimer->Cancel();
00547         
00548         switch( iState )
00549                 {
00550                 // After GPRS service has been started (or failed to start)
00551                 case EStartingGPRS:
00552                     {
00553                     iState = ENotConnected;
00554                     
00555                     if( iStatus == KErrNone )
00556                         {
00557                         iOpeningConnection = EFalse;
00558                         iTransactionObserver.ConnectingToServerL( (iOperation == TRequest::EFetchTasks ) );
00559                         ConnectL();
00560                         }
00561                     else
00562                         {
00563                         iTransactionObserver.ErrorL( KGPRSStartError );
00564                         }
00565                     break;
00566                     }
00567                 // After DNS resolver has finished
00568                 case ELookingUp:
00569                     {
00570                         iResolver.Close();
00571                         iState = ENotConnected;
00572                         
00573                         if( iStatus == KErrNone ) // DNS-lookup was successful
00574                                 {
00575                                 iNameRecord = iNameEntry();
00576                                 
00577                                 TBuf<15> ipAddr;
00578                                 TInetAddr::Cast( iNameRecord.iAddr ).Output( ipAddr );
00579                                 
00580                                 ConnectL( TInetAddr::Cast( iNameRecord.iAddr ).Address() );
00581                                 }
00582                         else // DNS-lookup failed
00583                                 {
00584                                 iTransactionObserver.ErrorL( KLookupError );
00585                                 }
00586 
00587                     break;
00588                     }
00589         // After socket has finished connecting
00590                 case EConnecting:
00591                     {
00592                         // everything went as planned
00593                         if( iStatus == KErrNone )
00594                                 {
00595                                 iState = ESSLConnecting;
00596                 iSecureSocket = CSecureSocket::NewL( iSocket, KSSL3 );
00597 
00598                 #ifdef __SERIES60_30__
00599                 // The CSecureSocket implementation has changed slightly in S60 3rd Edition,
00600                 // this additional option with the server host name needs to be set (in a 8-bit
00601                 // descriptor).
00602                 TBuf8<KMaxServerNameLength> tempName;
00603                 tempName.Copy(iServer);
00604                 iSecureSocket->SetOpt(KSoSSLDomainName, KSolInetSSL, tempName);
00605                 #endif
00606 
00607                                 iSecureSocket->StartClientHandshake( iStatus );
00608                 iTimer->After( KTimeOut );
00609                                 SetActive();
00610                                 }
00611                         else // we couldn't open a connection
00612                                 {
00613                 iSocket.Close();
00614                                 iTransactionObserver.ErrorL( KConnectionError );
00615                                 iState = ENotConnected;
00616                                 }
00617 
00618                     break;
00619                     }
00620                 // After the SSL-handshake has been completed
00621                 case ESSLConnecting:
00622                     {
00623                     if( iStatus == KErrNone )
00624                         {
00625                         iState = EConnected;
00626                         iReader->SetSecureSocket( iSecureSocket );
00627 
00628                 // These parameters are formatted into a string that is saved to iWriteBuffer
00629                 TRequest::GetMessage( iUsername, iPassword, iOperation, iMarkId, iWriteBuffer );
00630                 
00631                 // We begin reading just before writing just so we won't miss anything
00632                 iReader->Start();
00633                 
00634                 // We send the command data to the server
00635                 iSecureSocket->Send( iWriteBuffer, iStatus );
00636                 iState = EWriting;
00637 
00638                 // Start a timer
00639                 iTimer->After( KTimeOut );
00640                 SetActive();
00641                         }
00642                 else
00643                         {
00644                         // CSecureSocket can be instantiated only after a socket has been connected so
00645                         // it must be deleted after every connection
00646                         iSecureSocket->Close(); // this closes the regular socket as well
00647                         delete iSecureSocket;
00648                         iSecureSocket = NULL;
00649 
00650                         iState = ENotConnected;
00651                         iTransactionObserver.ErrorL( KSSLConnError );
00652                         }
00653                     break;
00654                     }
00655                 case EWriting:
00656                     {
00657                         if( iStatus == KErrNone )
00658                             {
00659                         iTimer->After( KTimeOut ); // So we won't wait forever for a response
00660                         iState = EReading;
00661                             }
00662                     else
00663                                 { // writing failed
00664                                 iState = EConnected;
00665                                 
00666                                 iTransactionObserver.ErrorL( KWriteError );
00667                                 }
00668                     break;
00669                     }
00670                 default:
00671                     {
00672                     User::Leave( ETaskManagerInvalidState );
00673                     }
00674                 };
00675         }
00676 
00677 TInt CTaskManagerEngine::RunError( TInt /*aError*/ )
00678     {
00679     return KErrNone;
00680     }
00681 
00682 // ----------------------------------------------------
00683 // CTaskManagerEngine::GPRSConnectL()
00684 // Will open up a (GPRS) connection. We open it using the
00685 // IAP the user chose at the beginning. (View)
00686 // ----------------------------------------------------
00687 //              
00688 void CTaskManagerEngine::GPRSConnectL()
00689         {
00690 // this functionality is not applicable for the emulator
00691 #ifndef __WINS__ 
00692         TBool connected = EFalse;
00693         
00694         // Lets first check are we already connected.
00695         TUint connectionCount;
00696         User::LeaveIfError(iConnection.EnumerateConnections(connectionCount));
00697         TPckgBuf<TConnectionInfo> connectionInfo; 
00698         for (TUint i = 1; i <= connectionCount; i++)
00699                 {
00700                 User::LeaveIfError(iConnection.GetConnectionInfo(i, connectionInfo));
00701                 if (connectionInfo().iIapId == iIap)
00702                         {
00703                         connected = ETrue;
00704                         break;
00705                         }
00706                 }
00707         
00708         // Not yet connected, start connection
00709         if (!connected)
00710                 {
00711                 //Define preferences for connection
00712                 TCommDbConnPref prefs;
00713                 prefs.SetIapId(iIap);
00714                 prefs.SetDialogPreference(ECommDbDialogPrefDoNotPrompt);
00715 
00716                 //Start Connection
00717                 iTransactionObserver.OpeningConnectionL();
00718                 
00719         //Asnychronic call
00720         iOpeningConnection = ETrue;
00721                 iConnection.Start(prefs, iStatus);
00722 
00723                 iState = EStartingGPRS;
00724                 iTimer->After( KTimeOut );
00725                 SetActive();
00726                 
00727                 return;
00728                 }
00729 #endif // __WINS__
00730 
00731     ConnectL();
00732         }
00733 
00734 // ----------------------------------------------------
00735 // CTaskManagerEngine::PackageReceivedL()
00736 // The socket reader calls this function to pass the received
00737 // data to the engine. The function passes it on to CResponse
00738 // which does the actual parsing.
00739 // ----------------------------------------------------
00740 //
00741 TBool CTaskManagerEngine::PackageReceivedL( const TDesC8& aData )
00742     {
00743     // Timer is stopped
00744     iTimer->Cancel();
00745     
00746     if( !iResponse )
00747         {
00748         iResponse = CResponse::NewL();
00749         }
00750     
00751     iResponse->InputDataL( aData );
00752     
00753     switch( iResponse->GetState() )
00754         {
00755         case CResponse::ENotComplete:
00756             {
00757             iTimer->After( KTimeOut );
00758             return ETrue;
00759             }
00760         case CResponse::EError:
00761             {
00762             iTransactionObserver.ErrorL( KErroneousPackage );
00763             }
00764         case CResponse::EComplete:
00765             {
00766             iTransactionObserver.SuccessL( *iResponse );
00767             CheckRefreshL();
00768             }
00769         }
00770 
00771     EndConnection();
00772     return EFalse;
00773     }
00774 
00775 // ----------------------------------------------------
00776 // CTaskManagerEngine::EndConnection()
00777 // Ends the connection to the server in a controlled manner.
00778 // ----------------------------------------------------
00779 //
00780 void CTaskManagerEngine::EndConnection()
00781     {
00782     delete iResponse;
00783     iResponse = NULL;
00784         
00785         if( iSecureSocket )
00786             {
00787             iSecureSocket->Close(); // also closes the normal socket    
00788             }
00789     else
00790         {
00791         iSocket.Close();
00792         }
00793     
00794     delete iSecureSocket;
00795     iSecureSocket = NULL;
00796 
00797         iRunning = EFalse;
00798         iState = ENotConnected;
00799     }
00800 
00801 // ----------------------------------------------------
00802 // CTaskManagerEngine::TimerExpired()
00803 // Timer calls this function to notify a timeout with
00804 // connection, write or read.
00805 // ----------------------------------------------------
00806 //
00807 void CTaskManagerEngine::TimerExpired()
00808     {
00809     TRAPD( err, iTransactionObserver.FailedL( 1 ) );
00810     
00811     if( err != KErrNone )
00812         {
00813         Panic( err );
00814         }
00815     Cancel();
00816     }
00817 
00818 // End of file

Generated by  doxygen 1.6.2