hti/HtiServicePlugins/HtiIpProxyServicePlugin/src/HtiIpProxyServicePlugin.cpp
branchRCL_3
changeset 59 8ad140f3dd41
equal deleted inserted replaced
49:7fdc9a71d314 59:8ad140f3dd41
       
     1 /*
       
     2 * Copyright (c) 2009 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:  HtiIpProxyServicePlugin implementation
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include "HtiIpProxyServicePlugin.h"
       
    21 #include <HtiDispatcherInterface.h>
       
    22 #include <HtiLogging.h>
       
    23 #include <badesca.h>
       
    24 #include <in_sock.h>
       
    25 #include "MSocketObserver.h"
       
    26 #include "Mhostconnectionobserver.h"
       
    27 #include "CIPProxyEngine.h"
       
    28 
       
    29 // CONSTANTS
       
    30 const static TUid KIpProxyServiceUid = { 0x10210CD3 };
       
    31 
       
    32 // MACROS
       
    33 
       
    34 // LOCAL CONSTANTS AND MACROS
       
    35 
       
    36 // MODULE DATA STRUCTURES
       
    37 
       
    38 // LOCAL FUNCTION PROTOTYPES
       
    39 
       
    40 // FORWARD DECLARATIONS
       
    41 
       
    42 // ============================ MEMBER FUNCTIONS ===============================
       
    43 
       
    44 // Create instance of concrete ECOM interface implementation
       
    45 CHtiIpProxyServicePlugin* CHtiIpProxyServicePlugin::NewL()
       
    46     {
       
    47     CHtiIpProxyServicePlugin* self = new( ELeave ) CHtiIpProxyServicePlugin;
       
    48     CleanupStack::PushL( self );
       
    49     self->ConstructL();
       
    50     CleanupStack::Pop();
       
    51     return self;
       
    52     }
       
    53 
       
    54 // Constructor
       
    55 CHtiIpProxyServicePlugin::CHtiIpProxyServicePlugin() :
       
    56     iSocketObserver( NULL ),
       
    57     iHostObserver( NULL ),
       
    58     iProxyEngine( NULL ),
       
    59     iOutgoingArray( NULL ),
       
    60     iBusy( EFalse ),
       
    61     iConnected( EFalse )
       
    62     {
       
    63     HTI_LOG_FUNC_IN( "CHtiIpProxyServicePlugin" );
       
    64     HTI_LOG_FUNC_OUT( "CHtiIpProxyServicePlugin" );
       
    65     }
       
    66 
       
    67 CHtiIpProxyServicePlugin::~CHtiIpProxyServicePlugin()
       
    68     {
       
    69     HTI_LOG_FUNC_IN( "~CHtiIpProxyServicePlugin" );
       
    70 
       
    71     if ( iProxyEngine )
       
    72         {
       
    73         iProxyEngine->StopListening();
       
    74         }
       
    75     delete iProxyEngine;
       
    76     delete iOutgoingArray;
       
    77 
       
    78     HTI_LOG_FUNC_OUT( "~CHtiIpProxyServicePlugin" );
       
    79     }
       
    80 
       
    81 // Second phase construction.
       
    82 void CHtiIpProxyServicePlugin::ConstructL()
       
    83     {
       
    84     HTI_LOG_FUNC_IN( "CHtiIpProxyServicePlugin::ConstructL" );
       
    85 
       
    86     iOutgoingArray = new( ELeave ) CDesC8ArraySeg( 10 );
       
    87     iProxyEngine = CIPProxyEngine::NewL( this );
       
    88 
       
    89     HTI_LOG_FUNC_OUT( "CHtiIpProxyServicePlugin::ConstructL" );
       
    90     }
       
    91 
       
    92 
       
    93 void CHtiIpProxyServicePlugin::InitL()
       
    94     {
       
    95     HTI_LOG_FUNC_IN( "CHtiIpProxyServicePlugin::InitL" );
       
    96 
       
    97     if ( iProxyEngine )
       
    98         {
       
    99         iProxyEngine->StartListening();
       
   100         }
       
   101 
       
   102     HTI_LOG_FUNC_OUT( "CHtiIpProxyServicePlugin::InitL" );
       
   103     }
       
   104 
       
   105 TBool CHtiIpProxyServicePlugin::IsBusy()
       
   106     {
       
   107     HTI_LOG_FUNC_IN( "CHtiIpProxyServicePlugin::IsBusy" );
       
   108     HTI_LOG_FUNC_OUT( "CHtiIpProxyServicePlugin::IsBusy" );
       
   109     return iBusy;
       
   110     }
       
   111 
       
   112 void CHtiIpProxyServicePlugin::ProcessMessageL( const TDesC8& aMessage,
       
   113                                                 THtiMessagePriority /* aPriority */ )
       
   114     {
       
   115     HTI_LOG_FUNC_IN( "CHtiIpProxyServicePlugin::ProcessMessageL" );
       
   116 
       
   117     iBusy = ETrue;
       
   118 
       
   119     __ASSERT_ALWAYS( iSocketObserver, User::Panic( _L ( "IP-Proxy ECom-plugin" ), KErrBadHandle ) );
       
   120     iSocketObserver->DataReceivedL( this, aMessage );
       
   121 
       
   122     iBusy = EFalse;
       
   123 
       
   124     HTI_LOG_FUNC_OUT( "CHtiIpProxyServicePlugin::ProcessMessageL" );
       
   125     }
       
   126 
       
   127 void CHtiIpProxyServicePlugin::NotifyMemoryChange( TInt aAvailableMemory )
       
   128     {
       
   129     HTI_LOG_FUNC_IN( "CHtiIpProxyServicePlugin::NotifyMemoryChange" );
       
   130 
       
   131     iBusy = ETrue;
       
   132 
       
   133     TInt count = iOutgoingArray->Count();
       
   134     if ( !count )
       
   135         {
       
   136         // No outgoing data
       
   137         return;
       
   138         }
       
   139 
       
   140     // Calculate needed outgoing buffer size
       
   141     TInt i = 0;
       
   142     TInt neededSize = 0;
       
   143     for ( ; i < count; ++i )
       
   144         {
       
   145         TInt size = ( *iOutgoingArray )[i].Size();
       
   146         if ( neededSize + size <= aAvailableMemory )
       
   147             {
       
   148             neededSize += size;
       
   149             }
       
   150         }
       
   151 
       
   152     if ( !neededSize )
       
   153        {
       
   154        // Not enough memory
       
   155        return;
       
   156        }
       
   157 
       
   158     // Create an outgoing buffer and join as many buffers as possible
       
   159     HBufC8* message = HBufC8::New( neededSize );
       
   160     if ( !message )
       
   161         {
       
   162         // Not enough memory
       
   163         return;
       
   164         }
       
   165 
       
   166     TPtr8 messagePtr( message->Des() );
       
   167     for ( TInt j = 0; j < i; ++j )
       
   168         {
       
   169         messagePtr.Append( ( *iOutgoingArray )[j] );
       
   170         }
       
   171 
       
   172     // Dispatch message to HTI-Framework which takes the ownership of message
       
   173     TInt err = iDispatcher->DispatchOutgoingMessage( message, KIpProxyServiceUid );
       
   174     if ( err == KErrNone )
       
   175         {
       
   176         message = NULL;
       
   177         iOutgoingArray->Delete( 0, i );
       
   178         iDispatcher->RemoveMemoryObserver( this );
       
   179         }
       
   180     else if ( err != KErrNoMemory )
       
   181         {
       
   182         delete message;
       
   183         iDispatcher->RemoveMemoryObserver( this );
       
   184 
       
   185         __ASSERT_ALWAYS( iHostObserver, User::Panic( _L ( "IP-Proxy ECom-plugin" ), KErrBadHandle ) );
       
   186         TRAP( err, iHostObserver->HostConnectionErrorL( err ) );
       
   187         }
       
   188 
       
   189     iBusy = EFalse;
       
   190 
       
   191     HTI_LOG_FUNC_OUT( "CHtiIpProxyServicePlugin::NotifyMemoryChange" );
       
   192     }
       
   193 
       
   194 MSocket* CHtiIpProxyServicePlugin::GetSocket()
       
   195     {
       
   196     return this;
       
   197     }
       
   198 
       
   199 MHostConnection* CHtiIpProxyServicePlugin::GetHostConnection()
       
   200     {
       
   201     return this;
       
   202     }
       
   203 
       
   204 RSocket* CHtiIpProxyServicePlugin::GetRSocket()
       
   205     {
       
   206     HTI_LOG_FUNC_IN( "CHtiIpProxyServicePlugin::GetRSocket" );
       
   207 
       
   208     RSocket* KSocket = NULL;
       
   209 
       
   210     HTI_LOG_FUNC_OUT( "CHtiIpProxyServicePlugin::GetRSocket" );
       
   211     return KSocket;
       
   212     }
       
   213 
       
   214 TUint CHtiIpProxyServicePlugin::LocalPort() const
       
   215     {
       
   216     HTI_LOG_FUNC_IN( "CHtiIpProxyServicePlugin::LocalPort" );
       
   217 
       
   218     const TUint KLocalPort = 0;
       
   219 
       
   220     HTI_LOG_FUNC_OUT( "CHtiIpProxyServicePlugin::LocalPort" );
       
   221     return KLocalPort;
       
   222     }
       
   223 
       
   224 TUint CHtiIpProxyServicePlugin::RemotePort() const
       
   225     {
       
   226     HTI_LOG_FUNC_IN( "CHtiIpProxyServicePlugin::RemotePort" );
       
   227 
       
   228     const TUint KRemotePort = 0;
       
   229 
       
   230     HTI_LOG_FUNC_OUT( "CHtiIpProxyServicePlugin::RemotePort" );
       
   231     return KRemotePort;
       
   232     }
       
   233 
       
   234 void CHtiIpProxyServicePlugin::SetObserver( MSocketObserver* aObserver )
       
   235     {
       
   236     HTI_LOG_FUNC_IN( "CHtiIpProxyServicePlugin::SetObserver" );
       
   237 
       
   238     iSocketObserver = aObserver;
       
   239 
       
   240     HTI_LOG_FUNC_OUT( "CHtiIpProxyServicePlugin::SetObserver" );
       
   241     }
       
   242 
       
   243 void CHtiIpProxyServicePlugin::SetSocketOwnershipMode( TBool /* aHasOwnership */ )
       
   244     {
       
   245     HTI_LOG_FUNC_IN( "CHtiIpProxyServicePlugin::SetSocketOwnershipMode" );
       
   246 
       
   247     /* Empty implementation */
       
   248 
       
   249     HTI_LOG_FUNC_OUT( "CHtiIpProxyServicePlugin::SetSocketOwnershipMode" );
       
   250     }
       
   251 
       
   252 void CHtiIpProxyServicePlugin::IssueRead()
       
   253     {
       
   254     HTI_LOG_FUNC_IN( "CHtiIpProxyServicePlugin::IssueRead" );
       
   255 
       
   256     /* Empty implementation */
       
   257 
       
   258     HTI_LOG_FUNC_OUT( "CHtiIpProxyServicePlugin::IssueRead" );
       
   259     }
       
   260 
       
   261 void CHtiIpProxyServicePlugin::Cancel()
       
   262     {
       
   263     HTI_LOG_FUNC_IN( "CHtiIpProxyServicePlugin::Cancel" );
       
   264 
       
   265     /* Empty implementation */
       
   266 
       
   267     HTI_LOG_FUNC_OUT( "CHtiIpProxyServicePlugin::Cancel" );
       
   268     }
       
   269 
       
   270 void CHtiIpProxyServicePlugin::SocketInfo( TProtocolDesc& aDesc ) const
       
   271     {
       
   272     HTI_LOG_FUNC_IN( "CHtiIpProxyServicePlugin::SocketInfo" );
       
   273 
       
   274     aDesc.iProtocol = KProtocolInetTcp;
       
   275 
       
   276     HTI_LOG_FUNC_OUT( "CHtiIpProxyServicePlugin::SocketInfo" );
       
   277     }
       
   278 
       
   279 TBool CHtiIpProxyServicePlugin::IsUDP() const
       
   280     {
       
   281     HTI_LOG_FUNC_IN( "CHtiIpProxyServicePlugin::IsUDP" );
       
   282     HTI_LOG_FUNC_OUT( "CHtiIpProxyServicePlugin::IsUDP" );
       
   283     return EFalse;
       
   284     }
       
   285 
       
   286 void CHtiIpProxyServicePlugin::WriteL( const TDesC8& aData )
       
   287     {
       
   288     HTI_LOG_FUNC_IN( "CHtiIpProxyServicePlugin::WriteL" );
       
   289 
       
   290     iBusy = ETrue;
       
   291 
       
   292     // Dispatcher takes ownership of the message if no error has occured
       
   293     HBufC8* message = aData.AllocL();
       
   294     TInt err = iDispatcher->DispatchOutgoingMessage( message, KIpProxyServiceUid );
       
   295     if ( err != KErrNone && err != KErrNoMemory )
       
   296         {
       
   297         // Some error occured while dispatching the message
       
   298         delete message;
       
   299         __ASSERT_ALWAYS( iHostObserver, User::Panic( _L ( "IP-Proxy ECom-plugin" ), KErrBadHandle ) );
       
   300         iHostObserver->HostConnectionErrorL( err );
       
   301         }
       
   302     else if ( err == KErrNoMemory )
       
   303         {
       
   304         // No memory was available so add message to queue
       
   305         delete message;
       
   306         iOutgoingArray->AppendL( aData.Mid( 0 ) );
       
   307         iDispatcher->AddMemoryObserver( this );
       
   308         }
       
   309 
       
   310     iBusy = EFalse;
       
   311 
       
   312     HTI_LOG_FUNC_OUT( "CHtiIpProxyServicePlugin::WriteL" );
       
   313     }
       
   314 
       
   315 void CHtiIpProxyServicePlugin::IssueConnectL()
       
   316     {
       
   317     HTI_LOG_FUNC_IN( "CHtiIpProxyServicePlugin::IssueConnectL" );
       
   318 
       
   319     iConnected = ETrue;
       
   320 
       
   321     __ASSERT_ALWAYS( iHostObserver, User::Panic( _L ( "IP-Proxy ECom-plugin" ), KErrBadHandle ) );
       
   322     iHostObserver->ConnectionEstablishedL();
       
   323 
       
   324     HTI_LOG_FUNC_OUT( "CHtiIpProxyServicePlugin::IssueConnectL" );
       
   325     }
       
   326 
       
   327 void CHtiIpProxyServicePlugin::IssueDisconnect()
       
   328     {
       
   329     HTI_LOG_FUNC_IN( "CHtiIpProxyServicePlugin::IssueDisconnect" );
       
   330 
       
   331     __ASSERT_ALWAYS( iSocketObserver, User::Panic( _L ( "IP-Proxy ECom-plugin" ), KErrBadHandle ) );
       
   332     TRAP_IGNORE( iSocketObserver->DisconnectedL( this ) );
       
   333 
       
   334     iConnected = EFalse;
       
   335 
       
   336     HTI_LOG_FUNC_OUT( "CHtiIpProxyServicePlugin::IssueDisconnect" );
       
   337     }
       
   338 
       
   339 void CHtiIpProxyServicePlugin::SetObserver( MHostConnectionObserver* aObserver )
       
   340     {
       
   341     HTI_LOG_FUNC_IN( "CHtiIpProxyServicePlugin::SetObserver 2" );
       
   342 
       
   343     iHostObserver = aObserver;
       
   344 
       
   345     HTI_LOG_FUNC_OUT( "CHtiIpProxyServicePlugin::SetObserver 2" );
       
   346     }
       
   347 
       
   348 TBool CHtiIpProxyServicePlugin::IsConnected()
       
   349     {
       
   350     HTI_LOG_FUNC_IN( "CHtiIpProxyServicePlugin::IsConnected" );
       
   351     HTI_LOG_FUNC_OUT( "CHtiIpProxyServicePlugin::IsConnected" );
       
   352     return iConnected;
       
   353     }
       
   354 
       
   355 RSocket* CHtiIpProxyServicePlugin::Socket()
       
   356     {
       
   357     HTI_LOG_FUNC_IN( "CHtiIpProxyServicePlugin::Socket" );
       
   358 
       
   359     RSocket* KSocket = NULL;
       
   360 
       
   361     HTI_LOG_FUNC_OUT( "CHtiIpProxyServicePlugin::Socket" );
       
   362     return KSocket;
       
   363     }