upnp/upnpstack/upnpconnectionmanagersession/src/upnpconnectionmanagerproxy.cpp
changeset 0 f5a58ecadc66
equal deleted inserted replaced
-1:000000000000 0:f5a58ecadc66
       
     1 /** @file
       
     2 * Copyright (c) 2005-2008 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:  CUpnpConnectionManagerProxy
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "upnpconnectionmanagernetworkeventprovider.h"
       
    21 #include "upnpconnectionmanagerproxy.h"
       
    22 #include "upnpcustomlog.h"
       
    23 
       
    24 
       
    25 // -----------------------------------------------------------------------------
       
    26 // CUpnpConnectionManagerProxy::NewL
       
    27 // Two-phased constructor.
       
    28 // -----------------------------------------------------------------------------
       
    29 //
       
    30 EXPORT_C CUpnpConnectionManagerProxy* CUpnpConnectionManagerProxy::NewL(
       
    31     RSocketServ &aSocketServer )
       
    32     {
       
    33     CUpnpConnectionManagerProxy* self =
       
    34         CUpnpConnectionManagerProxy::NewLC( aSocketServer );
       
    35     CleanupStack::Pop( self );
       
    36     return self;
       
    37     }
       
    38 
       
    39 // -----------------------------------------------------------------------------
       
    40 // CUpnpConnectionManagerProxy::NewLC
       
    41 // Two-phased constructor, leave object on the cleanup stack.
       
    42 // -----------------------------------------------------------------------------
       
    43 //
       
    44 EXPORT_C CUpnpConnectionManagerProxy* CUpnpConnectionManagerProxy::NewLC(
       
    45     RSocketServ &aSocketServer )
       
    46     {
       
    47     CUpnpConnectionManagerProxy* self = new( ELeave ) CUpnpConnectionManagerProxy();
       
    48     CleanupStack::PushL( self );
       
    49     self->ConstructL( aSocketServer );
       
    50     return self;
       
    51     }
       
    52 
       
    53 // -----------------------------------------------------------------------------
       
    54 // CUpnpConnectionManagerProxy::CUpnpConnectionManagerProxy
       
    55 // C++ default constructor can NOT contain any code, that might leave.
       
    56 // -----------------------------------------------------------------------------
       
    57 //
       
    58 CUpnpConnectionManagerProxy::CUpnpConnectionManagerProxy() : iActiveIap( KErrNotFound )
       
    59     {
       
    60     }
       
    61 
       
    62 // -----------------------------------------------------------------------------
       
    63 // CUpnpConnectionManagerProxy::ConstructL
       
    64 // Symbian 2nd phase constructor can leave.
       
    65 // -----------------------------------------------------------------------------
       
    66 //
       
    67 void CUpnpConnectionManagerProxy::ConstructL( RSocketServ& aSocketServer )
       
    68     {
       
    69     User::LeaveIfError( iConnection.Open( aSocketServer ) );
       
    70     User::LeaveIfError( iConnectionManagerSession.Connect() );
       
    71     }
       
    72 
       
    73 // -----------------------------------------------------------------------------
       
    74 // CUpnpConnectionManagerProxy::~CUpnpConnectionManagerProxy
       
    75 // Destructor.
       
    76 // -----------------------------------------------------------------------------
       
    77 //
       
    78 CUpnpConnectionManagerProxy::~CUpnpConnectionManagerProxy()
       
    79     {
       
    80     delete iNetworkEventProvider;
       
    81     iConnection.Close();
       
    82     iConnectionManagerSession.Close();
       
    83     }
       
    84 
       
    85 // -----------------------------------------------------------------------------
       
    86 // CUpnpConnectionManagerProxy::EnsureStart
       
    87 // Calls remote process to ensure that interface is started.
       
    88 // -----------------------------------------------------------------------------
       
    89 //
       
    90 EXPORT_C TInt CUpnpConnectionManagerProxy::EnsureStart()
       
    91     {
       
    92     TInt error = iConnectionManagerSession.RequestEnsureStartRConnection( iActiveIap );
       
    93     if ( error == KErrNone )
       
    94         {
       
    95         error = Attach( iActiveIap );
       
    96         }
       
    97     else if ( error < KErrNone )
       
    98         {
       
    99         iActiveIap = KErrNotFound;
       
   100         }
       
   101 
       
   102     return error;
       
   103     }
       
   104 
       
   105 // -----------------------------------------------------------------------------
       
   106 // CUpnpConnectionManagerProxy::IsStarted
       
   107 // Checks whether EnsureStart has been invoked.
       
   108 // -----------------------------------------------------------------------------
       
   109 //
       
   110 TBool CUpnpConnectionManagerProxy::IsStarted()
       
   111     {
       
   112     TBool result = EFalse;
       
   113     iConnectionManagerSession.RequestIsRConnectionStarted( result );
       
   114     return result;
       
   115     }
       
   116 
       
   117 // -----------------------------------------------------------------------------
       
   118 // CUpnpConnectionManagerProxy::ConnectionL
       
   119 // -----------------------------------------------------------------------------
       
   120 //
       
   121 EXPORT_C RConnection& CUpnpConnectionManagerProxy::ConnectionL()
       
   122     {
       
   123     if ( !IsStarted() )
       
   124         {
       
   125         User::LeaveIfError( EnsureStart() );
       
   126         }
       
   127     return iConnection;
       
   128     }
       
   129 
       
   130 // -----------------------------------------------------------------------------
       
   131 // CUpnpConnectionManagerProxy::Attach
       
   132 // Internal function.
       
   133 // -----------------------------------------------------------------------------
       
   134 //
       
   135 TInt CUpnpConnectionManagerProxy::Attach( TInt aAccessPoint )
       
   136     {
       
   137     TUint connNumber;
       
   138     TInt attachError = KErrNotFound;
       
   139     TInt error = iConnection.EnumerateConnections( connNumber );
       
   140 
       
   141     if ( error == KErrNone && connNumber > 0 )
       
   142         {
       
   143         TPckgBuf<TConnectionInfo> connInfo;
       
   144 
       
   145         for ( TInt i = 1; i <= connNumber; i++ )
       
   146             {
       
   147             error = iConnection.GetConnectionInfo( i, connInfo );
       
   148 
       
   149             if ( error == KErrNone && connInfo().iIapId == aAccessPoint )
       
   150                 {
       
   151                 attachError = iConnection.Attach( connInfo,
       
   152                                                   RConnection::EAttachTypeNormal );
       
   153 
       
   154                 if ( attachError == KErrNone || attachError == KErrInUse )
       
   155                     {
       
   156                     attachError = KErrNone; //KErrInUse means already attached
       
   157                     break;
       
   158                     }
       
   159                 }
       
   160             }
       
   161         }
       
   162 
       
   163     return attachError;
       
   164     }
       
   165 
       
   166 // -----------------------------------------------------------------------------
       
   167 // CUpnpConnectionManagerProxy::ActiveIap
       
   168 // Returns active Iap or KErrNotFound if not connected.
       
   169 // -----------------------------------------------------------------------------
       
   170 //
       
   171 EXPORT_C TInt CUpnpConnectionManagerProxy::ActiveIap()
       
   172     {
       
   173     if ( iActiveIap == KErrNotFound )
       
   174         {
       
   175         iActiveIap = iConnectionManagerSession.RequestActiveIap();
       
   176         }
       
   177 
       
   178     return iActiveIap;
       
   179     }
       
   180 
       
   181 // -----------------------------------------------------------------------------
       
   182 // CUpnpConnectionManagerProxy::LocalAddress
       
   183 // Returns local address
       
   184 // -----------------------------------------------------------------------------
       
   185 //
       
   186 EXPORT_C TInetAddr CUpnpConnectionManagerProxy::LocalAddress()
       
   187     {    
       
   188     return iConnectionManagerSession.RequestLocalAddress();
       
   189     }
       
   190 
       
   191 // -----------------------------------------------------------------------------
       
   192 // CUpnpConnectionManagerProxy::SubscribeForNetworkEventsL
       
   193 // Subscribe for network events.
       
   194 // -----------------------------------------------------------------------------
       
   195 //
       
   196 EXPORT_C void CUpnpConnectionManagerProxy::SubscribeForNetworkEventsL( 
       
   197                                            MUpnpNetworkEventObserver *aObserver )
       
   198     {
       
   199     delete iNetworkEventProvider;
       
   200     iNetworkEventProvider = NULL;
       
   201     if ( aObserver )
       
   202         {
       
   203         iNetworkEventProvider = CUpnpConnectionManagerNetworkEventProvider::NewL( 
       
   204                                                 iConnectionManagerSession, *aObserver );
       
   205         }
       
   206     }
       
   207 
       
   208 // -----------------------------------------------------------------------------
       
   209 // CUpnpConnectionManagerProxy::GetActiveIapL
       
   210 // Static function for straightforward way of obtaining IAP from server.
       
   211 // -----------------------------------------------------------------------------
       
   212 //
       
   213 EXPORT_C TInt CUpnpConnectionManagerProxy::GetActiveIapL()
       
   214     {
       
   215     RSocketServ socketServer;
       
   216     User::LeaveIfError( socketServer.Connect() );
       
   217     CleanupClosePushL( socketServer );    
       
   218     CUpnpConnectionManagerProxy *proxy = CUpnpConnectionManagerProxy::NewLC( socketServer );
       
   219     
       
   220     TInt iap = proxy->ActiveIap();
       
   221         
       
   222     CleanupStack::PopAndDestroy( proxy );
       
   223     CleanupStack::PopAndDestroy( &socketServer );
       
   224     
       
   225     return iap;
       
   226     }
       
   227 
       
   228 // End of file