LCOV - code coverage report
Current view: top level - libreoffice/io/source/acceptor - acceptor.cxx (source / functions) Hit Total Coverage
Test: libreoffice_filtered.info Lines: 0 110 0.0 %
Date: 2012-12-27 Functions: 0 15 0.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2             : /*
       3             :  * This file is part of the LibreOffice project.
       4             :  *
       5             :  * This Source Code Form is subject to the terms of the Mozilla Public
       6             :  * License, v. 2.0. If a copy of the MPL was not distributed with this
       7             :  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
       8             :  *
       9             :  * This file incorporates work covered by the following license notice:
      10             :  *
      11             :  *   Licensed to the Apache Software Foundation (ASF) under one or more
      12             :  *   contributor license agreements. See the NOTICE file distributed
      13             :  *   with this work for additional information regarding copyright
      14             :  *   ownership. The ASF licenses this file to you under the Apache
      15             :  *   License, Version 2.0 (the "License"); you may not use this file
      16             :  *   except in compliance with the License. You may obtain a copy of
      17             :  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
      18             :  */
      19             : 
      20             : #include <osl/mutex.hxx>
      21             : 
      22             : #include <uno/mapping.hxx>
      23             : 
      24             : #include <cppuhelper/factory.hxx>
      25             : #include <cppuhelper/implbase2.hxx>
      26             : #include <cppuhelper/implementationentry.hxx>
      27             : #include "cppuhelper/unourl.hxx"
      28             : #include "rtl/malformeduriexception.hxx"
      29             : 
      30             : #include <com/sun/star/connection/XAcceptor.hpp>
      31             : #include <com/sun/star/lang/XServiceInfo.hpp>
      32             : 
      33             : #include "acceptor.hxx"
      34             : 
      35             : #define IMPLEMENTATION_NAME "com.sun.star.comp.io.Acceptor"
      36             : #define SERVICE_NAME "com.sun.star.connection.Acceptor"
      37             : 
      38             : using namespace ::osl;
      39             : using namespace ::rtl;
      40             : using namespace ::cppu;
      41             : using namespace ::com::sun::star::uno;
      42             : using namespace ::com::sun::star::lang;
      43             : using namespace ::com::sun::star::registry;
      44             : using namespace ::com::sun::star::connection;
      45             : 
      46             : namespace io_acceptor
      47             : {
      48             :     rtl_StandardModuleCount g_moduleCount = MODULE_COUNT_INIT;
      49             : 
      50             :     class OAcceptor : public WeakImplHelper2< XAcceptor, XServiceInfo >
      51             :     {
      52             :     public:
      53             :         OAcceptor(const Reference< XComponentContext > & xCtx);
      54             :         virtual ~OAcceptor();
      55             :     public:
      56             :         // Methods
      57             :         virtual Reference< XConnection > SAL_CALL accept( const OUString& sConnectionDescription )
      58             :             throw( AlreadyAcceptingException,
      59             :                    ConnectionSetupException,
      60             :                    IllegalArgumentException,
      61             :                    RuntimeException);
      62             :         virtual void SAL_CALL stopAccepting(  ) throw( RuntimeException);
      63             : 
      64             :     public: // XServiceInfo
      65             :                 virtual OUString              SAL_CALL getImplementationName() throw();
      66             :                 virtual Sequence< OUString >  SAL_CALL getSupportedServiceNames(void) throw();
      67             :                 virtual sal_Bool              SAL_CALL supportsService(const OUString& ServiceName) throw();
      68             : 
      69             :     private:
      70             :         PipeAcceptor *m_pPipe;
      71             :         SocketAcceptor *m_pSocket;
      72             :         Mutex m_mutex;
      73             :         OUString m_sLastDescription;
      74             :         sal_Bool m_bInAccept;
      75             : 
      76             :         Reference< XMultiComponentFactory > _xSMgr;
      77             :         Reference< XComponentContext > _xCtx;
      78             :         Reference<XAcceptor>         _xAcceptor;
      79             :     };
      80             : 
      81             : 
      82           0 :     OAcceptor::OAcceptor( const Reference< XComponentContext > & xCtx )
      83             :         : m_pPipe( 0 )
      84             :         , m_pSocket( 0 )
      85             :         , m_bInAccept( sal_False )
      86           0 :         , _xSMgr( xCtx->getServiceManager() )
      87           0 :         , _xCtx( xCtx )
      88             :     {
      89           0 :         g_moduleCount.modCnt.acquire( &g_moduleCount.modCnt );
      90           0 :     }
      91             : 
      92           0 :     OAcceptor::~OAcceptor()
      93             :     {
      94           0 :         if( m_pPipe )
      95             :         {
      96           0 :             delete m_pPipe;
      97             :         }
      98           0 :         if( m_pSocket )
      99             :         {
     100           0 :             delete m_pSocket;
     101             :         }
     102           0 :         g_moduleCount.modCnt.release( &g_moduleCount.modCnt );
     103           0 :     }
     104             : 
     105             :     struct BeingInAccept
     106             :     {
     107           0 :         BeingInAccept( sal_Bool *pFlag,const OUString & sConnectionDescription  ) throw( AlreadyAcceptingException)
     108           0 :             : m_pFlag( pFlag )
     109             :             {
     110           0 :                   if( *m_pFlag )
     111             :                   {
     112           0 :                       OUString sMessage( "AlreadyAcceptingException :" );
     113           0 :                       sMessage += sConnectionDescription;
     114           0 :                       throw AlreadyAcceptingException( sMessage , Reference< XInterface > () );
     115             :                   }
     116           0 :                   *m_pFlag = sal_True;
     117           0 :             }
     118           0 :         ~BeingInAccept()
     119             :             {
     120           0 :                 *m_pFlag = sal_False;
     121           0 :             }
     122             :         sal_Bool *m_pFlag;
     123             :     };
     124             : 
     125           0 :     Reference< XConnection > OAcceptor::accept( const OUString &sConnectionDescription )
     126             :         throw( AlreadyAcceptingException,
     127             :                ConnectionSetupException,
     128             :                IllegalArgumentException,
     129             :                RuntimeException)
     130             :     {
     131             :         OSL_TRACE(
     132             :             "acceptor %s\n",
     133             :             OUStringToOString(
     134             :                 sConnectionDescription, RTL_TEXTENCODING_ASCII_US).getStr());
     135             :         // if there is a thread alread accepting in this object, throw an exception.
     136           0 :         struct BeingInAccept guard( &m_bInAccept, sConnectionDescription );
     137             : 
     138           0 :         Reference< XConnection > r;
     139           0 :         if( !m_sLastDescription.isEmpty() &&
     140           0 :             m_sLastDescription != sConnectionDescription )
     141             :         {
     142             :             // instantiate another acceptor for different ports
     143           0 :             OUString sMessage = OUString("acceptor::accept called multiple times with different conncetion strings\n" );
     144           0 :             throw ConnectionSetupException( sMessage, Reference< XInterface > () );
     145             :         }
     146             : 
     147           0 :         if( m_sLastDescription.isEmpty() )
     148             :         {
     149             :             // setup the acceptor
     150             :             try
     151             :             {
     152           0 :                 cppu::UnoUrlDescriptor aDesc(sConnectionDescription);
     153           0 :                 if ( aDesc.getName() == "pipe" )
     154             :                 {
     155             :                     OUString aName(
     156             :                         aDesc.getParameter(
     157           0 :                             OUString("name")));
     158             : 
     159           0 :                     m_pPipe = new PipeAcceptor(aName, sConnectionDescription);
     160             : 
     161             :                     try
     162             :                     {
     163           0 :                         m_pPipe->init();
     164             :                     }
     165           0 :                     catch( ... )
     166             :                     {
     167             :                         {
     168           0 :                             MutexGuard g( m_mutex );
     169           0 :                             delete m_pPipe;
     170           0 :                             m_pPipe = 0;
     171             :                         }
     172           0 :                         throw;
     173           0 :                     }
     174             :                 }
     175           0 :                 else if ( aDesc.getName() == "socket" )
     176             :                 {
     177           0 :                     OUString aHost;
     178           0 :                     if (aDesc.hasParameter(
     179           0 :                             OUString("host")))
     180             :                         aHost = aDesc.getParameter(
     181           0 :                             OUString("host"));
     182             :                     else
     183           0 :                         aHost = OUString("localhost");
     184             :                     sal_uInt16 nPort = static_cast< sal_uInt16 >(
     185             :                         aDesc.getParameter(
     186             :                             OUString("port")).
     187           0 :                         toInt32());
     188             :                     bool bTcpNoDelay
     189             :                         = aDesc.getParameter(
     190           0 :                             OUString("tcpnodelay")).toInt32() != 0;
     191             : 
     192             :                     m_pSocket = new SocketAcceptor(
     193           0 :                         aHost, nPort, bTcpNoDelay, sConnectionDescription);
     194             : 
     195             :                     try
     196             :                     {
     197           0 :                         m_pSocket->init();
     198             :                     }
     199           0 :                     catch( ... )
     200             :                     {
     201             :                         {
     202           0 :                             MutexGuard g( m_mutex );
     203           0 :                             delete m_pSocket;
     204           0 :                             m_pSocket = 0;
     205             :                         }
     206           0 :                         throw;
     207           0 :                     }
     208             :                 }
     209             :                 else
     210             :                 {
     211           0 :                     OUString delegatee = OUString("com.sun.star.connection.Acceptor.");
     212           0 :                     delegatee += aDesc.getName();
     213             : 
     214             :                     OSL_TRACE(
     215             :                         "trying to get service %s\n",
     216             :                         OUStringToOString(
     217             :                             delegatee, RTL_TEXTENCODING_ASCII_US).getStr());
     218             :                     _xAcceptor = Reference<XAcceptor>(
     219           0 :                         _xSMgr->createInstanceWithContext(delegatee, _xCtx), UNO_QUERY);
     220             : 
     221           0 :                     if(!_xAcceptor.is())
     222             :                     {
     223           0 :                         OUString message("Acceptor: unknown delegatee ");
     224           0 :                         message += delegatee;
     225             : 
     226           0 :                         throw ConnectionSetupException(message, Reference<XInterface>());
     227           0 :                     }
     228           0 :                 }
     229             :             }
     230           0 :             catch (const rtl::MalformedUriException & rEx)
     231             :             {
     232             :                 throw IllegalArgumentException(
     233           0 :                     rEx.getMessage(),
     234             :                     Reference< XInterface > (),
     235           0 :                     0 );
     236             :             }
     237           0 :             m_sLastDescription = sConnectionDescription;
     238             :         }
     239             : 
     240           0 :         if( m_pPipe )
     241             :         {
     242           0 :             r = m_pPipe->accept();
     243             :         }
     244           0 :         else if( m_pSocket )
     245             :         {
     246           0 :             r = m_pSocket->accept();
     247             :         }
     248             :         else
     249             :         {
     250           0 :             r = _xAcceptor->accept(sConnectionDescription);
     251             :         }
     252             : 
     253           0 :         return r;
     254             :     }
     255             : 
     256           0 :     void SAL_CALL OAcceptor::stopAccepting(  ) throw( RuntimeException)
     257             :     {
     258           0 :         MutexGuard guard( m_mutex );
     259             : 
     260           0 :         if( m_pPipe )
     261             :         {
     262           0 :             m_pPipe->stopAccepting();
     263             :         }
     264           0 :         else if ( m_pSocket )
     265             :         {
     266           0 :             m_pSocket->stopAccepting();
     267             :         }
     268           0 :         else if( _xAcceptor.is() )
     269             :         {
     270           0 :             _xAcceptor->stopAccepting();
     271           0 :         }
     272             : 
     273           0 :     }
     274             : 
     275           0 :     OUString acceptor_getImplementationName()
     276             :     {
     277           0 :         return OUString( IMPLEMENTATION_NAME );
     278             :     }
     279             : 
     280           0 :     Reference< XInterface > SAL_CALL acceptor_CreateInstance( const Reference< XComponentContext > & xCtx)
     281             :     {
     282           0 :         return Reference < XInterface >( ( OWeakObject * ) new OAcceptor(xCtx) );
     283             :     }
     284             : 
     285           0 :     Sequence< OUString > acceptor_getSupportedServiceNames()
     286             :     {
     287           0 :         Sequence< OUString > seqNames(1);
     288           0 :         seqNames.getArray()[0] = OUString(SERVICE_NAME);
     289           0 :         return seqNames;
     290             :     }
     291             : 
     292           0 :         OUString OAcceptor::getImplementationName() throw()
     293             :     {
     294           0 :         return acceptor_getImplementationName();
     295             :     }
     296             : 
     297           0 :         sal_Bool OAcceptor::supportsService(const OUString& ServiceName) throw()
     298             :     {
     299           0 :         Sequence< OUString > aSNL = getSupportedServiceNames();
     300           0 :         const OUString * pArray = aSNL.getConstArray();
     301             : 
     302           0 :         for( sal_Int32 i = 0; i < aSNL.getLength(); i++ )
     303           0 :             if( pArray[i] == ServiceName )
     304           0 :                 return sal_True;
     305             : 
     306           0 :         return sal_False;
     307             :     }
     308             : 
     309           0 :         Sequence< OUString > OAcceptor::getSupportedServiceNames(void) throw()
     310             :     {
     311           0 :         return acceptor_getSupportedServiceNames();
     312             :     }
     313             : 
     314             : 
     315             : }
     316             : 
     317             : using namespace io_acceptor;
     318             : 
     319             : static struct ImplementationEntry g_entries[] =
     320             : {
     321             :     {
     322             :         acceptor_CreateInstance, acceptor_getImplementationName ,
     323             :         acceptor_getSupportedServiceNames, createSingleComponentFactory ,
     324             :         &g_moduleCount.modCnt , 0
     325             :     },
     326             :     { 0, 0, 0, 0, 0, 0 }
     327             : };
     328             : 
     329             : extern "C"
     330             : {
     331             : 
     332           0 : SAL_DLLPUBLIC_EXPORT sal_Bool SAL_CALL component_canUnload( TimeValue *pTime )
     333             : {
     334           0 :     return g_moduleCount.canUnload( &g_moduleCount , pTime );
     335             : }
     336             : 
     337             : //==================================================================================================
     338           0 : SAL_DLLPUBLIC_EXPORT void * SAL_CALL acceptor_component_getFactory(
     339             :     const sal_Char * pImplName, void * pServiceManager, void * pRegistryKey )
     340             : {
     341           0 :     return component_getFactoryHelper( pImplName, pServiceManager, pRegistryKey , g_entries );
     342             : }
     343             : }
     344             : 
     345             : 
     346             : 
     347             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10