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 <uno/mapping.hxx>
24 #include <cppuhelper/factory.hxx>
25 #include <cppuhelper/implbase2.hxx>
26 #include <cppuhelper/implementationentry.hxx>
27 #include "cppuhelper/unourl.hxx"
28 #include "rtl/malformeduriexception.hxx"
30 #include <com/sun/star/connection/XAcceptor.hpp>
31 #include <com/sun/star/lang/XServiceInfo.hpp>
33 #include "acceptor.hxx"
35 #define IMPLEMENTATION_NAME "com.sun.star.comp.io.Acceptor"
36 #define SERVICE_NAME "com.sun.star.connection.Acceptor"
38 using namespace ::osl
;
39 using namespace ::rtl
;
40 using namespace ::cppu
;
41 using namespace ::com::sun::star::uno
;
42 using namespace ::com::sun::star::lang
;
43 using namespace ::com::sun::star::registry
;
44 using namespace ::com::sun::star::connection
;
48 rtl_StandardModuleCount g_moduleCount
= MODULE_COUNT_INIT
;
50 class OAcceptor
: public WeakImplHelper2
< XAcceptor
, XServiceInfo
>
53 OAcceptor(const Reference
< XComponentContext
> & xCtx
);
57 virtual Reference
< XConnection
> SAL_CALL
accept( const OUString
& sConnectionDescription
)
58 throw( AlreadyAcceptingException
,
59 ConnectionSetupException
,
60 IllegalArgumentException
,
62 virtual void SAL_CALL
stopAccepting( ) throw( RuntimeException
);
64 public: // XServiceInfo
65 virtual OUString SAL_CALL
getImplementationName() throw();
66 virtual Sequence
< OUString
> SAL_CALL
getSupportedServiceNames(void) throw();
67 virtual sal_Bool SAL_CALL
supportsService(const OUString
& ServiceName
) throw();
70 PipeAcceptor
*m_pPipe
;
71 SocketAcceptor
*m_pSocket
;
73 OUString m_sLastDescription
;
76 Reference
< XMultiComponentFactory
> _xSMgr
;
77 Reference
< XComponentContext
> _xCtx
;
78 Reference
<XAcceptor
> _xAcceptor
;
82 OAcceptor::OAcceptor( const Reference
< XComponentContext
> & xCtx
)
85 , m_bInAccept( sal_False
)
86 , _xSMgr( xCtx
->getServiceManager() )
89 g_moduleCount
.modCnt
.acquire( &g_moduleCount
.modCnt
);
92 OAcceptor::~OAcceptor()
102 g_moduleCount
.modCnt
.release( &g_moduleCount
.modCnt
);
107 BeingInAccept( sal_Bool
*pFlag
,const OUString
& sConnectionDescription
) throw( AlreadyAcceptingException
)
112 OUString
sMessage( "AlreadyAcceptingException :" );
113 sMessage
+= sConnectionDescription
;
114 throw AlreadyAcceptingException( sMessage
, Reference
< XInterface
> () );
120 *m_pFlag
= sal_False
;
125 Reference
< XConnection
> OAcceptor::accept( const OUString
&sConnectionDescription
)
126 throw( AlreadyAcceptingException
,
127 ConnectionSetupException
,
128 IllegalArgumentException
,
134 sConnectionDescription
, RTL_TEXTENCODING_ASCII_US
).getStr());
135 // if there is a thread alread accepting in this object, throw an exception.
136 struct BeingInAccept
guard( &m_bInAccept
, sConnectionDescription
);
138 Reference
< XConnection
> r
;
139 if( !m_sLastDescription
.isEmpty() &&
140 m_sLastDescription
!= sConnectionDescription
)
142 // instantiate another acceptor for different ports
143 OUString sMessage
= OUString("acceptor::accept called multiple times with different conncetion strings\n" );
144 throw ConnectionSetupException( sMessage
, Reference
< XInterface
> () );
147 if( m_sLastDescription
.isEmpty() )
149 // setup the acceptor
152 cppu::UnoUrlDescriptor
aDesc(sConnectionDescription
);
153 if ( aDesc
.getName() == "pipe" )
159 m_pPipe
= new PipeAcceptor(aName
, sConnectionDescription
);
168 MutexGuard
g( m_mutex
);
175 else if ( aDesc
.getName() == "socket" )
178 if (aDesc
.hasParameter(
180 aHost
= aDesc
.getParameter(
183 aHost
= OUString("localhost");
184 sal_uInt16 nPort
= static_cast< sal_uInt16
>(
189 = aDesc
.getParameter(
190 OUString("tcpnodelay")).toInt32() != 0;
192 m_pSocket
= new SocketAcceptor(
193 aHost
, nPort
, bTcpNoDelay
, sConnectionDescription
);
202 MutexGuard
g( m_mutex
);
211 OUString delegatee
= OUString("com.sun.star.connection.Acceptor.");
212 delegatee
+= aDesc
.getName();
215 "trying to get service %s\n",
217 delegatee
, RTL_TEXTENCODING_ASCII_US
).getStr());
218 _xAcceptor
= Reference
<XAcceptor
>(
219 _xSMgr
->createInstanceWithContext(delegatee
, _xCtx
), UNO_QUERY
);
223 OUString
message("Acceptor: unknown delegatee ");
224 message
+= delegatee
;
226 throw ConnectionSetupException(message
, Reference
<XInterface
>());
230 catch (const rtl::MalformedUriException
& rEx
)
232 throw IllegalArgumentException(
234 Reference
< XInterface
> (),
237 m_sLastDescription
= sConnectionDescription
;
242 r
= m_pPipe
->accept();
246 r
= m_pSocket
->accept();
250 r
= _xAcceptor
->accept(sConnectionDescription
);
256 void SAL_CALL
OAcceptor::stopAccepting( ) throw( RuntimeException
)
258 MutexGuard
guard( m_mutex
);
262 m_pPipe
->stopAccepting();
264 else if ( m_pSocket
)
266 m_pSocket
->stopAccepting();
268 else if( _xAcceptor
.is() )
270 _xAcceptor
->stopAccepting();
275 OUString
acceptor_getImplementationName()
277 return OUString( IMPLEMENTATION_NAME
);
280 Reference
< XInterface
> SAL_CALL
acceptor_CreateInstance( const Reference
< XComponentContext
> & xCtx
)
282 return Reference
< XInterface
>( ( OWeakObject
* ) new OAcceptor(xCtx
) );
285 Sequence
< OUString
> acceptor_getSupportedServiceNames()
287 Sequence
< OUString
> seqNames(1);
288 seqNames
.getArray()[0] = OUString(SERVICE_NAME
);
292 OUString
OAcceptor::getImplementationName() throw()
294 return acceptor_getImplementationName();
297 sal_Bool
OAcceptor::supportsService(const OUString
& ServiceName
) throw()
299 Sequence
< OUString
> aSNL
= getSupportedServiceNames();
300 const OUString
* pArray
= aSNL
.getConstArray();
302 for( sal_Int32 i
= 0; i
< aSNL
.getLength(); i
++ )
303 if( pArray
[i
] == ServiceName
)
309 Sequence
< OUString
> OAcceptor::getSupportedServiceNames(void) throw()
311 return acceptor_getSupportedServiceNames();
317 using namespace io_acceptor
;
319 static struct ImplementationEntry g_entries
[] =
322 acceptor_CreateInstance
, acceptor_getImplementationName
,
323 acceptor_getSupportedServiceNames
, createSingleComponentFactory
,
324 &g_moduleCount
.modCnt
, 0
332 SAL_DLLPUBLIC_EXPORT sal_Bool SAL_CALL
component_canUnload( TimeValue
*pTime
)
334 return g_moduleCount
.canUnload( &g_moduleCount
, pTime
);
337 //==================================================================================================
338 SAL_DLLPUBLIC_EXPORT
void * SAL_CALL
acceptor_component_getFactory(
339 const sal_Char
* pImplName
, void * pServiceManager
, void * pRegistryKey
)
341 return component_getFactoryHelper( pImplName
, pServiceManager
, pRegistryKey
, g_entries
);
347 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */