xref: /trunk/main/io/source/acceptor/acceptor.cxx (revision cf6516809c57e1bb0a940545cca99cdad54d4ce2)
13716f815SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
33716f815SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
43716f815SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
53716f815SAndrew Rist  * distributed with this work for additional information
63716f815SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
73716f815SAndrew Rist  * to you under the Apache License, Version 2.0 (the
83716f815SAndrew Rist  * "License"); you may not use this file except in compliance
93716f815SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
113716f815SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
133716f815SAndrew Rist  * Unless required by applicable law or agreed to in writing,
143716f815SAndrew Rist  * software distributed under the License is distributed on an
153716f815SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
163716f815SAndrew Rist  * KIND, either express or implied.  See the License for the
173716f815SAndrew Rist  * specific language governing permissions and limitations
183716f815SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
203716f815SAndrew Rist  *************************************************************/
213716f815SAndrew Rist 
223716f815SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_io.hxx"
26cdf0e10cSrcweir #include <osl/mutex.hxx>
27cdf0e10cSrcweir 
28cdf0e10cSrcweir #include <uno/mapping.hxx>
29cdf0e10cSrcweir 
30cdf0e10cSrcweir #include <cppuhelper/factory.hxx>
31cdf0e10cSrcweir #include <cppuhelper/implbase2.hxx>
32cdf0e10cSrcweir #include <cppuhelper/implementationentry.hxx>
33cdf0e10cSrcweir #include "cppuhelper/unourl.hxx"
34cdf0e10cSrcweir #include "rtl/malformeduriexception.hxx"
35cdf0e10cSrcweir 
36cdf0e10cSrcweir #include <com/sun/star/connection/XAcceptor.hpp>
37cdf0e10cSrcweir #include <com/sun/star/lang/XServiceInfo.hpp>
38cdf0e10cSrcweir 
39cdf0e10cSrcweir #include "acceptor.hxx"
40cdf0e10cSrcweir 
41cdf0e10cSrcweir #define IMPLEMENTATION_NAME "com.sun.star.comp.io.Acceptor"
42cdf0e10cSrcweir #define SERVICE_NAME "com.sun.star.connection.Acceptor"
43cdf0e10cSrcweir 
44cdf0e10cSrcweir using namespace ::osl;
45cdf0e10cSrcweir using namespace ::rtl;
46cdf0e10cSrcweir using namespace ::cppu;
47cdf0e10cSrcweir using namespace ::com::sun::star::uno;
48cdf0e10cSrcweir using namespace ::com::sun::star::lang;
49cdf0e10cSrcweir using namespace ::com::sun::star::registry;
50cdf0e10cSrcweir using namespace ::com::sun::star::connection;
51cdf0e10cSrcweir 
52cdf0e10cSrcweir namespace io_acceptor
53cdf0e10cSrcweir {
54cdf0e10cSrcweir     rtl_StandardModuleCount g_moduleCount = MODULE_COUNT_INIT;
55cdf0e10cSrcweir 
56cdf0e10cSrcweir     class OAcceptor : public WeakImplHelper2< XAcceptor, XServiceInfo >
57cdf0e10cSrcweir     {
58cdf0e10cSrcweir     public:
59cdf0e10cSrcweir         OAcceptor(const Reference< XComponentContext > & xCtx);
60cdf0e10cSrcweir         virtual ~OAcceptor();
61cdf0e10cSrcweir     public:
62cdf0e10cSrcweir         // Methods
63cdf0e10cSrcweir         virtual Reference< XConnection > SAL_CALL accept( const OUString& sConnectionDescription )
64cdf0e10cSrcweir             throw( AlreadyAcceptingException,
65cdf0e10cSrcweir                    ConnectionSetupException,
66cdf0e10cSrcweir                    IllegalArgumentException,
67cdf0e10cSrcweir                    RuntimeException);
68cdf0e10cSrcweir         virtual void SAL_CALL stopAccepting(  ) throw( RuntimeException);
69cdf0e10cSrcweir 
70cdf0e10cSrcweir     public: // XServiceInfo
71cdf0e10cSrcweir                 virtual OUString              SAL_CALL getImplementationName() throw();
72cdf0e10cSrcweir                 virtual Sequence< OUString >  SAL_CALL getSupportedServiceNames(void) throw();
73cdf0e10cSrcweir                 virtual sal_Bool              SAL_CALL supportsService(const OUString& ServiceName) throw();
74cdf0e10cSrcweir 
75cdf0e10cSrcweir     private:
76cdf0e10cSrcweir         PipeAcceptor *m_pPipe;
77cdf0e10cSrcweir         SocketAcceptor *m_pSocket;
78cdf0e10cSrcweir         Mutex m_mutex;
79cdf0e10cSrcweir         OUString m_sLastDescription;
80cdf0e10cSrcweir         sal_Bool m_bInAccept;
81cdf0e10cSrcweir 
82cdf0e10cSrcweir         Reference< XMultiComponentFactory > _xSMgr;
83cdf0e10cSrcweir         Reference< XComponentContext > _xCtx;
84cdf0e10cSrcweir         Reference<XAcceptor>         _xAcceptor;
85cdf0e10cSrcweir     };
86cdf0e10cSrcweir 
87cdf0e10cSrcweir 
OAcceptor(const Reference<XComponentContext> & xCtx)88cdf0e10cSrcweir     OAcceptor::OAcceptor( const Reference< XComponentContext > & xCtx )
89cdf0e10cSrcweir         : m_pPipe( 0 )
90cdf0e10cSrcweir         , m_pSocket( 0 )
91cdf0e10cSrcweir         , m_bInAccept( sal_False )
92cdf0e10cSrcweir         , _xSMgr( xCtx->getServiceManager() )
93cdf0e10cSrcweir         , _xCtx( xCtx )
94cdf0e10cSrcweir     {
95cdf0e10cSrcweir         g_moduleCount.modCnt.acquire( &g_moduleCount.modCnt );
96cdf0e10cSrcweir     }
97cdf0e10cSrcweir 
~OAcceptor()98cdf0e10cSrcweir     OAcceptor::~OAcceptor()
99cdf0e10cSrcweir     {
100cdf0e10cSrcweir         if( m_pPipe )
101cdf0e10cSrcweir         {
102cdf0e10cSrcweir             delete m_pPipe;
103cdf0e10cSrcweir         }
104cdf0e10cSrcweir         if( m_pSocket )
105cdf0e10cSrcweir         {
106cdf0e10cSrcweir             delete m_pSocket;
107cdf0e10cSrcweir         }
108cdf0e10cSrcweir         g_moduleCount.modCnt.release( &g_moduleCount.modCnt );
109cdf0e10cSrcweir     }
110cdf0e10cSrcweir 
111cdf0e10cSrcweir     struct BeingInAccept
112cdf0e10cSrcweir     {
BeingInAcceptio_acceptor::BeingInAccept113cdf0e10cSrcweir         BeingInAccept( sal_Bool *pFlag,const OUString & sConnectionDescription  ) throw( AlreadyAcceptingException)
114cdf0e10cSrcweir             : m_pFlag( pFlag )
115cdf0e10cSrcweir             {
116cdf0e10cSrcweir                 if( *m_pFlag )
117cdf0e10cSrcweir                 {
118cdf0e10cSrcweir                     OUString sMessage( RTL_CONSTASCII_USTRINGPARAM( "AlreadyAcceptingException :" ) );
119cdf0e10cSrcweir                     sMessage += sConnectionDescription;
120cdf0e10cSrcweir                     throw AlreadyAcceptingException( sMessage , Reference< XInterface > () );
121cdf0e10cSrcweir                 }
122cdf0e10cSrcweir                 *m_pFlag = sal_True;
123cdf0e10cSrcweir             }
~BeingInAcceptio_acceptor::BeingInAccept124cdf0e10cSrcweir         ~BeingInAccept()
125cdf0e10cSrcweir             {
126cdf0e10cSrcweir                 *m_pFlag = sal_False;
127cdf0e10cSrcweir             }
128cdf0e10cSrcweir         sal_Bool *m_pFlag;
129cdf0e10cSrcweir     };
130cdf0e10cSrcweir 
accept(const OUString & sConnectionDescription)131cdf0e10cSrcweir     Reference< XConnection > OAcceptor::accept( const OUString &sConnectionDescription )
132cdf0e10cSrcweir         throw( AlreadyAcceptingException,
133cdf0e10cSrcweir                ConnectionSetupException,
134cdf0e10cSrcweir                IllegalArgumentException,
135cdf0e10cSrcweir                RuntimeException)
136cdf0e10cSrcweir     {
137cdf0e10cSrcweir         OSL_TRACE(
138cdf0e10cSrcweir             "acceptor %s\n",
139cdf0e10cSrcweir             OUStringToOString(
140cdf0e10cSrcweir                 sConnectionDescription, RTL_TEXTENCODING_ASCII_US).getStr());
141cdf0e10cSrcweir         // if there is a thread alread accepting in this object, throw an exception.
142cdf0e10cSrcweir         struct BeingInAccept guard( &m_bInAccept, sConnectionDescription );
143cdf0e10cSrcweir 
144cdf0e10cSrcweir         Reference< XConnection > r;
145cdf0e10cSrcweir         if( m_sLastDescription.getLength() &&
146cdf0e10cSrcweir             m_sLastDescription != sConnectionDescription )
147cdf0e10cSrcweir         {
148cdf0e10cSrcweir             // instantiate another acceptor for different ports
149cdf0e10cSrcweir             OUString sMessage = OUString( RTL_CONSTASCII_USTRINGPARAM(
150*6362920dSJohn Bampton                 "acceptor::accept called multiple times with different connection strings\n" ) );
151cdf0e10cSrcweir             throw ConnectionSetupException( sMessage, Reference< XInterface > () );
152cdf0e10cSrcweir         }
153cdf0e10cSrcweir 
154cdf0e10cSrcweir         if( ! m_sLastDescription.getLength() )
155cdf0e10cSrcweir         {
156cdf0e10cSrcweir             // setup the acceptor
157cdf0e10cSrcweir             try
158cdf0e10cSrcweir             {
159cdf0e10cSrcweir                 cppu::UnoUrlDescriptor aDesc(sConnectionDescription);
160cdf0e10cSrcweir                 if (aDesc.getName().equalsAsciiL(
161cdf0e10cSrcweir                         RTL_CONSTASCII_STRINGPARAM("pipe")))
162cdf0e10cSrcweir                 {
163cdf0e10cSrcweir                     rtl::OUString aName(
164cdf0e10cSrcweir                         aDesc.getParameter(
165cdf0e10cSrcweir                             rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
166cdf0e10cSrcweir                                               "name"))));
167cdf0e10cSrcweir 
168cdf0e10cSrcweir                     m_pPipe = new PipeAcceptor(aName, sConnectionDescription);
169cdf0e10cSrcweir 
170cdf0e10cSrcweir                     try
171cdf0e10cSrcweir                     {
172cdf0e10cSrcweir                         m_pPipe->init();
173cdf0e10cSrcweir                     }
174cdf0e10cSrcweir                     catch( ... )
175cdf0e10cSrcweir                     {
176cdf0e10cSrcweir                         {
177cdf0e10cSrcweir                             MutexGuard g( m_mutex );
178cdf0e10cSrcweir                             delete m_pPipe;
179cdf0e10cSrcweir                             m_pPipe = 0;
180cdf0e10cSrcweir                         }
181cdf0e10cSrcweir                         throw;
182cdf0e10cSrcweir                     }
183cdf0e10cSrcweir                 }
184cdf0e10cSrcweir                 else if (aDesc.getName().equalsAsciiL(
185cdf0e10cSrcweir                              RTL_CONSTASCII_STRINGPARAM("socket")))
186cdf0e10cSrcweir                 {
187cdf0e10cSrcweir                     rtl::OUString aHost;
188cdf0e10cSrcweir                     if (aDesc.hasParameter(
189cdf0e10cSrcweir                             rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("host"))))
190cdf0e10cSrcweir                         aHost = aDesc.getParameter(
191cdf0e10cSrcweir                             rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("host")));
192cdf0e10cSrcweir                     else
193cdf0e10cSrcweir                         aHost = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
194cdf0e10cSrcweir                                                   "localhost"));
195cdf0e10cSrcweir                     sal_uInt16 nPort = static_cast< sal_uInt16 >(
196cdf0e10cSrcweir                         aDesc.getParameter(
197cdf0e10cSrcweir                             rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("port"))).
198cdf0e10cSrcweir                         toInt32());
199cdf0e10cSrcweir                     bool bTcpNoDelay
200cdf0e10cSrcweir                         = aDesc.getParameter(
201cdf0e10cSrcweir                             rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(
202cdf0e10cSrcweir                                               "tcpnodelay"))).toInt32() != 0;
203cdf0e10cSrcweir 
204cdf0e10cSrcweir                     m_pSocket = new SocketAcceptor(
205cdf0e10cSrcweir                         aHost, nPort, bTcpNoDelay, sConnectionDescription);
206cdf0e10cSrcweir 
207cdf0e10cSrcweir                     try
208cdf0e10cSrcweir                     {
209cdf0e10cSrcweir                         m_pSocket->init();
210cdf0e10cSrcweir                     }
211cdf0e10cSrcweir                     catch( ... )
212cdf0e10cSrcweir                     {
213cdf0e10cSrcweir                         {
214cdf0e10cSrcweir                             MutexGuard g( m_mutex );
215cdf0e10cSrcweir                             delete m_pSocket;
216cdf0e10cSrcweir                             m_pSocket = 0;
217cdf0e10cSrcweir                         }
218cdf0e10cSrcweir                         throw;
219cdf0e10cSrcweir                     }
220cdf0e10cSrcweir                 }
221cdf0e10cSrcweir                 else
222cdf0e10cSrcweir                 {
223cdf0e10cSrcweir                     OUString delegatee = OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.connection.Acceptor."));
224cdf0e10cSrcweir                     delegatee += aDesc.getName();
225cdf0e10cSrcweir 
226cdf0e10cSrcweir                     OSL_TRACE(
227cdf0e10cSrcweir                         "trying to get service %s\n",
228cdf0e10cSrcweir                         OUStringToOString(
229cdf0e10cSrcweir                             delegatee, RTL_TEXTENCODING_ASCII_US).getStr());
230cdf0e10cSrcweir                     _xAcceptor = Reference<XAcceptor>(
231cdf0e10cSrcweir                         _xSMgr->createInstanceWithContext(delegatee, _xCtx), UNO_QUERY);
232cdf0e10cSrcweir 
233cdf0e10cSrcweir                     if(!_xAcceptor.is())
234cdf0e10cSrcweir                     {
235cdf0e10cSrcweir                         OUString message(RTL_CONSTASCII_USTRINGPARAM("Acceptor: unknown delegatee "));
236cdf0e10cSrcweir                         message += delegatee;
237cdf0e10cSrcweir 
238cdf0e10cSrcweir                         throw ConnectionSetupException(message, Reference<XInterface>());
239cdf0e10cSrcweir                     }
240cdf0e10cSrcweir                 }
241cdf0e10cSrcweir             }
242cdf0e10cSrcweir             catch (rtl::MalformedUriException & rEx)
243cdf0e10cSrcweir             {
244cdf0e10cSrcweir                 throw IllegalArgumentException(
245cdf0e10cSrcweir                     rEx.getMessage(),
246cdf0e10cSrcweir                     Reference< XInterface > (),
247cdf0e10cSrcweir                     0 );
248cdf0e10cSrcweir             }
249cdf0e10cSrcweir             m_sLastDescription = sConnectionDescription;
250cdf0e10cSrcweir         }
251cdf0e10cSrcweir 
252cdf0e10cSrcweir         if( m_pPipe )
253cdf0e10cSrcweir         {
254cdf0e10cSrcweir             r = m_pPipe->accept();
255cdf0e10cSrcweir         }
256cdf0e10cSrcweir         else if( m_pSocket )
257cdf0e10cSrcweir         {
258cdf0e10cSrcweir             r = m_pSocket->accept();
259cdf0e10cSrcweir         }
260cdf0e10cSrcweir         else
261cdf0e10cSrcweir         {
262cdf0e10cSrcweir             r = _xAcceptor->accept(sConnectionDescription);
263cdf0e10cSrcweir         }
264cdf0e10cSrcweir 
265cdf0e10cSrcweir         return r;
266cdf0e10cSrcweir     }
267cdf0e10cSrcweir 
stopAccepting()268cdf0e10cSrcweir     void SAL_CALL OAcceptor::stopAccepting(  ) throw( RuntimeException)
269cdf0e10cSrcweir     {
270cdf0e10cSrcweir         MutexGuard guard( m_mutex );
271cdf0e10cSrcweir 
272cdf0e10cSrcweir         if( m_pPipe )
273cdf0e10cSrcweir         {
274cdf0e10cSrcweir             m_pPipe->stopAccepting();
275cdf0e10cSrcweir         }
276cdf0e10cSrcweir         else if ( m_pSocket )
277cdf0e10cSrcweir         {
278cdf0e10cSrcweir             m_pSocket->stopAccepting();
279cdf0e10cSrcweir         }
280cdf0e10cSrcweir         else if( _xAcceptor.is() )
281cdf0e10cSrcweir         {
282cdf0e10cSrcweir             _xAcceptor->stopAccepting();
283cdf0e10cSrcweir         }
284cdf0e10cSrcweir 
285cdf0e10cSrcweir     }
286cdf0e10cSrcweir 
acceptor_getImplementationName()287cdf0e10cSrcweir     OUString acceptor_getImplementationName()
288cdf0e10cSrcweir     {
289cdf0e10cSrcweir         return OUString( RTL_CONSTASCII_USTRINGPARAM( IMPLEMENTATION_NAME ) );
290cdf0e10cSrcweir     }
291cdf0e10cSrcweir 
acceptor_CreateInstance(const Reference<XComponentContext> & xCtx)292cdf0e10cSrcweir     Reference< XInterface > SAL_CALL acceptor_CreateInstance( const Reference< XComponentContext > & xCtx)
293cdf0e10cSrcweir     {
294cdf0e10cSrcweir         return Reference < XInterface >( ( OWeakObject * ) new OAcceptor(xCtx) );
295cdf0e10cSrcweir     }
296cdf0e10cSrcweir 
acceptor_getSupportedServiceNames()297cdf0e10cSrcweir     Sequence< OUString > acceptor_getSupportedServiceNames()
298cdf0e10cSrcweir     {
299cdf0e10cSrcweir         static Sequence < OUString > *pNames = 0;
300cdf0e10cSrcweir         if( ! pNames )
301cdf0e10cSrcweir         {
302cdf0e10cSrcweir             MutexGuard guard( Mutex::getGlobalMutex() );
303cdf0e10cSrcweir             if( !pNames )
304cdf0e10cSrcweir             {
305cdf0e10cSrcweir                 static Sequence< OUString > seqNames(1);
306cdf0e10cSrcweir                 seqNames.getArray()[0] = OUString::createFromAscii( SERVICE_NAME );
307cdf0e10cSrcweir                 pNames = &seqNames;
308cdf0e10cSrcweir             }
309cdf0e10cSrcweir         }
310cdf0e10cSrcweir         return *pNames;
311cdf0e10cSrcweir     }
312cdf0e10cSrcweir 
getImplementationName()313cdf0e10cSrcweir         OUString OAcceptor::getImplementationName() throw()
314cdf0e10cSrcweir     {
315cdf0e10cSrcweir         return acceptor_getImplementationName();
316cdf0e10cSrcweir     }
317cdf0e10cSrcweir 
supportsService(const OUString & ServiceName)318cdf0e10cSrcweir         sal_Bool OAcceptor::supportsService(const OUString& ServiceName) throw()
319cdf0e10cSrcweir     {
320cdf0e10cSrcweir         Sequence< OUString > aSNL = getSupportedServiceNames();
321cdf0e10cSrcweir         const OUString * pArray = aSNL.getConstArray();
322cdf0e10cSrcweir 
323cdf0e10cSrcweir         for( sal_Int32 i = 0; i < aSNL.getLength(); i++ )
324cdf0e10cSrcweir             if( pArray[i] == ServiceName )
325cdf0e10cSrcweir                 return sal_True;
326cdf0e10cSrcweir 
327cdf0e10cSrcweir         return sal_False;
328cdf0e10cSrcweir     }
329cdf0e10cSrcweir 
getSupportedServiceNames(void)330cdf0e10cSrcweir         Sequence< OUString > OAcceptor::getSupportedServiceNames(void) throw()
331cdf0e10cSrcweir     {
332cdf0e10cSrcweir         return acceptor_getSupportedServiceNames();
333cdf0e10cSrcweir     }
334cdf0e10cSrcweir 
335cdf0e10cSrcweir 
336cdf0e10cSrcweir }
337cdf0e10cSrcweir 
338cdf0e10cSrcweir using namespace io_acceptor;
339cdf0e10cSrcweir 
340cdf0e10cSrcweir static struct ImplementationEntry g_entries[] =
341cdf0e10cSrcweir {
342cdf0e10cSrcweir     {
343cdf0e10cSrcweir         acceptor_CreateInstance, acceptor_getImplementationName ,
344cdf0e10cSrcweir         acceptor_getSupportedServiceNames, createSingleComponentFactory ,
345cdf0e10cSrcweir         &g_moduleCount.modCnt , 0
346cdf0e10cSrcweir     },
347cdf0e10cSrcweir     { 0, 0, 0, 0, 0, 0 }
348cdf0e10cSrcweir };
349cdf0e10cSrcweir 
350cdf0e10cSrcweir extern "C"
351cdf0e10cSrcweir {
352cdf0e10cSrcweir 
component_canUnload(TimeValue * pTime)353032611ffSdamjan SAL_DLLPUBLIC_EXPORT sal_Bool SAL_CALL component_canUnload( TimeValue *pTime )
354cdf0e10cSrcweir {
355cdf0e10cSrcweir     return g_moduleCount.canUnload( &g_moduleCount , pTime );
356cdf0e10cSrcweir }
357cdf0e10cSrcweir 
358cdf0e10cSrcweir //==================================================================================================
component_getImplementationEnvironment(const sal_Char ** ppEnvTypeName,uno_Environment **)359032611ffSdamjan SAL_DLLPUBLIC_EXPORT void SAL_CALL component_getImplementationEnvironment(
360cdf0e10cSrcweir     const sal_Char ** ppEnvTypeName, uno_Environment ** )
361cdf0e10cSrcweir {
362cdf0e10cSrcweir     *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
363cdf0e10cSrcweir }
364cdf0e10cSrcweir //==================================================================================================
component_getFactory(const sal_Char * pImplName,void * pServiceManager,void * pRegistryKey)365032611ffSdamjan SAL_DLLPUBLIC_EXPORT void * SAL_CALL component_getFactory(
366cdf0e10cSrcweir     const sal_Char * pImplName, void * pServiceManager, void * pRegistryKey )
367cdf0e10cSrcweir {
368cdf0e10cSrcweir     return component_getFactoryHelper( pImplName, pServiceManager, pRegistryKey , g_entries );
369cdf0e10cSrcweir }
370cdf0e10cSrcweir }
371