Bump version to 6.4-15
[LibreOffice.git] / io / source / acceptor / acceptor.cxx
blob217c23b9a84583ce735aa57a15e652badb1d2966
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 <cppuhelper/implbase.hxx>
23 #include <cppuhelper/supportsservice.hxx>
24 #include <cppuhelper/unourl.hxx>
25 #include <rtl/malformeduriexception.hxx>
27 #include <com/sun/star/connection/AlreadyAcceptingException.hpp>
28 #include <com/sun/star/connection/ConnectionSetupException.hpp>
29 #include <com/sun/star/connection/XAcceptor.hpp>
30 #include <com/sun/star/lang/IllegalArgumentException.hpp>
31 #include <com/sun/star/lang/XServiceInfo.hpp>
32 #include <com/sun/star/uno/XComponentContext.hpp>
34 #include <services.hxx>
35 #include "acceptor.hxx"
36 #include <memory>
38 #define IMPLEMENTATION_NAME "com.sun.star.comp.io.Acceptor"
39 #define SERVICE_NAME "com.sun.star.connection.Acceptor"
41 using namespace ::osl;
42 using namespace ::cppu;
43 using namespace ::com::sun::star::uno;
44 using namespace ::com::sun::star::lang;
45 using namespace ::com::sun::star::connection;
47 namespace io_acceptor
49 class OAcceptor : public WeakImplHelper< XAcceptor, XServiceInfo >
51 public:
52 explicit OAcceptor(const Reference< XComponentContext > & xCtx);
53 virtual ~OAcceptor() override;
54 public:
55 // Methods
56 virtual Reference< XConnection > SAL_CALL accept( const OUString& sConnectionDescription ) override;
57 virtual void SAL_CALL stopAccepting( ) override;
59 public: // XServiceInfo
60 virtual OUString SAL_CALL getImplementationName() override;
61 virtual Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
62 virtual sal_Bool SAL_CALL supportsService(const OUString& ServiceName) override;
64 private:
65 std::unique_ptr<PipeAcceptor> m_pPipe;
66 std::unique_ptr<SocketAcceptor> m_pSocket;
67 Mutex m_mutex;
68 OUString m_sLastDescription;
69 bool m_bInAccept;
71 Reference< XMultiComponentFactory > _xSMgr;
72 Reference< XComponentContext > _xCtx;
73 Reference<XAcceptor> _xAcceptor;
77 OAcceptor::OAcceptor( const Reference< XComponentContext > & xCtx )
78 : m_bInAccept( false )
79 , _xSMgr( xCtx->getServiceManager() )
80 , _xCtx( xCtx )
83 OAcceptor::~OAcceptor()
85 m_pPipe.reset();
88 struct BeingInAccept
90 /// @throws AlreadyAcceptingException
91 BeingInAccept( bool *pFlag,const OUString & sConnectionDescription )
92 : m_pFlag( pFlag )
94 if( *m_pFlag )
95 throw AlreadyAcceptingException( "AlreadyAcceptingException :" + sConnectionDescription );
96 *m_pFlag = true;
98 ~BeingInAccept()
100 *m_pFlag = false;
102 bool *m_pFlag;
105 Reference< XConnection > OAcceptor::accept( const OUString &sConnectionDescription )
107 // if there is a thread already accepting in this object, throw an exception.
108 struct BeingInAccept guard( &m_bInAccept, sConnectionDescription );
110 Reference< XConnection > r;
111 if( !m_sLastDescription.isEmpty() &&
112 m_sLastDescription != sConnectionDescription )
114 // instantiate another acceptor for different ports
115 OUString sMessage = "acceptor::accept called multiple times with different connection strings\n";
116 throw ConnectionSetupException( sMessage );
119 if( m_sLastDescription.isEmpty() )
121 // setup the acceptor
124 cppu::UnoUrlDescriptor aDesc(sConnectionDescription);
125 if ( aDesc.getName() == "pipe" )
127 OUString aName(
128 aDesc.getParameter(
129 "name"));
131 m_pPipe.reset(new PipeAcceptor(aName, sConnectionDescription));
135 m_pPipe->init();
137 catch( ... )
140 MutexGuard g( m_mutex );
141 m_pPipe.reset();
143 throw;
146 else if ( aDesc.getName() == "socket" )
148 OUString aHost;
149 if (aDesc.hasParameter(
150 "host"))
151 aHost = aDesc.getParameter(
152 "host");
153 else
154 aHost = "localhost";
155 sal_uInt16 nPort = static_cast< sal_uInt16 >(
156 aDesc.getParameter(
157 "port").
158 toInt32());
159 bool bTcpNoDelay
160 = aDesc.getParameter(
161 "tcpnodelay").toInt32() != 0;
163 m_pSocket.reset(new SocketAcceptor(
164 aHost, nPort, bTcpNoDelay, sConnectionDescription));
168 m_pSocket->init();
170 catch( ... )
173 MutexGuard g( m_mutex );
174 m_pSocket.reset();
176 throw;
179 else
181 OUString delegatee = "com.sun.star.connection.Acceptor." + aDesc.getName();
182 _xAcceptor.set(_xSMgr->createInstanceWithContext(delegatee, _xCtx), UNO_QUERY);
184 if(!_xAcceptor.is())
185 throw ConnectionSetupException("Acceptor: unknown delegatee " + delegatee);
188 catch (const rtl::MalformedUriException & rEx)
190 throw IllegalArgumentException(
191 rEx.getMessage(),
192 Reference< XInterface > (),
193 0 );
195 m_sLastDescription = sConnectionDescription;
198 if( m_pPipe )
200 r = m_pPipe->accept();
202 else if( m_pSocket )
204 r = m_pSocket->accept();
206 else
208 r = _xAcceptor->accept(sConnectionDescription);
211 return r;
214 void SAL_CALL OAcceptor::stopAccepting( )
216 MutexGuard guard( m_mutex );
218 if( m_pPipe )
220 m_pPipe->stopAccepting();
222 else if ( m_pSocket )
224 m_pSocket->stopAccepting();
226 else if( _xAcceptor.is() )
228 _xAcceptor->stopAccepting();
233 OUString acceptor_getImplementationName()
235 return IMPLEMENTATION_NAME;
238 Reference< XInterface > acceptor_CreateInstance( const Reference< XComponentContext > & xCtx)
240 return Reference < XInterface >( static_cast<OWeakObject *>(new OAcceptor(xCtx)) );
243 Sequence< OUString > acceptor_getSupportedServiceNames()
245 Sequence< OUString > seqNames { SERVICE_NAME };
246 return seqNames;
249 OUString OAcceptor::getImplementationName()
251 return acceptor_getImplementationName();
254 sal_Bool OAcceptor::supportsService(const OUString& ServiceName)
256 return cppu::supportsService(this, ServiceName);
259 Sequence< OUString > OAcceptor::getSupportedServiceNames()
261 return acceptor_getSupportedServiceNames();
267 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */