messagingfw/alwaysonline/AlwaysOnlineManager/src/AlwaysOnlineManagerClient.cpp
changeset 22 bde600d88860
parent 0 8e480a14352b
equal deleted inserted replaced
21:08008ce8a6df 22:bde600d88860
       
     1 /*
       
     2 * Copyright (c) 2002 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 *     Main class
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 #include "AlwaysOnlineManagerClient.h"
       
    21 #include "AlwaysOnlineManagerLogging.h"
       
    22 #include "AlwaysOnlineManagerStartupSignaller.h"
       
    23 #include "AlwaysOnlineManagerServer.h"
       
    24 #include "AOActiveScheduler.h"
       
    25 
       
    26 // ---------------------------------------------------------------------------
       
    27 // RAlwaysOnlineClientSession::RunServerL()
       
    28 // Called from AlwaysOnlineStarterApp
       
    29 // ---------------------------------------------------------------------------
       
    30 //
       
    31 EXPORT_C void RunServerL()
       
    32     {
       
    33     KAOMANAGER_LOGGER_FN1("RunServerL()");
       
    34 
       
    35     //first thing to do, rename the thread. If construct fails, 
       
    36     //it is easier to debug when it's been named.
       
    37     User::LeaveIfError( User::RenameThread( KAlwaysOnlineManagerServerName ) );
       
    38     
       
    39     
       
    40     // Create and install the active scheduler we need
       
    41 	CAOActiveScheduler* AOScheduler = new (ELeave) CAOActiveScheduler( );
       
    42     CleanupStack::PushL( AOScheduler );
       
    43 	CActiveScheduler::Install( AOScheduler );
       
    44     
       
    45     CAlwaysOnlineManagerServer* AOserver = CAlwaysOnlineManagerServer::NewL();
       
    46     CleanupStack::PushL( AOserver );
       
    47     
       
    48     // set AOserver object to AOActiveScheduler
       
    49     AOScheduler->SetServerRef( AOserver );
       
    50     
       
    51     // Rendezvous with starter
       
    52     RProcess::Rendezvous( KErrNone );
       
    53     
       
    54     //
       
    55     // Ready to run
       
    56     CActiveScheduler::Start();
       
    57     //
       
    58     // Cleanup the server and scheduler
       
    59     CleanupStack::PopAndDestroy( 2, AOScheduler );	// AOserver
       
    60     KAOMANAGER_LOGGER_FN2("RunServerL()");
       
    61     }
       
    62 
       
    63 
       
    64 // ---------------------------------------------------------------------------
       
    65 // StartThread
       
    66 // ---------------------------------------------------------------------------
       
    67 //
       
    68 TInt StartThread()
       
    69     {
       
    70     KAOMANAGER_LOGGER_FN1("StartThread");
       
    71 
       
    72 	TInt ret = KErrNone;
       
    73 	TRequestStatus startedRequestStatus;
       
    74 	TOAManagerStartupSignaller start(startedRequestStatus);
       
    75 
       
    76     // check server not already started
       
    77     TFindServer findAlwaysOnlineServer( KAlwaysOnlineManagerServerName );
       
    78     TFullName name;
       
    79     if (findAlwaysOnlineServer.Next(name)==KErrNone)
       
    80         { // found server already - bad news
       
    81         KAOMANAGER_LOGGER_FN2("StartThread1");
       
    82         return KErrAlreadyExists;
       
    83         }
       
    84     const TUid KAlwaysOnlineManagerServerUid = {0x101F85EB};
       
    85     const TUidType serverUid( KNullUid,KNullUid,KAlwaysOnlineManagerServerUid );
       
    86 
       
    87         // We are on target HW.
       
    88         RProcess server;
       
    89         ret =server.Create(
       
    90             KAlwaysOnlineManagerServerName, // Full path to BT server
       
    91             start.AsCommand(), // Descriptor of parameters
       
    92             serverUid);                // Triplet UID of executable
       
    93 
       
    94 	// Did we manage to create the thread/process?
       
    95 	if	(ret != KErrNone)
       
    96         {
       
    97         KAOMANAGER_LOGGER_FN2("StartThread4");
       
    98 		return ret;
       
    99         }
       
   100 
       
   101 	// Wait to see if the thread/process died during construction
       
   102 	TRequestStatus serverDiedRequestStatus;
       
   103 	server.Logon(serverDiedRequestStatus);
       
   104 	
       
   105 	if	(serverDiedRequestStatus != KRequestPending)
       
   106 		{
       
   107 		//
       
   108 		// Logon failed - server is not yet running, so cannot 
       
   109 		// have terminated
       
   110 		//
       
   111 
       
   112 		// Eat signal
       
   113 		User::WaitForRequest(serverDiedRequestStatus);	// CSI: 94 # This is server side context
       
   114 		
       
   115 		// Abort startup
       
   116 		server.Kill(KErrNone);
       
   117 		server.Close();
       
   118 
       
   119         KAOMANAGER_LOGGER_FN2("StartThread5");
       
   120 		return serverDiedRequestStatus.Int();
       
   121 		}
       
   122 
       
   123 	// Logon OK - start the server
       
   124 	server.Resume();
       
   125 
       
   126 	// Wait for either the server to start or for it to die
       
   127 	User::WaitForRequest(startedRequestStatus, serverDiedRequestStatus);	// CSI: 94 # This is server side context
       
   128 	if	(startedRequestStatus == KRequestPending)
       
   129 		{
       
   130 		// Server has died - never made it to the startup signal
       
   131 		server.Close();
       
   132         KAOMANAGER_LOGGER_FN2("StartThread6");
       
   133 		return serverDiedRequestStatus.Int();
       
   134 		}
       
   135 
       
   136 	//
       
   137 	// Server started (at last). Cancel and consume the death-notification
       
   138 	// before reporting success
       
   139 	//
       
   140 	server.LogonCancel(serverDiedRequestStatus);
       
   141 	server.Close();
       
   142 
       
   143 	// Eat the signal (from the cancel)
       
   144 	User::WaitForRequest(serverDiedRequestStatus);	// CSI: 94 # This is server side context
       
   145     KAOMANAGER_LOGGER_FN2("StartThread7");
       
   146 	return KErrNone;
       
   147 	}
       
   148 
       
   149 // ---------------------------------------------------------------------------
       
   150 // RMailboxPollingSession
       
   151 // ---------------------------------------------------------------------------
       
   152 //
       
   153 EXPORT_C RAlwaysOnlineClientSession::RAlwaysOnlineClientSession()
       
   154     {
       
   155     }
       
   156 
       
   157 // ---------------------------------------------------------------------------
       
   158 // ---------------------------------------------------------------------------
       
   159 //
       
   160 EXPORT_C RAlwaysOnlineClientSession::~RAlwaysOnlineClientSession()
       
   161     {
       
   162     }
       
   163 
       
   164 // ---------------------------------------------------------------------------
       
   165 // Connect
       
   166 // ---------------------------------------------------------------------------
       
   167 //
       
   168 EXPORT_C TInt RAlwaysOnlineClientSession::Connect()
       
   169     {
       
   170     KAOMANAGER_LOGGER_FN1("RAlwaysOnlineClientSession::Connect");
       
   171 
       
   172     if ( Handle() )
       
   173         {
       
   174         KAOMANAGER_LOGGER_FN2("RAlwaysOnlineClientSession::Connect1");
       
   175         return KErrAlreadyExists;
       
   176         }
       
   177 
       
   178     FOREVER
       
   179         {
       
   180         // Try to make session with server
       
   181         TInt ret = CreateSession( KAlwaysOnlineManagerServerName,
       
   182         						Version(), 
       
   183         						KDefaultMessageSlots );
       
   184         
       
   185         if ( ret != KErrNotFound && ret != KErrServerTerminated )
       
   186             {
       
   187             KAOMANAGER_LOGGER_FN2("RAlwaysOnlineClientSession::Connect2");
       
   188             return ret;
       
   189             }
       
   190         
       
   191         // Try to start the server
       
   192         ret=StartThread();
       
   193         
       
   194         if ( ret != KErrNone && ret != KErrAlreadyExists )
       
   195             {
       
   196             // Error can't be handled.
       
   197             KAOMANAGER_LOGGER_FN2("RAlwaysOnlineClientSession::Connect3");
       
   198             return ret;
       
   199             }
       
   200         }
       
   201     }
       
   202 
       
   203 // ---------------------------------------------------------------------------
       
   204 // RAlwaysOnlineClientSession::Version()
       
   205 // ---------------------------------------------------------------------------
       
   206 //
       
   207 EXPORT_C TVersion RAlwaysOnlineClientSession::Version() const
       
   208     {
       
   209     return(TVersion(KAlwaysOnlineMajorVersionNumber,
       
   210         KAlwaysOnlineMinorVersionNumber,
       
   211         KAlwaysOnlineVersionNumber));
       
   212     }
       
   213 
       
   214 // ---------------------------------------------------------------------------
       
   215 // RAlwaysOnlineClientSession::RelayCommandL
       
   216 // ---------------------------------------------------------------------------
       
   217 //
       
   218 EXPORT_C void RAlwaysOnlineClientSession::RelayCommandL( 
       
   219     TAlwaysOnlineServerAPICommands aCommand, 
       
   220     TDes8& aParameter )
       
   221     {
       
   222     User::LeaveIfError( SendReceive( aCommand, TIpcArgs( &aParameter ) ) );
       
   223     }
       
   224 
       
   225 // ---------------------------------------------------------------------------
       
   226 //  RAlwaysOnlineClientSession::AoApiSendMessageL()
       
   227 // ---------------------------------------------------------------------------
       
   228 //
       
   229 EXPORT_C void RAlwaysOnlineClientSession::SendSinglePacketL(        
       
   230     const TAlwaysOnlineServerAPICommands aCommand, 
       
   231     TDes8& aParameter ) 
       
   232     {
       
   233     // Connect client and close the connection after the message is sent
       
   234     User::LeaveIfError( Connect() );
       
   235 
       
   236     // Send message to server and close it
       
   237     RelayCommandL( aCommand, aParameter );
       
   238     Close();
       
   239     }
       
   240 
       
   241 // ---------------------------------------------------------------------------
       
   242 // ---------------------------------------------------------------------------
       
   243 //
       
   244 EXPORT_C void RAlwaysOnlineClientSession::SendCommandAsync( 
       
   245     const TAlwaysOnlineServerAPICommands aCommand,
       
   246     TDes8& aParameter,
       
   247     TRequestStatus& aStatus )
       
   248     {
       
   249 
       
   250     aStatus = KRequestPending;
       
   251     SendReceive( aCommand, TIpcArgs( &aParameter ), aStatus );
       
   252     }
       
   253 
       
   254 // ---------------------------------------------------------------------------
       
   255 // ---------------------------------------------------------------------------
       
   256 //
       
   257 EXPORT_C TInt RAlwaysOnlineClientSession::SendReceiveSync(
       
   258     const TAlwaysOnlineServerAPICommands aCommand,
       
   259     TDes8& aParameter,
       
   260     TDes8& aResultParameter )
       
   261     {
       
   262     TRequestStatus status = KRequestPending;
       
   263     SendReceive( aCommand, 
       
   264             TIpcArgs( &aParameter, &aResultParameter ), status );
       
   265     
       
   266     User::WaitForRequest( status );
       
   267     
       
   268     return status.Int();
       
   269     }
       
   270 
       
   271 //#endif