LCOV - code coverage report
Current view: top level - io/source/acceptor - acceptor.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 50 100 50.0 %
Date: 2014-04-11 Functions: 10 13 76.9 %
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/supportsservice.hxx>
      28             : #include "cppuhelper/unourl.hxx"
      29             : #include "rtl/malformeduriexception.hxx"
      30             : 
      31             : #include <com/sun/star/connection/XAcceptor.hpp>
      32             : #include <com/sun/star/lang/XServiceInfo.hpp>
      33             : 
      34             : #include "services.hxx"
      35             : #include "acceptor.hxx"
      36             : 
      37             : #define IMPLEMENTATION_NAME "com.sun.star.comp.io.Acceptor"
      38             : #define SERVICE_NAME "com.sun.star.connection.Acceptor"
      39             : 
      40             : using namespace ::osl;
      41             : using namespace ::rtl;
      42             : using namespace ::cppu;
      43             : using namespace ::com::sun::star::uno;
      44             : using namespace ::com::sun::star::lang;
      45             : using namespace ::com::sun::star::registry;
      46             : using namespace ::com::sun::star::connection;
      47             : 
      48             : namespace io_acceptor
      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, std::exception) SAL_OVERRIDE;
      62             :         virtual void SAL_CALL stopAccepting(  ) throw( RuntimeException, std::exception) SAL_OVERRIDE;
      63             : 
      64             :     public: // XServiceInfo
      65             :                 virtual OUString              SAL_CALL getImplementationName() throw(std::exception) SAL_OVERRIDE;
      66             :                 virtual Sequence< OUString >  SAL_CALL getSupportedServiceNames(void) throw(std::exception) SAL_OVERRIDE;
      67             :                 virtual sal_Bool              SAL_CALL supportsService(const OUString& ServiceName) throw(std::exception) SAL_OVERRIDE;
      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          82 :     OAcceptor::OAcceptor( const Reference< XComponentContext > & xCtx )
      83             :         : m_pPipe( 0 )
      84             :         , m_pSocket( 0 )
      85             :         , m_bInAccept( sal_False )
      86          82 :         , _xSMgr( xCtx->getServiceManager() )
      87         164 :         , _xCtx( xCtx )
      88          82 :     {}
      89             : 
      90         246 :     OAcceptor::~OAcceptor()
      91             :     {
      92          82 :         if( m_pPipe )
      93             :         {
      94          49 :             delete m_pPipe;
      95             :         }
      96          82 :         if( m_pSocket )
      97             :         {
      98           0 :             delete m_pSocket;
      99             :         }
     100         164 :     }
     101             : 
     102             :     struct BeingInAccept
     103             :     {
     104          97 :         BeingInAccept( sal_Bool *pFlag,const OUString & sConnectionDescription  ) throw( AlreadyAcceptingException)
     105          97 :             : m_pFlag( pFlag )
     106             :             {
     107          97 :                   if( *m_pFlag )
     108             :                   {
     109           0 :                       OUString sMessage( "AlreadyAcceptingException :" );
     110           0 :                       sMessage += sConnectionDescription;
     111           0 :                       throw AlreadyAcceptingException( sMessage , Reference< XInterface > () );
     112             :                   }
     113          97 :                   *m_pFlag = sal_True;
     114          97 :             }
     115          97 :         ~BeingInAccept()
     116             :             {
     117          97 :                 *m_pFlag = sal_False;
     118          97 :             }
     119             :         sal_Bool *m_pFlag;
     120             :     };
     121             : 
     122          97 :     Reference< XConnection > OAcceptor::accept( const OUString &sConnectionDescription )
     123             :         throw( AlreadyAcceptingException,
     124             :                ConnectionSetupException,
     125             :                IllegalArgumentException,
     126             :                RuntimeException, std::exception)
     127             :     {
     128             :         OSL_TRACE(
     129             :             "acceptor %s\n",
     130             :             OUStringToOString(
     131             :                 sConnectionDescription, RTL_TEXTENCODING_ASCII_US).getStr());
     132             :         // if there is a thread alread accepting in this object, throw an exception.
     133          97 :         struct BeingInAccept guard( &m_bInAccept, sConnectionDescription );
     134             : 
     135          97 :         Reference< XConnection > r;
     136         145 :         if( !m_sLastDescription.isEmpty() &&
     137          48 :             m_sLastDescription != sConnectionDescription )
     138             :         {
     139             :             // instantiate another acceptor for different ports
     140           0 :             OUString sMessage = "acceptor::accept called multiple times with different conncetion strings\n";
     141           0 :             throw ConnectionSetupException( sMessage, Reference< XInterface > () );
     142             :         }
     143             : 
     144          97 :         if( m_sLastDescription.isEmpty() )
     145             :         {
     146             :             // setup the acceptor
     147             :             try
     148             :             {
     149          49 :                 cppu::UnoUrlDescriptor aDesc(sConnectionDescription);
     150          49 :                 if ( aDesc.getName() == "pipe" )
     151             :                 {
     152             :                     OUString aName(
     153             :                         aDesc.getParameter(
     154          49 :                             OUString("name")));
     155             : 
     156          49 :                     m_pPipe = new PipeAcceptor(aName, sConnectionDescription);
     157             : 
     158             :                     try
     159             :                     {
     160          49 :                         m_pPipe->init();
     161             :                     }
     162           0 :                     catch( ... )
     163             :                     {
     164             :                         {
     165           0 :                             MutexGuard g( m_mutex );
     166           0 :                             delete m_pPipe;
     167           0 :                             m_pPipe = 0;
     168             :                         }
     169           0 :                         throw;
     170          49 :                     }
     171             :                 }
     172           0 :                 else if ( aDesc.getName() == "socket" )
     173             :                 {
     174           0 :                     OUString aHost;
     175           0 :                     if (aDesc.hasParameter(
     176           0 :                             OUString("host")))
     177           0 :                         aHost = aDesc.getParameter(
     178           0 :                             OUString("host"));
     179             :                     else
     180           0 :                         aHost = "localhost";
     181             :                     sal_uInt16 nPort = static_cast< sal_uInt16 >(
     182             :                         aDesc.getParameter(
     183             :                             OUString("port")).
     184           0 :                         toInt32());
     185             :                     bool bTcpNoDelay
     186             :                         = aDesc.getParameter(
     187           0 :                             OUString("tcpnodelay")).toInt32() != 0;
     188             : 
     189             :                     m_pSocket = new SocketAcceptor(
     190           0 :                         aHost, nPort, bTcpNoDelay, sConnectionDescription);
     191             : 
     192             :                     try
     193             :                     {
     194           0 :                         m_pSocket->init();
     195             :                     }
     196           0 :                     catch( ... )
     197             :                     {
     198             :                         {
     199           0 :                             MutexGuard g( m_mutex );
     200           0 :                             delete m_pSocket;
     201           0 :                             m_pSocket = 0;
     202             :                         }
     203           0 :                         throw;
     204           0 :                     }
     205             :                 }
     206             :                 else
     207             :                 {
     208           0 :                     OUString delegatee = "com.sun.star.connection.Acceptor." + aDesc.getName();
     209             : 
     210             :                     OSL_TRACE(
     211             :                         "trying to get service %s\n",
     212             :                         OUStringToOString(
     213             :                             delegatee, RTL_TEXTENCODING_ASCII_US).getStr());
     214           0 :                     _xAcceptor = Reference<XAcceptor>(
     215           0 :                         _xSMgr->createInstanceWithContext(delegatee, _xCtx), UNO_QUERY);
     216             : 
     217           0 :                     if(!_xAcceptor.is())
     218             :                     {
     219           0 :                         OUString message("Acceptor: unknown delegatee ");
     220           0 :                         message += delegatee;
     221             : 
     222           0 :                         throw ConnectionSetupException(message, Reference<XInterface>());
     223           0 :                     }
     224          49 :                 }
     225             :             }
     226           0 :             catch (const rtl::MalformedUriException & rEx)
     227             :             {
     228             :                 throw IllegalArgumentException(
     229           0 :                     rEx.getMessage(),
     230             :                     Reference< XInterface > (),
     231           0 :                     0 );
     232             :             }
     233          49 :             m_sLastDescription = sConnectionDescription;
     234             :         }
     235             : 
     236          97 :         if( m_pPipe )
     237             :         {
     238          97 :             r = m_pPipe->accept();
     239             :         }
     240           0 :         else if( m_pSocket )
     241             :         {
     242           0 :             r = m_pSocket->accept();
     243             :         }
     244             :         else
     245             :         {
     246           0 :             r = _xAcceptor->accept(sConnectionDescription);
     247             :         }
     248             : 
     249          97 :         return r;
     250             :     }
     251             : 
     252          80 :     void SAL_CALL OAcceptor::stopAccepting(  ) throw( RuntimeException, std::exception)
     253             :     {
     254          80 :         MutexGuard guard( m_mutex );
     255             : 
     256          80 :         if( m_pPipe )
     257             :         {
     258          48 :             m_pPipe->stopAccepting();
     259             :         }
     260          32 :         else if ( m_pSocket )
     261             :         {
     262           0 :             m_pSocket->stopAccepting();
     263             :         }
     264          32 :         else if( _xAcceptor.is() )
     265             :         {
     266           0 :             _xAcceptor->stopAccepting();
     267          80 :         }
     268             : 
     269          80 :     }
     270             : 
     271         103 :     OUString acceptor_getImplementationName()
     272             :     {
     273         103 :         return OUString( IMPLEMENTATION_NAME );
     274             :     }
     275             : 
     276          82 :     Reference< XInterface > SAL_CALL acceptor_CreateInstance( const Reference< XComponentContext > & xCtx)
     277             :     {
     278          82 :         return Reference < XInterface >( ( OWeakObject * ) new OAcceptor(xCtx) );
     279             :     }
     280             : 
     281          82 :     Sequence< OUString > acceptor_getSupportedServiceNames()
     282             :     {
     283          82 :         Sequence< OUString > seqNames(1);
     284          82 :         seqNames.getArray()[0] = SERVICE_NAME;
     285          82 :         return seqNames;
     286             :     }
     287             : 
     288           0 :         OUString OAcceptor::getImplementationName() throw(std::exception)
     289             :     {
     290           0 :         return acceptor_getImplementationName();
     291             :     }
     292             : 
     293           0 :         sal_Bool OAcceptor::supportsService(const OUString& ServiceName) throw(std::exception)
     294             :     {
     295           0 :         return cppu::supportsService(this, ServiceName);
     296             :     }
     297             : 
     298           0 :         Sequence< OUString > OAcceptor::getSupportedServiceNames(void) throw(std::exception)
     299             :     {
     300           0 :         return acceptor_getSupportedServiceNames();
     301             :     }
     302             : 
     303             : 
     304             : }
     305             : 
     306             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10