LCOV - code coverage report
Current view: top level - desktop/source/offacc - acceptor.cxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 94 115 81.7 %
Date: 2014-11-03 Functions: 14 17 82.4 %
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 <sal/config.h>
      21             : 
      22             : #include "acceptor.hxx"
      23             : #include <com/sun/star/bridge/BridgeFactory.hpp>
      24             : #include <com/sun/star/connection/Acceptor.hpp>
      25             : #include <com/sun/star/uno/XNamingService.hpp>
      26             : #include <comphelper/processfactory.hxx>
      27             : #include <cppuhelper/factory.hxx>
      28             : #include <cppuhelper/supportsservice.hxx>
      29             : 
      30             : using namespace css::bridge;
      31             : using namespace css::connection;
      32             : using namespace css::container;
      33             : using namespace css::lang;
      34             : using namespace css::uno;
      35             : 
      36             : namespace desktop
      37             : {
      38             : 
      39         160 : extern "C" void offacc_workerfunc (void * acc)
      40             : {
      41         160 :     osl_setThreadName("URP Acceptor");
      42             : 
      43         160 :     ((Acceptor*)acc)->run();
      44         160 : }
      45             : 
      46         160 : Acceptor::Acceptor( const Reference< XComponentContext >& rxContext )
      47             :     : m_thread(NULL)
      48             :     , m_rContext(rxContext)
      49             :     , m_bInit(false)
      50         160 :     , m_bDying(false)
      51             : {
      52         160 :     m_rAcceptor = css::connection::Acceptor::create(m_rContext);
      53         160 :     m_rBridgeFactory = BridgeFactory::create(m_rContext);
      54         160 : }
      55             : 
      56             : 
      57         480 : Acceptor::~Acceptor()
      58             : {
      59         160 :     m_rAcceptor->stopAccepting();
      60             :     oslThread t;
      61             :     {
      62         160 :         osl::MutexGuard g(m_aMutex);
      63         160 :         t = m_thread;
      64             :     }
      65             :     //prevent locking if the thread is still waiting
      66         160 :     m_bDying = true;
      67         160 :     m_cEnable.set();
      68         160 :     osl_joinWithThread(t);
      69             :     {
      70             :         // Make the final state of m_bridges visible to this thread (since
      71             :         // m_thread is joined, the code that follows is the only one left
      72             :         // accessing m_bridges):
      73         160 :         osl::MutexGuard g(m_aMutex);
      74             :     }
      75             :     for (;;) {
      76         254 :         css::uno::Reference< css::bridge::XBridge > b(m_bridges.remove());
      77         254 :         if (!b.is()) {
      78         160 :             break;
      79             :         }
      80             :         css::uno::Reference< css::lang::XComponent >(
      81          94 :             b, css::uno::UNO_QUERY_THROW)->dispose();
      82          94 :     }
      83         320 : }
      84             : 
      85         160 : void Acceptor::run()
      86             : {
      87             :     SAL_INFO( "desktop.offacc", "Acceptor::run" );
      88             :     for (;;)
      89             :     {
      90             :         try
      91             :         {
      92             :             // wait until we get enabled
      93             :             SAL_INFO( "desktop.offacc",
      94             :                 "Acceptor::run waiting for office to come up");
      95         256 :             m_cEnable.wait();
      96         256 :             if (m_bDying) //see destructor
      97         224 :                 break;
      98             :             SAL_INFO( "desktop.offacc",
      99             :                 "Acceptor::run now enabled and continuing");
     100             : 
     101             :             // accept connection
     102         192 :             Reference< XConnection > rConnection = m_rAcceptor->accept( m_aConnectString );
     103             :             // if we return without a valid connection we mus assume that the acceptor
     104             :             // is destructed so we break out of the run method terminating the thread
     105         192 :             if (! rConnection.is()) break;
     106         192 :             OUString aDescription = rConnection->getDescription();
     107             :             SAL_INFO( "desktop.offacc", "Acceptor::run connection " << aDescription );
     108             : 
     109             :             // create instanceprovider for this connection
     110             :             Reference< XInstanceProvider > rInstanceProvider(
     111         192 :                 new AccInstanceProvider(m_rContext, rConnection));
     112             :             // create the bridge. The remote end will have a reference to this bridge
     113             :             // thus preventing the bridge from being disposed. When the remote end releases
     114             :             // the bridge, it will be destructed.
     115          96 :             Reference< XBridge > rBridge = m_rBridgeFactory->createBridge(
     116         192 :                 "", m_aProtocol, rConnection, rInstanceProvider);
     117         192 :             osl::MutexGuard g(m_aMutex);
     118         192 :             m_bridges.add(rBridge);
     119           0 :         } catch (const Exception& e) {
     120             :             SAL_WARN("desktop.offacc", "caught Exception \"" << e.Message << "\"");
     121             :             // connection failed...
     122             :             // something went wrong during connection setup.
     123             :             // just wait for a new connection to accept
     124             :         }
     125          96 :     }
     126         160 : }
     127             : 
     128             : // XInitialize
     129         256 : void Acceptor::initialize( const Sequence<Any>& aArguments )
     130             :     throw( Exception, std::exception )
     131             : {
     132             :     // prevent multiple initialization
     133         256 :     osl::ClearableMutexGuard aGuard( m_aMutex );
     134             :     SAL_INFO( "desktop.offacc", "Acceptor::initialize()" );
     135             : 
     136         256 :     bool bOk = false;
     137             : 
     138             :     // arg count
     139         256 :     int nArgs = aArguments.getLength();
     140             : 
     141             :     // not yet initialized and acceptstring
     142         256 :     if (!m_bInit && nArgs > 0 && (aArguments[0] >>= m_aAcceptString))
     143             :     {
     144             :         SAL_INFO( "desktop.offacc", "Acceptor::initialize string=" << m_aAcceptString );
     145             : 
     146             :         // get connect string and protocol from accept string
     147             :         // "<connectString>;<protocol>"
     148         160 :         sal_Int32 nIndex1 = m_aAcceptString.indexOf( ';' );
     149         160 :         if (nIndex1 < 0)
     150             :             throw IllegalArgumentException(
     151           0 :                     "Invalid accept-string format", m_rContext, 1);
     152         160 :         m_aConnectString = m_aAcceptString.copy( 0 , nIndex1 ).trim();
     153         160 :         nIndex1++;
     154         160 :         sal_Int32 nIndex2 = m_aAcceptString.indexOf( ';' , nIndex1 );
     155         160 :         if (nIndex2 < 0) nIndex2 = m_aAcceptString.getLength();
     156         160 :         m_aProtocol = m_aAcceptString.copy( nIndex1, nIndex2 - nIndex1 );
     157             : 
     158             :         // start accepting in new thread...
     159         160 :         m_thread = osl_createThread(offacc_workerfunc, this);
     160         160 :         m_bInit = true;
     161         160 :         bOk = true;
     162             :     }
     163             : 
     164             :     // do we want to enable accepting?
     165         256 :     bool bEnable = false;
     166         352 :     if (((nArgs == 1 && (aArguments[0] >>= bEnable)) ||
     167         416 :          (nArgs == 2 && (aArguments[1] >>= bEnable))) &&
     168             :         bEnable )
     169             :     {
     170          96 :         m_cEnable.set();
     171          96 :         bOk = true;
     172             :     }
     173             : 
     174         256 :     if (!bOk)
     175             :     {
     176           0 :         throw IllegalArgumentException( "invalid initialization", m_rContext, 1);
     177         256 :     }
     178         256 : }
     179             : 
     180             : // XServiceInfo
     181         320 : OUString Acceptor::impl_getImplementationName()
     182             : {
     183         320 :     return OUString("com.sun.star.office.comp.Acceptor");
     184             : }
     185           0 : OUString Acceptor::getImplementationName()
     186             :     throw (RuntimeException, std::exception)
     187             : {
     188           0 :     return Acceptor::impl_getImplementationName();
     189             : }
     190         160 : Sequence<OUString> Acceptor::impl_getSupportedServiceNames()
     191             : {
     192         160 :     Sequence<OUString> aSequence(1);
     193         160 :     aSequence[0] = "com.sun.star.office.Acceptor";
     194         160 :     return aSequence;
     195             : }
     196           0 : Sequence<OUString> Acceptor::getSupportedServiceNames()
     197             :     throw (RuntimeException, std::exception)
     198             : {
     199           0 :     return Acceptor::impl_getSupportedServiceNames();
     200             : }
     201             : 
     202           0 : sal_Bool Acceptor::supportsService(OUString const & ServiceName)
     203             :     throw (css::uno::RuntimeException, std::exception)
     204             : {
     205           0 :     return cppu::supportsService(this, ServiceName);
     206             : }
     207             : 
     208             : // Factory
     209         160 : Reference< XInterface > Acceptor::impl_getInstance( const Reference< XMultiServiceFactory >& aFactory )
     210             : {
     211             :     try {
     212         160 :         return (XComponent*) new Acceptor( comphelper::getComponentContext(aFactory) );
     213           0 :     } catch ( const Exception& ) {
     214           0 :         return (XComponent*) NULL;
     215             :     }
     216             : }
     217             : 
     218             : // InstanceProvider
     219          96 : AccInstanceProvider::AccInstanceProvider(const Reference<XComponentContext>& rxContext, const Reference<XConnection>& rConnection)
     220             : {
     221          96 :     m_rContext = rxContext;
     222          96 :     m_rConnection = rConnection;
     223          96 : }
     224             : 
     225         184 : AccInstanceProvider::~AccInstanceProvider()
     226             : {
     227         184 : }
     228             : 
     229         132 : Reference<XInterface> AccInstanceProvider::getInstance (const OUString& aName )
     230             :         throw ( NoSuchElementException, std::exception )
     231             : {
     232             : 
     233         132 :     Reference<XInterface> rInstance;
     234             : 
     235         132 :     if ( aName.equalsAscii( "StarOffice.ServiceManager" ) )
     236             :     {
     237          36 :         rInstance = Reference< XInterface >( m_rContext->getServiceManager() );
     238             :     }
     239          96 :     else if(aName.equalsAscii( "StarOffice.ComponentContext" ) )
     240             :     {
     241          96 :         rInstance = m_rContext;
     242             :     }
     243           0 :     else if ( aName.equalsAscii("StarOffice.NamingService" ) )
     244             :     {
     245             :         Reference< XNamingService > rNamingService(
     246           0 :             m_rContext->getServiceManager()->createInstanceWithContext("com.sun.star.uno.NamingService", m_rContext),
     247           0 :             UNO_QUERY );
     248           0 :         if ( rNamingService.is() )
     249             :         {
     250           0 :             rNamingService->registerObject(
     251           0 :                 OUString("StarOffice.ServiceManager" ), m_rContext->getServiceManager() );
     252           0 :             rNamingService->registerObject(
     253           0 :                 OUString("StarOffice.ComponentContext" ), m_rContext );
     254           0 :             rInstance = rNamingService;
     255           0 :         }
     256             :     }
     257         132 :     return rInstance;
     258             : }
     259             : 
     260             : }
     261             : 
     262             : // component management stuff...
     263             : 
     264             : extern "C"
     265             : {
     266             : using namespace desktop;
     267             : 
     268         160 : SAL_DLLPUBLIC_EXPORT void * SAL_CALL offacc_component_getFactory(char const *pImplementationName, void *pServiceManager, void *)
     269             : {
     270         160 :     void* pReturn = NULL ;
     271         160 :     if  ( pImplementationName && pServiceManager )
     272             :     {
     273             :         // Define variables which are used in following macros.
     274         160 :         Reference< XSingleServiceFactory > xFactory;
     275             :         Reference< XMultiServiceFactory >  xServiceManager(
     276         320 :             reinterpret_cast< XMultiServiceFactory* >(pServiceManager));
     277             : 
     278         160 :         if (desktop::Acceptor::impl_getImplementationName().equalsAscii( pImplementationName ) )
     279             :         {
     280         320 :             xFactory = Reference< XSingleServiceFactory >( cppu::createSingleFactory(
     281             :                 xServiceManager, desktop::Acceptor::impl_getImplementationName(),
     282         160 :                 desktop::Acceptor::impl_getInstance, desktop::Acceptor::impl_getSupportedServiceNames()) );
     283             :         }
     284             : 
     285             :         // Factory is valid - service was found.
     286         160 :         if ( xFactory.is() )
     287             :         {
     288         160 :             xFactory->acquire();
     289         160 :             pReturn = xFactory.get();
     290         160 :         }
     291             :     }
     292             : 
     293             :     // Return with result of this operation.
     294         160 :     return pReturn ;
     295             : }
     296             : 
     297             : } // extern "C"
     298             : 
     299             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10