cbs/CbsServer/ServerSrc/CCbsMcnSession.cpp
changeset 46 2fa1fa551b0b
parent 42 35488577e233
child 48 78df25012fda
equal deleted inserted replaced
42:35488577e233 46:2fa1fa551b0b
     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:  MCN server-side subsession implementation.
       
    15 *
       
    16 *                Implementation of MCN topic subscription commits changes (reception 
       
    17 *                state, subscribed topics, etc) immediately after every request.
       
    18 *
       
    19 */
       
    20 
       
    21 
       
    22 
       
    23 // INCLUDE FILES
       
    24 
       
    25 #include "CbsCommon.h"
       
    26 #include "CbsServerPanic.h"
       
    27 #include "CbsServerConstants.h"
       
    28 #include "CCbsMcnSession.h"
       
    29 #include "CCbsRecMessage.h"
       
    30 #include "CCbsReceiverHelper.h"
       
    31 #include "CCbsDbImpSettings.H"
       
    32 #include "CCbsRecEtel.h"
       
    33 #include "CCbsSession.h"
       
    34 #include "CCbsServer.h"
       
    35 #include "CbsLogger.h"
       
    36 
       
    37 // CONSTANTS
       
    38 
       
    39 /// Initial size of the array holding numbers of subscribed topics.
       
    40 const TInt KInitialSpaceForSubscribedTopics = 1;
       
    41 
       
    42 // ================= MEMBER FUNCTIONS =======================
       
    43 
       
    44 // -----------------------------------------------------------------------------
       
    45 // CCbsMcnSession::CCbsMcnSession
       
    46 // C++ default constructor can NOT contain any code, that
       
    47 // might leave.
       
    48 // -----------------------------------------------------------------------------
       
    49 //
       
    50 CCbsMcnSession::CCbsMcnSession( 
       
    51     CCbsSession& aSession, 
       
    52     CCbsRecEtel& aReceiver )
       
    53     : CCbsObject( aSession ), 
       
    54     iReceiver( aReceiver ),
       
    55     iMcnPckg( iMcnMessage )
       
    56     {
       
    57     // Do nothing
       
    58     }
       
    59 
       
    60 // -----------------------------------------------------------------------------
       
    61 // CCbsEtelMessaging::ConstructL
       
    62 // Symbian 2nd phase constructor can leave.
       
    63 // -----------------------------------------------------------------------------
       
    64 //
       
    65 void CCbsMcnSession::ConstructL()
       
    66     {
       
    67     CBSLOGSTRING("CBSSERVER: >>> CCbsMcnSession::ConstructL()");
       
    68 
       
    69     iSubscribedTopics = new (ELeave) CArrayFixFlat<TUint16>
       
    70         ( KInitialSpaceForSubscribedTopics );
       
    71     iReceiver.AddSubscriptionProviderL( this );
       
    72 
       
    73 	CBSLOGSTRING("CBSSERVER: <<< CCbsMcnSession::ConstructL()");
       
    74     }
       
    75 
       
    76 // -----------------------------------------------------------------------------
       
    77 // CCbsEtelMessaging::NewL
       
    78 // Two-phased constructor.
       
    79 // -----------------------------------------------------------------------------
       
    80 //
       
    81 CCbsMcnSession* CCbsMcnSession::NewL( 
       
    82     CCbsSession& aSession, 
       
    83     CCbsRecEtel& aReceiver )
       
    84     {
       
    85 	CBSLOGSTRING("CBSSERVER: >>> CCbsMcnSession::NewL()");
       
    86 
       
    87     CCbsMcnSession* self = 
       
    88         new (ELeave) CCbsMcnSession( aSession, aReceiver );
       
    89     CleanupStack::PushL( self );
       
    90     self->ConstructL();
       
    91     CleanupStack::Pop();
       
    92 
       
    93     CBSLOGSTRING("CBSSERVER: <<< CCbsMcnSession::NewL()");
       
    94     return self;
       
    95     }
       
    96 
       
    97 // Destructor
       
    98 
       
    99 CCbsMcnSession::~CCbsMcnSession()
       
   100     {
       
   101     CBSLOGSTRING("CBSSERVER: >>> CCbsMcnSession::~CCbsMcnSession()");
       
   102     TRAP_IGNORE( iReceiver.RemoveSubscriptionProviderL( this ) );
       
   103     delete iSubscribedTopics;
       
   104     CBSLOGSTRING("CBSSERVER: <<< CCbsMcnSession::~CCbsMcnSession()");
       
   105     }
       
   106 
       
   107 // -----------------------------------------------------------------------------
       
   108 // CCbsMcnSession::HandleRequestsL
       
   109 // Handles MCN client requests.
       
   110 // (other items were commented in a header).
       
   111 // -----------------------------------------------------------------------------
       
   112 //  
       
   113 TBool CCbsMcnSession::HandleRequestsL( 
       
   114     const RMessage2& aMessage )
       
   115     {
       
   116     CBSLOGSTRING("CBSSERVER: >>> CCbsMcnSession::HandleRequestsL()");
       
   117 
       
   118     TBool requestHandled( ETrue );
       
   119 
       
   120     switch( aMessage.Function() )
       
   121         {
       
   122         case EMcnNotifyOnChange:
       
   123             CBSLOGSTRING("CBSSERVER: CCbsMcnSession::HandleRequestsL(): EMcnNotifyOnChange");
       
   124             NotifyOnChange();
       
   125             break;
       
   126 
       
   127         case EMcnCloseSubsession:
       
   128             CBSLOGSTRING("CBSSERVER: CCbsMcnSession::HandleRequestsL(): EMcnCloseSubsession");
       
   129             CloseMcnSession();
       
   130             aMessage.Complete( KErrNone );
       
   131             break;
       
   132 
       
   133         case EMcnNotifyOnChangeCancel:
       
   134             CBSLOGSTRING("CBSSERVER: CCbsMcnSession::HandleRequestsL(): EMcnNotifyOnChangeCancel");
       
   135             NotifyOnChangeCancel();
       
   136             break;
       
   137 
       
   138         case EMcnSubscribeTopic:
       
   139             CBSLOGSTRING("CBSSERVER: CCbsMcnSession::HandleRequestsL(): EMcnSubscribeTopic");
       
   140             SubscribeTopicL();
       
   141             break;
       
   142 
       
   143         case EMcnClearSubscriptions:
       
   144             CBSLOGSTRING("CBSSERVER: CCbsMcnSession::HandleRequestsL(): EMcnClearSubscriptions");
       
   145             ClearSubscriptionsL();
       
   146             break;
       
   147 
       
   148         case EMcnNoMoreSubscriptions:
       
   149             CBSLOGSTRING("CBSSERVER: CCbsMcnSession::HandleRequestsL(): EMcnNoMoreSubscriptions");
       
   150             ApplySubscriptionsL();
       
   151             break;
       
   152 
       
   153         default:
       
   154             CBSLOGSTRING("CBSSERVER: CCbsMcnSession::HandleRequestsL(): default");
       
   155             requestHandled = EFalse;
       
   156             break;
       
   157         }
       
   158     CBSLOGSTRING("CBSSERVER: <<< CCbsMcnSession::HandleRequestsL()");
       
   159 
       
   160     return requestHandled;
       
   161     }
       
   162 
       
   163 // -----------------------------------------------------------------------------
       
   164 // CCbsMcnSession::NumberOfSubscriptions
       
   165 // Returns the number of topic subscriptions of this MCN client.
       
   166 // (other items were commented in a header).
       
   167 // -----------------------------------------------------------------------------
       
   168 //  
       
   169 TUint CCbsMcnSession::NumberOfSubscriptions() const
       
   170     {
       
   171     return iSubscribedTopics->Count();
       
   172     }
       
   173 
       
   174 // -----------------------------------------------------------------------------
       
   175 // CCbsMcnSession::RouteMessageL
       
   176 // Routes messages to the subscribed Mcnclients.
       
   177 // If the topic of the message is subscribed by the client,
       
   178 // aMessage is copied to client-side. 
       
   179 // (other items were commented in a header).
       
   180 // -----------------------------------------------------------------------------
       
   181 //
       
   182 void CCbsMcnSession::RouteMessageL( 
       
   183     const CCbsMessage& aMessage )
       
   184     {
       
   185 	CBSLOGSTRING("CBSSERVER: >>> CCbsMcnSession::RouteMessageL()");
       
   186 
       
   187 	TKeyArrayFix key(0, ECmpTUint16);
       
   188 	TInt index;
       
   189     TUint16 topicNumber( aMessage.TopicNumber() );    
       
   190 	TInt result( iSubscribedTopics->FindIsq( topicNumber, key, index ) );
       
   191 
       
   192 
       
   193     TBool isLC( aMessage.IsLivecastMessage() );
       
   194 
       
   195     if ( isLC )
       
   196         {
       
   197         iMcnMessage.iBuffer8 = aMessage.Contents8();
       
   198         iMcnMessage.iBuffer = KNullDesC;
       
   199         }
       
   200     else
       
   201         {
       
   202         iMcnMessage.iBuffer = aMessage.Contents();
       
   203         iMcnMessage.iBuffer8 = KNullDesC8;
       
   204         }
       
   205     
       
   206     iMcnMessage.iNetworkMode = aMessage.NetworkMode();
       
   207     iMcnMessage.iTopicNumber = aMessage.TopicNumber();
       
   208 
       
   209     CBSLOGSTRING2("CBSSERVER: CCbsMcnSession::RouteMessageL(): topicNumber: %d", iMcnMessage.iTopicNumber );
       
   210     CBSLOGSTRING2("CBSSERVER: CCbsMcnSession::RouteMessageL(): networkMode: %d (0=GSM, 1=WCDMA, 2=Not defined).", iMcnMessage.iNetworkMode );    
       
   211     
       
   212 	if ( result == KErrNone )
       
   213 		{        
       
   214         CBSLOGSTRING("CBSSERVER: CCbsMcnSession::RouteMessageL(): Notifying client...");
       
   215         NotifyClientL( iMcnPckg );
       
   216         CBSLOGSTRING("CBSSERVER: CCbsMcnSession::RouteMessageL(): Client notified.");
       
   217 	    }
       
   218 	CBSLOGSTRING("CBSSERVER: <<< CCbsMcnSession::RouteMessageL()");
       
   219     }
       
   220 
       
   221 // -----------------------------------------------------------------------------
       
   222 // CCbsMcnSession::CloseMcnSession
       
   223 // Close the subsession.
       
   224 // (other items were commented in a header).
       
   225 // -----------------------------------------------------------------------------
       
   226 //
       
   227 void CCbsMcnSession::CloseMcnSession()
       
   228     {
       
   229     CBSLOGSTRING("CBSSERVER: >>> CCbsMcnSession::CloseMcnSession()");
       
   230 
       
   231     // Removes the object.
       
   232     Session().Server().DeleteObjectByHandle( Message().Int3() );
       
   233 
       
   234     CBSLOGSTRING("CBSSERVER: <<< CCbsMcnSession::CloseMcnSession()");
       
   235     }
       
   236 
       
   237 // -----------------------------------------------------------------------------
       
   238 // CCbsMcnSession::NotifyOnChange
       
   239 // Handles client request for message routing.
       
   240 // (other items were commented in a header).
       
   241 // -----------------------------------------------------------------------------
       
   242 //   
       
   243 void CCbsMcnSession::NotifyOnChange()
       
   244     {
       
   245     CBSLOGSTRING("CBSSERVER: >>> CCbsMcnSession::NotifyOnChange()");
       
   246 
       
   247     if ( iIsMessage )
       
   248         {
       
   249         NotifyOnChangeCancel();
       
   250         }
       
   251 
       
   252     iMessage = Message();
       
   253     iIsMessage = ETrue;
       
   254 
       
   255     CBSLOGSTRING("CBSSERVER: <<< CCbsMcnSession::NotifyOnChange()");
       
   256     }
       
   257 
       
   258 // -----------------------------------------------------------------------------
       
   259 // CCbsMcnSession::NotifyOnChangeCancel
       
   260 // Handles client request to cancel an outstanding routing request.
       
   261 // (other items were commented in a header).
       
   262 // -----------------------------------------------------------------------------
       
   263 //
       
   264 void CCbsMcnSession::NotifyOnChangeCancel()
       
   265     {
       
   266     CBSLOGSTRING("CBSSERVER: >>> CCbsMcnSession::NotifyOnChangeCancel()");
       
   267 
       
   268     if ( iIsMessage )
       
   269         {
       
   270         iMessage.Complete( KErrCancel );
       
   271         }
       
   272 
       
   273     iIsMessage = EFalse;
       
   274     Message().Complete( KErrNone );
       
   275 
       
   276     CBSLOGSTRING("CBSSERVER: <<< CCbsMcnSession::NotifyOnChangeCancel()");
       
   277     }
       
   278 
       
   279 // -----------------------------------------------------------------------------
       
   280 // CCbsMcnSession::SubscribeTopicL
       
   281 // Handles client-side request to subscribe a single topic.
       
   282 // An attempt to subscribe the same topic twice does not
       
   283 // cause any error - the topic remains subscribed. 
       
   284 // Note: ApplySubscriptionL has to be called in order to
       
   285 // apply receiver changes.
       
   286 // (other items were commented in a header).
       
   287 // -----------------------------------------------------------------------------
       
   288 //
       
   289 void CCbsMcnSession::SubscribeTopicL()
       
   290     {
       
   291     CBSLOGSTRING("CBSSERVER: >>> CCbsMcnSession::SubscribeTopicL()");
       
   292 
       
   293     // Read topic number from client.
       
   294     TUint16 topicNumber( 0 );
       
   295     topicNumber = static_cast<TUint16> ( Message().Int0() );
       
   296    
       
   297     // Store the topic number and notify receiver.
       
   298 	TKeyArrayFix key(0, ECmpTUint16);
       
   299 	TRAPD( err, iSubscribedTopics->InsertIsqL( topicNumber, key ) );
       
   300 	if( err != KErrAlreadyExists )
       
   301 		{        
       
   302         CBSLOGSTRING2("CBSSERVER: CCbsMcnSession::SubscribeTopicL(), leaving if error != 0: %d", err);
       
   303 		User::LeaveIfError( err );
       
   304 		}
       
   305 
       
   306     // Complete the request.
       
   307     Message().Complete( KErrNone );
       
   308 
       
   309     CBSLOGSTRING("CBSSERVER: <<< CCbsMcnSession::SubscribeTopicL()");
       
   310     }
       
   311 
       
   312 // -----------------------------------------------------------------------------
       
   313 // CCbsMcnSession::NotifyClientL
       
   314 // Called by RouteMessageL() to actually copy the message content
       
   315 // to client side.
       
   316 // If the client has a CB message routing request pending,
       
   317 // the message and topicnumber of the message are given to client side.
       
   318 // (other items were commented in a header).
       
   319 // -----------------------------------------------------------------------------
       
   320 //
       
   321 void CCbsMcnSession::NotifyClientL( 
       
   322     const TPckg<TCbsMcnMessage>& aMcnPckg )
       
   323     {
       
   324 	CBSLOGSTRING("CBSSERVER: >>> CCbsMcnSession::NotifyClientL()");
       
   325 
       
   326     if ( iIsMessage )
       
   327         {        
       
   328         CBSLOGSTRING("CBSSERVER: CCbsMcnSession::NotifyClientL(): Writing msg pckg to client...");
       
   329         // Write the message package to client
       
   330 		iMessage.WriteL( 0, aMcnPckg );        
       
   331         CBSLOGSTRING("CBSSERVER: CCbsMcnSession::NotifyClientL(): Writing msg pckg to client OK.");
       
   332 
       
   333         // Complete the request
       
   334         iMessage.Complete( KErrNone );
       
   335         iIsMessage = EFalse;
       
   336         }
       
   337 	CBSLOGSTRING("CBSSERVER: <<< CCbsMcnSession::NotifyClientL()");
       
   338     }
       
   339 
       
   340 // -----------------------------------------------------------------------------
       
   341 // CCbsMcnSession::ClearSubscriptionsL
       
   342 // Clears all topic subscriptions of this MCN session.
       
   343 // (other items were commented in a header).
       
   344 // -----------------------------------------------------------------------------
       
   345 //
       
   346 void CCbsMcnSession::ClearSubscriptionsL() 
       
   347     {
       
   348     CBSLOGSTRING("CBSSERVER: >>> CCbsMcnSession::ClearSubscriptionsL()");
       
   349 
       
   350     iSubscribedTopics->Reset();
       
   351     iReceiver.ApplyStateChangesL();
       
   352     Message().Complete( KErrNone );
       
   353     
       
   354     CBSLOGSTRING("CBSSERVER: <<< CCbsMcnSession::ClearSubscriptionsL()");
       
   355     }
       
   356 
       
   357 // -----------------------------------------------------------------------------
       
   358 // CCbsMcnSession::ApplySubscriptionsL
       
   359 // Forces receiver to reload CB topic subscriptions.
       
   360 // This function has to be called after MCN client has subscribed
       
   361 // topics with SubscribeTopicL().
       
   362 // (other items were commented in a header).
       
   363 // -----------------------------------------------------------------------------
       
   364 //
       
   365 void CCbsMcnSession::ApplySubscriptionsL() 
       
   366     {
       
   367 	CBSLOGSTRING("CBSSERVER: >>> CCbsMcnSession::ApplySubscriptionsL()");
       
   368 
       
   369     iReceiver.ApplyStateChangesL();
       
   370     Message().Complete( KErrNone );
       
   371 
       
   372 	CBSLOGSTRING("CBSSERVER: <<< CCbsMcnSession::ApplySubscriptionsL()");
       
   373     }
       
   374 
       
   375 // ========================== OTHER EXPORTED FUNCTIONS =========================
       
   376 
       
   377 //  End of File