datacommsserver/esockserver/ssock/ss_apiext_register.cpp
changeset 0 dfb7c4ff071f
equal deleted inserted replaced
-1:000000000000 0:dfb7c4ff071f
       
     1 // Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 // All rights reserved.
       
     3 // This component and the accompanying materials are made available
       
     4 // under the terms of "Eclipse Public License v1.0"
       
     5 // which accompanies this distribution, and is available
       
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 //
       
     8 // Initial Contributors:
       
     9 // Nokia Corporation - initial contribution.
       
    10 //
       
    11 // Contributors:
       
    12 //
       
    13 // Description:
       
    14 //
       
    15 
       
    16 #include "ss_apiext_register.h"
       
    17 #include <ecom/ecom.h>
       
    18 #include <comms-infras/ss_thread.h>
       
    19 #include <ss_glob.h>
       
    20 #include <comms-infras/ss_api_ext.h>
       
    21 
       
    22 
       
    23 #ifdef _DEBUG
       
    24 // Panic category for "absolutely impossible!" vanilla ASSERT()-type panics from this module
       
    25 // (if it could happen through user error then you should give it an explicit, documented, category + code)
       
    26 _LIT(KSpecAssert_ESockSSockspxtrg, "ESockSSockspxtrg");
       
    27 #endif
       
    28 
       
    29 using namespace ESock;
       
    30 using namespace Messages;
       
    31 
       
    32 TCommsApiExtTable::TCommsApiExtTable(TInt aExtensionId, const Meta::STypeId& aMsgImplTid)
       
    33 :	iExtensionId(aExtensionId),
       
    34 	iMsgImplTid(aMsgImplTid),
       
    35 	iMsgPluginInfo(NULL)
       
    36 	{
       
    37 	}
       
    38 
       
    39 //static
       
    40 void TCommsApiExtTable::DeregisterInterfaceOnCleanup(TAny* aTid)
       
    41 	{
       
    42 	Meta::STypeId* tid = static_cast<Meta::STypeId*>(aTid);
       
    43 	TlsGlobals::Get().DeregisterInterface(tid->iUid);
       
    44 	}
       
    45 
       
    46 void TCommsApiExtTable::AddClientL(TSubSessionUniqueId aClientId, CGlobals& aGlobals)
       
    47 	{
       
    48 	__ASSERT_DEBUG(iMsgImplTid.iUid.iUid!=0, User::Panic(KSpecAssert_ESockSSockspxtrg, 1));
       
    49 	User::LeaveIfError(iClients.FindInOrder(aClientId)!=KErrNotFound? KErrAlreadyExists : KErrNone);
       
    50 
       
    51 	//For the first client we need to register messages
       
    52 	TBool isFirst = iClients.Count()==0;
       
    53 	if (isFirst)
       
    54 		{
       
    55 		__ASSERT_DEBUG(iMsgPluginInfo==NULL, User::Panic(KSpecAssert_ESockSSockspxtrg, 2));
       
    56 		iMsgPluginInfo = CExtItfMsgPluginInfo::NewInstanceL(iMsgImplTid);
       
    57 		CleanupStack::PushL(iMsgPluginInfo);
       
    58 
       
    59 		aGlobals.RegisterInterfaceL(iMsgImplTid.iUid, iMsgPluginInfo->ImplementationProxyTableSize(), iMsgPluginInfo->ImplementationProxy());
       
    60 		CleanupStack::PushL(TCleanupItem(&TCommsApiExtTable::DeregisterInterfaceOnCleanup, (TAny*)&iMsgImplTid));
       
    61 		}
       
    62 
       
    63 	iClients.InsertInOrderL(aClientId);
       
    64 
       
    65 	if (isFirst)
       
    66 		{
       
    67 		CleanupStack::Pop(); //DeregisterInterfaceOnCleanup
       
    68 		CleanupStack::Pop(iMsgPluginInfo);
       
    69 		}
       
    70 	}
       
    71 
       
    72 //If aClientId==0 we will remove all interfaces for all clients!
       
    73 TBool TCommsApiExtTable::RemoveClient(TSubSessionUniqueId aClientId, CGlobals& aGlobals)
       
    74 	{
       
    75 	TInt index = KErrNotFound;
       
    76 	if (aClientId!=0)
       
    77 		{
       
    78 		index = iClients.FindInOrder(aClientId);
       
    79 		if(index!=KErrNotFound)
       
    80 			{
       
    81 			iClients.Remove(index);
       
    82 			}
       
    83 		}
       
    84 	else
       
    85 		{
       
    86 		iClients.Reset();
       
    87 		}
       
    88 
       
    89 	//If no clients, deregister messages
       
    90 	if (iClients.Count()==0)
       
    91 		{
       
    92 		iClients.Close();
       
    93 		aGlobals.DeregisterInterface(iMsgImplTid.iUid);
       
    94 		delete iMsgPluginInfo;
       
    95 		iMsgPluginInfo = NULL;
       
    96 		index = 0;
       
    97 		}
       
    98 
       
    99 	if (index != KErrNotFound && index < iClients.Count())
       
   100 		{
       
   101 		return iClients[index];
       
   102 		}
       
   103 
       
   104 	return EFalse;
       
   105 	}
       
   106 
       
   107 
       
   108 TCommsApiExtRegister::TCommsApiExtRegister(CGlobals& aGlobals)
       
   109 :	iGlobals(aGlobals)
       
   110 	{
       
   111 	}
       
   112 
       
   113 TCommsApiExtRegister::~TCommsApiExtRegister()
       
   114 	{
       
   115 	//We should not have anything to cleanup here, if we do, some interface hasn't been closed properly
       
   116 	__ASSERT_DEBUG(iApiExtList.Count()==0, User::Panic(KSpecAssert_ESockSSockspxtrg, 3)); //Panic in debug
       
   117 	for (TInt i = iApiExtList.Count() - 1; i >= 0; --i)
       
   118 		{
       
   119 		TCommsApiExtTable& ref = iApiExtList[i];
       
   120 		if (!ref.RemoveClient(0, iGlobals))
       
   121 			{
       
   122 			iApiExtList.Remove(i);
       
   123 			}
       
   124 		}
       
   125 	iApiExtList.Close();
       
   126 	}
       
   127 
       
   128 TInt TCommsApiExtRegister::FindApiExtClientsTable(TInt aExtItfId)
       
   129 	{
       
   130 	TCommsApiExtTable entry(aExtItfId, Meta::STypeId::CreateSTypeId());
       
   131 	return iApiExtList.FindInUnsignedKeyOrder(entry);
       
   132 	}
       
   133 
       
   134 void TCommsApiExtRegister::RegisterInterfaceL(TInt aExtItfId, const Meta::STypeId& aMsgImplTid, TSubSessionUniqueId aClientId)
       
   135 	{
       
   136 	TInt index = FindApiExtClientsTable(aExtItfId);
       
   137 	if (index!=KErrNotFound)
       
   138 		{
       
   139 		TCommsApiExtTable& ref = iApiExtList[index];
       
   140 		ref.AddClientL(aClientId, iGlobals);
       
   141 		}
       
   142 	else
       
   143 		{
       
   144 		TCommsApiExtTable entry(aExtItfId, aMsgImplTid);
       
   145 		entry.AddClientL(aClientId, iGlobals);
       
   146 		if (iApiExtList.InsertInUnsignedKeyOrder(entry)!=KErrNone)
       
   147 			{
       
   148 			entry.RemoveClient(aClientId, iGlobals);
       
   149 			}
       
   150 		}
       
   151 	}
       
   152 
       
   153 TSubSessionUniqueId TCommsApiExtRegister::FirstClient(TInt aExtItfId)
       
   154 	{
       
   155 	TCommsApiExtTable entry(aExtItfId, Meta::STypeId::CreateSTypeId());
       
   156 	TInt index = iApiExtList.FindInUnsignedKeyOrder(entry);
       
   157 	TSubSessionUniqueId firstClient = 0;
       
   158 	if (index!=KErrNotFound)
       
   159 		{
       
   160 		TCommsApiExtTable& ref = iApiExtList[index];
       
   161 		__ASSERT_DEBUG(ref.iClients.Count(), User::Panic(KSpecAssert_ESockSSockspxtrg, 4)); //Would not be here otherwise
       
   162 		firstClient = ref.iClients[0];
       
   163 		}
       
   164 	return firstClient;
       
   165 	}
       
   166 
       
   167 //If aClientId==0 we will deregister this itf for all all clients!
       
   168 TSubSessionUniqueId TCommsApiExtRegister::DeRegisterInterface(TInt aExtItfId, TSubSessionUniqueId aClientId)
       
   169 	{
       
   170 	TCommsApiExtTable entry(aExtItfId, Meta::STypeId::CreateSTypeId());
       
   171 	TInt index = iApiExtList.FindInUnsignedKeyOrder(entry);
       
   172 	TSubSessionUniqueId nextClient = 0;
       
   173 	if (index!=KErrNotFound)
       
   174 		{
       
   175 		TCommsApiExtTable& ref = iApiExtList[index];
       
   176 		nextClient = ref.RemoveClient(aClientId, iGlobals);
       
   177 		if (nextClient==0)
       
   178 			{
       
   179 			iApiExtList.Remove(index);
       
   180 			}
       
   181 		}
       
   182 	return nextClient;
       
   183 	}
       
   184 
       
   185