realtimenetprots/sipfw/ClientResolver/Server/src/CSIPCRRoutingEntry.cpp
changeset 0 307788aac0a8
equal deleted inserted replaced
-1:000000000000 0:307788aac0a8
       
     1 // Copyright (c) 2005-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 // Name          : CSIPCRRoutingEntry.cpp
       
    15 // Part of       : SIP Client Resolver
       
    16 // Version       : 1.0
       
    17 //
       
    18 
       
    19 
       
    20 
       
    21 // INCLUDES
       
    22 #include "CSIPCRRoutingEntry.h"
       
    23 
       
    24 // ----------------------------------------------------------------------------
       
    25 // CSIPCRRoutingEntry::NewL
       
    26 // ----------------------------------------------------------------------------
       
    27 //
       
    28 CSIPCRRoutingEntry* CSIPCRRoutingEntry::NewL( const TUid& aUid )
       
    29     {
       
    30     CSIPCRRoutingEntry* self = new (ELeave) CSIPCRRoutingEntry(aUid);
       
    31     return self;
       
    32     }
       
    33 
       
    34 // ----------------------------------------------------------------------------
       
    35 // CSIPCRRoutingEntry::CSIPCRRoutingEntry
       
    36 // ----------------------------------------------------------------------------
       
    37 //
       
    38 CSIPCRRoutingEntry::CSIPCRRoutingEntry(const TUid& aUid) 
       
    39  : iReferenceCount(1),
       
    40    iUID(aUid)
       
    41     {
       
    42     }
       
    43 
       
    44 // ----------------------------------------------------------------------------
       
    45 // CSIPCRRoutingEntry::~CSIPCRRoutingEntry
       
    46 // ----------------------------------------------------------------------------
       
    47 //
       
    48 CSIPCRRoutingEntry::~CSIPCRRoutingEntry()
       
    49     {
       
    50     iServedClients.ResetAndDestroy();
       
    51     }
       
    52 
       
    53 // ----------------------------------------------------------------------------
       
    54 // CSIPCRRoutingEntry::ReferenceCount 
       
    55 // ----------------------------------------------------------------------------
       
    56 //
       
    57 TInt& CSIPCRRoutingEntry::ReferenceCount()
       
    58     {
       
    59     return iReferenceCount;
       
    60     }
       
    61 
       
    62 // ----------------------------------------------------------------------------
       
    63 // CSIPCRRoutingEntry::UID 
       
    64 // ----------------------------------------------------------------------------
       
    65 //
       
    66 const TUid& CSIPCRRoutingEntry::UID() const
       
    67     {
       
    68     return iUID;
       
    69     }
       
    70     
       
    71 // ----------------------------------------------------------------------------
       
    72 // CSIPCRRoutingEntry::SetUID
       
    73 // ----------------------------------------------------------------------------
       
    74 //
       
    75 void CSIPCRRoutingEntry::SetUID( const TUid& aUid )
       
    76     {
       
    77     iUID = aUid;    
       
    78     }
       
    79     
       
    80 // ----------------------------------------------------------------------------
       
    81 // CSIPCRRoutingEntry::AddL
       
    82 // ----------------------------------------------------------------------------
       
    83 //
       
    84 void CSIPCRRoutingEntry::AddL( const TUid& aUid )
       
    85     {
       
    86 	if (FindEntry(aUid)) 
       
    87 	    {	    
       
    88 	    User::Leave (KErrAlreadyExists);    
       
    89 	    }
       
    90 	    	    
       
    91     	
       
    92     CSIPCRRoutingEntry* entry = CSIPCRRoutingEntry::NewL(aUid);    
       
    93     CleanupStack::PushL(entry);
       
    94     
       
    95     User::LeaveIfError(iServedClients.Append(entry));
       
    96     CleanupStack::Pop(entry);
       
    97     }
       
    98 
       
    99 // ----------------------------------------------------------------------------
       
   100 // CSIPCRRoutingEntry::AddL
       
   101 // ----------------------------------------------------------------------------
       
   102 //
       
   103 void CSIPCRRoutingEntry::AddL( const CSIPCRRoutingEntry* aEntry )
       
   104     {        
       
   105 	if (FindEntry(aEntry->UID())) 
       
   106 	    {	    
       
   107 	    User::Leave (KErrAlreadyExists);    
       
   108 	    }
       
   109 	    	        	   
       
   110     User::LeaveIfError(iServedClients.Append(aEntry));
       
   111     }
       
   112 
       
   113 // ----------------------------------------------------------------------------
       
   114 // CSIPCRRoutingEntry::Compare
       
   115 // ----------------------------------------------------------------------------
       
   116 //
       
   117 TBool CSIPCRRoutingEntry::Compare(const CSIPCRRoutingEntry& aItem,
       
   118 				const CSIPCRRoutingEntry& aItem2) 
       
   119 	{
       
   120 	return (aItem.iUID == aItem2.iUID);
       
   121 	}
       
   122 
       
   123 // ----------------------------------------------------------------------------
       
   124 // CSIPCRRoutingEntry::FindEntry
       
   125 // ----------------------------------------------------------------------------
       
   126 //
       
   127 CSIPCRRoutingEntry* CSIPCRRoutingEntry::FindEntry(const TUid& aUid)
       
   128 	{
       
   129 		
       
   130 	TIdentityRelation<CSIPCRRoutingEntry> compareId(
       
   131 	                                            CSIPCRRoutingEntry::Compare);
       
   132 		
       
   133     // Setting temporarily this entry to be the searched one.
       
   134     TUid currentUid = iUID;
       
   135     iUID = aUid;
       
   136 	
       
   137 	TInt index = iServedClients.Find(this, compareId);
       
   138 	
       
   139     // Setting the original uid back.
       
   140     iUID = currentUid;
       
   141 	
       
   142 	if (index == KErrNotFound)
       
   143 	    {	    
       
   144 	    return NULL;
       
   145 	    }
       
   146 	
       
   147 	return (CSIPCRRoutingEntry*) iServedClients[index]; 
       
   148 	}
       
   149 
       
   150 // ----------------------------------------------------------------------------
       
   151 // CSIPCRRoutingEntry::FindEntryIndex
       
   152 // ----------------------------------------------------------------------------
       
   153 //
       
   154 TInt CSIPCRRoutingEntry::FindEntryIndex(const TUid& aUid)
       
   155 	{
       
   156 	TIdentityRelation<CSIPCRRoutingEntry> compareId(
       
   157 	                                            CSIPCRRoutingEntry::Compare);
       
   158 		
       
   159     // Setting temporarily this entry to be the searched one.
       
   160     TUid currentUid = iUID;
       
   161     iUID = aUid;
       
   162 	
       
   163 	TInt index = iServedClients.Find(this, compareId);
       
   164 	
       
   165     // Setting the original uid back.     
       
   166     iUID = currentUid;
       
   167 	
       
   168 	return index;
       
   169 	}
       
   170 
       
   171 // ----------------------------------------------------------------------------
       
   172 // CSIPCRRoutingEntry::FindEntryInTree
       
   173 // ----------------------------------------------------------------------------
       
   174 //
       
   175 CSIPCRRoutingEntry* CSIPCRRoutingEntry::FindEntryInTree( const TUid& aUid)
       
   176     {
       
   177     // if this is the entry, return it..
       
   178     if (iUID == aUid)
       
   179         {        
       
   180         return this;
       
   181         }
       
   182     
       
   183     // search all levels in this routing tree..
       
   184     TInt count = iServedClients.Count();
       
   185     if (count>0)
       
   186         {
       
   187         CSIPCRRoutingEntry* entry = NULL;
       
   188         TInt index;
       
   189         for (index = 0; index < count; index++)
       
   190             {
       
   191             entry = iServedClients[index];            
       
   192             if (entry->UID() == aUid)
       
   193                 {                
       
   194                 return entry;                       
       
   195                 }
       
   196             if (entry->HasClient(aUid))
       
   197                 {                
       
   198                 return (entry->FindEntryInTree(aUid));
       
   199                 }
       
   200             }
       
   201         }
       
   202         
       
   203     return NULL;    
       
   204     }
       
   205 
       
   206 // ----------------------------------------------------------------------------
       
   207 // CSIPCRRoutingEntry::FindParent
       
   208 // ----------------------------------------------------------------------------
       
   209 //
       
   210 CSIPCRRoutingEntry* CSIPCRRoutingEntry::FindParent( const TUid& aUid )
       
   211     {
       
   212     if (UID() == aUid)
       
   213         {                
       
   214         return NULL;           
       
   215         }            
       
   216     
       
   217     TInt count = iServedClients.Count();
       
   218     if (count>0)
       
   219         {
       
   220         CSIPCRRoutingEntry* entry = NULL;
       
   221         TInt index;
       
   222         for (index = 0; index < count; index++)
       
   223             {
       
   224             entry = iServedClients[index];
       
   225             if (entry->UID() == aUid)
       
   226                 {                
       
   227                 return this;
       
   228                 }            
       
   229             if (entry->HasClient(aUid))
       
   230                 {                
       
   231                 return (entry->FindParent(aUid));
       
   232                 }
       
   233             }
       
   234         }
       
   235         
       
   236     return NULL;    
       
   237     }
       
   238 
       
   239 // ----------------------------------------------------------------------------
       
   240 // CSIPCRRoutingEntry::HasClient
       
   241 // ----------------------------------------------------------------------------
       
   242 //
       
   243 TBool CSIPCRRoutingEntry::HasClient( const TUid& aUid ) const
       
   244     {
       
   245     TInt count = iServedClients.Count();
       
   246     
       
   247     if (count>0)
       
   248         {
       
   249         CSIPCRRoutingEntry* entry = NULL;
       
   250         TInt index;
       
   251         for (index = 0; index < count; index++)
       
   252             {
       
   253             entry = iServedClients[index];
       
   254             if (entry->UID() == aUid)
       
   255                 {                
       
   256                 return ETrue;            
       
   257                 }
       
   258             if (entry->HasClient(aUid))
       
   259                 {                
       
   260                 return ETrue;            
       
   261                 }
       
   262             }
       
   263         }
       
   264         
       
   265     return EFalse;    
       
   266     }
       
   267 
       
   268 // ----------------------------------------------------------------------------
       
   269 // CSIPCRRoutingEntry::Remove
       
   270 // ----------------------------------------------------------------------------
       
   271 //
       
   272 TInt CSIPCRRoutingEntry::Remove( const TUid& aUid )
       
   273     {
       
   274     // search the correct entry 
       
   275 	TInt index = FindEntryIndex(aUid);
       
   276 	
       
   277 	if (index == KErrNotFound)
       
   278 	    {	    
       
   279 	    return KErrNotFound;
       
   280 	    }
       
   281 	
       
   282     CSIPCRRoutingEntry* entry = iServedClients[index];
       
   283     iServedClients.Remove(index);    
       
   284     delete entry;
       
   285     
       
   286 	return KErrNone;	
       
   287     }
       
   288 
       
   289 // ----------------------------------------------------------------------------
       
   290 // CSIPCRRoutingEntry::Detach
       
   291 // ----------------------------------------------------------------------------
       
   292 //
       
   293 CSIPCRRoutingEntry* CSIPCRRoutingEntry::Detach( const TUid& aUid )
       
   294     {
       
   295     // search the correct entry 
       
   296 	TInt index = FindEntryIndex(aUid);
       
   297 	
       
   298 	if (index == KErrNotFound)
       
   299 	    {	    
       
   300 	    return NULL;
       
   301 	    }
       
   302 	
       
   303     CSIPCRRoutingEntry* entry = iServedClients[index];
       
   304     iServedClients.Remove(index);    
       
   305 
       
   306 	return entry;	
       
   307     }