Bump for 4.0-15
[LibreOffice.git] / io / source / acceptor / acceptor.cxx
bloba8bd18c7839e004cd56aab1646d2c39237b3f139
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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;
46 namespace io_acceptor
48 rtl_StandardModuleCount g_moduleCount = MODULE_COUNT_INIT;
50 class OAcceptor : public WeakImplHelper2< XAcceptor, XServiceInfo >
52 public:
53 OAcceptor(const Reference< XComponentContext > & xCtx);
54 virtual ~OAcceptor();
55 public:
56 // Methods
57 virtual Reference< XConnection > SAL_CALL accept( const OUString& sConnectionDescription )
58 throw( AlreadyAcceptingException,
59 ConnectionSetupException,
60 IllegalArgumentException,
61 RuntimeException);
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();
69 private:
70 PipeAcceptor *m_pPipe;
71 SocketAcceptor *m_pSocket;
72 Mutex m_mutex;
73 OUString m_sLastDescription;
74 sal_Bool m_bInAccept;
76 Reference< XMultiComponentFactory > _xSMgr;
77 Reference< XComponentContext > _xCtx;
78 Reference<XAcceptor> _xAcceptor;
82 OAcceptor::OAcceptor( const Reference< XComponentContext > & xCtx )
83 : m_pPipe( 0 )
84 , m_pSocket( 0 )
85 , m_bInAccept( sal_False )
86 , _xSMgr( xCtx->getServiceManager() )
87 , _xCtx( xCtx )
89 g_moduleCount.modCnt.acquire( &g_moduleCount.modCnt );
92 OAcceptor::~OAcceptor()
94 if( m_pPipe )
96 delete m_pPipe;
98 if( m_pSocket )
100 delete m_pSocket;
102 g_moduleCount.modCnt.release( &g_moduleCount.modCnt );
105 struct BeingInAccept
107 BeingInAccept( sal_Bool *pFlag,const OUString & sConnectionDescription ) throw( AlreadyAcceptingException)
108 : m_pFlag( pFlag )
110 if( *m_pFlag )
112 OUString sMessage( "AlreadyAcceptingException :" );
113 sMessage += sConnectionDescription;
114 throw AlreadyAcceptingException( sMessage , Reference< XInterface > () );
116 *m_pFlag = sal_True;
118 ~BeingInAccept()
120 *m_pFlag = sal_False;
122 sal_Bool *m_pFlag;
125 Reference< XConnection > OAcceptor::accept( const OUString &sConnectionDescription )
126 throw( AlreadyAcceptingException,
127 ConnectionSetupException,
128 IllegalArgumentException,
129 RuntimeException)
131 OSL_TRACE(
132 "acceptor %s\n",
133 OUStringToOString(
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" )
155 OUString aName(
156 aDesc.getParameter(
157 OUString("name")));
159 m_pPipe = new PipeAcceptor(aName, sConnectionDescription);
163 m_pPipe->init();
165 catch( ... )
168 MutexGuard g( m_mutex );
169 delete m_pPipe;
170 m_pPipe = 0;
172 throw;
175 else if ( aDesc.getName() == "socket" )
177 OUString aHost;
178 if (aDesc.hasParameter(
179 OUString("host")))
180 aHost = aDesc.getParameter(
181 OUString("host"));
182 else
183 aHost = OUString("localhost");
184 sal_uInt16 nPort = static_cast< sal_uInt16 >(
185 aDesc.getParameter(
186 OUString("port")).
187 toInt32());
188 bool bTcpNoDelay
189 = aDesc.getParameter(
190 OUString("tcpnodelay")).toInt32() != 0;
192 m_pSocket = new SocketAcceptor(
193 aHost, nPort, bTcpNoDelay, sConnectionDescription);
197 m_pSocket->init();
199 catch( ... )
202 MutexGuard g( m_mutex );
203 delete m_pSocket;
204 m_pSocket = 0;
206 throw;
209 else
211 OUString delegatee = OUString("com.sun.star.connection.Acceptor.");
212 delegatee += aDesc.getName();
214 OSL_TRACE(
215 "trying to get service %s\n",
216 OUStringToOString(
217 delegatee, RTL_TEXTENCODING_ASCII_US).getStr());
218 _xAcceptor = Reference<XAcceptor>(
219 _xSMgr->createInstanceWithContext(delegatee, _xCtx), UNO_QUERY);
221 if(!_xAcceptor.is())
223 OUString message("Acceptor: unknown delegatee ");
224 message += delegatee;
226 throw ConnectionSetupException(message, Reference<XInterface>());
230 catch (const rtl::MalformedUriException & rEx)
232 throw IllegalArgumentException(
233 rEx.getMessage(),
234 Reference< XInterface > (),
235 0 );
237 m_sLastDescription = sConnectionDescription;
240 if( m_pPipe )
242 r = m_pPipe->accept();
244 else if( m_pSocket )
246 r = m_pSocket->accept();
248 else
250 r = _xAcceptor->accept(sConnectionDescription);
253 return r;
256 void SAL_CALL OAcceptor::stopAccepting( ) throw( RuntimeException)
258 MutexGuard guard( m_mutex );
260 if( m_pPipe )
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);
289 return seqNames;
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 )
304 return sal_True;
306 return sal_False;
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
326 { 0, 0, 0, 0, 0, 0 }
329 extern "C"
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: */