1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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
;
49 class OAcceptor
: public WeakImplHelper2
< XAcceptor
, XServiceInfo
>
52 OAcceptor(const Reference
< XComponentContext
> & xCtx
);
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
;
69 PipeAcceptor
*m_pPipe
;
70 SocketAcceptor
*m_pSocket
;
72 OUString m_sLastDescription
;
75 Reference
< XMultiComponentFactory
> _xSMgr
;
76 Reference
< XComponentContext
> _xCtx
;
77 Reference
<XAcceptor
> _xAcceptor
;
81 OAcceptor::OAcceptor( const Reference
< XComponentContext
> & xCtx
)
84 , m_bInAccept( false )
85 , _xSMgr( xCtx
->getServiceManager() )
89 OAcceptor::~OAcceptor()
103 BeingInAccept( bool *pFlag
,const OUString
& sConnectionDescription
) throw( AlreadyAcceptingException
)
108 OUString
sMessage( "AlreadyAcceptingException :" );
109 sMessage
+= sConnectionDescription
;
110 throw AlreadyAcceptingException( sMessage
);
121 Reference
< XConnection
> OAcceptor::accept( const OUString
&sConnectionDescription
)
122 throw( AlreadyAcceptingException
,
123 ConnectionSetupException
,
124 IllegalArgumentException
,
125 RuntimeException
, std::exception
)
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" )
155 m_pPipe
= new PipeAcceptor(aName
, sConnectionDescription
);
164 MutexGuard
g( m_mutex
);
171 else if ( aDesc
.getName() == "socket" )
174 if (aDesc
.hasParameter(
176 aHost
= aDesc
.getParameter(
180 sal_uInt16 nPort
= static_cast< sal_uInt16
>(
185 = aDesc
.getParameter(
186 OUString("tcpnodelay")).toInt32() != 0;
188 m_pSocket
= new SocketAcceptor(
189 aHost
, nPort
, bTcpNoDelay
, sConnectionDescription
);
198 MutexGuard
g( m_mutex
);
207 OUString delegatee
= "com.sun.star.connection.Acceptor." + aDesc
.getName();
210 "trying to get service %s\n",
212 delegatee
, RTL_TEXTENCODING_ASCII_US
).getStr());
213 _xAcceptor
= Reference
<XAcceptor
>(
214 _xSMgr
->createInstanceWithContext(delegatee
, _xCtx
), UNO_QUERY
);
218 OUString
message("Acceptor: unknown delegatee ");
219 message
+= delegatee
;
221 throw ConnectionSetupException(message
);
225 catch (const rtl::MalformedUriException
& rEx
)
227 throw IllegalArgumentException(
229 Reference
< XInterface
> (),
232 m_sLastDescription
= sConnectionDescription
;
237 r
= m_pPipe
->accept();
241 r
= m_pSocket
->accept();
245 r
= _xAcceptor
->accept(sConnectionDescription
);
251 void SAL_CALL
OAcceptor::stopAccepting( ) throw( RuntimeException
, std::exception
)
253 MutexGuard
guard( m_mutex
);
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
;
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: */