1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 29*cdf0e10cSrcweir #include "precompiled_io.hxx" 30*cdf0e10cSrcweir #include <osl/mutex.hxx> 31*cdf0e10cSrcweir 32*cdf0e10cSrcweir #include <uno/mapping.hxx> 33*cdf0e10cSrcweir 34*cdf0e10cSrcweir #include <cppuhelper/factory.hxx> 35*cdf0e10cSrcweir #include <cppuhelper/implbase2.hxx> 36*cdf0e10cSrcweir #include <cppuhelper/implementationentry.hxx> 37*cdf0e10cSrcweir #include "cppuhelper/unourl.hxx" 38*cdf0e10cSrcweir #include "rtl/malformeduriexception.hxx" 39*cdf0e10cSrcweir 40*cdf0e10cSrcweir #include <com/sun/star/connection/XAcceptor.hpp> 41*cdf0e10cSrcweir #include <com/sun/star/lang/XServiceInfo.hpp> 42*cdf0e10cSrcweir 43*cdf0e10cSrcweir #include "acceptor.hxx" 44*cdf0e10cSrcweir 45*cdf0e10cSrcweir #define IMPLEMENTATION_NAME "com.sun.star.comp.io.Acceptor" 46*cdf0e10cSrcweir #define SERVICE_NAME "com.sun.star.connection.Acceptor" 47*cdf0e10cSrcweir 48*cdf0e10cSrcweir using namespace ::osl; 49*cdf0e10cSrcweir using namespace ::rtl; 50*cdf0e10cSrcweir using namespace ::cppu; 51*cdf0e10cSrcweir using namespace ::com::sun::star::uno; 52*cdf0e10cSrcweir using namespace ::com::sun::star::lang; 53*cdf0e10cSrcweir using namespace ::com::sun::star::registry; 54*cdf0e10cSrcweir using namespace ::com::sun::star::connection; 55*cdf0e10cSrcweir 56*cdf0e10cSrcweir namespace io_acceptor 57*cdf0e10cSrcweir { 58*cdf0e10cSrcweir rtl_StandardModuleCount g_moduleCount = MODULE_COUNT_INIT; 59*cdf0e10cSrcweir 60*cdf0e10cSrcweir class OAcceptor : public WeakImplHelper2< XAcceptor, XServiceInfo > 61*cdf0e10cSrcweir { 62*cdf0e10cSrcweir public: 63*cdf0e10cSrcweir OAcceptor(const Reference< XComponentContext > & xCtx); 64*cdf0e10cSrcweir virtual ~OAcceptor(); 65*cdf0e10cSrcweir public: 66*cdf0e10cSrcweir // Methods 67*cdf0e10cSrcweir virtual Reference< XConnection > SAL_CALL accept( const OUString& sConnectionDescription ) 68*cdf0e10cSrcweir throw( AlreadyAcceptingException, 69*cdf0e10cSrcweir ConnectionSetupException, 70*cdf0e10cSrcweir IllegalArgumentException, 71*cdf0e10cSrcweir RuntimeException); 72*cdf0e10cSrcweir virtual void SAL_CALL stopAccepting( ) throw( RuntimeException); 73*cdf0e10cSrcweir 74*cdf0e10cSrcweir public: // XServiceInfo 75*cdf0e10cSrcweir virtual OUString SAL_CALL getImplementationName() throw(); 76*cdf0e10cSrcweir virtual Sequence< OUString > SAL_CALL getSupportedServiceNames(void) throw(); 77*cdf0e10cSrcweir virtual sal_Bool SAL_CALL supportsService(const OUString& ServiceName) throw(); 78*cdf0e10cSrcweir 79*cdf0e10cSrcweir private: 80*cdf0e10cSrcweir PipeAcceptor *m_pPipe; 81*cdf0e10cSrcweir SocketAcceptor *m_pSocket; 82*cdf0e10cSrcweir Mutex m_mutex; 83*cdf0e10cSrcweir OUString m_sLastDescription; 84*cdf0e10cSrcweir sal_Bool m_bInAccept; 85*cdf0e10cSrcweir 86*cdf0e10cSrcweir Reference< XMultiComponentFactory > _xSMgr; 87*cdf0e10cSrcweir Reference< XComponentContext > _xCtx; 88*cdf0e10cSrcweir Reference<XAcceptor> _xAcceptor; 89*cdf0e10cSrcweir }; 90*cdf0e10cSrcweir 91*cdf0e10cSrcweir 92*cdf0e10cSrcweir OAcceptor::OAcceptor( const Reference< XComponentContext > & xCtx ) 93*cdf0e10cSrcweir : m_pPipe( 0 ) 94*cdf0e10cSrcweir , m_pSocket( 0 ) 95*cdf0e10cSrcweir , m_bInAccept( sal_False ) 96*cdf0e10cSrcweir , _xSMgr( xCtx->getServiceManager() ) 97*cdf0e10cSrcweir , _xCtx( xCtx ) 98*cdf0e10cSrcweir { 99*cdf0e10cSrcweir g_moduleCount.modCnt.acquire( &g_moduleCount.modCnt ); 100*cdf0e10cSrcweir } 101*cdf0e10cSrcweir 102*cdf0e10cSrcweir OAcceptor::~OAcceptor() 103*cdf0e10cSrcweir { 104*cdf0e10cSrcweir if( m_pPipe ) 105*cdf0e10cSrcweir { 106*cdf0e10cSrcweir delete m_pPipe; 107*cdf0e10cSrcweir } 108*cdf0e10cSrcweir if( m_pSocket ) 109*cdf0e10cSrcweir { 110*cdf0e10cSrcweir delete m_pSocket; 111*cdf0e10cSrcweir } 112*cdf0e10cSrcweir g_moduleCount.modCnt.release( &g_moduleCount.modCnt ); 113*cdf0e10cSrcweir } 114*cdf0e10cSrcweir 115*cdf0e10cSrcweir struct BeingInAccept 116*cdf0e10cSrcweir { 117*cdf0e10cSrcweir BeingInAccept( sal_Bool *pFlag,const OUString & sConnectionDescription ) throw( AlreadyAcceptingException) 118*cdf0e10cSrcweir : m_pFlag( pFlag ) 119*cdf0e10cSrcweir { 120*cdf0e10cSrcweir if( *m_pFlag ) 121*cdf0e10cSrcweir { 122*cdf0e10cSrcweir OUString sMessage( RTL_CONSTASCII_USTRINGPARAM( "AlreadyAcceptingException :" ) ); 123*cdf0e10cSrcweir sMessage += sConnectionDescription; 124*cdf0e10cSrcweir throw AlreadyAcceptingException( sMessage , Reference< XInterface > () ); 125*cdf0e10cSrcweir } 126*cdf0e10cSrcweir *m_pFlag = sal_True; 127*cdf0e10cSrcweir } 128*cdf0e10cSrcweir ~BeingInAccept() 129*cdf0e10cSrcweir { 130*cdf0e10cSrcweir *m_pFlag = sal_False; 131*cdf0e10cSrcweir } 132*cdf0e10cSrcweir sal_Bool *m_pFlag; 133*cdf0e10cSrcweir }; 134*cdf0e10cSrcweir 135*cdf0e10cSrcweir Reference< XConnection > OAcceptor::accept( const OUString &sConnectionDescription ) 136*cdf0e10cSrcweir throw( AlreadyAcceptingException, 137*cdf0e10cSrcweir ConnectionSetupException, 138*cdf0e10cSrcweir IllegalArgumentException, 139*cdf0e10cSrcweir RuntimeException) 140*cdf0e10cSrcweir { 141*cdf0e10cSrcweir OSL_TRACE( 142*cdf0e10cSrcweir "acceptor %s\n", 143*cdf0e10cSrcweir OUStringToOString( 144*cdf0e10cSrcweir sConnectionDescription, RTL_TEXTENCODING_ASCII_US).getStr()); 145*cdf0e10cSrcweir // if there is a thread alread accepting in this object, throw an exception. 146*cdf0e10cSrcweir struct BeingInAccept guard( &m_bInAccept, sConnectionDescription ); 147*cdf0e10cSrcweir 148*cdf0e10cSrcweir Reference< XConnection > r; 149*cdf0e10cSrcweir if( m_sLastDescription.getLength() && 150*cdf0e10cSrcweir m_sLastDescription != sConnectionDescription ) 151*cdf0e10cSrcweir { 152*cdf0e10cSrcweir // instantiate another acceptor for different ports 153*cdf0e10cSrcweir OUString sMessage = OUString( RTL_CONSTASCII_USTRINGPARAM( 154*cdf0e10cSrcweir "acceptor::accept called multiple times with different conncetion strings\n" ) ); 155*cdf0e10cSrcweir throw ConnectionSetupException( sMessage, Reference< XInterface > () ); 156*cdf0e10cSrcweir } 157*cdf0e10cSrcweir 158*cdf0e10cSrcweir if( ! m_sLastDescription.getLength() ) 159*cdf0e10cSrcweir { 160*cdf0e10cSrcweir // setup the acceptor 161*cdf0e10cSrcweir try 162*cdf0e10cSrcweir { 163*cdf0e10cSrcweir cppu::UnoUrlDescriptor aDesc(sConnectionDescription); 164*cdf0e10cSrcweir if (aDesc.getName().equalsAsciiL( 165*cdf0e10cSrcweir RTL_CONSTASCII_STRINGPARAM("pipe"))) 166*cdf0e10cSrcweir { 167*cdf0e10cSrcweir rtl::OUString aName( 168*cdf0e10cSrcweir aDesc.getParameter( 169*cdf0e10cSrcweir rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( 170*cdf0e10cSrcweir "name")))); 171*cdf0e10cSrcweir 172*cdf0e10cSrcweir m_pPipe = new PipeAcceptor(aName, sConnectionDescription); 173*cdf0e10cSrcweir 174*cdf0e10cSrcweir try 175*cdf0e10cSrcweir { 176*cdf0e10cSrcweir m_pPipe->init(); 177*cdf0e10cSrcweir } 178*cdf0e10cSrcweir catch( ... ) 179*cdf0e10cSrcweir { 180*cdf0e10cSrcweir { 181*cdf0e10cSrcweir MutexGuard g( m_mutex ); 182*cdf0e10cSrcweir delete m_pPipe; 183*cdf0e10cSrcweir m_pPipe = 0; 184*cdf0e10cSrcweir } 185*cdf0e10cSrcweir throw; 186*cdf0e10cSrcweir } 187*cdf0e10cSrcweir } 188*cdf0e10cSrcweir else if (aDesc.getName().equalsAsciiL( 189*cdf0e10cSrcweir RTL_CONSTASCII_STRINGPARAM("socket"))) 190*cdf0e10cSrcweir { 191*cdf0e10cSrcweir rtl::OUString aHost; 192*cdf0e10cSrcweir if (aDesc.hasParameter( 193*cdf0e10cSrcweir rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("host")))) 194*cdf0e10cSrcweir aHost = aDesc.getParameter( 195*cdf0e10cSrcweir rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("host"))); 196*cdf0e10cSrcweir else 197*cdf0e10cSrcweir aHost = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( 198*cdf0e10cSrcweir "localhost")); 199*cdf0e10cSrcweir sal_uInt16 nPort = static_cast< sal_uInt16 >( 200*cdf0e10cSrcweir aDesc.getParameter( 201*cdf0e10cSrcweir rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("port"))). 202*cdf0e10cSrcweir toInt32()); 203*cdf0e10cSrcweir bool bTcpNoDelay 204*cdf0e10cSrcweir = aDesc.getParameter( 205*cdf0e10cSrcweir rtl::OUString(RTL_CONSTASCII_USTRINGPARAM( 206*cdf0e10cSrcweir "tcpnodelay"))).toInt32() != 0; 207*cdf0e10cSrcweir 208*cdf0e10cSrcweir m_pSocket = new SocketAcceptor( 209*cdf0e10cSrcweir aHost, nPort, bTcpNoDelay, sConnectionDescription); 210*cdf0e10cSrcweir 211*cdf0e10cSrcweir try 212*cdf0e10cSrcweir { 213*cdf0e10cSrcweir m_pSocket->init(); 214*cdf0e10cSrcweir } 215*cdf0e10cSrcweir catch( ... ) 216*cdf0e10cSrcweir { 217*cdf0e10cSrcweir { 218*cdf0e10cSrcweir MutexGuard g( m_mutex ); 219*cdf0e10cSrcweir delete m_pSocket; 220*cdf0e10cSrcweir m_pSocket = 0; 221*cdf0e10cSrcweir } 222*cdf0e10cSrcweir throw; 223*cdf0e10cSrcweir } 224*cdf0e10cSrcweir } 225*cdf0e10cSrcweir else 226*cdf0e10cSrcweir { 227*cdf0e10cSrcweir OUString delegatee = OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.connection.Acceptor.")); 228*cdf0e10cSrcweir delegatee += aDesc.getName(); 229*cdf0e10cSrcweir 230*cdf0e10cSrcweir OSL_TRACE( 231*cdf0e10cSrcweir "trying to get service %s\n", 232*cdf0e10cSrcweir OUStringToOString( 233*cdf0e10cSrcweir delegatee, RTL_TEXTENCODING_ASCII_US).getStr()); 234*cdf0e10cSrcweir _xAcceptor = Reference<XAcceptor>( 235*cdf0e10cSrcweir _xSMgr->createInstanceWithContext(delegatee, _xCtx), UNO_QUERY); 236*cdf0e10cSrcweir 237*cdf0e10cSrcweir if(!_xAcceptor.is()) 238*cdf0e10cSrcweir { 239*cdf0e10cSrcweir OUString message(RTL_CONSTASCII_USTRINGPARAM("Acceptor: unknown delegatee ")); 240*cdf0e10cSrcweir message += delegatee; 241*cdf0e10cSrcweir 242*cdf0e10cSrcweir throw ConnectionSetupException(message, Reference<XInterface>()); 243*cdf0e10cSrcweir } 244*cdf0e10cSrcweir } 245*cdf0e10cSrcweir } 246*cdf0e10cSrcweir catch (rtl::MalformedUriException & rEx) 247*cdf0e10cSrcweir { 248*cdf0e10cSrcweir throw IllegalArgumentException( 249*cdf0e10cSrcweir rEx.getMessage(), 250*cdf0e10cSrcweir Reference< XInterface > (), 251*cdf0e10cSrcweir 0 ); 252*cdf0e10cSrcweir } 253*cdf0e10cSrcweir m_sLastDescription = sConnectionDescription; 254*cdf0e10cSrcweir } 255*cdf0e10cSrcweir 256*cdf0e10cSrcweir if( m_pPipe ) 257*cdf0e10cSrcweir { 258*cdf0e10cSrcweir r = m_pPipe->accept(); 259*cdf0e10cSrcweir } 260*cdf0e10cSrcweir else if( m_pSocket ) 261*cdf0e10cSrcweir { 262*cdf0e10cSrcweir r = m_pSocket->accept(); 263*cdf0e10cSrcweir } 264*cdf0e10cSrcweir else 265*cdf0e10cSrcweir { 266*cdf0e10cSrcweir r = _xAcceptor->accept(sConnectionDescription); 267*cdf0e10cSrcweir } 268*cdf0e10cSrcweir 269*cdf0e10cSrcweir return r; 270*cdf0e10cSrcweir } 271*cdf0e10cSrcweir 272*cdf0e10cSrcweir void SAL_CALL OAcceptor::stopAccepting( ) throw( RuntimeException) 273*cdf0e10cSrcweir { 274*cdf0e10cSrcweir MutexGuard guard( m_mutex ); 275*cdf0e10cSrcweir 276*cdf0e10cSrcweir if( m_pPipe ) 277*cdf0e10cSrcweir { 278*cdf0e10cSrcweir m_pPipe->stopAccepting(); 279*cdf0e10cSrcweir } 280*cdf0e10cSrcweir else if ( m_pSocket ) 281*cdf0e10cSrcweir { 282*cdf0e10cSrcweir m_pSocket->stopAccepting(); 283*cdf0e10cSrcweir } 284*cdf0e10cSrcweir else if( _xAcceptor.is() ) 285*cdf0e10cSrcweir { 286*cdf0e10cSrcweir _xAcceptor->stopAccepting(); 287*cdf0e10cSrcweir } 288*cdf0e10cSrcweir 289*cdf0e10cSrcweir } 290*cdf0e10cSrcweir 291*cdf0e10cSrcweir OUString acceptor_getImplementationName() 292*cdf0e10cSrcweir { 293*cdf0e10cSrcweir return OUString( RTL_CONSTASCII_USTRINGPARAM( IMPLEMENTATION_NAME ) ); 294*cdf0e10cSrcweir } 295*cdf0e10cSrcweir 296*cdf0e10cSrcweir Reference< XInterface > SAL_CALL acceptor_CreateInstance( const Reference< XComponentContext > & xCtx) 297*cdf0e10cSrcweir { 298*cdf0e10cSrcweir return Reference < XInterface >( ( OWeakObject * ) new OAcceptor(xCtx) ); 299*cdf0e10cSrcweir } 300*cdf0e10cSrcweir 301*cdf0e10cSrcweir Sequence< OUString > acceptor_getSupportedServiceNames() 302*cdf0e10cSrcweir { 303*cdf0e10cSrcweir static Sequence < OUString > *pNames = 0; 304*cdf0e10cSrcweir if( ! pNames ) 305*cdf0e10cSrcweir { 306*cdf0e10cSrcweir MutexGuard guard( Mutex::getGlobalMutex() ); 307*cdf0e10cSrcweir if( !pNames ) 308*cdf0e10cSrcweir { 309*cdf0e10cSrcweir static Sequence< OUString > seqNames(1); 310*cdf0e10cSrcweir seqNames.getArray()[0] = OUString::createFromAscii( SERVICE_NAME ); 311*cdf0e10cSrcweir pNames = &seqNames; 312*cdf0e10cSrcweir } 313*cdf0e10cSrcweir } 314*cdf0e10cSrcweir return *pNames; 315*cdf0e10cSrcweir } 316*cdf0e10cSrcweir 317*cdf0e10cSrcweir OUString OAcceptor::getImplementationName() throw() 318*cdf0e10cSrcweir { 319*cdf0e10cSrcweir return acceptor_getImplementationName(); 320*cdf0e10cSrcweir } 321*cdf0e10cSrcweir 322*cdf0e10cSrcweir sal_Bool OAcceptor::supportsService(const OUString& ServiceName) throw() 323*cdf0e10cSrcweir { 324*cdf0e10cSrcweir Sequence< OUString > aSNL = getSupportedServiceNames(); 325*cdf0e10cSrcweir const OUString * pArray = aSNL.getConstArray(); 326*cdf0e10cSrcweir 327*cdf0e10cSrcweir for( sal_Int32 i = 0; i < aSNL.getLength(); i++ ) 328*cdf0e10cSrcweir if( pArray[i] == ServiceName ) 329*cdf0e10cSrcweir return sal_True; 330*cdf0e10cSrcweir 331*cdf0e10cSrcweir return sal_False; 332*cdf0e10cSrcweir } 333*cdf0e10cSrcweir 334*cdf0e10cSrcweir Sequence< OUString > OAcceptor::getSupportedServiceNames(void) throw() 335*cdf0e10cSrcweir { 336*cdf0e10cSrcweir return acceptor_getSupportedServiceNames(); 337*cdf0e10cSrcweir } 338*cdf0e10cSrcweir 339*cdf0e10cSrcweir 340*cdf0e10cSrcweir } 341*cdf0e10cSrcweir 342*cdf0e10cSrcweir using namespace io_acceptor; 343*cdf0e10cSrcweir 344*cdf0e10cSrcweir static struct ImplementationEntry g_entries[] = 345*cdf0e10cSrcweir { 346*cdf0e10cSrcweir { 347*cdf0e10cSrcweir acceptor_CreateInstance, acceptor_getImplementationName , 348*cdf0e10cSrcweir acceptor_getSupportedServiceNames, createSingleComponentFactory , 349*cdf0e10cSrcweir &g_moduleCount.modCnt , 0 350*cdf0e10cSrcweir }, 351*cdf0e10cSrcweir { 0, 0, 0, 0, 0, 0 } 352*cdf0e10cSrcweir }; 353*cdf0e10cSrcweir 354*cdf0e10cSrcweir extern "C" 355*cdf0e10cSrcweir { 356*cdf0e10cSrcweir 357*cdf0e10cSrcweir sal_Bool SAL_CALL component_canUnload( TimeValue *pTime ) 358*cdf0e10cSrcweir { 359*cdf0e10cSrcweir return g_moduleCount.canUnload( &g_moduleCount , pTime ); 360*cdf0e10cSrcweir } 361*cdf0e10cSrcweir 362*cdf0e10cSrcweir //================================================================================================== 363*cdf0e10cSrcweir void SAL_CALL component_getImplementationEnvironment( 364*cdf0e10cSrcweir const sal_Char ** ppEnvTypeName, uno_Environment ** ) 365*cdf0e10cSrcweir { 366*cdf0e10cSrcweir *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME; 367*cdf0e10cSrcweir } 368*cdf0e10cSrcweir //================================================================================================== 369*cdf0e10cSrcweir void * SAL_CALL component_getFactory( 370*cdf0e10cSrcweir const sal_Char * pImplName, void * pServiceManager, void * pRegistryKey ) 371*cdf0e10cSrcweir { 372*cdf0e10cSrcweir return component_getFactoryHelper( pImplName, pServiceManager, pRegistryKey , g_entries ); 373*cdf0e10cSrcweir } 374*cdf0e10cSrcweir } 375*cdf0e10cSrcweir 376*cdf0e10cSrcweir 377*cdf0e10cSrcweir 378