Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*
3 : : * This file is part of the LibreOffice project.
4 : : *
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/.
8 : : *
9 : : * This file incorporates work covered by the following license notice:
10 : : *
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 .
18 : : */
19 : :
20 : : #include <osl/mutex.hxx>
21 : :
22 : : #include <uno/mapping.hxx>
23 : :
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"
29 : :
30 : : #include <com/sun/star/connection/XAcceptor.hpp>
31 : : #include <com/sun/star/lang/XServiceInfo.hpp>
32 : :
33 : : #include "acceptor.hxx"
34 : :
35 : : #define IMPLEMENTATION_NAME "com.sun.star.comp.io.Acceptor"
36 : : #define SERVICE_NAME "com.sun.star.connection.Acceptor"
37 : :
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;
45 : :
46 : : namespace io_acceptor
47 : : {
48 : : rtl_StandardModuleCount g_moduleCount = MODULE_COUNT_INIT;
49 : :
50 : : class OAcceptor : public WeakImplHelper2< XAcceptor, XServiceInfo >
51 : : {
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);
63 : :
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();
68 : :
69 : : private:
70 : : PipeAcceptor *m_pPipe;
71 : : SocketAcceptor *m_pSocket;
72 : : Mutex m_mutex;
73 : : OUString m_sLastDescription;
74 : : sal_Bool m_bInAccept;
75 : :
76 : : Reference< XMultiComponentFactory > _xSMgr;
77 : : Reference< XComponentContext > _xCtx;
78 : : Reference<XAcceptor> _xAcceptor;
79 : : };
80 : :
81 : :
82 : 160 : OAcceptor::OAcceptor( const Reference< XComponentContext > & xCtx )
83 : : : m_pPipe( 0 )
84 : : , m_pSocket( 0 )
85 : : , m_bInAccept( sal_False )
86 [ + - ]: 160 : , _xSMgr( xCtx->getServiceManager() )
87 [ + - ][ + - ]: 320 : , _xCtx( xCtx )
88 : : {
89 [ + - ]: 160 : g_moduleCount.modCnt.acquire( &g_moduleCount.modCnt );
90 : 160 : }
91 : :
92 [ + - ]: 160 : OAcceptor::~OAcceptor()
93 : : {
94 [ + + ]: 160 : if( m_pPipe )
95 : : {
96 [ + - ][ + - ]: 98 : delete m_pPipe;
97 : : }
98 [ - + ]: 160 : if( m_pSocket )
99 : : {
100 [ # # ][ # # ]: 0 : delete m_pSocket;
101 : : }
102 [ + - ]: 160 : g_moduleCount.modCnt.release( &g_moduleCount.modCnt );
103 [ - + ]: 320 : }
104 : :
105 : : struct BeingInAccept
106 : : {
107 : 194 : BeingInAccept( sal_Bool *pFlag,const OUString & sConnectionDescription ) throw( AlreadyAcceptingException)
108 : 194 : : m_pFlag( pFlag )
109 : : {
110 [ - + ]: 194 : if( *m_pFlag )
111 : : {
112 [ # # ]: 0 : OUString sMessage( RTL_CONSTASCII_USTRINGPARAM( "AlreadyAcceptingException :" ) );
113 : 0 : sMessage += sConnectionDescription;
114 [ # # ]: 0 : throw AlreadyAcceptingException( sMessage , Reference< XInterface > () );
115 : : }
116 : 194 : *m_pFlag = sal_True;
117 : 194 : }
118 : 194 : ~BeingInAccept()
119 : : {
120 : 194 : *m_pFlag = sal_False;
121 : 194 : }
122 : : sal_Bool *m_pFlag;
123 : : };
124 : :
125 : 194 : Reference< XConnection > OAcceptor::accept( const OUString &sConnectionDescription )
126 : : throw( AlreadyAcceptingException,
127 : : ConnectionSetupException,
128 : : IllegalArgumentException,
129 : : RuntimeException)
130 : : {
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 [ + - ]: 194 : struct BeingInAccept guard( &m_bInAccept, sConnectionDescription );
137 : :
138 : 194 : Reference< XConnection > r;
139 [ - + ]: 290 : if( !m_sLastDescription.isEmpty() &&
[ + + - + ]
140 : 96 : m_sLastDescription != sConnectionDescription )
141 : : {
142 : : // instantiate another acceptor for different ports
143 : : OUString sMessage = OUString( RTL_CONSTASCII_USTRINGPARAM(
144 [ # # ]: 0 : "acceptor::accept called multiple times with different conncetion strings\n" ) );
145 [ # # ]: 0 : throw ConnectionSetupException( sMessage, Reference< XInterface > () );
146 : : }
147 : :
148 [ + + ]: 194 : if( m_sLastDescription.isEmpty() )
149 : : {
150 : : // setup the acceptor
151 : : try
152 : : {
153 [ + - ]: 98 : cppu::UnoUrlDescriptor aDesc(sConnectionDescription);
154 [ + - ][ + - ]: 98 : if ( aDesc.getName() == "pipe" )
155 : : {
156 : : rtl::OUString aName(
157 : : aDesc.getParameter(
158 : : rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
159 [ + - ][ + - ]: 98 : "name"))));
160 : :
161 [ + - ][ + - ]: 98 : m_pPipe = new PipeAcceptor(aName, sConnectionDescription);
162 : :
163 : : try
164 : : {
165 [ + - ]: 98 : m_pPipe->init();
166 : : }
167 : 0 : catch( ... )
168 : : {
169 : : {
170 [ # # ]: 0 : MutexGuard g( m_mutex );
171 [ # # # # ]: 0 : delete m_pPipe;
172 [ # # ]: 0 : m_pPipe = 0;
173 : : }
174 : 0 : throw;
175 : 98 : }
176 : : }
177 [ # # ][ # # ]: 0 : else if ( aDesc.getName() == "socket" )
178 : : {
179 : 0 : rtl::OUString aHost;
180 [ # # # # ]: 0 : if (aDesc.hasParameter(
181 [ # # ]: 0 : rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("host"))))
182 : : aHost = aDesc.getParameter(
183 [ # # ][ # # ]: 0 : rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("host")));
184 : : else
185 : : aHost = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
186 [ # # ]: 0 : "localhost"));
187 : : sal_uInt16 nPort = static_cast< sal_uInt16 >(
188 : : aDesc.getParameter(
189 : : rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("port"))).
190 [ # # ][ # # ]: 0 : toInt32());
191 : : bool bTcpNoDelay
192 : : = aDesc.getParameter(
193 : : rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
194 [ # # ][ # # ]: 0 : "tcpnodelay"))).toInt32() != 0;
195 : :
196 : : m_pSocket = new SocketAcceptor(
197 [ # # ][ # # ]: 0 : aHost, nPort, bTcpNoDelay, sConnectionDescription);
198 : :
199 : : try
200 : : {
201 [ # # ]: 0 : m_pSocket->init();
202 : : }
203 : 0 : catch( ... )
204 : : {
205 : : {
206 [ # # ]: 0 : MutexGuard g( m_mutex );
207 [ # # # # ]: 0 : delete m_pSocket;
208 [ # # ]: 0 : m_pSocket = 0;
209 : : }
210 : 0 : throw;
211 : 0 : }
212 : : }
213 : : else
214 : : {
215 [ # # ]: 0 : OUString delegatee = OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.connection.Acceptor."));
216 [ # # ]: 0 : delegatee += aDesc.getName();
217 : :
218 : : OSL_TRACE(
219 : : "trying to get service %s\n",
220 : : OUStringToOString(
221 : : delegatee, RTL_TEXTENCODING_ASCII_US).getStr());
222 : : _xAcceptor = Reference<XAcceptor>(
223 [ # # ][ # # ]: 0 : _xSMgr->createInstanceWithContext(delegatee, _xCtx), UNO_QUERY);
[ # # ][ # # ]
224 : :
225 [ # # ]: 0 : if(!_xAcceptor.is())
226 : : {
227 [ # # ]: 0 : OUString message(RTL_CONSTASCII_USTRINGPARAM("Acceptor: unknown delegatee "));
228 : 0 : message += delegatee;
229 : :
230 [ # # ]: 0 : throw ConnectionSetupException(message, Reference<XInterface>());
231 : 0 : }
232 [ + - ]: 98 : }
233 : : }
234 [ # # ]: 0 : catch (const rtl::MalformedUriException & rEx)
235 : : {
236 : : throw IllegalArgumentException(
237 : 0 : rEx.getMessage(),
238 : : Reference< XInterface > (),
239 [ # # ]: 0 : 0 );
240 : : }
241 : 98 : m_sLastDescription = sConnectionDescription;
242 : : }
243 : :
244 [ + - ]: 194 : if( m_pPipe )
245 : : {
246 [ + - ][ + - ]: 194 : r = m_pPipe->accept();
247 : : }
248 [ # # ]: 0 : else if( m_pSocket )
249 : : {
250 [ # # ][ # # ]: 0 : r = m_pSocket->accept();
251 : : }
252 : : else
253 : : {
254 [ # # ][ # # ]: 0 : r = _xAcceptor->accept(sConnectionDescription);
[ # # ]
255 : : }
256 : :
257 : 194 : return r;
258 : : }
259 : :
260 : 158 : void SAL_CALL OAcceptor::stopAccepting( ) throw( RuntimeException)
261 : : {
262 [ + - ]: 158 : MutexGuard guard( m_mutex );
263 : :
264 [ + + ]: 158 : if( m_pPipe )
265 : : {
266 [ + - ]: 96 : m_pPipe->stopAccepting();
267 : : }
268 [ - + ]: 62 : else if ( m_pSocket )
269 : : {
270 [ # # ]: 0 : m_pSocket->stopAccepting();
271 : : }
272 [ - + ]: 62 : else if( _xAcceptor.is() )
273 : : {
274 [ # # ][ # # ]: 0 : _xAcceptor->stopAccepting();
275 [ + - ]: 158 : }
276 : :
277 : 158 : }
278 : :
279 : 160 : OUString acceptor_getImplementationName()
280 : : {
281 : 160 : return OUString( RTL_CONSTASCII_USTRINGPARAM( IMPLEMENTATION_NAME ) );
282 : : }
283 : :
284 : 160 : Reference< XInterface > SAL_CALL acceptor_CreateInstance( const Reference< XComponentContext > & xCtx)
285 : : {
286 [ + - ]: 160 : return Reference < XInterface >( ( OWeakObject * ) new OAcceptor(xCtx) );
287 : : }
288 : :
289 : 160 : Sequence< OUString > acceptor_getSupportedServiceNames()
290 : : {
291 : 160 : Sequence< OUString > seqNames(1);
292 [ + - ][ + - ]: 160 : seqNames.getArray()[0] = OUString(RTL_CONSTASCII_USTRINGPARAM(SERVICE_NAME));
293 : 160 : return seqNames;
294 : : }
295 : :
296 : 0 : OUString OAcceptor::getImplementationName() throw()
297 : : {
298 : 0 : return acceptor_getImplementationName();
299 : : }
300 : :
301 : 0 : sal_Bool OAcceptor::supportsService(const OUString& ServiceName) throw()
302 : : {
303 : 0 : Sequence< OUString > aSNL = getSupportedServiceNames();
304 : 0 : const OUString * pArray = aSNL.getConstArray();
305 : :
306 [ # # ]: 0 : for( sal_Int32 i = 0; i < aSNL.getLength(); i++ )
307 [ # # ]: 0 : if( pArray[i] == ServiceName )
308 : 0 : return sal_True;
309 : :
310 [ # # ]: 0 : return sal_False;
311 : : }
312 : :
313 : 0 : Sequence< OUString > OAcceptor::getSupportedServiceNames(void) throw()
314 : : {
315 : 0 : return acceptor_getSupportedServiceNames();
316 : : }
317 : :
318 : :
319 : : }
320 : :
321 : : using namespace io_acceptor;
322 : :
323 : : static struct ImplementationEntry g_entries[] =
324 : : {
325 : : {
326 : : acceptor_CreateInstance, acceptor_getImplementationName ,
327 : : acceptor_getSupportedServiceNames, createSingleComponentFactory ,
328 : : &g_moduleCount.modCnt , 0
329 : : },
330 : : { 0, 0, 0, 0, 0, 0 }
331 : : };
332 : :
333 : : extern "C"
334 : : {
335 : :
336 : 0 : SAL_DLLPUBLIC_EXPORT sal_Bool SAL_CALL component_canUnload( TimeValue *pTime )
337 : : {
338 : 0 : return g_moduleCount.canUnload( &g_moduleCount , pTime );
339 : : }
340 : :
341 : : //==================================================================================================
342 : 160 : SAL_DLLPUBLIC_EXPORT void * SAL_CALL component_getFactory(
343 : : const sal_Char * pImplName, void * pServiceManager, void * pRegistryKey )
344 : : {
345 : 160 : return component_getFactoryHelper( pImplName, pServiceManager, pRegistryKey , g_entries );
346 : : }
347 : : }
348 : :
349 : :
350 : :
351 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|