1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <osl/mutex.hxx>
22 #include <cppuhelper/implbase.hxx>
23 #include <cppuhelper/supportsservice.hxx>
24 #include <cppuhelper/unourl.hxx>
25 #include <rtl/malformeduriexception.hxx>
27 #include <com/sun/star/connection/AlreadyAcceptingException.hpp>
28 #include <com/sun/star/connection/ConnectionSetupException.hpp>
29 #include <com/sun/star/connection/XAcceptor.hpp>
30 #include <com/sun/star/lang/IllegalArgumentException.hpp>
31 #include <com/sun/star/lang/XServiceInfo.hpp>
32 #include <com/sun/star/uno/XComponentContext.hpp>
34 #include <services.hxx>
35 #include "acceptor.hxx"
38 #define IMPLEMENTATION_NAME "com.sun.star.comp.io.Acceptor"
39 #define SERVICE_NAME "com.sun.star.connection.Acceptor"
41 using namespace ::osl
;
42 using namespace ::cppu
;
43 using namespace ::com::sun::star::uno
;
44 using namespace ::com::sun::star::lang
;
45 using namespace ::com::sun::star::connection
;
49 class OAcceptor
: public WeakImplHelper
< XAcceptor
, XServiceInfo
>
52 explicit OAcceptor(const Reference
< XComponentContext
> & xCtx
);
53 virtual ~OAcceptor() override
;
56 virtual Reference
< XConnection
> SAL_CALL
accept( const OUString
& sConnectionDescription
) override
;
57 virtual void SAL_CALL
stopAccepting( ) override
;
59 public: // XServiceInfo
60 virtual OUString SAL_CALL
getImplementationName() override
;
61 virtual Sequence
< OUString
> SAL_CALL
getSupportedServiceNames() override
;
62 virtual sal_Bool SAL_CALL
supportsService(const OUString
& ServiceName
) override
;
65 std::unique_ptr
<PipeAcceptor
> m_pPipe
;
66 std::unique_ptr
<SocketAcceptor
> m_pSocket
;
68 OUString m_sLastDescription
;
71 Reference
< XMultiComponentFactory
> _xSMgr
;
72 Reference
< XComponentContext
> _xCtx
;
73 Reference
<XAcceptor
> _xAcceptor
;
77 OAcceptor::OAcceptor( const Reference
< XComponentContext
> & xCtx
)
78 : m_bInAccept( false )
79 , _xSMgr( xCtx
->getServiceManager() )
83 OAcceptor::~OAcceptor()
90 /// @throws AlreadyAcceptingException
91 BeingInAccept( bool *pFlag
,const OUString
& sConnectionDescription
)
95 throw AlreadyAcceptingException( "AlreadyAcceptingException :" + sConnectionDescription
);
105 Reference
< XConnection
> OAcceptor::accept( const OUString
&sConnectionDescription
)
107 // if there is a thread already accepting in this object, throw an exception.
108 struct BeingInAccept
guard( &m_bInAccept
, sConnectionDescription
);
110 Reference
< XConnection
> r
;
111 if( !m_sLastDescription
.isEmpty() &&
112 m_sLastDescription
!= sConnectionDescription
)
114 // instantiate another acceptor for different ports
115 OUString sMessage
= "acceptor::accept called multiple times with different connection strings\n";
116 throw ConnectionSetupException( sMessage
);
119 if( m_sLastDescription
.isEmpty() )
121 // setup the acceptor
124 cppu::UnoUrlDescriptor
aDesc(sConnectionDescription
);
125 if ( aDesc
.getName() == "pipe" )
131 m_pPipe
.reset(new PipeAcceptor(aName
, sConnectionDescription
));
140 MutexGuard
g( m_mutex
);
146 else if ( aDesc
.getName() == "socket" )
149 if (aDesc
.hasParameter(
151 aHost
= aDesc
.getParameter(
155 sal_uInt16 nPort
= static_cast< sal_uInt16
>(
160 = aDesc
.getParameter(
161 "tcpnodelay").toInt32() != 0;
163 m_pSocket
.reset(new SocketAcceptor(
164 aHost
, nPort
, bTcpNoDelay
, sConnectionDescription
));
173 MutexGuard
g( m_mutex
);
181 OUString delegatee
= "com.sun.star.connection.Acceptor." + aDesc
.getName();
182 _xAcceptor
.set(_xSMgr
->createInstanceWithContext(delegatee
, _xCtx
), UNO_QUERY
);
185 throw ConnectionSetupException("Acceptor: unknown delegatee " + delegatee
);
188 catch (const rtl::MalformedUriException
& rEx
)
190 throw IllegalArgumentException(
192 Reference
< XInterface
> (),
195 m_sLastDescription
= sConnectionDescription
;
200 r
= m_pPipe
->accept();
204 r
= m_pSocket
->accept();
208 r
= _xAcceptor
->accept(sConnectionDescription
);
214 void SAL_CALL
OAcceptor::stopAccepting( )
216 MutexGuard
guard( m_mutex
);
220 m_pPipe
->stopAccepting();
222 else if ( m_pSocket
)
224 m_pSocket
->stopAccepting();
226 else if( _xAcceptor
.is() )
228 _xAcceptor
->stopAccepting();
233 OUString
acceptor_getImplementationName()
235 return IMPLEMENTATION_NAME
;
238 Reference
< XInterface
> acceptor_CreateInstance( const Reference
< XComponentContext
> & xCtx
)
240 return Reference
< XInterface
>( static_cast<OWeakObject
*>(new OAcceptor(xCtx
)) );
243 Sequence
< OUString
> acceptor_getSupportedServiceNames()
245 Sequence
< OUString
> seqNames
{ SERVICE_NAME
};
249 OUString
OAcceptor::getImplementationName()
251 return acceptor_getImplementationName();
254 sal_Bool
OAcceptor::supportsService(const OUString
& ServiceName
)
256 return cppu::supportsService(this, ServiceName
);
259 Sequence
< OUString
> OAcceptor::getSupportedServiceNames()
261 return acceptor_getSupportedServiceNames();
267 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */