simpledatamodeladapter/src/presenceplugin.cpp
branchRCL_3
changeset 18 fbd2e7cec7ef
equal deleted inserted replaced
17:2669f8761a99 18:fbd2e7cec7ef
       
     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:  IETF SIMPLE Protocol implementation for XIMP Framework
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <badesca.h>
       
    20 #include <ecom/implementationproxy.h>
       
    21 #include <ximpserviceinfo.h>
       
    22 #include <ximpbase.h>
       
    23 #include <ximpidentity.h>
       
    24 #include <presenceinfo.h>
       
    25 #include <ximpprotocolconnectionhost.h>
       
    26 #include <ximpprotocolconnection.h>
       
    27 #include <ximpserviceinfo.h>
       
    28 #include <ximpcontextclientinfo.h>
       
    29 
       
    30 #include "presenceplugin.h"
       
    31 #include "presenceplugindef.h"
       
    32 #include "presencepluginconnection.h"
       
    33 #include "presenceplugincommon.h"
       
    34 #include "presenceplugin_resource.hrh"
       
    35 #include "presenceconnectioninfo.h"
       
    36  
       
    37 #include <spsettings.h>
       
    38 #include <spentry.h>
       
    39 #include <spproperty.h>
       
    40 #include <spdefinitions.h>
       
    41 
       
    42 // ======== MEMBER FUNCTIONS ========
       
    43 
       
    44 // ---------------------------------------------------------------------------
       
    45 // Key value pair table to identify correct constructor
       
    46 // function for the requested interface.
       
    47 // ---------------------------------------------------------------------------
       
    48 //
       
    49 const TImplementationProxy ImplementationTable[] =
       
    50     {
       
    51     //Warning in Lint 611
       
    52     IMPLEMENTATION_PROXY_ENTRY( PRESENCEPLUGIN_1_IMPLEMENTATION_UID,
       
    53         CPresencePlugin::NewL )
       
    54     };
       
    55 
       
    56 // ---------------------------------------------------------------------------
       
    57 // Exported function to return the implementation proxy table
       
    58 // ---------------------------------------------------------------------------
       
    59 //
       
    60 EXPORT_C const TImplementationProxy* ImplementationGroupProxy( 
       
    61     TInt& aTableCount )
       
    62     {
       
    63     aTableCount = sizeof( ImplementationTable ) /
       
    64         sizeof( TImplementationProxy );
       
    65     return ImplementationTable;
       
    66     }
       
    67     
       
    68 // ---------------------------------------------------------------------------
       
    69 // CPresencePlugin::CPresencePlugin()
       
    70 // ---------------------------------------------------------------------------
       
    71 //
       
    72 CPresencePlugin::CPresencePlugin()
       
    73     {
       
    74     //empty
       
    75     }
       
    76    
       
    77 // ---------------------------------------------------------------------------
       
    78 // CPresencePlugin::NewLC()
       
    79 // ---------------------------------------------------------------------------
       
    80 //
       
    81 CPresencePlugin* CPresencePlugin::NewLC()
       
    82     {
       
    83     CPresencePlugin* self = new( ELeave ) CPresencePlugin();
       
    84     CleanupStack::PushL( self );
       
    85     return self;
       
    86     }
       
    87 
       
    88 // ---------------------------------------------------------------------------
       
    89 // CPresencePlugin::NewL()
       
    90 // ---------------------------------------------------------------------------
       
    91 //
       
    92 CPresencePlugin* CPresencePlugin::NewL()
       
    93     {
       
    94     CPresencePlugin* self = CPresencePlugin::NewLC();
       
    95     CleanupStack::Pop( self );
       
    96     return self;
       
    97     }
       
    98 
       
    99 // ---------------------------------------------------------------------------
       
   100 // CPresencePlugin::~CPresencePlugin()
       
   101 // ---------------------------------------------------------------------------
       
   102 //
       
   103 CPresencePlugin::~CPresencePlugin()
       
   104     {
       
   105     DP_SDA("CPresencePlugin::~CPresencePlugin");
       
   106     iConnections.ResetAndDestroy();
       
   107     iConnectionArray.ResetAndDestroy();
       
   108     }
       
   109     
       
   110 // ---------------------------------------------------------------------------
       
   111 // CPresencePlugin::~CPresencePlugin()
       
   112 // ---------------------------------------------------------------------------
       
   113 //   
       
   114 void CPresencePlugin::PrimeHost( MXIMPProtocolPluginHost& aHost ) 
       
   115     {
       
   116     DP_SDA("CPresencePlugin::PrimeHost");
       
   117     iHost = &aHost;
       
   118     }
       
   119     
       
   120 // ---------------------------------------------------------------------------
       
   121 // CPresencePlugin::AcquireConnectionL()
       
   122 // ---------------------------------------------------------------------------
       
   123 //
       
   124 MXIMPProtocolConnection& CPresencePlugin::AcquireConnectionL(
       
   125     const MXIMPServiceInfo& aServiceInfo,
       
   126     const MXIMPContextClientInfo& aContextClient )
       
   127     {
       
   128     DP_SDA("CPresencePlugin::AcquireConnectionL");
       
   129     MXIMPProtocolConnection* tmp = NULL;
       
   130     CPresenceConnectionInfo* connectionArray = NULL;
       
   131     TBool connected = EFalse;
       
   132     
       
   133     //Check if presence is allready connected to id
       
   134     //!!!! IN this adapter that IapID is service id ID!!!!
       
   135     
       
   136     DP_SDA2("CPresencePlugin::AcquireConnectionL - service id: %d", 
       
   137         aServiceInfo.IapId() );
       
   138     
       
   139     TInt presenceId = KErrNotFound;
       
   140     CSPSettings* spSettings = CSPSettings::NewL();
       
   141     CleanupStack::PushL( spSettings );
       
   142     
       
   143     CSPProperty* property = CSPProperty::NewLC();
       
   144     spSettings->FindPropertyL( aServiceInfo.IapId(),
       
   145                                 ESubPropertyPresenceSettingsId,
       
   146                                 *property );    
       
   147     if ( property )
       
   148         {
       
   149         property->GetValue( presenceId );
       
   150         }
       
   151     CleanupStack::PopAndDestroy( property );        
       
   152     CleanupStack::PopAndDestroy( spSettings );
       
   153 
       
   154     DP_SDA2(" -> presence settings id: %d",presenceId );
       
   155     __ASSERT_ALWAYS( presenceId > 0, User::Leave( KErrArgument ) );
       
   156     
       
   157     TInt connectionsCount = iConnections.Count();
       
   158     DP_SDA2("AcquireConnectionL : connectionsCount =%d ", connectionsCount );
       
   159     
       
   160     for( TInt i( connectionsCount - 1 ); i >= 0; i-- )
       
   161     	{
       
   162 		DP_SDA2("AcquireConnectionL connection count %d", connectionsCount);
       
   163         if( iConnections[i]->GetPresenceSetId() == presenceId )
       
   164             {
       
   165             DP_SDA2("AcquireConnectionL RETURN OLD CONNECTION : i = %d " , i );
       
   166             //Check connection status
       
   167 			tmp = iConnections[i];
       
   168 			connectionArray = iConnections[i]->GetConnectionArray();
       
   169 			
       
   170 			//Check connection status from connectionArray
       
   171 			CPresencePluginConnection::TConnectionStatus status =
       
   172 				connectionArray->GetConnectionStatus();
       
   173 			
       
   174 			if( CPresencePluginConnection::ENotActive == status )
       
   175 			    {
       
   176 			    DP_SDA("AcquireConnectionL ENotActive ");
       
   177                 DP_SDA("AcquireConnectionL Del old inactive connection");
       
   178                 //Call session CLOSE here!!!!!
       
   179                 //Ensure there is no any client to bind this connect
       
   180                 //Release this failed connection
       
   181                 DeleteConnection(i);
       
   182 			    }
       
   183 			else if ( CPresencePluginConnection::ETerminated == status )
       
   184                 {
       
   185                 DP_SDA("AcquireConnectionL ETerminated CREATE NEW CONNECTION ");
       
   186                 //Roamning try use lates ETag
       
   187                 //Delete old terminaded connection and create new with old ETag                             
       
   188                 CPresencePluginConnection* connection =
       
   189                 	CPresencePluginConnection::NewL(
       
   190                         aServiceInfo, aContextClient,
       
   191                         *iConnectionArray[i]->GetConnectionEtag() );
       
   192                 CleanupStack::PushL( connection );
       
   193                 
       
   194                 DeleteConnection( i );
       
   195                 
       
   196                 //Add this new connection to connectionArray
       
   197                 CPresenceConnectionInfo* connectionInfo =
       
   198                 	CPresenceConnectionInfo::NewL( connection );
       
   199                 CleanupStack::PushL( connectionInfo );
       
   200                 iConnectionArray.AppendL( connectionInfo );
       
   201                 CleanupStack::Pop();
       
   202                 connection->SetConnectionArray( connectionInfo );
       
   203                 iConnections.AppendL( connection );
       
   204                 CleanupStack::Pop( connection ); 
       
   205                 tmp = connection;
       
   206                 connected = ETrue;
       
   207                 
       
   208                 iConnectionArray[i]->IncreaseClientCount();
       
   209                 }
       
   210 			else if ( CPresencePluginConnection::EActive == status )
       
   211 			    {
       
   212 			    DP_SDA("AcquireConnectionL EActive");
       
   213                 // NOTE: Incase there is no reply from XIMP before client is
       
   214                 // shutdown , then closeconnection wouldnt be called, reopening 
       
   215                 // client will increase clientCount which is wrong. ClientCount 
       
   216                 // should be checked for validity before increasing? 
       
   217                 // If already connected
       
   218                 iConnectionArray[i]->IncreaseClientCount();
       
   219                 connected = ETrue;
       
   220                 tmp = iConnections[i];
       
   221                 DP_SDA("AcquireConnectionL RETURN OLD CONNECTION done");
       
   222 			    }
       
   223 			break;
       
   224             }
       
   225         }   
       
   226     if( !connected )
       
   227         {
       
   228         DP_SDA("CPresencePlugin::AcquireConnectionL CREATE NEW CONNECTION");
       
   229         // If not already connected 
       
   230         CPresencePluginConnection* connection = CPresencePluginConnection::NewL(
       
   231         	aServiceInfo, aContextClient );
       
   232         CleanupStack::PushL( connection );
       
   233         
       
   234         //Add this new connection to connectionArray
       
   235         CPresenceConnectionInfo* connectionInfo =
       
   236         	CPresenceConnectionInfo::NewL( connection );
       
   237         CleanupStack::PushL( connectionInfo );
       
   238         iConnectionArray.AppendL( connectionInfo );
       
   239         CleanupStack::Pop();
       
   240         connection->SetConnectionArray( connectionInfo );
       
   241         //Add on binded client
       
   242         connectionInfo->IncreaseClientCount();
       
   243         
       
   244         iConnections.AppendL( connection );
       
   245         CleanupStack::Pop( connection ); 
       
   246     	tmp = connection;
       
   247     	DP_SDA("AcquireConnectionL CREATE NEW CONNECTION DONE"); 
       
   248         }
       
   249     DP_SDA("CPresencePlugin::AcquireConnectionL ret");    
       
   250 	return *tmp;				
       
   251     }
       
   252     
       
   253 // ---------------------------------------------------------------------------
       
   254 // CPresencePlugin::ReleaseConnection()
       
   255 // ---------------------------------------------------------------------------
       
   256 //
       
   257 void CPresencePlugin::ReleaseConnection( MXIMPProtocolConnection& aConnection )
       
   258     {
       
   259     DP_SDA("CPresencePlugin::ReleaseConnection");   
       
   260 	TInt connectionsCount = iConnections.Count();
       
   261     
       
   262     DP_SDA2("ReleaseConnection : connectionsCount =%d ", connectionsCount );    
       
   263         
       
   264     for ( TInt i=0; i < connectionsCount; i++ )          
       
   265         {
       
   266         DP_SDA("CPresencePlugin::ReleaseConnection for start");
       
   267         if ( iConnectionArray[i]->GetConnection() == &aConnection )
       
   268             {
       
   269             //Check how many client is using this connection
       
   270             TInt clientCount = iConnectionArray[i]->ClientCount();
       
   271             
       
   272             DP_SDA2("ReleaseConnection :  clientCount = %d ", clientCount );    
       
   273             DP_SDA2("CPresencePlugin::ReleaseConnection :  i = %d", i );    
       
   274                 
       
   275             if ( connectionsCount == 1 && clientCount <= 0 )
       
   276                 {
       
   277                 DP_SDA("CPresencePlugin::ReleaseConnection Lets start to check");
       
   278                 DP_SDA2("CPresencePlugin::ReleaseConnection connect status %d,", iConnections[i]->GetSessionStatus());
       
   279                 //Check this connection status
       
   280                 if( !(CPresencePluginConnection::ENotActive ==
       
   281                 	iConnectionArray[i]->GetConnectionStatus() ))
       
   282                    {
       
   283                    DP_SDA("CPresencePlugin::ReleaseConnection not have clients");
       
   284                    DeleteConnection(i);
       
   285                    break;
       
   286                    }
       
   287                 DP_SDA("connectionsCount == 1 && i == 0 && clientCount == 0 ");    
       
   288                 DP_SDA("ReleaseConnection : Don't delete this connection ");    
       
   289                 break; //don't delete first connection
       
   290                 } 
       
   291             DP_SDA("CPresencePlugin::ReleaseConnection for end");  
       
   292             }
       
   293         }     
       
   294     DP_SDA("CPresencePlugin::ReleaseConnection end");     
       
   295     }
       
   296     
       
   297 // ---------------------------------------------------------------------------
       
   298 // CPresencePlugin::GetInterface()
       
   299 // ---------------------------------------------------------------------------
       
   300 //
       
   301 TAny* CPresencePlugin::GetInterface(
       
   302     TInt32 aInterfaceId,
       
   303     TIfGetOps aOptions )
       
   304     {       
       
   305     if ( aInterfaceId == GetInterfaceId() )
       
   306         {
       
   307         // caller wants this interface 
       
   308         return this;
       
   309         }
       
   310     if ( aOptions == MXIMPBase::EPanicIfUnknown )
       
   311         {
       
   312         User::Panic( KPluginName, KErrExtensionNotSupported );
       
   313         }  
       
   314     return NULL;    
       
   315     }
       
   316 
       
   317 // ---------------------------------------------------------------------------
       
   318 // CPresencePlugin::GetInterface()
       
   319 // ---------------------------------------------------------------------------
       
   320 //
       
   321 const TAny* CPresencePlugin::GetInterface(
       
   322     TInt32 aInterfaceId,
       
   323     TIfGetOps aOptions ) const
       
   324     {
       
   325     DP_SDA("CPresencePlugin::GetInterface");
       
   326     if ( aInterfaceId == GetInterfaceId() )
       
   327         {
       
   328         // caller wants this interface
       
   329         return const_cast<CPresencePlugin*>(this);
       
   330         }
       
   331     if ( aOptions == MXIMPBase::EPanicIfUnknown )
       
   332         {
       
   333         User::Panic( KPluginName, KErrExtensionNotSupported );
       
   334         }       
       
   335     return NULL; 
       
   336     }
       
   337 
       
   338 // ---------------------------------------------------------------------------
       
   339 // CPresencePlugin::GetInterfaceId()
       
   340 // ---------------------------------------------------------------------------
       
   341 //
       
   342 TInt32 CPresencePlugin::GetInterfaceId() const
       
   343     {
       
   344     return MXIMPProtocolPlugin::KInterfaceId;
       
   345     }    
       
   346 
       
   347 // ---------------------------------------------------------------------------
       
   348 // CPresencePlugin::DeleteConnection()
       
   349 // ---------------------------------------------------------------------------
       
   350 //
       
   351 void CPresencePlugin::DeleteConnection( TInt aIndex )
       
   352     {
       
   353     //First delete connectionArray where connection is same at connection
       
   354     DP_SDA("CPresencePlugin::DeleteConnection"); 
       
   355     
       
   356     TInt connectionArrayCount = iConnectionArray.Count();
       
   357     
       
   358     for ( TInt i=0; i < connectionArrayCount; i++ )
       
   359         {
       
   360         DP_SDA2("CPresencePlugin::DeleteConnection FINDING, con status %d", iConnectionArray[i]->GetConnectionStatus() ); 
       
   361         if( (iConnections[aIndex] == iConnectionArray[i]->GetConnection()) &&
       
   362             !(CPresencePluginConnection::EActive ==
       
   363             iConnectionArray[i]->GetConnectionStatus() )    )
       
   364             {
       
   365             DP_SDA("CPresencePlugin::DeleteConnection connection Match"); 
       
   366             delete iConnectionArray[i];
       
   367             iConnectionArray.Remove( i );
       
   368             iConnectionArray.Compress();
       
   369             break; 
       
   370             }
       
   371         }
       
   372     delete iConnections[aIndex];
       
   373     iConnections.Remove( aIndex );
       
   374     iConnections.Compress();
       
   375     DP_SDA("CPresencePlugin::DeleteConnection end"); 
       
   376     }
       
   377 
       
   378 // End of file