PECengine/TransferAdapter2/Src/PEngPacketLogger.h
changeset 0 094583676ce7
equal deleted inserted replaced
-1:000000000000 0:094583676ce7
       
     1 /*
       
     2 * Copyright (c) 2003 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: this class writes the given data fragemnt to xml file.
       
    15 *
       
    16 */
       
    17 
       
    18 #ifndef __PENGPACKETLOGGER_H__
       
    19 #define __PENGPACKETLOGGER_H__
       
    20 
       
    21 //  INCLUDES
       
    22 #include    "PresenceDebugPrint.h"
       
    23 #include    <F32File.h>
       
    24 
       
    25 
       
    26 // CONSTS
       
    27 _LIT( KLogsFolder, "C:\\logs\\peng\\" ); // CSI: 25 #
       
    28 _LIT( KLogsOut, "_Out_" );
       
    29 _LIT( KLogsIn, "_In_" );
       
    30 _LIT( KLogsNotify, "_Notf_" );
       
    31 _LIT( KLogsSuffix, ".xml" );
       
    32 _LIT( KUnderLine, "_" );
       
    33 _LIT( KNotifFileName, "_Notf_*.xml" );
       
    34 _LIT( KAllFilesMask, "*.*" );
       
    35 
       
    36 // for logging
       
    37 _LIT8( KLog_WVEngineError, "<WVEngineErrorCode>%d</WVEngineErrorCode>" );
       
    38 _LIT8( KLog_PecOpCode, "<PEC><OpCode>%d</OpCode>" );
       
    39 _LIT8( KLog_WVEngOpCode, "<WVEngineOpCode>%d</WVEngineOpCode>" );
       
    40 _LIT8( KLog_ConnectionId, "<ConnectionId>%d</ConnectionId>" );
       
    41 _LIT8( KLog_TransTime, "<TransactionTime>%d:%d:%d:%d</TransactionTime>" );
       
    42 _LIT8( KLog_Tag_UserName, "<UserName>" );
       
    43 _LIT8( KLog_Tag_End_UserName, "</UserName>" );
       
    44 _LIT8( KLog_Tag_ServAddress, "<ServerAddress>" );
       
    45 _LIT8( KLog_Tag_End_ServAddress, "</ServerAddress>" );
       
    46 _LIT8( KLog_Tag_End_Pec, "</PEC>" );
       
    47 
       
    48 
       
    49 const TInt KConnIdLength = 2;
       
    50 const TInt KOpIdLength =  4;
       
    51 const TInt KNotifyIdLength = 3;
       
    52 const TInt KErrorCodeLength = 60;
       
    53 const TInt KLogsFileNameLength = 50;
       
    54 
       
    55 
       
    56 // CLASS DECLARATION
       
    57 /**
       
    58  *  Writes the given data fragemnt to xml file.
       
    59  *
       
    60  *  @lib PEngTraAdap.lib
       
    61  *  @since 2.1
       
    62  */
       
    63 class PEngPacketLogger
       
    64     {
       
    65     public:  // Logging functions
       
    66 
       
    67         /**
       
    68          * Writes a send packet.
       
    69          *
       
    70          * @param aData The buffer.
       
    71          * @param aOpCode The Operation id.
       
    72          *
       
    73          * @since 3.0
       
    74          * @return returns connection id, if it was gained again
       
    75          */
       
    76         inline static TInt WriteOutPacket( const TDesC8& aData,
       
    77                                            TInt aOpCode,
       
    78                                            TInt aWVEngOpCode,
       
    79                                            TInt aConnectionId,
       
    80                                            const TDesC& aUserName,
       
    81                                            const TDesC& aUrl )
       
    82             {
       
    83             return DoWrite( aData,
       
    84                             aOpCode,
       
    85                             aWVEngOpCode,
       
    86                             KLogsOut,
       
    87                             aConnectionId,
       
    88                             aUserName,
       
    89                             aUrl );
       
    90             }
       
    91 
       
    92 
       
    93         /**
       
    94          * Writes a received packet.
       
    95          *
       
    96          * @param aData The buffer.
       
    97          * @param aOpCode The Operation id.
       
    98          *
       
    99          * @since 3.0
       
   100          */
       
   101         inline static TInt WriteInPacket( const TDesC8& aData,
       
   102                                           TInt aOpCode,
       
   103                                           TInt aWVEngOpCode,
       
   104                                           TInt aConnectionId,
       
   105                                           const TDesC& aUserName,
       
   106                                           const TDesC& aUrl )
       
   107             {
       
   108             return DoWrite( aData,
       
   109                             aOpCode,
       
   110                             aWVEngOpCode,
       
   111                             KLogsIn,
       
   112                             aConnectionId,
       
   113                             aUserName,
       
   114                             aUrl );
       
   115             }
       
   116 
       
   117 
       
   118         /**
       
   119          * Writes a notification packet.
       
   120          *
       
   121          * @param aData The buffer.
       
   122          * @param aOpCode The Operation id.
       
   123          *
       
   124          * @since 3.0
       
   125          */
       
   126         inline static TInt WriteNotifyPacket( const TDesC8& aData,
       
   127                                               TInt aOpCode,
       
   128                                               TInt aConnectionId,
       
   129                                               const TDesC& aUserName,
       
   130                                               const TDesC& aUrl )
       
   131             {
       
   132             return DoWriteNotification( aData,
       
   133                                         aOpCode,
       
   134                                         KLogsNotify,
       
   135                                         aConnectionId,
       
   136                                         aUserName,
       
   137                                         aUrl );
       
   138             }
       
   139 
       
   140         inline static TInt WriteError( TInt aErrorCode,
       
   141                                        TInt aOpCode,
       
   142                                        TInt aWVEngOpCode,
       
   143                                        TInt aConnectionId,
       
   144                                        const TDesC& aUserName,
       
   145                                        const TDesC& aUrl )
       
   146             {
       
   147             TBuf8<KErrorCodeLength> buf;
       
   148             buf.Format( KLog_WVEngineError, aErrorCode );
       
   149             return DoWrite( buf,
       
   150                             aOpCode,
       
   151                             aWVEngOpCode,
       
   152                             KLogsIn,
       
   153                             aConnectionId,
       
   154                             aUserName,
       
   155                             aUrl );
       
   156             }
       
   157 
       
   158         /**
       
   159          * Get connection id
       
   160          *
       
   161          * @since 3.0
       
   162          * @return connection id
       
   163          */
       
   164         inline static TInt GetConnectionId()
       
   165             {
       
   166             return ResolveConnectionId();
       
   167             }
       
   168 
       
   169     private:  // Implementation
       
   170 
       
   171         /**
       
   172          *
       
   173          */
       
   174         inline static TInt ConnectFs( RFs& aFs )
       
   175             {
       
   176             if ( aFs.Connect() != KErrNone )
       
   177                 {
       
   178                 return KErrNotFound;
       
   179                 }
       
   180 
       
   181             TEntry entry;
       
   182             if ( aFs.Entry( KLogsFolder, entry ) != KErrNone )
       
   183                 {
       
   184                 //directory doesn't exist
       
   185                 aFs.Close();
       
   186                 return KErrNotFound;
       
   187                 }
       
   188             return KErrNone;
       
   189             }
       
   190 
       
   191         /**
       
   192          *
       
   193          */
       
   194         inline static void AppendNum( TDes& aDes, TInt aValue, TInt aDigits )
       
   195             {
       
   196             TInt marker( aValue );
       
   197             TBuf<5> value;
       
   198             for ( TInt x( 0 ) ; x < aDigits  ; ++x )
       
   199                 {
       
   200                 TBuf<1> oneDig;
       
   201                 TInt numVal( marker % 10 );
       
   202                 marker /= 10;
       
   203                 oneDig.Num( numVal );
       
   204                 value.Insert( 0, oneDig );
       
   205                 }
       
   206             aDes.Append( value );
       
   207             }
       
   208 
       
   209         inline static TInt WriteToFile(
       
   210             RFs& aFs,
       
   211             const TDesC& aFileName,
       
   212             const TDesC8& aData,
       
   213             TInt aOpCode,
       
   214             TInt aWVEngOpCode,
       
   215             TInt aConnectionId,
       
   216             const TDesC& aUserName,
       
   217             const TDesC& aUrl )
       
   218             {
       
   219             PENG_DP( D_PENG_LIT( "PEC:TransAdap Log File: " ), &aFileName );
       
   220             RFile f;
       
   221             if ( f.Create( aFs, aFileName, EFileWrite ) != KErrNone )
       
   222                 {
       
   223                 return aConnectionId;
       
   224                 }
       
   225             TBuf8<0x300> buf;
       
   226             buf.Format( KLog_PecOpCode, aOpCode );
       
   227             buf.AppendFormat( KLog_WVEngOpCode, aWVEngOpCode );
       
   228             buf.AppendFormat( KLog_ConnectionId, aConnectionId );
       
   229             // write time
       
   230             TTime time;
       
   231             time.HomeTime();
       
   232             TDateTime dayTime( time.DateTime() );
       
   233             buf.AppendFormat( KLog_TransTime,
       
   234                               dayTime.Hour(),
       
   235                               dayTime.Minute(),
       
   236                               dayTime.Second(),
       
   237                               dayTime.MicroSecond() );
       
   238             buf.Append( KLog_Tag_UserName );
       
   239             buf.Append( aUserName );
       
   240             buf.Append( KLog_Tag_End_UserName );
       
   241             buf.Append( KLog_Tag_ServAddress );
       
   242             buf.Append( aUrl );
       
   243             buf.Append( KLog_Tag_End_ServAddress );
       
   244 
       
   245             f.Write( buf );
       
   246             f.Write( aData );
       
   247             buf.Copy( KLog_Tag_End_Pec );
       
   248             f.Write( buf );
       
   249             f.Close();
       
   250             return aConnectionId;
       
   251             }
       
   252         /**
       
   253          * Writes packet.
       
   254          * @since 3.0
       
   255          */
       
   256         inline static TInt DoWrite( const TDesC8& aData,
       
   257                                     TInt aOpCode,
       
   258                                     TInt aWVEngOpCode,
       
   259                                     const TDesC& aFileNameBase,
       
   260                                     TInt aConnectionId,
       
   261                                     const TDesC& aUserName,
       
   262                                     const TDesC& aUrl )
       
   263             {
       
   264             RFs fs;
       
   265             TInt err( ConnectFs( fs ) );
       
   266             if ( err != KErrNone )
       
   267                 {
       
   268                 return err;
       
   269                 }
       
   270 
       
   271             //locate free file name
       
   272             TInt connectionId( aConnectionId );
       
   273             if ( connectionId == KErrNotFound )
       
   274                 {
       
   275                 connectionId = ResolveConnectionId();
       
   276                 }
       
   277             if ( connectionId < 0 )
       
   278                 {
       
   279                 return connectionId;
       
   280                 }
       
   281 
       
   282             // test file name freelance
       
   283             TBuf<KLogsFileNameLength> fileName;
       
   284             fileName.Copy( KLogsFolder );
       
   285             AppendNum( fileName, connectionId, KConnIdLength  );
       
   286             fileName.Append( aFileNameBase );
       
   287             AppendNum( fileName, aOpCode, KOpIdLength );
       
   288             fileName.Append( KLogsSuffix );
       
   289             TEntry entry;
       
   290             if ( fs.Entry( fileName, entry ) == KErrNone )
       
   291                 {
       
   292                 fs.Delete( fileName );
       
   293                 }
       
   294             err = WriteToFile( fs,
       
   295                                fileName,
       
   296                                aData,
       
   297                                aOpCode,
       
   298                                aWVEngOpCode,
       
   299                                connectionId,
       
   300                                aUserName,
       
   301                                aUrl );
       
   302             fs.Close();
       
   303             return err;
       
   304             }
       
   305 
       
   306         /**
       
   307          * Writes packet.
       
   308          * @since 3.0
       
   309          */
       
   310         inline static TInt DoWriteNotification( const TDesC8& aData,
       
   311                                                 TInt aOpCode,
       
   312                                                 const TDesC& aFileNameBase,
       
   313                                                 TInt aConnectionId,
       
   314                                                 const TDesC& aUserName,
       
   315                                                 const TDesC& aUrl )
       
   316             {
       
   317             RFs fs;
       
   318             TInt err( ConnectFs( fs ) );
       
   319             if ( err != KErrNone )
       
   320                 {
       
   321                 return err;
       
   322                 }
       
   323 
       
   324             //locate free file name
       
   325             TInt connectionId( aConnectionId );
       
   326             if ( connectionId == KErrNotFound )
       
   327                 {
       
   328                 connectionId = ResolveConnectionId();
       
   329                 }
       
   330             if ( connectionId < 0 )
       
   331                 {
       
   332                 return connectionId;
       
   333                 }
       
   334 
       
   335             //locate free file name
       
   336             CDir* dirList = NULL;
       
   337             TBuf<15> wildCard;
       
   338             AppendNum( wildCard, connectionId, KConnIdLength  );
       
   339             wildCard.Append( KNotifFileName );
       
   340             TInt counter( 0 );
       
   341             TFindFile ff( fs );
       
   342             if ( ff.FindWildByDir( wildCard, KLogsFolder, dirList ) == KErrNone )
       
   343                 {
       
   344                 TInt count( dirList->Count() );
       
   345                 for ( TInt x ( 0 ) ; x < count ; ++x )
       
   346                     {
       
   347                     if ( ( *dirList )[x].IsDir() )
       
   348                         {
       
   349                         continue;
       
   350                         }
       
   351                     TLex check;
       
   352                     check.Assign( ( *dirList )[x].iName.Mid( 8, 3 ) );
       
   353                     TInt foundCount( KErrNotFound );
       
   354                     if ( ( KErrNone == check.Val( foundCount ) ) && ( foundCount >= counter ) )
       
   355                         {
       
   356                         counter = foundCount + 1;
       
   357                         }
       
   358                     }
       
   359                 }
       
   360             delete dirList;
       
   361 
       
   362             TBuf<KLogsFileNameLength> fileName;
       
   363             fileName.Append( KLogsFolder );
       
   364             AppendNum( fileName, connectionId, KConnIdLength  );
       
   365             fileName.Append( aFileNameBase );
       
   366             AppendNum( fileName, counter, KNotifyIdLength );
       
   367             fileName.Append( KUnderLine );
       
   368             AppendNum( fileName, aOpCode, KOpIdLength );
       
   369             fileName.Append( KLogsSuffix );
       
   370 
       
   371             err = WriteToFile( fs,
       
   372                                fileName,
       
   373                                aData,
       
   374                                0,
       
   375                                0,
       
   376                                connectionId,
       
   377                                aUserName,
       
   378                                aUrl );
       
   379             fs.Close();
       
   380             return err;
       
   381             }
       
   382 
       
   383         inline static TInt ResolveConnectionId()
       
   384             {
       
   385             RFs fs;
       
   386             TInt err( ConnectFs( fs ) );
       
   387             if ( err != KErrNone )
       
   388                 {
       
   389                 return err;
       
   390                 }
       
   391 
       
   392             //locate free connection Id
       
   393             TFindFile ff( fs );
       
   394             CDir* dirList = NULL;
       
   395             TInt connectionId( 0 );
       
   396             if ( ff.FindWildByDir( KAllFilesMask, KLogsFolder, dirList ) == KErrNone )
       
   397                 {
       
   398                 TInt count( dirList->Count() );
       
   399                 for ( TInt x ( 0 ) ; x < count ; ++x )
       
   400                     {
       
   401                     if ( ( *dirList )[x].IsDir() )
       
   402                         {
       
   403                         continue;
       
   404                         }
       
   405                     TLex check;
       
   406                     TInt foundId( KErrNotFound );
       
   407                     TPtrC fileName( ( *dirList )[x].iName );
       
   408                     if (
       
   409                         (
       
   410                             ( KErrNotFound != fileName.FindF( KLogsIn ) )
       
   411                             &&
       
   412                             ( ( *dirList )[x].iName.Length() > 6 )
       
   413                         )
       
   414                         ||
       
   415                         (
       
   416                             ( KErrNotFound != fileName.FindF( KLogsOut ) )
       
   417                             &&
       
   418                             ( ( *dirList )[x].iName.Length() > 7 )
       
   419                         )
       
   420                         ||
       
   421                         (
       
   422                             ( KErrNotFound != fileName.FindF( KLogsNotify ) )
       
   423                             &&
       
   424                             ( ( *dirList )[x].iName.Length() > 10 )
       
   425                         )
       
   426                     )
       
   427                         {
       
   428                         check.Assign( fileName.Left( 2 ) );
       
   429                         check.Val( foundId );
       
   430                         }
       
   431                     if ( foundId >= connectionId )
       
   432                         {
       
   433                         connectionId = foundId + 1;
       
   434                         }
       
   435                     }
       
   436                 fs.Close();
       
   437                 delete dirList;
       
   438                 return connectionId;
       
   439                 }
       
   440             fs.Close();
       
   441             delete dirList;
       
   442             return connectionId;
       
   443             }
       
   444     };
       
   445 #endif      // __PENGPACKETLOGGER_H__
       
   446 
       
   447