zeroconf/client/src/cmdnsclient.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 // cmdnsclient.cpp
       
    15 // 
       
    16 //
       
    17 /**
       
    18 @file
       
    19 @PublishedAll
       
    20 */
       
    21 
       
    22 //User include 
       
    23 #include "cmdnsclient.h"
       
    24 #include "cmdnsserverconstants.h"
       
    25 
       
    26 
       
    27 //System include
       
    28 #include <mdns/dnsconstants.h>
       
    29 #include <s32mem.h>
       
    30 #include <in_sock.h>
       
    31 
       
    32 __FLOG_STMT(_LIT8(KComponent,"MDNSClient");)
       
    33 /*
       
    34  * Default constructor -Assigns the buffer pointer to Null 
       
    35  */
       
    36 EXPORT_C RMdns::RMdns():iBufPointer(NULL,0)
       
    37 	{
       
    38 	__FLOG_OPEN(KMDNSSubsystem, KComponent);
       
    39 	}
       
    40 
       
    41 /*
       
    42  * Interface to start the Mdns server .
       
    43  * KMdnsServerName referes to the name of the Mdns server.
       
    44  * @param aConnectionHandle handle to the RConnection object // TODO
       
    45  * @param aSocketServerHandle handler to the RSocketServ object //TODO 
       
    46  * 
       
    47  */
       
    48 EXPORT_C TInt RMdns::Open(TInt aConnectionHandle)
       
    49 	{
       
    50 	__FLOG(_L8("RMdns::Open - Entry"));
       
    51 	TVersion serverVersion(0, 0, 0);
       
    52 	//creates a new session if the server exists and it is up and running .
       
    53 	//returns KErrNotFound if the server needs to be launched.
       
    54 	TInt err = CreateSession(KMdnsServerName, serverVersion, 2);
       
    55 	if (err == KErrNotFound)
       
    56 		{
       
    57 		// try starting the server, since create session didn't work
       
    58 		//iap refers to the iap application wants to use.
       
    59 		TBuf<16> iap;
       
    60 		iap.AppendNum(aConnectionHandle);
       
    61 		RProcess server;
       
    62 		User::LeaveIfError(server.Create(KMdnsServerExe, iap));
       
    63 		server.Resume();
       
    64 		TRequestStatus status;
       
    65 		status = KRequestPending;
       
    66 		server.Rendezvous(status);
       
    67 		//waits for the server to construct all the required object.
       
    68 	 	User::WaitForRequest(status);
       
    69 		User::LeaveIfError(status.Int());
       
    70 		User::LeaveIfError(CreateSession(KMdnsServerName, serverVersion, 2));
       
    71 		server.Close();
       
    72 		}
       
    73 	else
       
    74 		{
       
    75 		User::LeaveIfError(err);
       
    76 		}
       
    77 	iNotifierCount =0;
       
    78 	//creates a buffer object with buffer length KDnsBufferlength = 1000 this is the max application
       
    79 	//could send stored in the bundle.
       
    80 	iBuffer = HBufC8::NewL(KDnsBufferlength);
       
    81 	//Set the pointer to point buffer.
       
    82 	iBufPointer.Set(iBuffer->Des());
       
    83 	//Create a notifier which will be used to catch all the publish and 
       
    84 	// query response . There will be only one eventobserver for the client.
       
    85 	//Will be destroyed when the client will be closed.
       
    86 	TRAPD(error,iEventNotifier = CMdnsQueryPublishNotifier::NewL(*this));
       
    87 	__FLOG(_L8("RMdns::Open - Exit"));
       
    88 	return error;
       
    89 	}
       
    90 
       
    91 /*
       
    92  * Hanldes any queries either the ptr ,srv txt and address record
       
    93  * IN case of PTR query will be send to the network . 
       
    94  * For SRV and TXT if it exist in the cache ,request will be completed immediately .
       
    95  * @param aQueryParams contains the query bundle.
       
    96  * Leaves with KErrAlreadyExists if a request is pending.
       
    97  */
       
    98 EXPORT_C void RMdns::QueryL( const RPnPParameterBundle& aQueryParams )
       
    99 	{
       
   100 	__FLOG(_L8("RMdns::QueryL - Entry"));
       
   101 	//Get the status of the event notifies . If it is KRequestPending 
       
   102 	//there exists a query which needs to be completed . 
       
   103 	//Leaves with KErrAlreadyExist if there is query which is pending.
       
   104 	TRequestStatus &status =  iEventNotifier->EventNotifierStatus();
       
   105 	if( status.Int() == KRequestPending)
       
   106         {
       
   107         User::Leave(KErrAlreadyExists);
       
   108         }
       
   109 	//Set the buffer pointer to zero and convert bundle in to buffer and store it in buffer.
       
   110 	iBufPointer.Zero();
       
   111 	TInt temp = aQueryParams.Length();
       
   112 	aQueryParams.Store(iBufPointer);
       
   113 	//TODO
       
   114 	//Create a reference to buffer length and send it along the with the request.
       
   115 	//On completion contains the length of the buffer client should create to get the 
       
   116 	//response for the query.
       
   117 	//TPckg<TInt> len(iEventNotifier->BufferLength());
       
   118 	TIpcArgs args(&iBufPointer,&iEventNotifier->BufferLength());
       
   119 	//Set Event notifier active
       
   120 	iEventNotifier->SetEventNotifierActive();
       
   121 	iEventNotifier->SetPnpObserver(aQueryParams.PnPObserver());
       
   122 	//Set the state as query message so that on response it can decide wheteher it is query or publish.
       
   123 	iEventNotifier->SetState(EMdnsServerQueryMessages);
       
   124 	TRAPD(error,SendReceive(EMdnsServerQueryMessages,args, status));
       
   125 	__FLOG(_L8("RMdns::QueryL - Exit"));
       
   126 	}
       
   127 
       
   128 //TODO yet to decide wat to do with this 
       
   129 //as query itself can handle all the requests
       
   130 
       
   131 EXPORT_C void RMdns::ResolveL( const RPnPParameterBundle& aResolveParams )
       
   132 	{
       
   133 	__FLOG(_L8("RMdns::ResolveL - Entry"));
       
   134 	QueryL(aResolveParams);
       
   135 	__FLOG(_L8("RMdns::ResolveL - Exit"));
       
   136 	}
       
   137 	
       
   138 /*
       
   139  * Interferace for the applicaion to register for a particular service it serves
       
   140  * on the network , A copy of this will be kept in the cache .
       
   141  * applicaion dont have to renew the service .Consistency Manager will take care of renewing the service.
       
   142  * @param aPublishParam contains the srv record and the txt record if required need to be published.
       
   143  * 
       
   144  */
       
   145 EXPORT_C void RMdns::RegisterServiceL( const RPnPParameterBundle& aPublishParams ,TBool aIsUpdate)
       
   146 	{
       
   147 	__FLOG(_L8("RMdns::RegisterServiceL - Entry"));
       
   148 	iBufPointer.Zero();
       
   149 	aPublishParams.Store(iBufPointer);
       
   150 	TIpcArgs args(&iBufPointer,&iEventNotifier->BufferLength(),aIsUpdate);
       
   151 	iEventNotifier->SetEventNotifierActive();
       
   152 	iEventNotifier->SetPnpObserver(aPublishParams.PnPObserver());
       
   153 	TRequestStatus &status =  iEventNotifier->EventNotifierStatus();
       
   154 	iEventNotifier->SetState(EMndsServerPublishMessages);
       
   155 	TRAPD(error,SendReceive(EMndsServerPublishMessages,args,status));
       
   156 	User::LeaveIfError(error);
       
   157 	__FLOG(_L8("RMdns::RegisterServiceL - Exit"));
       
   158 	}
       
   159 
       
   160 /*
       
   161  * An Asychronous request which tells the mdns about the service it is interested in .
       
   162  * Internally it calls RecieveNotifyMessageL which is a private function, feasible only to CMDnsregisterNotifier
       
   163  * CMDnsRegisterNotifier is an active object running all the time waiting for any ptr enteries.
       
   164  * @param aNotifyParam contains the ptr type the application is interrested in ;to be notified.
       
   165  */
       
   166 EXPORT_C void RMdns::WaitForNotificationL( const RPnPParameterBundle& aNotifyParams )
       
   167 	{
       
   168 	__FLOG(_L8("RMdns::WaitForNotificationL - Entry"));
       
   169 	if(!iRegisterNotifier)
       
   170         {
       
   171         iRegisterNotifier = CMdnsRegisterNotifier::NewL(*this);
       
   172         }
       
   173 	iRegisterNotifier->SetPnpObserver(aNotifyParams.PnPObserver());
       
   174 	HBufC8* buffer  = HBufC8::NewL(KDnsBufferlength);
       
   175 	TPtr8 aNotifyMessage(NULL,0,0);
       
   176 	aNotifyMessage.Set(buffer->Des());
       
   177 	aNotifyParams.Store(aNotifyMessage);
       
   178 	TIpcArgs args(&aNotifyMessage);
       
   179 	SendReceive(EMdnsServerStartNotifyMessages,args);
       
   180 	iNotifierCount++;
       
   181 	delete buffer;
       
   182 	__FLOG(_L8("RMdns::WaitForNotificationL - Exit"));
       
   183 	}
       
   184 	
       
   185 /*
       
   186  *Interface to cancel any notification client requested for .
       
   187  *@param aCancelParams contains the name of the ptr type client is not interested to be notified.
       
   188  * 
       
   189  */	
       
   190 EXPORT_C TInt RMdns::Cancel( const RPnPParameterBundle& aCancelParams )
       
   191 	{
       
   192 	__FLOG(_L8("RMdns::Cancel - Entry"));
       
   193 	//notifier count keeps track of the number of entries requested for.
       
   194 	iNotifierCount--;
       
   195 	HBufC8* buffer  = HBufC8::NewL(KDnsBufferlength);
       
   196 	TPtr8 aNotifyMessage(NULL,0,0);
       
   197 	aNotifyMessage.Set(buffer->Des());
       
   198 	aCancelParams.Store(aNotifyMessage);
       
   199 	TIpcArgs args(&aNotifyMessage);
       
   200 	SendReceive(EMdnsServerStopNotifyMessages,args);
       
   201 	//when notifier count is zero close the CMdnsRegisterNotifier active object.
       
   202 	//count =0 means the concerned client is not interested in any notification.
       
   203 	if(iNotifierCount == 0)
       
   204 	    {
       
   205 	    iRegisterNotifier->Cancel();
       
   206 	    }
       
   207 	delete buffer;
       
   208 	__FLOG(_L8("RMdns::Cancel - Exit"));
       
   209 	return KErrNone;
       
   210 	}
       
   211 
       
   212 /*
       
   213  * Destructor 
       
   214  */	
       
   215 EXPORT_C TInt RMdns::Close()
       
   216 	{
       
   217 	__FLOG(_L8("RMdns::Close - Entry"));
       
   218 	RSessionBase::Close(); 
       
   219 	delete iEventNotifier;
       
   220 	delete iRegisterNotifier;
       
   221 	delete iBuffer;
       
   222 	__FLOG(_L8("RMdns::Close - Exit"));
       
   223 	__FLOG_CLOSE;
       
   224 	return KErrNone;
       
   225 	}
       
   226 
       
   227 /*
       
   228  * Private function used by the Query and publish notifier .
       
   229  * Based on the size of the buffer recieved by the query and publish request.
       
   230  * a new buffer is allocated and sent to recieve the response.
       
   231  * @param aData contains the buffer allocated to recieve the response.
       
   232  */
       
   233 void RMdns::RecieveMessage(TDes8& aData)
       
   234 	{
       
   235 	__FLOG(_L8("RMdns::RecieveMessage - Entry"));
       
   236 	TIpcArgs args(&aData);
       
   237 	TRAPD(error,SendReceive(EMdnsServerRecieveMessages,args));	
       
   238 	__FLOG(_L8("RMdns::RecieveMessage - Exit"));
       
   239 	}
       
   240 
       
   241 /*
       
   242  * An synchronous request will be running all the time . whenever a waitfornotification been called.
       
   243  * @param aData contains the buffer for which data to be set to .
       
   244  * @param aStatus used to complete the asynchronous request.
       
   245  */
       
   246 void RMdns::RecieveNotifyMessageL(TDes8& aData,TRequestStatus& aStatus)
       
   247     {
       
   248     __FLOG(_L8("RMdns::RecieveNotifyMessageL - Entry"));
       
   249     TIpcArgs args(&aData);
       
   250     SendReceive(EMdnsServerRecieveNotifyMessages,args,aStatus);
       
   251     __FLOG(_L8("RMdns::RecieveNotifyMessageL - Exit"));
       
   252     }