bump product version to 5.0.4.1
[LibreOffice.git] / io / source / acceptor / acceptor.cxx
blob4791dc21b8c8b51975af87c686bef2725a78333c
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/supportsservice.hxx>
28 #include "cppuhelper/unourl.hxx"
29 #include "rtl/malformeduriexception.hxx"
31 #include <com/sun/star/connection/XAcceptor.hpp>
32 #include <com/sun/star/lang/XServiceInfo.hpp>
34 #include "services.hxx"
35 #include "acceptor.hxx"
37 #define IMPLEMENTATION_NAME "com.sun.star.comp.io.Acceptor"
38 #define SERVICE_NAME "com.sun.star.connection.Acceptor"
40 using namespace ::osl;
41 using namespace ::cppu;
42 using namespace ::com::sun::star::uno;
43 using namespace ::com::sun::star::lang;
44 using namespace ::com::sun::star::registry;
45 using namespace ::com::sun::star::connection;
47 namespace io_acceptor
49 class OAcceptor : public WeakImplHelper2< XAcceptor, XServiceInfo >
51 public:
52 OAcceptor(const Reference< XComponentContext > & xCtx);
53 virtual ~OAcceptor();
54 public:
55 // Methods
56 virtual Reference< XConnection > SAL_CALL accept( const OUString& sConnectionDescription )
57 throw( AlreadyAcceptingException,
58 ConnectionSetupException,
59 IllegalArgumentException,
60 RuntimeException, std::exception) SAL_OVERRIDE;
61 virtual void SAL_CALL stopAccepting( ) throw( RuntimeException, std::exception) SAL_OVERRIDE;
63 public: // XServiceInfo
64 virtual OUString SAL_CALL getImplementationName() throw(std::exception) SAL_OVERRIDE;
65 virtual Sequence< OUString > SAL_CALL getSupportedServiceNames() throw(std::exception) SAL_OVERRIDE;
66 virtual sal_Bool SAL_CALL supportsService(const OUString& ServiceName) throw(std::exception) SAL_OVERRIDE;
68 private:
69 PipeAcceptor *m_pPipe;
70 SocketAcceptor *m_pSocket;
71 Mutex m_mutex;
72 OUString m_sLastDescription;
73 bool m_bInAccept;
75 Reference< XMultiComponentFactory > _xSMgr;
76 Reference< XComponentContext > _xCtx;
77 Reference<XAcceptor> _xAcceptor;
81 OAcceptor::OAcceptor( const Reference< XComponentContext > & xCtx )
82 : m_pPipe( 0 )
83 , m_pSocket( 0 )
84 , m_bInAccept( false )
85 , _xSMgr( xCtx->getServiceManager() )
86 , _xCtx( xCtx )
89 OAcceptor::~OAcceptor()
91 if( m_pPipe )
93 delete m_pPipe;
95 if( m_pSocket )
97 delete m_pSocket;
101 struct BeingInAccept
103 BeingInAccept( bool *pFlag,const OUString & sConnectionDescription ) throw( AlreadyAcceptingException)
104 : m_pFlag( pFlag )
106 if( *m_pFlag )
108 OUString sMessage( "AlreadyAcceptingException :" );
109 sMessage += sConnectionDescription;
110 throw AlreadyAcceptingException( sMessage );
112 *m_pFlag = true;
114 ~BeingInAccept()
116 *m_pFlag = false;
118 bool *m_pFlag;
121 Reference< XConnection > OAcceptor::accept( const OUString &sConnectionDescription )
122 throw( AlreadyAcceptingException,
123 ConnectionSetupException,
124 IllegalArgumentException,
125 RuntimeException, std::exception)
127 OSL_TRACE(
128 "acceptor %s\n",
129 OUStringToOString(
130 sConnectionDescription, RTL_TEXTENCODING_ASCII_US).getStr());
131 // if there is a thread alread accepting in this object, throw an exception.
132 struct BeingInAccept guard( &m_bInAccept, sConnectionDescription );
134 Reference< XConnection > r;
135 if( !m_sLastDescription.isEmpty() &&
136 m_sLastDescription != sConnectionDescription )
138 // instantiate another acceptor for different ports
139 OUString sMessage = "acceptor::accept called multiple times with different conncetion strings\n";
140 throw ConnectionSetupException( sMessage );
143 if( m_sLastDescription.isEmpty() )
145 // setup the acceptor
148 cppu::UnoUrlDescriptor aDesc(sConnectionDescription);
149 if ( aDesc.getName() == "pipe" )
151 OUString aName(
152 aDesc.getParameter(
153 OUString("name")));
155 m_pPipe = new PipeAcceptor(aName, sConnectionDescription);
159 m_pPipe->init();
161 catch( ... )
164 MutexGuard g( m_mutex );
165 delete m_pPipe;
166 m_pPipe = 0;
168 throw;
171 else if ( aDesc.getName() == "socket" )
173 OUString aHost;
174 if (aDesc.hasParameter(
175 OUString("host")))
176 aHost = aDesc.getParameter(
177 OUString("host"));
178 else
179 aHost = "localhost";
180 sal_uInt16 nPort = static_cast< sal_uInt16 >(
181 aDesc.getParameter(
182 OUString("port")).
183 toInt32());
184 bool bTcpNoDelay
185 = aDesc.getParameter(
186 OUString("tcpnodelay")).toInt32() != 0;
188 m_pSocket = new SocketAcceptor(
189 aHost, nPort, bTcpNoDelay, sConnectionDescription);
193 m_pSocket->init();
195 catch( ... )
198 MutexGuard g( m_mutex );
199 delete m_pSocket;
200 m_pSocket = 0;
202 throw;
205 else
207 OUString delegatee = "com.sun.star.connection.Acceptor." + aDesc.getName();
209 OSL_TRACE(
210 "trying to get service %s\n",
211 OUStringToOString(
212 delegatee, RTL_TEXTENCODING_ASCII_US).getStr());
213 _xAcceptor = Reference<XAcceptor>(
214 _xSMgr->createInstanceWithContext(delegatee, _xCtx), UNO_QUERY);
216 if(!_xAcceptor.is())
218 OUString message("Acceptor: unknown delegatee ");
219 message += delegatee;
221 throw ConnectionSetupException(message);
225 catch (const rtl::MalformedUriException & rEx)
227 throw IllegalArgumentException(
228 rEx.getMessage(),
229 Reference< XInterface > (),
230 0 );
232 m_sLastDescription = sConnectionDescription;
235 if( m_pPipe )
237 r = m_pPipe->accept();
239 else if( m_pSocket )
241 r = m_pSocket->accept();
243 else
245 r = _xAcceptor->accept(sConnectionDescription);
248 return r;
251 void SAL_CALL OAcceptor::stopAccepting( ) throw( RuntimeException, std::exception)
253 MutexGuard guard( m_mutex );
255 if( m_pPipe )
257 m_pPipe->stopAccepting();
259 else if ( m_pSocket )
261 m_pSocket->stopAccepting();
263 else if( _xAcceptor.is() )
265 _xAcceptor->stopAccepting();
270 OUString acceptor_getImplementationName()
272 return OUString( IMPLEMENTATION_NAME );
275 Reference< XInterface > SAL_CALL acceptor_CreateInstance( const Reference< XComponentContext > & xCtx)
277 return Reference < XInterface >( ( OWeakObject * ) new OAcceptor(xCtx) );
280 Sequence< OUString > acceptor_getSupportedServiceNames()
282 Sequence< OUString > seqNames(1);
283 seqNames.getArray()[0] = SERVICE_NAME;
284 return seqNames;
287 OUString OAcceptor::getImplementationName() throw(std::exception)
289 return acceptor_getImplementationName();
292 sal_Bool OAcceptor::supportsService(const OUString& ServiceName) throw(std::exception)
294 return cppu::supportsService(this, ServiceName);
297 Sequence< OUString > OAcceptor::getSupportedServiceNames() throw(std::exception)
299 return acceptor_getSupportedServiceNames();
305 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */