servicediscoveryandcontrol/pnp/test/upnp/Server/AppProtIntf/src/app_protintf_base.cpp
changeset 0 f5a58ecadc66
equal deleted inserted replaced
-1:000000000000 0:f5a58ecadc66
       
     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 //
       
    15 
       
    16 #include "app_protintf_base.h"
       
    17 #include "app_protintf_fact.h"
       
    18 #include "app_protintf_msgs.h"
       
    19 #include <in_sock.h>
       
    20 
       
    21 using namespace ESock;
       
    22 using namespace Messages;
       
    23 
       
    24 CApplicationProtocolIntfBase::CApplicationProtocolIntfBase ( TUint aPort, TInt aProtocolType )
       
    25 	: CFactoryObject ( aProtocolType ),
       
    26  	iSocketHandler ( *this ),
       
    27 	iPort ( aPort )
       
    28 	{
       
    29 
       
    30 	}
       
    31 
       
    32 CApplicationProtocolIntfBase::~CApplicationProtocolIntfBase ()
       
    33 	{
       
    34 	delete iSocketOpener;
       
    35 	iLinks.Close ();
       
    36 	Shutdown();
       
    37 	}
       
    38 
       
    39 void CApplicationProtocolIntfBase::Shutdown ()
       
    40 	{
       
    41 	iSocketHandler.CancelAll();
       
    42 	iSocket.Close ();
       
    43 	}
       
    44 
       
    45 void CApplicationProtocolIntfBase::ReceivedL ( const TRuntimeCtxId& aSender, const TNodeId& /*aRecipient*/, TSignatureBase& aMessage )
       
    46 	{
       
    47 	switch ( aMessage.MessageId ().MessageId () )
       
    48 		{
       
    49 		case TAppProtIntfMessage::TClientLeavingRequest::EId:
       
    50 			{
       
    51 			HandleClientLeavingRequest ( address_cast<TNodeCtxId>(aSender) );
       
    52 			}
       
    53 			break;
       
    54 
       
    55 		case TAppProtIntfMessage::TDestroy::EId:
       
    56 			{
       
    57 			CloseLink ( address_cast<TNodeId>(aSender) );
       
    58 			}
       
    59 		break;
       
    60 		}
       
    61 	}
       
    62 
       
    63 
       
    64 // MSocketHandlerObserver
       
    65 void CApplicationProtocolIntfBase::OpenComplete ( RInternalSocket& aSocket )
       
    66 	{
       
    67 	iSocket = aSocket;
       
    68 	iSocketHandler.Attach ( aSocket );
       
    69 	TInetAddr addr ( iPort );
       
    70 
       
    71 	addr.SetAddress ( KInetAddrAny );
       
    72 	TInt err = aSocket.Bind ( addr );
       
    73 
       
    74 	if ( err == KErrNone )
       
    75 		err = Startup ();
       
    76 
       
    77 	if ( err == KErrNone )
       
    78 		{
       
    79 		// Post to all all links that we have successfully opened and bound to the socket. ie; Join is
       
    80 		// successful
       
    81 		JoinCompleted ();
       
    82 		}
       
    83 	else
       
    84 		{
       
    85 		// Post the error to all links
       
    86 		CApplicationProtocolIntfBase::Error ( EOpenByProtocol, err );
       
    87 		}
       
    88 	}
       
    89 
       
    90 void CApplicationProtocolIntfBase::Error ( TOperation /* aOperation */, TInt aError  )
       
    91 	{
       
    92 	// This error means something went seriously wrong. We will close down everything and post
       
    93 	// an error to all links.
       
    94 	TAppProtIntfMessage::TError msg ( aError );
       
    95 	PostToAllLinks ( msg );
       
    96 
       
    97 	// no other new link should get access to this object. So mark for deletion
       
    98 	// MarkMeForDeletion ();
       
    99 	Shutdown ();
       
   100 	}
       
   101 
       
   102 void CApplicationProtocolIntfBase::JoinCompleted ()
       
   103 	{	
       
   104 	PostToAllLinks ( TAppProtIntfMessage::TJoinComplete ().CRef () );
       
   105 	}
       
   106 
       
   107 TInt CApplicationProtocolIntfBase::AddLink ( const TNodeId& aSender )
       
   108 	{
       
   109 	if ( KErrNotFound == FindLink ( aSender ) )
       
   110 		{
       
   111 		RNodeInterface newLink;
       
   112     	newLink.Open ( aSender );
       
   113     	iLinks.Append ( newLink );
       
   114 		}
       
   115 
       
   116 	return iLinks.Count ();
       
   117 	}
       
   118 
       
   119 TInt CApplicationProtocolIntfBase::FindLink ( const TNodeId& aSender )
       
   120 	{
       
   121 	for ( TInt i = 0; i < iLinks.Count(); ++i )
       
   122 		{
       
   123 		if ( aSender == iLinks[i].RecipientId () )
       
   124 			{
       
   125 			return i;
       
   126 			}
       
   127 		}
       
   128 	return KErrNotFound;
       
   129 	}
       
   130 
       
   131 TInt CApplicationProtocolIntfBase::RemoveLink ( const TNodeId& aSender )
       
   132 	{
       
   133 	for ( TInt i = 0; i < iLinks.Count(); ++i )
       
   134 		{
       
   135 		if ( aSender == iLinks[i].RecipientId () )
       
   136 			{
       
   137 			iLinks.Remove (i);
       
   138 			break;
       
   139 			}
       
   140 		}
       
   141 	return iLinks.Count();
       
   142 	}
       
   143 
       
   144 void CApplicationProtocolIntfBase::PostToLink ( const TNodeCtxId& aRecipient, const TSignatureBase& aMessage )
       
   145 	{
       
   146 	RClientInterface::OpenPostMessageClose ( NodeId (), aRecipient, aMessage );
       
   147 	}
       
   148 
       
   149 void CApplicationProtocolIntfBase::PostToLink (const RNodeInterface& aRecipient, const TSignatureBase& aMessage )
       
   150 	{
       
   151 	RClientInterface::OpenPostMessageClose ( NodeId (), aRecipient.RecipientId (), aMessage );	
       
   152 	}
       
   153 
       
   154 void CApplicationProtocolIntfBase::PostToAllLinks ( const TSignatureBase& aMessage )
       
   155 	{
       
   156 	for ( TInt i = 0; i < iLinks.Count(); ++i )
       
   157 		{
       
   158 		// PostTo links
       
   159 		PostToLink ( iLinks[i], aMessage );
       
   160 		}
       
   161 	}
       
   162 
       
   163 TInt CApplicationProtocolIntfBase::OpenSocket ( TUint aAddrFamily, TUint aSockType, TUint aProtocol )
       
   164 	{
       
   165 	TInt err = KErrNone;
       
   166 	if ( NULL == iSocketOpener )
       
   167 		{
       
   168 		TRAP ( err, iSocketOpener = CSocketOpener::NewL ( *this ) );
       
   169 		}
       
   170 
       
   171 	if ( err == KErrNone )
       
   172 		iSocketOpener->MakeSocket ( aAddrFamily, aSockType, aProtocol );
       
   173 
       
   174 	return err;
       
   175 	}
       
   176 
       
   177 void CApplicationProtocolIntfBase::InitiateLinkL ( const TNodeCtxId& aSender, TUint aSockType, TUint aProtocol )
       
   178 	{
       
   179 	const TInt KFirstLink = 1;
       
   180 	if ( AddLink ( aSender ) == KFirstLink )
       
   181 		{
       
   182 		// Open the socket
       
   183 		User::LeaveIfError( OpenSocket ( KAfInet, aSockType, aProtocol ) );
       
   184 		}
       
   185 	else
       
   186 		{		
       
   187 		PostToLink ( aSender, TAppProtIntfMessage::TJoinComplete ().CRef () );
       
   188 		}
       
   189 	}
       
   190 
       
   191 void CApplicationProtocolIntfBase::HandleClientLeavingRequest ( const TNodeCtxId& aSender )
       
   192 	{
       
   193 	// set client flags to leaving and post leave complete
       
   194 	TInt idx = FindLink ( aSender );
       
   195 	__ASSERT_DEBUG ( idx != KErrNotFound, User::Invariant() );
       
   196 
       
   197 	iLinks[idx].SetFlags ( TClientType::ELeaving );
       
   198 	PostToLink ( aSender, TAppProtIntfMessage::TLeaveComplete ().CRef () ); // activity id is missing
       
   199 	}
       
   200 
       
   201 void CApplicationProtocolIntfBase::CloseLink ( const TNodeId& aSender )
       
   202 	{
       
   203 	if ( RemoveLink ( aSender ) == 0 )
       
   204 		{
       
   205 		delete this; // Do a self destruction now.
       
   206 		}
       
   207 	}