webservices/wsxml/src/senfilterstatemachine.cpp
changeset 0 62f9d29f7211
equal deleted inserted replaced
-1:000000000000 0:62f9d29f7211
       
     1 /*
       
     2 * Copyright (c) 2005 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 
       
    20 
       
    21 
       
    22 
       
    23 
       
    24 
       
    25 #include "SenFilterCondition.h"
       
    26 
       
    27 #include "SenFilterStateMachine.h"
       
    28 
       
    29 #include "SenDebug.h"
       
    30 #include "SenLogger.h"
       
    31 
       
    32 
       
    33 _LIT(KLogFileDir, "WsLog");
       
    34 _LIT(KLogFileName, "SenXml.log");
       
    35 
       
    36 // ----------------------------------------------
       
    37 //
       
    38 // Implementation of class CSenFilterStateMachine
       
    39 //
       
    40 // ----------------------------------------------
       
    41 
       
    42 
       
    43 EXPORT_C CSenFilterStateMachine::CSenFilterStateMachine()
       
    44 : iStateTable(ETrue, ETrue)
       
    45 {
       
    46     TLSLOG_OPEN(KSenXmlLogChannel, KSenXmlLogLevel, KSenXml, KSenXmlLog);
       
    47     TLSLOG(KSenXmlLogChannel  , KSenXmlLogLevel , _L8("CSenFilterStateMachine::CSenFilterStateMachine(): Log opened"));
       
    48 
       
    49     iStates = 0;
       
    50     iFilterState = 0;
       
    51 
       
    52 		TLSLOG(KSenXmlLogChannel  , KSenXmlLogLevel ,  _L8("CSenFilterStateMachine::CSenFilterStateMachine(): End"));
       
    53 } 
       
    54 
       
    55 
       
    56 EXPORT_C CSenFilterStateMachine::~CSenFilterStateMachine()
       
    57 {
       
    58     iStateTable.Reset();
       
    59 
       
    60     // Close the log file and the connection to the server.
       
    61   	TLSLOG(KSenXmlLogChannel  , KSenXmlLogLevel ,  (_L("Log file closed.")));
       
    62 		TLSLOG_CLOSE(KSenXmlLogChannel);
       
    63 }
       
    64 
       
    65 
       
    66 /* Add a node to the state tree */
       
    67 EXPORT_C CSenFilterStateNode* 
       
    68 CSenFilterStateMachine::AddChildLC(CSenFilterStateNode& aStartState,
       
    69                                  const TDesC8& aEvent, 
       
    70                                  const CSenFilterCondition& aCondition,
       
    71                                  MSenFilterAction& aStartAction,
       
    72                                  MSenFilterAction& aEndAction)
       
    73 {
       
    74     CSenFilterStateNode* newNode = new CSenFilterStateNode(aEvent, 
       
    75                                                            aCondition, 
       
    76                                                            aStartAction, 
       
    77                                                            aEndAction, 
       
    78                                                            *this);
       
    79 
       
    80     CleanupStack::PushL(newNode);
       
    81     newNode->SetParent(aStartState);
       
    82 
       
    83     // Array takes care of memory deallocation
       
    84     aStartState.Children().Append(newNode);
       
    85 
       
    86     return newNode;
       
    87 }
       
    88 
       
    89 
       
    90 /* Add the root node to the state tree */
       
    91 EXPORT_C CSenFilterStateNode* 
       
    92 CSenFilterStateMachine::AddParentLC(const TDesC8& aEvent, 
       
    93                                  const CSenFilterCondition& aCondition,
       
    94                                  MSenFilterAction& aStartAction,
       
    95                                  MSenFilterAction& aEndAction)
       
    96 {
       
    97     CSenFilterStateNode* newNode = new CSenFilterStateNode(aEvent,
       
    98                                                            aCondition, 
       
    99                                                            aStartAction, 
       
   100                                                            aEndAction, 
       
   101                                                            *this);
       
   102     CleanupStack::PushL(newNode);
       
   103     return newNode;
       
   104 }
       
   105 
       
   106 
       
   107 /* Recursively compile the tree into a state table */
       
   108 EXPORT_C void CSenFilterStateMachine::CompileL(CSenFilterStateNode& aStartNode)
       
   109 {
       
   110     TInt p = 0; // initial state
       
   111 
       
   112     p = aStartNode.Parent().State();
       
   113 
       
   114     AddEntryL(p, 
       
   115              aStartNode.Event(), 
       
   116              aStartNode.Condition(), 
       
   117              aStartNode.State(), 
       
   118              aStartNode.StartAction(), 
       
   119              aStartNode.EndAction());
       
   120 
       
   121     TInt count = aStartNode.Children().Count();
       
   122     for ( TInt i=0; i < count; i++ )
       
   123     {
       
   124         CSenFilterStateNode* node = (aStartNode.Children())[i];
       
   125         CompileL(*node);
       
   126     }
       
   127 }
       
   128 
       
   129 
       
   130 EXPORT_C void CSenFilterStateMachine::StartElementL(const TDesC8& aName, 
       
   131                                                     const RAttributeArray& aAttributes)
       
   132 {
       
   133     TransitionL(iFilterState, aName, (TInt)EStart, aAttributes);
       
   134 }
       
   135 
       
   136 
       
   137 EXPORT_C void CSenFilterStateMachine::EndElementL(const TDesC8& aName)
       
   138 {
       
   139     RAttributeArray attrs;
       
   140     TransitionL(iFilterState, aName, (TInt)EEnd, attrs);
       
   141 }
       
   142 
       
   143 
       
   144 
       
   145 const TInt CSenFilterStateMachine::RegisterState()
       
   146 {
       
   147     return ++iStates;
       
   148 }
       
   149 
       
   150 
       
   151 const TInt CSenFilterStateMachine::States() const
       
   152 {
       
   153     return iStates;
       
   154 }
       
   155 
       
   156 
       
   157 void CSenFilterStateMachine::AddEntryL(TInt aInitialState, 
       
   158                                       const TDesC8& aEvent, 
       
   159                                       const CSenFilterCondition& aCondition, 
       
   160                                       TInt aNextState, 
       
   161                                       MSenFilterAction& aStartAction, 
       
   162                                       MSenFilterAction& aEndAction)
       
   163 {
       
   164     CSenFilterTableEntry *entry = NULL;
       
   165     TUint16 c;
       
   166 
       
   167 
       
   168     // Start node
       
   169     c = GetStateKeyL(aInitialState, 
       
   170                     aEvent, 
       
   171                     (TInt)EStart);
       
   172 
       
   173     entry = new CSenFilterTableEntry(aNextState, 
       
   174                                      aCondition, 
       
   175                                      aStartAction);
       
   176     CleanupStack::PushL(entry);
       
   177 
       
   178     // Map takes care of memory deallocation
       
   179     iStateTable.Append(&c, entry);
       
   180 
       
   181     CleanupStack::Pop(); // entry
       
   182 
       
   183     // End node
       
   184     c = GetStateKeyL(aNextState, 
       
   185                     aEvent, 
       
   186                     (TInt)EEnd);
       
   187 
       
   188     entry = new CSenFilterTableEntry(aInitialState, 
       
   189                                      aCondition, 
       
   190                                      aEndAction);
       
   191     CleanupStack::PushL(entry);
       
   192 
       
   193     // Map takes care of memory deallocation
       
   194     iStateTable.Append(&c, entry);
       
   195 
       
   196     CleanupStack::Pop(); // entry
       
   197 
       
   198 }
       
   199 
       
   200 
       
   201 // Create an unique key for state table node
       
   202 const TUint16 CSenFilterStateMachine::GetStateKeyL(TInt aState, 
       
   203                                                   const TDesC8& aEvent, 
       
   204                                                   TInt aStart)
       
   205 {
       
   206     TUint16 ret = 0;
       
   207     TInt intMaxLen = 32;
       
   208 
       
   209     TInt bufLen = aEvent.Size() + (2 * intMaxLen);
       
   210     HBufC8* buf = HBufC8::NewLC(bufLen);
       
   211     TPtr8 bufPtr = (buf->Des());
       
   212     bufPtr.Append(aEvent);
       
   213     bufPtr.AppendNum(aState);
       
   214     bufPtr.AppendNum(aStart);
       
   215 
       
   216     Mem::Crc(ret, (TAny *)bufPtr.Ptr(), bufPtr.Size());
       
   217 
       
   218     CleanupStack::PopAndDestroy(); // buf
       
   219 
       
   220     return(ret);
       
   221 }
       
   222 
       
   223 
       
   224 const TInt CSenFilterStateMachine::TransitionL(TInt aState, 
       
   225                                               const TDesC8& aEvent, 
       
   226                                               TInt start, 
       
   227                                               const RAttributeArray& aAttributes)
       
   228 {
       
   229 
       
   230     TUint16 c = GetStateKeyL(aState, aEvent, start);
       
   231     TInt i = iStateTable.Find(c);
       
   232     if ( KErrNotFound != i ) {
       
   233         const CSenFilterTableEntry* entry = iStateTable.ValueAt(i);
       
   234         if ( entry != NULL )
       
   235         {
       
   236             // Evaluate conditions
       
   237             if ( !entry->Condition().Test() )
       
   238             {
       
   239                 return iFilterState;
       
   240             }
       
   241             //Perform action
       
   242             entry->Action().PerformL(aEvent, aAttributes);
       
   243     
       
   244             // Make the state transition
       
   245             iFilterState = entry->NewState();
       
   246         }
       
   247     }
       
   248     return iFilterState;
       
   249 }
       
   250 
       
   251 
       
   252 RFileLogger* CSenFilterStateMachine::Log() const
       
   253     {
       
   254     return (RFileLogger*) &iLog;
       
   255     }
       
   256 
       
   257 
       
   258 // -------------------------------------
       
   259 //
       
   260 // Implementation of class CSenFilterStateNode
       
   261 //
       
   262 // -------------------------------------
       
   263 
       
   264 
       
   265 EXPORT_C CSenFilterStateNode::CSenFilterStateNode(const TDesC8& aEvent, 
       
   266                                                   const CSenFilterCondition& aCondition, 
       
   267                                                   MSenFilterAction& aStartAction, 
       
   268                                                   MSenFilterAction& aEndAction,
       
   269                                                   CSenFilterStateMachine& aStateMachine)
       
   270 : ipPrev(NULL),
       
   271   iEvent(aEvent),
       
   272   iCondition(aCondition),
       
   273   iStartAction(aStartAction),
       
   274   iEndAction(aEndAction)
       
   275 {
       
   276     TLSLOG_OPEN(KSenXmlLogChannel, KSenXmlLogLevel, KSenXml, KSenXmlLog);
       
   277     TLSLOG(KSenXmlLogChannel  , KSenXmlLogLevel , _L8("CSenFilterStateNode::CSenFilterStateNode(): Log opened"));
       
   278 
       
   279     iState = aStateMachine.RegisterState();
       
   280 
       
   281     TLSLOG(KSenXmlLogChannel  , KSenXmlLogLevel , _L8("CSenFilterStateNode::CSenFilterStateNode(): End"));
       
   282 } 
       
   283 
       
   284 
       
   285 EXPORT_C CSenFilterStateNode::CSenFilterStateNode(const CSenFilterStateNode& aFilterStateNode)
       
   286 : ipPrev(NULL),
       
   287   iEvent(aFilterStateNode.Event()),
       
   288   iCondition(aFilterStateNode.Condition()),
       
   289   iStartAction(aFilterStateNode.StartAction()),
       
   290   iEndAction(aFilterStateNode.EndAction())
       
   291 {
       
   292     TLSLOG_OPEN(KSenXmlLogChannel, KSenXmlLogLevel, KSenXml, KSenXmlLog);
       
   293     TLSLOG(KSenXmlLogChannel  , KSenXmlLogLevel , _L8("CSenFilterStateNode::CSenFilterStateNode(): Log opened"));
       
   294 
       
   295     iState = aFilterStateNode.State();
       
   296     
       
   297 
       
   298     if (NULL != aFilterStateNode.ipPrev)
       
   299     {
       
   300         ipPrev = new CSenFilterStateNode(aFilterStateNode.Parent());
       
   301     }
       
   302     
       
   303     for (TInt i = 0; i < aFilterStateNode.Children().Count(); i++)
       
   304     {
       
   305         iChildren.Append((const_cast<CSenFilterStateNode>(aFilterStateNode).Children())[i]);
       
   306     }
       
   307 
       
   308     TLSLOG(KSenXmlLogChannel  , KSenXmlLogLevel , _L8("CSenFilterStateNode::CSenFilterStateNode(): End"));
       
   309 }
       
   310 
       
   311 
       
   312 EXPORT_C CSenFilterStateNode::~CSenFilterStateNode()
       
   313 {
       
   314     iChildren.ResetAndDestroy();
       
   315 
       
   316     // Close the log file and the connection to the server.
       
   317     TLSLOG(KSenXmlLogChannel  , KSenXmlLogLevel , _L("Log file closed."));
       
   318  		TLSLOG_CLOSE(KSenXmlLogChannel);
       
   319 }
       
   320 
       
   321 
       
   322 EXPORT_C TInt CSenFilterStateNode::State() const
       
   323 {
       
   324     return iState;
       
   325 }
       
   326 
       
   327 
       
   328 EXPORT_C TDesC8 CSenFilterStateNode::Event() const
       
   329 {
       
   330     return iEvent;
       
   331 }
       
   332 
       
   333 
       
   334 EXPORT_C CSenFilterCondition CSenFilterStateNode::Condition() const
       
   335 {
       
   336     return iCondition;
       
   337 }
       
   338 
       
   339 
       
   340 EXPORT_C MSenFilterAction& CSenFilterStateNode::StartAction() const
       
   341 {
       
   342     return iStartAction;
       
   343 }
       
   344 
       
   345 
       
   346 EXPORT_C MSenFilterAction& CSenFilterStateNode::EndAction() const
       
   347 {
       
   348     return iEndAction;
       
   349 }
       
   350 
       
   351 
       
   352 EXPORT_C CSenFilterStateNode& CSenFilterStateNode::Parent() const
       
   353 {
       
   354     return *ipPrev;
       
   355 }
       
   356 
       
   357 
       
   358 EXPORT_C RStateNodeArray CSenFilterStateNode::Children() const
       
   359 {
       
   360     return iChildren;
       
   361 }
       
   362 
       
   363 
       
   364 EXPORT_C void CSenFilterStateNode::SetParent(CSenFilterStateNode& aNode)
       
   365 {
       
   366     ipPrev = &aNode;
       
   367 }
       
   368 
       
   369 
       
   370 RFileLogger* CSenFilterStateNode::Log() const
       
   371     {
       
   372     return (RFileLogger*) &iLog;
       
   373     }
       
   374 
       
   375 
       
   376 // --------------------------------------
       
   377 //
       
   378 // Implementation of class CSenFilterTableEntry
       
   379 //
       
   380 // --------------------------------------
       
   381 
       
   382 
       
   383 CSenFilterTableEntry::CSenFilterTableEntry(TInt aNewState, 
       
   384                                            const CSenFilterCondition& aCondition,
       
   385                                            MSenFilterAction& aAction)
       
   386 : iAction(aAction),
       
   387   iCondition(aCondition)
       
   388 {
       
   389     TLSLOG_OPEN(KSenXmlLogChannel, KSenXmlLogLevel, KSenXml, KSenXmlLog);
       
   390     TLSLOG(KSenXmlLogChannel  , KSenXmlLogLevel , _L8("CSenFilterTableEntry::CSenFilterTableEntry(): Log opened"));
       
   391 
       
   392     iNewState = aNewState;
       
   393 
       
   394     TLSLOG(KSenXmlLogChannel  , KSenXmlLogLevel , _L8("CSenFilterTableEntry::CSenFilterTableEntry(): End"));
       
   395 }
       
   396 
       
   397 
       
   398 CSenFilterTableEntry::~CSenFilterTableEntry()
       
   399 {
       
   400     // Close the log file and the connection to the server.
       
   401     TLSLOG(KSenXmlLogChannel  , KSenXmlLogLevel , _L("Log file closed."));
       
   402 		TLSLOG_CLOSE(KSenXmlLogChannel);
       
   403 }
       
   404 
       
   405 
       
   406 TInt CSenFilterTableEntry::NewState() const
       
   407 {
       
   408     return iNewState;
       
   409 }
       
   410 
       
   411 
       
   412 CSenFilterCondition CSenFilterTableEntry::Condition() const
       
   413 {
       
   414     return iCondition;
       
   415 }
       
   416 
       
   417 
       
   418 MSenFilterAction& CSenFilterTableEntry::Action() const
       
   419 {
       
   420     return iAction;
       
   421 }
       
   422 
       
   423 
       
   424 RFileLogger* CSenFilterTableEntry::Log() const
       
   425     {
       
   426     return (RFileLogger*) &iLog;
       
   427     }
       
   428 
       
   429 
       
   430