zeroconf/cachemanager/src/cmdnscachemanager.cpp
changeset 14 da856f45b798
equal deleted inserted replaced
12:78fbd574edf4 14:da856f45b798
       
     1 /*
       
     2 * Copyright (c) 2010 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 #include "cmdnscachemanager.h"
       
    20 #include "cserviceinfo.h"
       
    21 #include "ccacheentry.h"
       
    22 #include "dnsconstants.h"
       
    23 
       
    24 using namespace HashInfo;
       
    25 
       
    26 EXPORT_C CMDNSCacheManager* CMDNSCacheManager::NewL(TUint aMaxCacheEntries)
       
    27 	{
       
    28 	CMDNSCacheManager* self = CMDNSCacheManager::NewLC(aMaxCacheEntries);
       
    29 	CleanupStack::Pop(self);
       
    30 	return self;
       
    31 	}
       
    32 
       
    33 EXPORT_C CMDNSCacheManager* CMDNSCacheManager::NewLC(TUint aMaxCacheEntries)
       
    34 	{
       
    35 	CMDNSCacheManager* self = new (ELeave)CMDNSCacheManager();
       
    36 	CleanupStack::PushL(self);
       
    37 	self->ConstructL(aMaxCacheEntries);
       
    38 	return self;
       
    39 	}
       
    40 
       
    41 EXPORT_C CMDNSCacheManager* CMDNSCacheManager::NewL()
       
    42 	{
       
    43 	CMDNSCacheManager* self = new (ELeave)CMDNSCacheManager();
       
    44 	return self;
       
    45 	}
       
    46 
       
    47 void CMDNSCacheManager::ConstructL(TUint aMaxCacheEntries)
       
    48 	{
       
    49 	iHashMap=CMDNSCacheMap::NewL(aMaxCacheEntries);
       
    50 	}
       
    51 
       
    52 CMDNSCacheManager::CMDNSCacheManager()
       
    53 	{
       
    54 	
       
    55 	}
       
    56 
       
    57 CMDNSCacheManager::~CMDNSCacheManager()
       
    58 	{
       
    59 	delete iHashMap;
       
    60 	iAuthoritativeEntries.ResetAndDestroy();
       
    61 	iAuthoritativeEntries.Close();
       
    62 	}
       
    63 
       
    64 /*Method to Insert / Update entries into Cache
       
    65  * @param aEntry Record Entry to Insert
       
    66  * @param aAuthoritative Flag to indicate whether the entry is Authoritative
       
    67  * @param aSessionId To Store the Session Id for each entry
       
    68  * @leave KErrNoMemory If Insert into Cache Failed because memory unavailability
       
    69  * @leave KErrNotSupported if any record other than SRV, PTR, TXT ot A come
       
    70  */
       
    71 EXPORT_C void CMDNSCacheManager::UpdateCacheL(CDnsResourceData& aEntry,TBool aAuthoritative,TUint32 aSessionId)
       
    72 	{			
       
    73 	CDnsResourceData* resourceRecord = NULL;
       
    74 	CServiceInfo* foundEntry = NULL;
       
    75 	TBuf8<255> name;
       
    76 	 _LIT8(KDot,".");
       
    77 	//Clone the entry to Insert in Cache
       
    78 	resourceRecord = aEntry.CloneL();				
       
    79 	
       
    80 	TUint16 recType(resourceRecord->Type());
       
    81 	
       
    82 	if(recType == EDnsType_SRV || recType == EDnsType_TXT || recType == EDnsType_A || recType == EDnsType_PTR)
       
    83 		{
       
    84 		if(recType == EDnsType_PTR)
       
    85 			{
       
    86 			//Extract the Key- Service Instance Name
       
    87 			CRdTypePtr*  ptrRecord = static_cast <CRdTypePtr*> (resourceRecord);
       
    88 			// before that insert the dot. 
       
    89 		    name.Copy(ptrRecord->DomainName());
       
    90             if(name[name.Size()-1]!='.')
       
    91                 name.Append(KDot);
       
    92 		    ptrRecord->SetDomainNameL(name);
       
    93 			foundEntry = iHashMap->Find(ptrRecord->DomainName());						
       
    94 			}
       
    95 		else
       
    96 			{
       
    97 			//Extract the Key- Service Instance Name or Host Name
       
    98 			foundEntry = iHashMap->Find(resourceRecord->Name());			
       
    99 			}
       
   100 
       
   101 		if(foundEntry)
       
   102 			{
       
   103 			//Update if the entry is already present in the Cache.
       
   104 			UpdateCacheEntry(foundEntry,resourceRecord);
       
   105 			}
       
   106 		else
       
   107 			{			
       
   108 			//Entry not found in Cache,Insert it fresh
       
   109 			CServiceInfo* recordInfo = NULL;
       
   110 			recordInfo = CServiceInfo::NewL();
       
   111 			switch(recType)
       
   112 				{
       
   113 				case EDnsType_SRV:
       
   114 				CRdTypeSrv*  srvRecord = static_cast <CRdTypeSrv*> (resourceRecord);			
       
   115 				recordInfo->SetServiceRecord(srvRecord);
       
   116 				break;
       
   117 
       
   118 				case EDnsType_TXT:
       
   119 				CRdTypeTxt*  txtRecord = static_cast <CRdTypeTxt*> (resourceRecord);
       
   120 				recordInfo->SetTxtRecord(txtRecord);
       
   121 				break;
       
   122 
       
   123 				case EDnsType_PTR:
       
   124 				CRdTypePtr*  ptrRecord = static_cast <CRdTypePtr*> (resourceRecord);
       
   125 				recordInfo->SetPtrRecord(ptrRecord);			
       
   126 				break;
       
   127 
       
   128 				case EDnsType_A:
       
   129 				CRdTypeA*  addressRecord = static_cast <CRdTypeA*> (resourceRecord);
       
   130 				recordInfo->SetAddressRecord(addressRecord);
       
   131 				break;
       
   132 				
       
   133 				
       
   134 				default:
       
   135 				User::Leave(KErrNotSupported);
       
   136 				break;
       
   137 				}
       
   138 				
       
   139 				recordInfo->SetAuthoritative(aAuthoritative);
       
   140 				recordInfo->SetSessionId(aSessionId);
       
   141 
       
   142 				
       
   143 				// insert the entry
       
   144 				if(recType == EDnsType_PTR )
       
   145 					{					
       
   146 					CRdTypePtr*  ptrRecord = static_cast <CRdTypePtr*> (resourceRecord);
       
   147 					iHashMap->UpdateL(ptrRecord->DomainName(),recordInfo);			
       
   148 					}
       
   149 				else
       
   150 	 				{
       
   151 					iHashMap->UpdateL(resourceRecord->Name(),recordInfo);										
       
   152 					}
       
   153 				
       
   154 				/*Maintain a list of all Services Published by us
       
   155 				(Authoritative records)*/
       
   156 				if(recordInfo->IsAuthoritative())
       
   157 				{
       
   158 				iAuthoritativeEntries.Append(recordInfo->CloneL());	
       
   159 				}										
       
   160 			}			
       
   161 		
       
   162 		}
       
   163 	else 
       
   164 		{
       
   165 		User::Leave(KErrNotSupported);
       
   166 		}
       
   167 
       
   168 	}
       
   169 
       
   170 
       
   171 
       
   172 
       
   173 /*Caters to finding varying types of records, for e.g:- find all the entries
       
   174 	 that correspond to a particular service type or find the host name through the address record
       
   175 	 @param aEntries List of entries that match the search constraint
       
   176 	 @param	aName could represent any search constraint Service Instance Type/ Name or the HostName 
       
   177 	 @param aType the desired record type
       
   178  */
       
   179 EXPORT_C TInt CMDNSCacheManager::FindServiceL(RPointerArray <CCacheEntry> & aEntries,const TDesC8& aName,TDnsType aType)const
       
   180 	{
       
   181 	TInt error(KErrNotFound);
       
   182 	TPtrC8 keyToFind(aName);
       
   183 	TInt count;
       
   184 	switch(aType)
       
   185 		{
       
   186 		case EDnsType_A:
       
   187 		case EDnsType_SRV:
       
   188 		case EDnsType_TXT:
       
   189 			{
       
   190 			//Search for a specific CacheEntry
       
   191 			CServiceInfo* cacheEntry = iHashMap->Find(aName);
       
   192 			if(cacheEntry)
       
   193 				{
       
   194 				CCacheEntry* entry = CCacheEntry::NewL();
       
   195 				CacheEntryL(entry,cacheEntry);
       
   196 				aEntries.Append(entry);
       
   197 				error = KErrNone;
       
   198 				}			
       
   199 			}
       
   200 			break;
       
   201 		
       
   202 		
       
   203 		case EDnsType_PTR:
       
   204 		//Match for all Cache Entries of the given Service Type
       
   205 		RPointerArray <const CServiceInfo> entries;
       
   206 		CleanupClosePushL(entries);
       
   207 		iHashMap->FindEntries(aName,entries);
       
   208 		
       
   209 		for(count =0; count< entries.Count();count++)
       
   210 			{
       
   211 			if((*entries[count]).PtrRecord())
       
   212 				{
       
   213 				//The entries are considered only if they have PTR records
       
   214 				CCacheEntry* entry = CCacheEntry::NewL();
       
   215 				CacheEntryL(entry,entries[count]);							
       
   216 				aEntries.Append(entry);	
       
   217 				error = KErrNone;					
       
   218 				}			
       
   219 			}
       
   220 
       
   221 		CleanupStack::PopAndDestroy(&entries);
       
   222 		break;
       
   223 
       
   224 		
       
   225         case EDnsQType_Any:
       
   226             {
       
   227             //Search for a specific CacheEntry
       
   228             CServiceInfo* cacheEntry = iHashMap->Find(aName);
       
   229             if(cacheEntry)
       
   230                 {
       
   231                 CCacheEntry* entry = CCacheEntry::NewL();
       
   232                 CacheEntryL(entry,cacheEntry);
       
   233                 aEntries.Append(entry);
       
   234                 error = KErrNone;
       
   235                 }
       
   236             else  // may be its a PTR record
       
   237                 {
       
   238                 RPointerArray <const CServiceInfo> entries;
       
   239                 CleanupClosePushL(entries);
       
   240                 iHashMap->FindEntries(aName,entries);
       
   241                 
       
   242                 for(count =0; count< entries.Count();count++)
       
   243                     {
       
   244                     if((*entries[count]).PtrRecord())
       
   245                         {
       
   246                         //The entries are considered only if they have PTR records
       
   247                         CCacheEntry* entry = CCacheEntry::NewL();
       
   248                         CacheEntryL(entry,entries[count]);                          
       
   249                         aEntries.Append(entry); 
       
   250                         error = KErrNone;                   
       
   251                         }           
       
   252                     }
       
   253 
       
   254                 CleanupStack::PopAndDestroy(&entries);
       
   255                 }
       
   256             }
       
   257             
       
   258         break;
       
   259 		
       
   260 		default:
       
   261 		User::Leave(KErrNotSupported);		
       
   262 		break;
       
   263 		}
       
   264 		return error;
       
   265 	}
       
   266 
       
   267 /*Deletes the specified entry from the Cache
       
   268 	 @param aServiceInstanceName Instance Name , whose entry needs to be deleted 
       
   269 	 @leave KErrNotFound if specified entry is not found
       
   270 	 */
       
   271 EXPORT_C TInt CMDNSCacheManager::DeleteEntryL(const TDesC8& aServiceInstanceName)
       
   272 	{
       
   273 	TInt err =  iHashMap->DeleteL(aServiceInstanceName);
       
   274 	
       
   275 	// check if it also on the authoritative list, if so delete it
       
   276 	
       
   277 	for(TInt i=0; i<iAuthoritativeEntries.Count();i++)
       
   278 	    {
       
   279 	    if(iAuthoritativeEntries[i]->Key().Compare(aServiceInstanceName)==0)
       
   280 	        {
       
   281 	        // found the entry, now delete it;
       
   282 	        CServiceInfo* info = iAuthoritativeEntries[i];
       
   283 	        iAuthoritativeEntries.Remove(i);
       
   284 	        delete info;
       
   285 	        }
       
   286 	    }
       
   287 	return err;
       
   288 	}
       
   289 
       
   290 
       
   291 EXPORT_C void CMDNSCacheManager::FlushCache()
       
   292 	{
       
   293 	iHashMap->DeleteAllEntries();
       
   294 	}
       
   295 
       
   296 EXPORT_C CCacheEntry* CMDNSCacheManager::NextStaleEntry(TBool aActiveCacheMgmtEnabled,TBool aIteratorReset)
       
   297  	{
       
   298 	CServiceInfo* nextEntry = iHashMap->NextStaleEntry(aActiveCacheMgmtEnabled,aIteratorReset);
       
   299 	CCacheEntry* nextCacheEntry = NULL;
       
   300 	
       
   301 	if(nextEntry)		
       
   302 		{
       
   303 		
       
   304 		 nextCacheEntry = CCacheEntry::NewL();
       
   305 		//Construct CCacheEntry object
       
   306 		CacheEntryL(nextCacheEntry,nextEntry);
       
   307 		
       
   308 		//Mark whether the entry has to be deleted or to be queried for (i.e., has exceeded 80%)
       
   309 		nextCacheEntry->SetEntryExpired(nextEntry->StaleEntry());
       
   310 		}
       
   311 	
       
   312 	return nextCacheEntry;
       
   313 	}
       
   314 
       
   315 void CMDNSCacheManager::UpdateCacheEntry(CServiceInfo* aEntry,CDnsResourceData* aRecordInfo)
       
   316 	{
       
   317 	TUint16 recType(aRecordInfo->Type());
       
   318 	switch(recType)
       
   319 		{
       
   320 		case EDnsType_SRV:
       
   321 		CRdTypeSrv*  srvRecord = static_cast <CRdTypeSrv*> (aRecordInfo);
       
   322 		//Delete the previous Service Record and Insert the fresh one
       
   323 		delete aEntry->ServiceRecord();
       
   324 		aEntry->SetServiceRecord(srvRecord);
       
   325 		break;
       
   326 
       
   327 		case EDnsType_TXT:
       
   328 		CRdTypeTxt*  txtRecord = static_cast <CRdTypeTxt*> (aRecordInfo);
       
   329 		//Delete the previous Text Record and Insert the fresh one
       
   330 		delete aEntry->TxtRecord();
       
   331 		aEntry->SetTxtRecord(txtRecord);
       
   332 		break;
       
   333 
       
   334 		case EDnsType_PTR:
       
   335 		CRdTypePtr*  ptrRecord = static_cast <CRdTypePtr*> (aRecordInfo);
       
   336 		//Delete the previous Ptr Record and Insert the fresh one
       
   337 		delete aEntry->PtrRecord();
       
   338 		aEntry->SetPtrRecord(ptrRecord);			
       
   339 		break;
       
   340 
       
   341 		case EDnsType_A:
       
   342 		CRdTypeA*  addressRecord = static_cast <CRdTypeA*> (aRecordInfo);
       
   343 		//Delete the previous Address Record and Insert the fresh one
       
   344 		delete aEntry->AddressRecord();
       
   345 		aEntry->SetAddressRecord(addressRecord);
       
   346 		break;
       
   347 		
       
   348 		default:
       
   349 		//Do nothing
       
   350 		break;
       
   351 		}
       
   352 	}
       
   353 	
       
   354 EXPORT_C void CMDNSCacheManager::DumpCacheL()
       
   355 	{
       
   356 	iHashMap->DumpCacheL();
       
   357 	}
       
   358 	
       
   359 EXPORT_C	TUint32 CMDNSCacheManager::CacheEntryCount()
       
   360 	{
       
   361 	return iHashMap->NumberOfEntries();	
       
   362 	}
       
   363 	
       
   364 	
       
   365 void CMDNSCacheManager::CacheEntryL(CCacheEntry* aTargetEntry,const CServiceInfo* aSourceEntry)const
       
   366 	{
       
   367 	//Check whether the specific records existed before making a copy 
       
   368 	if(aSourceEntry->PtrRecord())
       
   369 		{
       
   370 		aTargetEntry->SetPtrRecord(static_cast <CRdTypePtr*>( aSourceEntry->PtrRecord()->CloneL()));
       
   371 		}
       
   372 		
       
   373 	if(aSourceEntry->AddressRecord())	
       
   374 		{
       
   375 		//This case occurs only for an idividual Address Record
       
   376 		aTargetEntry->SetAddressRecord(static_cast <CRdTypeA*>(aSourceEntry->AddressRecord()->CloneL()));
       
   377 		}
       
   378 	
       
   379 	if(aSourceEntry->ServiceRecord())		
       
   380 		{		
       
   381 		aTargetEntry->SetServiceRecord(static_cast <CRdTypeSrv*>(aSourceEntry->ServiceRecord()->CloneL()));
       
   382 		//Set Address Record ; if it exists
       
   383 		CServiceInfo* entry = iHashMap->Find(aTargetEntry->ServiceRecord()->Target());
       
   384 		if(entry)
       
   385 			{
       
   386 			aTargetEntry->SetAddressRecord(static_cast <CRdTypeA*>(entry->AddressRecord()->CloneL()));
       
   387 			}		
       
   388 		}
       
   389 	
       
   390 	if(aSourceEntry->TxtRecord())
       
   391 		{
       
   392 		aTargetEntry->SetTxtRecord(static_cast <CRdTypeTxt*>(aSourceEntry->TxtRecord()->CloneL()));
       
   393 		}
       
   394 		
       
   395 	aTargetEntry->SetAuthoritative(aSourceEntry->IsAuthoritative());
       
   396 	aTargetEntry->SetSessionId(aSourceEntry->SessionId());
       
   397 	}
       
   398 
       
   399 	
       
   400 void CMDNSCacheManager::AuthoritativeEntriesL(RPointerArray <CCacheEntry>& aCacheEntries)
       
   401 	{	
       
   402 	for(TInt index =0; index < iAuthoritativeEntries.Count();index++)
       
   403 		{				
       
   404 		CCacheEntry* entry = CCacheEntry::NewL();
       
   405 		CacheEntryL(entry,iAuthoritativeEntries[index]);							
       
   406 		aCacheEntries.Append(entry);											
       
   407 		}		
       
   408 	}
       
   409 	
       
   410 TUint CMDNSCacheManager::NumberOfEntries()
       
   411 	{
       
   412 	return iHashMap->NumberOfEntries();
       
   413 	}