2010-10-14 08:30:07 +02:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-07-03 13:27:15 +01:00
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*
|
|
|
|
* This file incorporates work covered by the following license notice:
|
|
|
|
*
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed
|
|
|
|
* with this work for additional information regarding copyright
|
|
|
|
* ownership. The ASF licenses this file to you under the Apache
|
|
|
|
* License, Version 2.0 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
|
|
|
|
*/
|
2006-09-16 22:42:36 +00:00
|
|
|
|
2000-09-18 16:24:28 +00:00
|
|
|
#include <osl/mutex.hxx>
|
|
|
|
|
|
|
|
#include <uno/mapping.hxx>
|
|
|
|
|
|
|
|
#include <cppuhelper/factory.hxx>
|
2015-08-19 10:15:06 +09:00
|
|
|
#include <cppuhelper/implbase.hxx>
|
2001-06-22 15:32:57 +00:00
|
|
|
#include <cppuhelper/implementationentry.hxx>
|
2013-10-21 19:04:05 -02:00
|
|
|
#include <cppuhelper/supportsservice.hxx>
|
2002-10-04 08:41:05 +00:00
|
|
|
#include "cppuhelper/unourl.hxx"
|
|
|
|
#include "rtl/malformeduriexception.hxx"
|
2000-09-18 16:24:28 +00:00
|
|
|
|
|
|
|
#include <com/sun/star/connection/XAcceptor.hpp>
|
2001-06-22 15:32:57 +00:00
|
|
|
#include <com/sun/star/lang/XServiceInfo.hpp>
|
2000-09-18 16:24:28 +00:00
|
|
|
|
2013-11-04 13:06:32 -02:00
|
|
|
#include "services.hxx"
|
2000-09-18 16:24:28 +00:00
|
|
|
#include "acceptor.hxx"
|
|
|
|
|
|
|
|
#define IMPLEMENTATION_NAME "com.sun.star.comp.io.Acceptor"
|
|
|
|
#define SERVICE_NAME "com.sun.star.connection.Acceptor"
|
|
|
|
|
|
|
|
using namespace ::osl;
|
|
|
|
using namespace ::cppu;
|
|
|
|
using namespace ::com::sun::star::uno;
|
|
|
|
using namespace ::com::sun::star::lang;
|
|
|
|
using namespace ::com::sun::star::registry;
|
|
|
|
using namespace ::com::sun::star::connection;
|
|
|
|
|
2000-11-28 07:23:24 +00:00
|
|
|
namespace io_acceptor
|
2000-09-18 16:24:28 +00:00
|
|
|
{
|
2015-08-19 10:15:06 +09:00
|
|
|
class OAcceptor : public WeakImplHelper< XAcceptor, XServiceInfo >
|
2000-09-18 16:24:28 +00:00
|
|
|
{
|
|
|
|
public:
|
2001-06-22 15:32:57 +00:00
|
|
|
OAcceptor(const Reference< XComponentContext > & xCtx);
|
|
|
|
virtual ~OAcceptor();
|
2000-09-18 16:24:28 +00:00
|
|
|
public:
|
|
|
|
// Methods
|
|
|
|
virtual Reference< XConnection > SAL_CALL accept( const OUString& sConnectionDescription )
|
|
|
|
throw( AlreadyAcceptingException,
|
|
|
|
ConnectionSetupException,
|
|
|
|
IllegalArgumentException,
|
2014-03-26 16:37:00 +01:00
|
|
|
RuntimeException, std::exception) SAL_OVERRIDE;
|
|
|
|
virtual void SAL_CALL stopAccepting( ) throw( RuntimeException, std::exception) SAL_OVERRIDE;
|
2000-09-18 16:24:28 +00:00
|
|
|
|
2001-06-22 15:32:57 +00:00
|
|
|
public: // XServiceInfo
|
2014-03-26 16:37:00 +01:00
|
|
|
virtual OUString SAL_CALL getImplementationName() throw(std::exception) SAL_OVERRIDE;
|
2015-04-14 12:44:47 +02:00
|
|
|
virtual Sequence< OUString > SAL_CALL getSupportedServiceNames() throw(std::exception) SAL_OVERRIDE;
|
2014-03-26 16:37:00 +01:00
|
|
|
virtual sal_Bool SAL_CALL supportsService(const OUString& ServiceName) throw(std::exception) SAL_OVERRIDE;
|
2001-06-22 15:32:57 +00:00
|
|
|
|
|
|
|
private:
|
2000-09-18 16:24:28 +00:00
|
|
|
PipeAcceptor *m_pPipe;
|
|
|
|
SocketAcceptor *m_pSocket;
|
|
|
|
Mutex m_mutex;
|
|
|
|
OUString m_sLastDescription;
|
2014-04-22 12:52:39 +02:00
|
|
|
bool m_bInAccept;
|
2000-09-18 16:24:28 +00:00
|
|
|
|
2001-06-22 15:32:57 +00:00
|
|
|
Reference< XMultiComponentFactory > _xSMgr;
|
|
|
|
Reference< XComponentContext > _xCtx;
|
|
|
|
Reference<XAcceptor> _xAcceptor;
|
2000-09-18 16:24:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2001-06-22 15:32:57 +00:00
|
|
|
OAcceptor::OAcceptor( const Reference< XComponentContext > & xCtx )
|
2001-01-08 08:40:11 +00:00
|
|
|
: m_pPipe( 0 )
|
2001-06-22 15:32:57 +00:00
|
|
|
, m_pSocket( 0 )
|
2014-04-22 12:52:39 +02:00
|
|
|
, m_bInAccept( false )
|
2001-06-22 15:32:57 +00:00
|
|
|
, _xSMgr( xCtx->getServiceManager() )
|
|
|
|
, _xCtx( xCtx )
|
2013-04-19 16:50:50 +02:00
|
|
|
{}
|
2000-09-18 16:24:28 +00:00
|
|
|
|
|
|
|
OAcceptor::~OAcceptor()
|
|
|
|
{
|
|
|
|
if( m_pPipe )
|
|
|
|
{
|
|
|
|
delete m_pPipe;
|
|
|
|
}
|
|
|
|
if( m_pSocket )
|
|
|
|
{
|
|
|
|
delete m_pSocket;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-01-08 08:40:11 +00:00
|
|
|
struct BeingInAccept
|
|
|
|
{
|
2014-04-22 12:52:39 +02:00
|
|
|
BeingInAccept( bool *pFlag,const OUString & sConnectionDescription ) throw( AlreadyAcceptingException)
|
2001-01-08 08:40:11 +00:00
|
|
|
: m_pFlag( pFlag )
|
|
|
|
{
|
|
|
|
if( *m_pFlag )
|
|
|
|
{
|
2012-11-29 01:37:54 -02:00
|
|
|
OUString sMessage( "AlreadyAcceptingException :" );
|
2001-01-08 08:40:11 +00:00
|
|
|
sMessage += sConnectionDescription;
|
2014-05-23 12:03:21 +02:00
|
|
|
throw AlreadyAcceptingException( sMessage );
|
2001-01-08 08:40:11 +00:00
|
|
|
}
|
2014-04-22 12:52:39 +02:00
|
|
|
*m_pFlag = true;
|
2001-01-08 08:40:11 +00:00
|
|
|
}
|
|
|
|
~BeingInAccept()
|
|
|
|
{
|
2014-04-22 12:52:39 +02:00
|
|
|
*m_pFlag = false;
|
2001-01-08 08:40:11 +00:00
|
|
|
}
|
2014-04-22 12:52:39 +02:00
|
|
|
bool *m_pFlag;
|
2001-01-08 08:40:11 +00:00
|
|
|
};
|
2000-11-28 07:23:24 +00:00
|
|
|
|
2000-09-18 16:24:28 +00:00
|
|
|
Reference< XConnection > OAcceptor::accept( const OUString &sConnectionDescription )
|
|
|
|
throw( AlreadyAcceptingException,
|
|
|
|
ConnectionSetupException,
|
|
|
|
IllegalArgumentException,
|
2014-02-25 21:31:58 +01:00
|
|
|
RuntimeException, std::exception)
|
2000-09-18 16:24:28 +00:00
|
|
|
{
|
2006-08-24 09:36:05 +00:00
|
|
|
OSL_TRACE(
|
|
|
|
"acceptor %s\n",
|
|
|
|
OUStringToOString(
|
|
|
|
sConnectionDescription, RTL_TEXTENCODING_ASCII_US).getStr());
|
2001-01-08 08:40:11 +00:00
|
|
|
// if there is a thread alread accepting in this object, throw an exception.
|
|
|
|
struct BeingInAccept guard( &m_bInAccept, sConnectionDescription );
|
2000-09-18 16:24:28 +00:00
|
|
|
|
|
|
|
Reference< XConnection > r;
|
2011-12-26 17:19:24 -02:00
|
|
|
if( !m_sLastDescription.isEmpty() &&
|
2000-09-18 16:24:28 +00:00
|
|
|
m_sLastDescription != sConnectionDescription )
|
|
|
|
{
|
|
|
|
// instantiate another acceptor for different ports
|
2013-11-04 14:00:40 +02:00
|
|
|
OUString sMessage = "acceptor::accept called multiple times with different conncetion strings\n";
|
2014-05-23 12:03:21 +02:00
|
|
|
throw ConnectionSetupException( sMessage );
|
2000-09-18 16:24:28 +00:00
|
|
|
}
|
|
|
|
|
2011-12-26 17:19:24 -02:00
|
|
|
if( m_sLastDescription.isEmpty() )
|
2000-09-18 16:24:28 +00:00
|
|
|
{
|
|
|
|
// setup the acceptor
|
2002-10-04 08:41:05 +00:00
|
|
|
try
|
2001-01-08 08:40:11 +00:00
|
|
|
{
|
2002-10-04 08:41:05 +00:00
|
|
|
cppu::UnoUrlDescriptor aDesc(sConnectionDescription);
|
2012-04-06 19:49:53 +02:00
|
|
|
if ( aDesc.getName() == "pipe" )
|
2000-09-18 16:24:28 +00:00
|
|
|
{
|
2012-11-29 01:37:54 -02:00
|
|
|
OUString aName(
|
2002-10-04 08:41:05 +00:00
|
|
|
aDesc.getParameter(
|
2012-11-29 01:37:54 -02:00
|
|
|
OUString("name")));
|
2002-10-04 08:41:05 +00:00
|
|
|
|
|
|
|
m_pPipe = new PipeAcceptor(aName, sConnectionDescription);
|
|
|
|
|
|
|
|
try
|
|
|
|
{
|
|
|
|
m_pPipe->init();
|
|
|
|
}
|
|
|
|
catch( ... )
|
2000-09-18 16:24:28 +00:00
|
|
|
{
|
|
|
|
{
|
2006-06-19 23:16:18 +00:00
|
|
|
MutexGuard g( m_mutex );
|
2002-10-04 08:41:05 +00:00
|
|
|
delete m_pPipe;
|
|
|
|
m_pPipe = 0;
|
2000-09-18 16:24:28 +00:00
|
|
|
}
|
2002-10-04 08:41:05 +00:00
|
|
|
throw;
|
2000-09-18 16:24:28 +00:00
|
|
|
}
|
|
|
|
}
|
2012-04-06 19:49:53 +02:00
|
|
|
else if ( aDesc.getName() == "socket" )
|
2000-09-18 16:24:28 +00:00
|
|
|
{
|
2012-11-29 01:37:54 -02:00
|
|
|
OUString aHost;
|
2002-10-04 08:41:05 +00:00
|
|
|
if (aDesc.hasParameter(
|
2012-11-29 01:37:54 -02:00
|
|
|
OUString("host")))
|
2002-10-04 08:41:05 +00:00
|
|
|
aHost = aDesc.getParameter(
|
2012-11-29 01:37:54 -02:00
|
|
|
OUString("host"));
|
2002-10-04 08:41:05 +00:00
|
|
|
else
|
2013-11-04 14:00:40 +02:00
|
|
|
aHost = "localhost";
|
2002-10-04 08:41:05 +00:00
|
|
|
sal_uInt16 nPort = static_cast< sal_uInt16 >(
|
|
|
|
aDesc.getParameter(
|
2012-11-29 01:37:54 -02:00
|
|
|
OUString("port")).
|
2002-10-04 08:41:05 +00:00
|
|
|
toInt32());
|
|
|
|
bool bTcpNoDelay
|
|
|
|
= aDesc.getParameter(
|
2012-11-29 01:37:54 -02:00
|
|
|
OUString("tcpnodelay")).toInt32() != 0;
|
2002-10-04 08:41:05 +00:00
|
|
|
|
|
|
|
m_pSocket = new SocketAcceptor(
|
|
|
|
aHost, nPort, bTcpNoDelay, sConnectionDescription);
|
|
|
|
|
|
|
|
try
|
2000-09-18 16:24:28 +00:00
|
|
|
{
|
2002-10-04 08:41:05 +00:00
|
|
|
m_pSocket->init();
|
2000-09-18 16:24:28 +00:00
|
|
|
}
|
2002-10-04 08:41:05 +00:00
|
|
|
catch( ... )
|
2000-09-18 16:24:28 +00:00
|
|
|
{
|
|
|
|
{
|
2006-06-19 23:16:18 +00:00
|
|
|
MutexGuard g( m_mutex );
|
2002-10-04 08:41:05 +00:00
|
|
|
delete m_pSocket;
|
|
|
|
m_pSocket = 0;
|
2000-09-18 16:24:28 +00:00
|
|
|
}
|
2002-10-04 08:41:05 +00:00
|
|
|
throw;
|
2000-09-18 16:24:28 +00:00
|
|
|
}
|
|
|
|
}
|
2002-10-04 08:41:05 +00:00
|
|
|
else
|
2000-09-18 16:24:28 +00:00
|
|
|
{
|
2013-11-04 14:00:40 +02:00
|
|
|
OUString delegatee = "com.sun.star.connection.Acceptor." + aDesc.getName();
|
2000-09-18 16:24:28 +00:00
|
|
|
|
2006-08-24 09:36:05 +00:00
|
|
|
OSL_TRACE(
|
|
|
|
"trying to get service %s\n",
|
|
|
|
OUStringToOString(
|
|
|
|
delegatee, RTL_TEXTENCODING_ASCII_US).getStr());
|
2002-10-04 08:41:05 +00:00
|
|
|
_xAcceptor = Reference<XAcceptor>(
|
|
|
|
_xSMgr->createInstanceWithContext(delegatee, _xCtx), UNO_QUERY);
|
2000-09-18 16:24:28 +00:00
|
|
|
|
2002-10-04 08:41:05 +00:00
|
|
|
if(!_xAcceptor.is())
|
|
|
|
{
|
2012-11-29 01:37:54 -02:00
|
|
|
OUString message("Acceptor: unknown delegatee ");
|
2002-10-04 08:41:05 +00:00
|
|
|
message += delegatee;
|
2000-09-18 16:24:28 +00:00
|
|
|
|
2014-05-23 12:03:21 +02:00
|
|
|
throw ConnectionSetupException(message);
|
2002-10-04 08:41:05 +00:00
|
|
|
}
|
2000-09-18 16:24:28 +00:00
|
|
|
}
|
|
|
|
}
|
2011-12-15 13:51:29 +09:00
|
|
|
catch (const rtl::MalformedUriException & rEx)
|
2002-10-04 08:41:05 +00:00
|
|
|
{
|
|
|
|
throw IllegalArgumentException(
|
|
|
|
rEx.getMessage(),
|
|
|
|
Reference< XInterface > (),
|
|
|
|
0 );
|
|
|
|
}
|
2000-09-18 16:24:28 +00:00
|
|
|
m_sLastDescription = sConnectionDescription;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( m_pPipe )
|
|
|
|
{
|
|
|
|
r = m_pPipe->accept();
|
|
|
|
}
|
|
|
|
else if( m_pSocket )
|
|
|
|
{
|
|
|
|
r = m_pSocket->accept();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2002-10-04 08:41:05 +00:00
|
|
|
r = _xAcceptor->accept(sConnectionDescription);
|
2000-09-18 16:24:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2014-02-25 21:31:58 +01:00
|
|
|
void SAL_CALL OAcceptor::stopAccepting( ) throw( RuntimeException, std::exception)
|
2000-09-18 16:24:28 +00:00
|
|
|
{
|
|
|
|
MutexGuard guard( m_mutex );
|
|
|
|
|
|
|
|
if( m_pPipe )
|
|
|
|
{
|
|
|
|
m_pPipe->stopAccepting();
|
|
|
|
}
|
|
|
|
else if ( m_pSocket )
|
|
|
|
{
|
|
|
|
m_pSocket->stopAccepting();
|
|
|
|
}
|
2000-12-08 07:48:20 +00:00
|
|
|
else if( _xAcceptor.is() )
|
2000-09-18 16:24:28 +00:00
|
|
|
{
|
|
|
|
_xAcceptor->stopAccepting();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2001-06-22 15:32:57 +00:00
|
|
|
OUString acceptor_getImplementationName()
|
|
|
|
{
|
2012-11-29 01:37:54 -02:00
|
|
|
return OUString( IMPLEMENTATION_NAME );
|
2001-06-22 15:32:57 +00:00
|
|
|
}
|
2000-09-18 16:24:28 +00:00
|
|
|
|
2001-06-22 15:32:57 +00:00
|
|
|
Reference< XInterface > SAL_CALL acceptor_CreateInstance( const Reference< XComponentContext > & xCtx)
|
2000-09-18 16:24:28 +00:00
|
|
|
{
|
2015-06-08 16:25:54 +02:00
|
|
|
return Reference < XInterface >( static_cast<OWeakObject *>(new OAcceptor(xCtx)) );
|
2000-09-18 16:24:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Sequence< OUString > acceptor_getSupportedServiceNames()
|
|
|
|
{
|
2011-05-31 13:03:18 +01:00
|
|
|
Sequence< OUString > seqNames(1);
|
2013-11-04 14:00:40 +02:00
|
|
|
seqNames.getArray()[0] = SERVICE_NAME;
|
2011-05-31 13:03:18 +01:00
|
|
|
return seqNames;
|
2000-09-18 16:24:28 +00:00
|
|
|
}
|
|
|
|
|
2014-02-25 21:31:58 +01:00
|
|
|
OUString OAcceptor::getImplementationName() throw(std::exception)
|
2001-06-22 15:32:57 +00:00
|
|
|
{
|
|
|
|
return acceptor_getImplementationName();
|
|
|
|
}
|
|
|
|
|
2014-02-25 21:31:58 +01:00
|
|
|
sal_Bool OAcceptor::supportsService(const OUString& ServiceName) throw(std::exception)
|
2001-06-22 15:32:57 +00:00
|
|
|
{
|
2013-10-21 19:04:05 -02:00
|
|
|
return cppu::supportsService(this, ServiceName);
|
2001-06-22 15:32:57 +00:00
|
|
|
}
|
|
|
|
|
2015-04-14 12:44:47 +02:00
|
|
|
Sequence< OUString > OAcceptor::getSupportedServiceNames() throw(std::exception)
|
2001-06-22 15:32:57 +00:00
|
|
|
{
|
|
|
|
return acceptor_getSupportedServiceNames();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-09-18 16:24:28 +00:00
|
|
|
}
|
|
|
|
|
2010-10-14 08:30:07 +02:00
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|