messagingfw/wappushfw/pushwatcher/src/pushwatcher.cpp
changeset 0 8e480a14352b
equal deleted inserted replaced
-1:000000000000 0:8e480a14352b
       
     1 // Copyright (c) 2000-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 <watcher.h>
       
    17 #include <wapmessage.h>
       
    18 
       
    19 #include "pushwatcher.h"
       
    20 #include <push/pushlog.h>
       
    21 #include <push/pushmessage.h>
       
    22 #include "flogger.h"
       
    23 
       
    24 #ifdef __WATCHER_API_V2__
       
    25 
       
    26 #include <ecom/implementationproxy.h>
       
    27 
       
    28 const TImplementationProxy ImplementationTable[] = 
       
    29 	{
       
    30 		IMPLEMENTATION_PROXY_ENTRY(0x10009145, CPushWatcher::NewL)
       
    31 	};
       
    32 
       
    33 EXPORT_C const TImplementationProxy* ImplementationGroupProxy(TInt& aTableCount)
       
    34 	{
       
    35 	aTableCount = sizeof(ImplementationTable) / sizeof(TImplementationProxy);
       
    36 
       
    37 	return ImplementationTable;
       
    38 	}
       
    39 #endif	// __WATCHER_API_V2__
       
    40 
       
    41 #ifdef __WATCHER_API_V2__
       
    42 CPushWatcher* CPushWatcher::NewL(TAny* aWatcherParams)
       
    43 #else
       
    44 /** single exported constructor to allow creation of push watcher
       
    45 	which in turn creates Connection Manager and waits for a push
       
    46 	message to arrive. 
       
    47 	@param RF
       
    48 	@param CWatcherLog
       
    49 	@return pointer to created object
       
    50 */
       
    51 EXPORT_C CPushWatcher* CPushWatcher::NewL(RFs&, CWatcherLog& aLog)
       
    52 #endif	// __WATCHER_API_V2__
       
    53 	{
       
    54 #ifdef __WATCHER_API_V2__
       
    55 	TWatcherParams* params = reinterpret_cast<TWatcherParams*>(aWatcherParams);
       
    56 	CPushWatcher* self= new (ELeave) CPushWatcher(CActive::EPriorityStandard, params->iLog);
       
    57 #else
       
    58 	CPushWatcher* self = new(ELeave) CPushWatcher(CActive::EPriorityStandard, aLog);
       
    59 #endif	// __WATCHER_API_V2__
       
    60 	CleanupStack::PushL(self);
       
    61 	self->ConstructL();
       
    62 	CleanupStack::Pop();
       
    63 	return self;
       
    64 	}
       
    65 
       
    66 /** private constructor */
       
    67 CPushWatcher::CPushWatcher(TInt aPriority, CWatcherLog& aLog)
       
    68 : CActive(aPriority), iLog(aLog)
       
    69 	{
       
    70 	}
       
    71 
       
    72 /** destructor to cancel any outstanding requests */
       
    73 CPushWatcher::~CPushWatcher()
       
    74 	{
       
    75 	Cancel();
       
    76 	}
       
    77 
       
    78 /** start scheduler and create connection manager, then wait */
       
    79 void CPushWatcher::ConstructL()
       
    80 	{
       
    81 	CActiveScheduler::Add(this);
       
    82 	iLog.Printf(_L("PushWatcher: Constructing..."));
       
    83 	iConnMan = CConnectionManager::NewL(*this);
       
    84 	}
       
    85 
       
    86 /** watcher starts connection manager and just idles, no other states or
       
    87 	activities so RunL is empty.
       
    88 
       
    89 	The Connection Manager processes any push messages. */
       
    90 void CPushWatcher::RunL()
       
    91 	{
       
    92 	// nothing to do
       
    93 	}
       
    94 
       
    95 /** no active RunL so no Cancel required */
       
    96 void CPushWatcher::DoCancel()
       
    97 	{
       
    98 	// nothing to do
       
    99 	}
       
   100 
       
   101 
       
   102 
       
   103 
       
   104 
       
   105 
       
   106 /** log debug information tp c:\logs\watcher directory if present
       
   107 	after formatting message to print.
       
   108 	@param list of args
       
   109 	@return void 
       
   110 */
       
   111 void CPushWatcher::WPLPrintf(const TDesC& aDescription)
       
   112 	{
       
   113 	_LIT(KPushLogFmt,"Push:%S");
       
   114 
       
   115 	// 7 is the length of "Push:" from above + '\n' tagged on in the Printf()
       
   116 	TPtrC	buf = aDescription.Left(Min(KWatcherLogBuffer-7, aDescription.Length()));
       
   117 	iLog.Printf(KPushLogFmt, &buf);
       
   118 	}
       
   119 
       
   120 	
       
   121 /**
       
   122  * Takes the data in a Push Message and prints it to console and the logs it to file.
       
   123  * Uses the accessor functions of the CPushMessage class and this classes Printf function 
       
   124  * to perform this.  Currently 4 headers and the message body are displayed and 
       
   125  * logged. The headers are:
       
   126  *		PushID, Content-Type, X-Application-ID, Expiry Date, 
       
   127  *
       
   128  *	@param	CPushMessage& aMessage
       
   129  *				in:  a reference to a Push Message.
       
   130  */
       
   131 void CPushWatcher::WPLPrintf(CPushMessage& aMessage)
       
   132 	{
       
   133 	TRAPD(error, WPLPrintfL(aMessage));
       
   134 	if (error != KErrNone)
       
   135 		{
       
   136 		_LIT(KErrMessage, "Wap Push Watcher logging failed");
       
   137 		WPLPrintf(KErrMessage);
       
   138 		}
       
   139 	}
       
   140 
       
   141 /**
       
   142  * Private WPLPrint leaving method
       
   143  */
       
   144 void CPushWatcher::WPLPrintfL(CPushMessage& aMessage)
       
   145 	{
       
   146 	TBuf<KWatcherLogBuffer> buf;
       
   147 	
       
   148 	// Content type
       
   149 	_LIT(KLogContentFmt,"Content Type : \t%S");
       
   150 	TPtrC contentPointer;
       
   151 	aMessage.GetContentType(contentPointer);
       
   152 	// max length, including the post-append '\n'
       
   153 	TInt maxSize(KWatcherLogBuffer - (KLogContentFmt().Length()+1));
       
   154 	TPtrC tmp(contentPointer.Left(maxSize));
       
   155 	buf.Format(KLogContentFmt,&tmp);
       
   156 	buf.Append('\n');
       
   157 	WPLPrintf(buf);
       
   158 
       
   159 	// Date Field
       
   160 	_LIT(KLogDateTimeFmt," %-B%:0%J%:1%T%:2%S%+B   %D %N %Y  %4 %5 %3");
       
   161 	_LIT(KLogDateFmt,"Date           :\tS");
       
   162 	TBool foundField;
       
   163 	TTime timeValue;
       
   164 	foundField = aMessage.GetHeaderField(EHttpDate, timeValue);
       
   165 	if (foundField)
       
   166 		{
       
   167 		timeValue.FormatL(buf, KLogDateTimeFmt);
       
   168 		buf.Format(KLogDateFmt, &buf);
       
   169 		WPLPrintf(buf);
       
   170 		}
       
   171 
       
   172 	//Expires Field
       
   173 	foundField = aMessage.GetHeaderField(EHttpExpires, timeValue);
       
   174 	if (foundField)
       
   175 		{
       
   176 		timeValue.FormatL(buf, KLogDateTimeFmt);
       
   177 		buf.Format(KLogDateFmt, &buf);
       
   178 		WPLPrintf(buf);
       
   179 		}
       
   180 
       
   181 	// X-Wap-Application-ID
       
   182 	TBool isInt;
       
   183 	TInt ID;
       
   184 	TPtrC8 generalPtr;
       
   185 	_LIT(KLogAppIdIntFmt,"App ID         :\t%X");
       
   186 	_LIT(KLogAppIdStringFmt,"App ID         :\t%S");
       
   187 	foundField = aMessage.GetAppIdL(generalPtr, ID, isInt);
       
   188 	if (foundField) // App ID is present
       
   189 		{
       
   190 		if (isInt)  // Field in integer format
       
   191 			{
       
   192 			buf.Format(KLogAppIdIntFmt,ID);
       
   193 			}
       
   194 		else  // should be descriptor format
       
   195 			{
       
   196 			buf.Format(KLogAppIdStringFmt,&generalPtr);
       
   197 			}
       
   198 		WPLPrintf(buf);
       
   199 		}
       
   200 
       
   201 	//Message Header Binary
       
   202 	_LIT(KLogMsgHdr,"Header Binary:");
       
   203 	WPLPrintf(KLogMsgHdr);
       
   204 	aMessage.GetHeader(generalPtr);
       
   205 	
       
   206 	HBufC* tempHdr = HBufC::NewLC(generalPtr.Length());
       
   207 	tempHdr->Des().Copy(generalPtr);
       
   208 	WPLLogBinaryAsHex(*tempHdr);
       
   209 	CleanupStack::PopAndDestroy(); //tempHdr
       
   210 
       
   211 	//Message Body
       
   212 	aMessage.GetMessageBody(generalPtr);
       
   213 
       
   214 	// Dump Body As Text
       
   215 	_LIT(KLogMsgBody,"Body Text:");
       
   216 	WPLPrintf(KLogMsgBody);
       
   217 	HBufC* tempBody = HBufC::NewLC(generalPtr.Length());
       
   218 	tempBody->Des().Copy(generalPtr);
       
   219 	WPLPrintf(*tempBody);
       
   220 
       
   221 	// Dump Body As Hex
       
   222 	_LIT(KBodyBinary,"Body Binary:");
       
   223 	WPLPrintf(KBodyBinary);
       
   224 	WPLLogBinaryAsHex(*tempBody);
       
   225 
       
   226 	CleanupStack::PopAndDestroy(); //tempBody
       
   227 	}
       
   228 
       
   229  /**
       
   230  * Prints out Buffer data in the format:
       
   231  *	%X %X %X %X %X %X %X\n etc 
       
   232  * For example
       
   233  *	AB CD 01 12 34 A2 
       
   234  *
       
   235  *	@param	aDescription
       
   236  *				in:  the descriptor to be dumped into the log
       
   237  */
       
   238 void CPushWatcher::WPLLogBinaryAsHex(const TDesC& aDescription)
       
   239 	{
       
   240 	_LIT(KHexSpace,"%02X ");
       
   241 	TBuf<KWatcherLogBuffer> hexBuf;
       
   242 	TBuf<KWatcherLogBuffer> buf;
       
   243 
       
   244 	TInt i = 0, bodyLen = aDescription.Length();
       
   245 
       
   246 	for (; i < bodyLen; i++)
       
   247 		{
       
   248 		hexBuf.Format(KHexSpace,aDescription[i]);
       
   249 		buf.Append(hexBuf);
       
   250 		if ( i && ((i+1) % 8) == 0  )
       
   251 			{
       
   252 			WPLPrintf(buf);
       
   253 			buf.Zero();
       
   254 			}
       
   255 		}
       
   256 
       
   257 	if (buf.Length())
       
   258 		WPLPrintf(buf);
       
   259 	}
       
   260 
       
   261 
       
   262 
       
   263 /** log error description to debug file */
       
   264 void CPushWatcher::WPLLogError(const TDesC& aDescription,TInt aError)
       
   265 	{
       
   266 	_LIT(KErrorLogFmt,"Push:%S, Error\t%d");
       
   267 	const TInt KWatcherLogBuffer=256;
       
   268 	TBuf<KWatcherLogBuffer> buf;
       
   269 	buf.Format(KErrorLogFmt,&aDescription,aError);
       
   270   	buf.Append('\n');
       
   271 	WPLPrintf(buf);
       
   272 	}
       
   273