zeroconf/server/src/cmdnscacheconsistencymgr.cpp
changeset 14 da856f45b798
equal deleted inserted replaced
12:78fbd574edf4 14:da856f45b798
       
     1 // Copyright (c) 2008-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 // cmdnsconsistencymgr.cpp
       
    15 // 
       
    16 //
       
    17 /**
       
    18 @file
       
    19 @internalTechnology
       
    20 */
       
    21 
       
    22 //User include
       
    23 #include "cmdnscacheconsistencymgr.h"
       
    24 #include "cmdnscachemanager.h"
       
    25 #include "cmessagehandler.h"
       
    26 
       
    27 __FLOG_STMT(_LIT8(KComponent,"MDNSServer");)
       
    28 
       
    29 
       
    30 const TUint32 KMinActivityCount = 5;//TODO: to Define correct Min Work Count
       
    31 
       
    32 
       
    33 /*
       
    34  * Two phase constructor
       
    35  * @param aActiveCacheMgmtEnabled
       
    36  * @param aMessageHandler reference to message handler.
       
    37  */
       
    38  CMDNSCacheConsistencyMgr* CMDNSCacheConsistencyMgr::NewL(TBool aActiveCacheMgmtEnabled,CMessageHandler& aMessageHandler)
       
    39 	{
       
    40 	CMDNSCacheConsistencyMgr* self = CMDNSCacheConsistencyMgr::NewLC(aActiveCacheMgmtEnabled,aMessageHandler);
       
    41 	CleanupStack::Pop(self);
       
    42 	return self;
       
    43 	}
       
    44 
       
    45 /*
       
    46  * Two phase constructor
       
    47  * @param aActiveCacheMgmtEnabled
       
    48  * @param aMessageHandler reference to message handler.
       
    49  */
       
    50 CMDNSCacheConsistencyMgr* CMDNSCacheConsistencyMgr::NewLC(TBool aActiveCacheMgmtEnabled,CMessageHandler& aMessageHandler)
       
    51 	{
       
    52 	CMDNSCacheConsistencyMgr* self = new (ELeave)CMDNSCacheConsistencyMgr(aActiveCacheMgmtEnabled,aMessageHandler);
       
    53 	CleanupStack::PushL(self);
       
    54 	self->ConstructL();
       
    55 	return self;
       
    56 	}
       
    57 
       
    58 /*
       
    59  * Constructor.
       
    60  */
       
    61 CMDNSCacheConsistencyMgr::CMDNSCacheConsistencyMgr(TBool aActiveCacheMgmtEnabled,CMessageHandler& aMessageHandler)
       
    62 :CActive(EPriorityStandard),iMessageHandler(aMessageHandler)
       
    63 	{
       
    64 	iActiveCacheMgmtEnabled=aActiveCacheMgmtEnabled;
       
    65 	iWalkInterval= KDefaultCheckInterval;
       
    66 	CActiveScheduler::Add(this);	
       
    67 	}
       
    68 
       
    69 
       
    70 //Destructor
       
    71 CMDNSCacheConsistencyMgr::~CMDNSCacheConsistencyMgr()
       
    72 	{
       
    73 	__FLOG(_L8("CMDNSCacheConsistencyMgr::~CMDNSCacheConsistencyMgr - Entry"));
       
    74 	iTimer.Close();
       
    75 	__FLOG(_L8("CMDNSCacheConsistencyMgr::~CMDNSCacheConsistencyMgr - Exit"));
       
    76 	__FLOG_CLOSE;
       
    77 	}
       
    78 /*
       
    79  * Two phase constructor
       
    80  */
       
    81 void CMDNSCacheConsistencyMgr::ConstructL()
       
    82 	{
       
    83 	__FLOG_OPEN(KMDNSSubsystem, KComponent);
       
    84 	__FLOG(_L8("CMDNSCacheConsistencyMgr::ConstructL - Entry"));
       
    85 	User::LeaveIfError(iTimer.CreateLocal());	
       
    86 	__FLOG(_L8("CMDNSCacheConsistencyMgr::ConstructL - Exit"));
       
    87 	}
       
    88 
       
    89 /*
       
    90  * Function to stop running the consistemcy manager .
       
    91  * Cancels all the asynchronous request.
       
    92  */
       
    93 void CMDNSCacheConsistencyMgr::Stop()
       
    94 	{
       
    95 	__FLOG(_L8("CMDNSCacheConsistencyMgr::Stop - Entry"));
       
    96 	Cancel();
       
    97 	__FLOG(_L8("CMDNSCacheConsistencyMgr::Stop - Exit"));
       
    98 	}
       
    99 	
       
   100 /*
       
   101  * Starts the consistency Manager
       
   102  * @param aWalkInterval specifies the walk interval in seconds.
       
   103  */	
       
   104 void CMDNSCacheConsistencyMgr::Start(TUint aWalkInterval)
       
   105 	{
       
   106 	__FLOG(_L8("CMDNSCacheConsistencyMgr::Start - Entry"));
       
   107 	iWalkInterval = aWalkInterval;
       
   108 	TUint Interval= iWalkInterval*1000000;
       
   109 	//Converted to Microseconds		
       
   110 	iTimer.After(iStatus,Interval);
       
   111 	SetActive();
       
   112 	__FLOG(_L8("CMDNSCacheConsistencyMgr::Start - Exit"));
       
   113 	}
       
   114 /*
       
   115  * Delay between two Walk thourgh .
       
   116  * Starts a time to complete the request after an interval mentioned in the iWalkinterval.
       
   117  * @param aStatus reference to status variable.
       
   118  */
       
   119  void CMDNSCacheConsistencyMgr::Delay(TRequestStatus& aStatus)
       
   120 	{
       
   121 	__FLOG(_L8("CMDNSCacheConsistencyMgr::Delay - Entry"));
       
   122 	TUint Interval= iWalkInterval*1000000;
       
   123 	//Converted to Microseconds		
       
   124 	iTimer.After(aStatus,Interval);
       
   125 	SetActive();
       
   126 	__FLOG(_L8("CMDNSCacheConsistencyMgr::Delay - Exit"));
       
   127 	}
       
   128 
       
   129 
       
   130  /*
       
   131   * Iterates the cache on each hit
       
   132   * and sets a delay for the nexthit.
       
   133   */
       
   134 void CMDNSCacheConsistencyMgr::RunL()
       
   135 	{	
       
   136 	__FLOG(_L8("CMDNSCacheConsistencyMgr::RunL - Entry"));
       
   137 	IterateCacheL();
       
   138 	Delay(iStatus);
       
   139 	__FLOG(_L8("CMDNSCacheConsistencyMgr::RunL - Exit"));
       
   140 	}
       
   141 
       
   142 /*
       
   143  * Handles any leave at RunL
       
   144  * 
       
   145  */
       
   146 TInt CMDNSCacheConsistencyMgr::RunError(TInt aError)
       
   147 	{
       
   148 	__FLOG(_L8("CMDNSCacheConsistencyMgr::RunError - Entry"));
       
   149 	User::LeaveIfError(aError);
       
   150 	return aError;
       
   151 	__FLOG(_L8("CMDNSCacheConsistencyMgr::RunError - Exit"));
       
   152 	}
       
   153 
       
   154 /*
       
   155  * Cancels any asynchoronous request.
       
   156  */
       
   157 void CMDNSCacheConsistencyMgr::DoCancel()
       
   158 	{
       
   159 	__FLOG(_L8("CMDNSCacheConsistencyMgr::DoCancel - Entry"));
       
   160 	iTimer.Cancel();
       
   161 	__FLOG(_L8("CMDNSCacheConsistencyMgr::DoCancel - Exit"));
       
   162 	}
       
   163 	
       
   164 /*
       
   165  * Walk through the cache .Does two function
       
   166  * 1. Publish any authoritative entries.
       
   167  * 2. Query for the enteries if not authoritative.
       
   168  */
       
   169 void CMDNSCacheConsistencyMgr::IterateCacheL()	
       
   170 	{	
       
   171 	__FLOG(_L8("CMDNSCacheConsistencyMgr::IterateCacheL - Entry"));
       
   172 	TBool iterateReset(ETrue);
       
   173 	TBool cacheUpToDate(ETrue);
       
   174 	TInt count = iMessageHandler.DnsCache().NumberOfEntries();
       
   175 	TInt index(0);
       
   176 	
       
   177 	
       
   178 		
       
   179 	for(index=0; index<count;index++)
       
   180 		{
       
   181 		//Fetch the next entry whose ttl has completely expired or 80% 
       
   182 		//of ttl has expired 
       
   183 		CCacheEntry* entry = iMessageHandler.DnsCache().NextStaleEntry(iActiveCacheMgmtEnabled,iterateReset);
       
   184 
       
   185 		//Reset the iterate flag
       
   186 		iterateReset = EFalse;
       
   187 	    __FLOG(_L8("CMDNSCacheConsistencyMgr::IterateCacheL - 1"));
       
   188 		if(entry)
       
   189 			{
       
   190 			if(entry->EntryExpired())
       
   191 				{
       
   192 				
       
   193 				if(entry->IsAuthoritative())
       
   194 					{
       
   195 					//Republish the record
       
   196 					//Goodbye packet is sent while the session for publish is closed
       
   197 					CDnsMessage * announcement = CDnsMessage::NewL(0,EFalse);
       
   198 					CleanupStack::PushL(announcement);
       
   199 					if(entry->PtrRecord())
       
   200 					    {
       
   201                         entry->PtrRecord()->SetTtl(120);
       
   202 					    announcement->AppendAnswerL(entry->PtrRecord()->CloneL());
       
   203 					    iMessageHandler.DnsCache().UpdateCacheL(*(entry->PtrRecord()),ETrue,entry->SessionId());
       
   204                         __FLOG(_L8("CMDNSCacheConsistencyMgr::IterateCacheL - 2"));
       
   205 					    }
       
   206 					if(entry->ServiceRecord())
       
   207 					    {
       
   208                         entry->ServiceRecord()->SetTtl(120);
       
   209 					    announcement->AppendAnswerL(entry->ServiceRecord()->CloneL());
       
   210 					    iMessageHandler.DnsCache().UpdateCacheL(*(entry->ServiceRecord()),ETrue,entry->SessionId());
       
   211 					    __FLOG(_L8("CMDNSCacheConsistencyMgr::IterateCacheL - 3"));
       
   212 					    }
       
   213 					if(entry->TxtRecord())
       
   214 					    {
       
   215                         entry->TxtRecord()->SetTtl(120);
       
   216 					    announcement->AppendAnswerL(entry->TxtRecord()->CloneL());
       
   217 					    iMessageHandler.DnsCache().UpdateCacheL(*(entry->TxtRecord()),ETrue,entry->SessionId());
       
   218 					    __FLOG(_L8("CMDNSCacheConsistencyMgr::IterateCacheL - 4"));
       
   219 					    }
       
   220 					if(entry->AddressRecord())
       
   221                         {
       
   222                         entry->AddressRecord()->SetTtl(120);
       
   223                         announcement->AppendAnswerL(entry->AddressRecord()->CloneL());
       
   224                         iMessageHandler.DnsCache().UpdateCacheL(*(entry->AddressRecord()),ETrue,0);
       
   225                         __FLOG(_L8("CMDNSCacheConsistencyMgr::IterateCacheL - 5"));
       
   226                         }
       
   227 					__FLOG(_L8("CMDNSCacheConsistencyMgr::IterateCacheL - 6"));
       
   228 					iMessageHandler.SendQueryL(announcement,*this);
       
   229 					__FLOG(_L8("CMDNSCacheConsistencyMgr::IterateCacheL - 7"));
       
   230 					CleanupStack::Pop();//announcement
       
   231 					}
       
   232 				else
       
   233 				    {
       
   234 	                if(!entry->IsAuthoritative())
       
   235 	                    {
       
   236 	                    DeleteEntry(entry);
       
   237 	                    //Query(entry);
       
   238 	                    }				    
       
   239 				    }
       
   240 				}
       
   241 			cacheUpToDate = EFalse;
       
   242 			}
       
   243 		delete entry;
       
   244 		}		
       
   245 	__FLOG(_L8("CMDNSCacheConsistencyMgr::IterateCacheL - Exit"));	 
       
   246 	}
       
   247 	
       
   248 /*
       
   249  * Delete the particular entry from the cache.
       
   250  * @param aEntry cacheentry to be delete.
       
   251  */	
       
   252 void CMDNSCacheConsistencyMgr::DeleteEntry(CCacheEntry* aEntry)
       
   253 	{	
       
   254 	__FLOG(_L8("CMDNSCacheConsistencyMgr::DeleteEntry - Entry"));
       
   255 	RBuf8 key;
       
   256 	//Extract the key & delete
       
   257 	
       
   258 	if(aEntry->AddressRecord())
       
   259 		{
       
   260 		key.CreateL(GetHostName(aEntry));
       
   261 		TInt delError = iMessageHandler.DnsCache().DeleteEntryL(key);//TODO: handle error
       
   262 		}
       
   263 
       
   264 	key.Close();
       
   265 	key.CreateL(GetServiceName(aEntry));
       
   266 	
       
   267 	TInt error = iMessageHandler.DnsCache().DeleteEntryL(key);
       
   268 	//Handle error
       
   269 	key.Close();
       
   270 	__FLOG(_L8("CMDNSCacheConsistencyMgr::DeleteEntry - Exit"));
       
   271 	}
       
   272 	
       
   273 /*
       
   274  * Create a CDnsMessage object to be sent to the network using aEntry.
       
   275  *  
       
   276  */	
       
   277 void CMDNSCacheConsistencyMgr::Query(CCacheEntry* aEntry)
       
   278 	{
       
   279 	__FLOG(_L8("CMDNSCacheConsistencyMgr::Query - Entry"));
       
   280 		//Construct DNS Message
       
   281 	CDnsMessage* message = CDnsMessage::NewL(0,ETrue);
       
   282 	CleanupStack::PushL(message);
       
   283 
       
   284 	//Form the Query/Question part of the message
       
   285 	CDnsQuestion* question = CDnsQuestion::NewL();
       
   286 	CleanupStack::PushL(question);
       
   287 	
       
   288 	question->SetNameL(GetServiceName(aEntry));
       
   289 	question->SetClass(EDnsClass_IN);
       
   290 	question->SetType(EDnsQType_Any);
       
   291 
       
   292 	//Append the Query to the Message
       
   293 	message->AppendQueryL(question);
       
   294 	if(aEntry->AddressRecord())
       
   295 	message->AppendAnswerL(aEntry->AddressRecord()->CloneL());
       
   296 	
       
   297 	if(aEntry->ServiceRecord())
       
   298 	message->AppendAnswerL(aEntry->ServiceRecord()->CloneL());
       
   299 	
       
   300 	if(aEntry->TxtRecord())
       
   301 	message->AppendAnswerL(aEntry->TxtRecord()->CloneL());	
       
   302 	
       
   303 	if(aEntry->PtrRecord())
       
   304 	message->AppendAnswerL(aEntry->PtrRecord()->CloneL());
       
   305 	
       
   306 	
       
   307 		
       
   308 	//Send the query
       
   309 	iMessageHandler.SendQueryL(message,*this);		
       
   310 
       
   311 	CleanupStack::Pop();//question
       
   312 	CleanupStack::Pop();//message
       
   313 	__FLOG(_L8("CMDNSCacheConsistencyMgr::Query - Exit"));
       
   314 	}
       
   315 
       
   316 /*
       
   317  * @return the hostname for the particular record.
       
   318  */	
       
   319 const TDesC8& CMDNSCacheConsistencyMgr::GetHostName(CCacheEntry* aEntry)
       
   320 	{
       
   321 	__FLOG(_L8("CMDNSCacheConsistencyMgr::GetHostName - Entry Exit"));
       
   322 	return aEntry->AddressRecord()->Name();
       
   323 	}
       
   324 
       
   325 
       
   326 /*
       
   327  * @param aEntry for which service name is required.
       
   328  * @return Domain name for the particular service.
       
   329  */
       
   330 const TDesC8& CMDNSCacheConsistencyMgr::GetServiceName(CCacheEntry* aEntry)
       
   331 	{
       
   332 	__FLOG(_L8("CMDNSCacheConsistencyMgr::GetServiceName - Entry"));
       
   333 	if(aEntry->ServiceRecord())
       
   334 		{
       
   335 		return aEntry->ServiceRecord()->Name();
       
   336 		}
       
   337 	else if (aEntry->TxtRecord())
       
   338 		{
       
   339 		return aEntry->TxtRecord()->Name();
       
   340 		}
       
   341 	else if(aEntry->PtrRecord())
       
   342 		{
       
   343 		return aEntry->PtrRecord()->DomainName();
       
   344 		}
       
   345 	else if(aEntry->AddressRecord())
       
   346 	        {
       
   347 	        return aEntry->AddressRecord()->Name();
       
   348 	        }
       
   349 	
       
   350 	__FLOG(_L8("CMDNSCacheConsistencyMgr::GetServiceName - Exit"));
       
   351 	}
       
   352 	
       
   353 /*
       
   354  * Notified for any queries sent .
       
   355  */	
       
   356 void CMDNSCacheConsistencyMgr::OnPacketSendL(TInt /*aError*/)
       
   357 	{
       
   358 	__FLOG(_L8("CMDNSCacheConsistencyMgr::OnPacketSendL - Entry"));
       
   359 	//Do nothing
       
   360 	}