uiacceltk/hitchcock/Client/src/alfgencomponent.cpp
changeset 0 15bf7259bb7c
equal deleted inserted replaced
-1:000000000000 0:15bf7259bb7c
       
     1 /*
       
     2 * Copyright (c) 2006 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 
       
    20 #include "alf/alfgencomponent.h"
       
    21 #include "alf/alfconstants.h"
       
    22 #include "alf/alfenv.h"
       
    23 #include "alfclient.h"
       
    24 #include "alflogger.h"
       
    25 
       
    26 #include <uiacceltk/HuiUtil.h>
       
    27 
       
    28 class CAlfMessageObserver;
       
    29  
       
    30 NONSHARABLE_CLASS(RAlfSubsessionSimple): public RSubSessionBase
       
    31     {
       
    32 public:
       
    33     TInt Open(CAlfEnv& aEnv, TInt aImplementationId, TInt aImplementationUid, const TDesC8& aConstuctionParams);
       
    34     TInt SendSynch(TUint aOp, const TDesC8& aInputBuf, TDes8& aOutBuf);
       
    35     TInt SendNoReply(TUint aOp, const TDesC8& aInputBuf);
       
    36     void SendAsync(const TDesC8& aInputBuf, TDes8& aOutBuf, CAlfMessageObserver* aObserver); 
       
    37     void Cancel(TInt aCmdId);
       
    38     void Close();
       
    39     TInt Identifier()
       
    40         {
       
    41         return SubSessionHandle();
       
    42         }
       
    43     
       
    44 private:
       
    45     RAlfClient* iClient;
       
    46     };
       
    47 
       
    48 
       
    49 NONSHARABLE_CLASS(CAlfMessageObserver):public CActive
       
    50     {
       
    51     public:
       
    52     static CAlfMessageObserver* NewRequestLC(RAlfSubsessionSimple& aClient, MAlfAsyncOpObserver* aObserver, TInt aCmdId, CAlfGenComponent& aComms)
       
    53         {
       
    54         CAlfMessageObserver* me = new (ELeave) CAlfMessageObserver(aClient, aObserver, aCmdId, aComms);
       
    55         me->SetActive();
       
    56         CleanupStack::PushL(me);
       
    57         return me;
       
    58         }
       
    59     
       
    60     TRequestStatus& Status() 
       
    61         {
       
    62         return iStatus;
       
    63         }
       
    64 
       
    65     ~CAlfMessageObserver()
       
    66         {
       
    67         Cancel();
       
    68         if (iObserver)
       
    69             {
       
    70             iObserver->AlfAsyncOpCompleted(TInt(this), KErrCancel);
       
    71             }
       
    72         }
       
    73 
       
    74     const TDesC8& Des() const
       
    75         {
       
    76         return iAsDescriptor;
       
    77         }
       
    78 
       
    79     private:
       
    80     CAlfMessageObserver(RAlfSubsessionSimple& aClient, MAlfAsyncOpObserver* aObserver, TInt aCmdId, CAlfGenComponent& aComms)
       
    81         :CActive(CActive::EPriorityHigh),iClient(aClient), iObserver(aObserver), iComms( aComms ),
       
    82         iParametersAsInts(0,0),
       
    83         iAsDescriptor( iParametersAsInts )
       
    84         {
       
    85         ASSERT(aClient.Identifier());
       
    86         CActiveScheduler::Add(this);
       
    87         iParametersAsInts.iInt1 = aCmdId;
       
    88         iParametersAsInts.iInt2 = reinterpret_cast<TInt>(this);
       
    89         }
       
    90         
       
    91     // ---------------------------------------------------------------------------
       
    92     // from CActive, called when asych request completed by server
       
    93     // ---------------------------------------------------------------------------
       
    94     //    
       
    95     void RunL()
       
    96         {
       
    97         iObserver->AlfAsyncOpCompleted(TInt(this), iStatus.Int());
       
    98         iObserver = 0;
       
    99         
       
   100         iComms.CommandCompleted( this ); // function call deletes 'this'
       
   101         }
       
   102         
       
   103     // ---------------------------------------------------------------------------
       
   104     // from CActive, called if Cancel called while active
       
   105     // ---------------------------------------------------------------------------
       
   106     //
       
   107     void DoCancel()
       
   108         {
       
   109         iClient.Cancel(TInt(this));        
       
   110         }
       
   111 
       
   112     // ---------------------------------------------------------------------------
       
   113     // from CActive, will be called if RunL leaves (in this case note allowed )
       
   114     // ---------------------------------------------------------------------------  
       
   115     //
       
   116     TInt RunError(TInt /*aError*/)
       
   117         {
       
   118         // client leaved in AlfAsyncOpCompleted, just panic it
       
   119         USER_INVARIANT();
       
   120         return KErrNone;
       
   121         }
       
   122 
       
   123     RAlfSubsessionSimple& iClient;
       
   124     MAlfAsyncOpObserver* iObserver;
       
   125     CAlfGenComponent& iComms;
       
   126     
       
   127     TInt2 iParametersAsInts;
       
   128     TPckgC<TInt2> iAsDescriptor;
       
   129     };
       
   130 
       
   131 
       
   132 // ---------------------------------------------------------------------------
       
   133 // creates new subsession
       
   134 // ---------------------------------------------------------------------------
       
   135 //
       
   136 TInt RAlfSubsessionSimple::Open(CAlfEnv& aEnv, TInt aImplementationId, TInt aImplementationUid, const TDesC8& aConstuctionParams)
       
   137     {
       
   138     iClient = &aEnv.Client();
       
   139     (void)iClient->FlushBatchBuffer();
       
   140     TAlfImplementationInformation info(aImplementationId,aImplementationUid, aEnv.ApiVersion());
       
   141     TPckgC<TAlfImplementationInformation> infoBuf(info);
       
   142     return CreateSubSession(*iClient, EAlfCreateSubSession, TIpcArgs(&infoBuf, &aConstuctionParams));
       
   143     }
       
   144 
       
   145 // ---------------------------------------------------------------------------
       
   146 // Synchronous server request
       
   147 // ---------------------------------------------------------------------------
       
   148 //
       
   149 TInt RAlfSubsessionSimple::SendSynch(TUint aOp, const TDesC8& aInputBuf, TDes8& aOutBuf)
       
   150     {
       
   151     (void)iClient->FlushBatchBuffer();
       
   152     return SendReceive(EAlfDoSubSessionCmd, TIpcArgs(aOp,&aInputBuf,&aOutBuf));
       
   153     }
       
   154 	 
       
   155 TInt RAlfSubsessionSimple::SendNoReply(TUint aOp, const TDesC8& aInputBuf )
       
   156     {
       
   157 	return(iClient->SendNoReply(aOp, aInputBuf, SubSessionHandle()));
       
   158     }
       
   159 
       
   160 // ---------------------------------------------------------------------------
       
   161 // Sets asynchronous request
       
   162 // ---------------------------------------------------------------------------
       
   163 //
       
   164 void RAlfSubsessionSimple::SendAsync(const TDesC8& aInputBuf, TDes8& aOutBuf, CAlfMessageObserver* aObserver)
       
   165     {
       
   166     (void)iClient->FlushBatchBuffer();
       
   167     SendReceive(EAlfDoAsyncSubSessionCmd, TIpcArgs(&aObserver->Des(),&aInputBuf,&aOutBuf), aObserver->Status());
       
   168     }
       
   169 
       
   170 // ---------------------------------------------------------------------------
       
   171 // cancels asynch operation if active
       
   172 // ---------------------------------------------------------------------------
       
   173 //
       
   174 void RAlfSubsessionSimple::Cancel(TInt aCmdId)
       
   175     {
       
   176     if (SubSessionHandle())
       
   177         {
       
   178         SendReceive(EAlfSubSCancelAsynchRequest, TIpcArgs(aCmdId));
       
   179         }
       
   180     }
       
   181 
       
   182 // ---------------------------------------------------------------------------
       
   183 // terminates server side resources
       
   184 // ---------------------------------------------------------------------------
       
   185 //
       
   186 void RAlfSubsessionSimple::Close()
       
   187     {
       
   188     if ( SubSessionHandle() )
       
   189         {
       
   190         (void)iClient->FlushBatchBuffer();
       
   191         }
       
   192     CloseSubSession(EAlfCloseSubSession);    
       
   193     }
       
   194 
       
   195 // private data
       
   196 NONSHARABLE_CLASS(CAlfGenComponent::TPrivateData)
       
   197     {
       
   198     public:
       
   199     
       
   200     RAlfSubsessionSimple iClient;
       
   201     RPointerArray<CAlfMessageObserver> iObservers;
       
   202     };
       
   203 
       
   204 // ---------------------------------------------------------------------------
       
   205 // Public constructor
       
   206 // ---------------------------------------------------------------------------
       
   207 //
       
   208 EXPORT_C CAlfGenComponent* CAlfGenComponent::NewL(
       
   209             CAlfEnv& aEnv,
       
   210             TInt aImplementationId, 
       
   211             TInt aImplementationUid, 
       
   212             const TDesC8& aConstructionParams)
       
   213     {
       
   214     CAlfGenComponent* me = new (ELeave) CAlfGenComponent(aEnv);
       
   215     CleanupStack::PushL(me);
       
   216     me->ConstructL( aImplementationId, aImplementationUid,aConstructionParams);
       
   217     CleanupStack::Pop(me);
       
   218     return me;
       
   219     }
       
   220 
       
   221 // ---------------------------------------------------------------------------
       
   222 // Destructor
       
   223 // ---------------------------------------------------------------------------
       
   224 //    
       
   225 EXPORT_C CAlfGenComponent::~CAlfGenComponent()
       
   226     {
       
   227     if (iData)
       
   228         {
       
   229         iData->iObservers.ResetAndDestroy(); // will cancel outstanding requests 
       
   230         iData->iObservers.Close();
       
   231         iData->iClient.Close();
       
   232         }
       
   233         
       
   234     delete iData;
       
   235     }
       
   236 
       
   237 // ---------------------------------------------------------------------------
       
   238 // performs synchronous request
       
   239 // ---------------------------------------------------------------------------
       
   240 //
       
   241 EXPORT_C TInt CAlfGenComponent::DoSynchronousCmd( TUint aOp, 
       
   242                                const TDesC8& aInputBuf, TDes8& aOutBuf)
       
   243     {
       
   244     ASSERT(iData);
       
   245     return iData->iClient.SendSynch(aOp, aInputBuf, aOutBuf);
       
   246     }
       
   247 
       
   248 // ---------------------------------------------------------------------------
       
   249 // performs synchronous request without waiting the reply from the server
       
   250 // ---------------------------------------------------------------------------
       
   251 //    
       
   252 EXPORT_C  TInt CAlfGenComponent::DoCmdNoReply( TUint aOp, const TDesC8& aInputBuf )
       
   253     {
       
   254     ASSERT(iData);
       
   255     TInt err = iData->iClient.SendNoReply(aOp, aInputBuf);
       
   256     if ( err != KErrNone )
       
   257         {
       
   258         __ALFLOGSTRING1( "CAlfGenComponent::DoCmdNoReply buffer error %d - using DoSynchronousCmd", err )
       
   259         
       
   260         TBuf8<1> dum;
       
   261         err = DoSynchronousCmd( aOp, aInputBuf, dum );
       
   262         }
       
   263     return err;
       
   264     }
       
   265 
       
   266 // ---------------------------------------------------------------------------
       
   267 // Raises async request to server
       
   268 // ---------------------------------------------------------------------------
       
   269 //
       
   270 EXPORT_C TInt CAlfGenComponent::DoAsynchronousCmdL(TUint aOp, const TDesC8& aInputBuf, 
       
   271                                TDes8& aOutBuf, MAlfAsyncOpObserver* aObserver)
       
   272     {
       
   273     ASSERT(iData);
       
   274     // ASSERT(aObserver); // todo: does client need to provide observer if it is not interested about response
       
   275 
       
   276     CAlfMessageObserver* obsEntry = CAlfMessageObserver::NewRequestLC(iData->iClient, aObserver, aOp, *this);
       
   277     iData->iObservers.AppendL(obsEntry);
       
   278     CleanupStack::Pop();
       
   279 
       
   280     iData->iClient.SendAsync(aInputBuf, aOutBuf, obsEntry);
       
   281     
       
   282     return TInt(obsEntry);
       
   283     }
       
   284 
       
   285 // ---------------------------------------------------------------------------
       
   286 // Request to cancel possible async request
       
   287 // ---------------------------------------------------------------------------
       
   288 //
       
   289 EXPORT_C void CAlfGenComponent::CancelAsynchCmd(TInt aCmdId)
       
   290     {
       
   291     if (iData)
       
   292         {
       
   293         const TInt index = iData->iObservers.Find(reinterpret_cast<CAlfMessageObserver*>(aCmdId));
       
   294         if (index != KErrNotFound)
       
   295             {
       
   296             CAlfMessageObserver* item = iData->iObservers[index];
       
   297             iData->iObservers.Remove(index);
       
   298             delete item;
       
   299             }
       
   300         }
       
   301     }
       
   302 
       
   303 EXPORT_C TInt CAlfGenComponent::Identifier()
       
   304     {
       
   305     if (iData)
       
   306         return iData->iClient.Identifier();
       
   307     
       
   308     return 0;
       
   309     }
       
   310 
       
   311 // ---------------------------------------------------------------------------
       
   312 // Default construtor, private
       
   313 // ---------------------------------------------------------------------------
       
   314 //
       
   315 CAlfGenComponent::CAlfGenComponent(CAlfEnv& aEnv)
       
   316     :iEnv(aEnv)
       
   317     {
       
   318     __ASSERT_ALWAYS(aEnv.Client().Handle(), USER_INVARIANT());
       
   319 
       
   320     }
       
   321 
       
   322 // ---------------------------------------------------------------------------
       
   323 // 2nd phase ctr, opens subsession to envs client
       
   324 // ---------------------------------------------------------------------------
       
   325 //    
       
   326 void CAlfGenComponent::ConstructL(
       
   327             TInt aImplementationId, 
       
   328             TInt aImplementationUid, 
       
   329             const TDesC8& aConstructionParams)
       
   330     {
       
   331     iData = new (ELeave) TPrivateData();
       
   332     User::LeaveIfError(iData->iClient.Open(iEnv, aImplementationId,aImplementationUid,aConstructionParams));
       
   333     iData->iObservers.Reset();
       
   334     }
       
   335 
       
   336 // ---------------------------------------------------------------------------
       
   337 // Called when message completes
       
   338 // ---------------------------------------------------------------------------
       
   339 //        
       
   340 void CAlfGenComponent::CommandCompleted( CAlfMessageObserver* aMessageObserver )
       
   341     {
       
   342     ASSERT(iData);
       
   343     
       
   344     const TInt index = iData->iObservers.Find(aMessageObserver);
       
   345     ASSERT(index != KErrNotFound);
       
   346 
       
   347     delete iData->iObservers[index];
       
   348     iData->iObservers.Remove(index);
       
   349     }
       
   350     
       
   351 // end of file