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

Generated by: LCOV version 1.10