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 .
21 #include <sal/log.hxx>
23 #include <com/sun/star/io/IOException.hpp>
24 #include <com/sun/star/io/NotConnectedException.hpp>
25 #include <com/sun/star/io/XActiveDataSource.hpp>
26 #include <com/sun/star/io/XActiveDataSink.hpp>
27 #include <com/sun/star/io/XActiveDataControl.hpp>
28 #include <com/sun/star/io/XConnectable.hpp>
29 #include <com/sun/star/lang/XServiceInfo.hpp>
30 #include <com/sun/star/uno/XComponentContext.hpp>
32 #include <cppuhelper/implbase.hxx>
33 #include <comphelper/interfacecontainer4.hxx>
34 #include <cppuhelper/supportsservice.hxx>
35 #include <osl/thread.h>
40 using namespace com::sun::star::uno
;
41 using namespace com::sun::star::lang
;
42 using namespace com::sun::star::io
;
48 class Pump
: public WeakImplHelper
<
49 XActiveDataSource
, XActiveDataSink
, XActiveDataControl
, XConnectable
, XServiceInfo
>
54 Reference
< XConnectable
> m_xPred
;
55 Reference
< XConnectable
> m_xSucc
;
56 Reference
< XInputStream
> m_xInput
;
57 Reference
< XOutputStream
> m_xOutput
;
58 comphelper::OInterfaceContainerHelper4
<XStreamListener
> m_cnt
;
62 static void static_run( void* pObject
);
67 void fireTerminated();
68 void fireError( const Any
&a
);
72 virtual ~Pump() override
;
75 virtual void SAL_CALL
setOutputStream( const Reference
< css::io::XOutputStream
>& xOutput
) override
;
76 virtual Reference
< css::io::XOutputStream
> SAL_CALL
getOutputStream() override
;
79 virtual void SAL_CALL
setInputStream( const Reference
< css::io::XInputStream
>& xStream
) override
;
80 virtual Reference
< css::io::XInputStream
> SAL_CALL
getInputStream() override
;
83 virtual void SAL_CALL
addListener( const Reference
< css::io::XStreamListener
>& xListener
) override
;
84 virtual void SAL_CALL
removeListener( const Reference
< css::io::XStreamListener
>& xListener
) override
;
85 virtual void SAL_CALL
start() override
;
86 virtual void SAL_CALL
terminate() override
;
89 virtual void SAL_CALL
setPredecessor( const Reference
< css::io::XConnectable
>& xPred
) override
;
90 virtual Reference
< css::io::XConnectable
> SAL_CALL
getPredecessor() override
;
91 virtual void SAL_CALL
setSuccessor( const Reference
< css::io::XConnectable
>& xSucc
) override
;
92 virtual Reference
< css::io::XConnectable
> SAL_CALL
getSuccessor() override
;
94 public: // XServiceInfo
95 virtual OUString SAL_CALL
getImplementationName() override
;
96 virtual Sequence
< OUString
> SAL_CALL
getSupportedServiceNames() override
;
97 virtual sal_Bool SAL_CALL
supportsService(const OUString
& ServiceName
) override
;
102 Pump::Pump() : m_aThread( nullptr ),
103 m_closeFired( false )
112 osl_joinWithThread( m_aThread
);
113 osl_destroyThread( m_aThread
);
117 void Pump::fireError( const Any
& exception
)
119 std::unique_lock
guard( m_aMutex
);
120 comphelper::OInterfaceIteratorHelper4
<XStreamListener
> iter( guard
, m_cnt
);
122 while( iter
.hasMoreElements() )
126 iter
.next()->error( exception
);
128 catch ( const RuntimeException
&e
)
130 SAL_WARN("io.streams","com.sun.star.comp.stoc.Pump: unexpected exception during calling listeners" << e
);
135 void Pump::fireClose()
139 std::unique_lock
guard( m_aMutex
);
150 std::unique_lock
guard( m_aMutex
);
151 comphelper::OInterfaceIteratorHelper4
<XStreamListener
> iter( guard
, m_cnt
);
153 while( iter
.hasMoreElements() )
157 iter
.next()->closed( );
159 catch ( const RuntimeException
&e
)
161 SAL_WARN("io.streams","com.sun.star.comp.stoc.Pump: unexpected exception during calling listeners" << e
);
166 void Pump::fireStarted()
168 std::unique_lock
guard( m_aMutex
);
169 comphelper::OInterfaceIteratorHelper4
<XStreamListener
> iter( guard
, m_cnt
);
171 while( iter
.hasMoreElements() )
175 iter
.next()->started( );
177 catch ( const RuntimeException
&e
)
179 SAL_WARN("io.streams","com.sun.star.comp.stoc.Pump: unexpected exception during calling listeners" << e
);
184 void Pump::fireTerminated()
186 std::unique_lock
guard( m_aMutex
);
187 comphelper::OInterfaceIteratorHelper4
<XStreamListener
> iter( guard
, m_cnt
);
189 while( iter
.hasMoreElements() )
193 iter
.next()->terminated();
195 catch ( const RuntimeException
&e
)
197 SAL_WARN("io.streams","com.sun.star.comp.stoc.Pump: unexpected exception during calling listeners" << e
);
205 // close streams and release references
206 Reference
< XInputStream
> rInput
;
207 Reference
< XOutputStream
> rOutput
;
209 std::unique_lock
guard( m_aMutex
);
222 rInput
->closeInput();
233 rOutput
->closeOutput();
242 void Pump::static_run( void* pObject
)
244 osl_setThreadName("io_stm::Pump::run()");
245 static_cast<Pump
*>(pObject
)->run();
246 static_cast<Pump
*>(pObject
)->release();
256 Reference
< XInputStream
> rInput
;
257 Reference
< XOutputStream
> rOutput
;
259 std::unique_lock
aGuard( m_aMutex
);
266 throw NotConnectedException( "no input stream set", static_cast<OWeakObject
*>(this) );
268 Sequence
< sal_Int8
> aData
;
269 while( rInput
->readSomeBytes( aData
, 65536 ) )
273 throw NotConnectedException( "no output stream set", static_cast<OWeakObject
*>(this) );
275 rOutput
->writeBytes( aData
);
279 catch ( const IOException
& e
)
281 fireError( Any( e
) );
283 catch ( const RuntimeException
& e
)
285 fireError( Any( e
) );
287 catch ( const Exception
& e
)
289 fireError( Any( e
) );
295 catch ( const css::uno::Exception
&e
)
297 // we are the last on the stack.
298 // this is to avoid crashing the program, when e.g. a bridge crashes
299 SAL_WARN("io.streams","com.sun.star.comp.stoc.Pump: unexpected exception during calling listeners" << e
);
308 void Pump::setPredecessor( const Reference
< XConnectable
>& xPred
)
310 std::unique_lock
aGuard( m_aMutex
);
315 Reference
< XConnectable
> Pump::getPredecessor()
317 std::unique_lock
aGuard( m_aMutex
);
322 void Pump::setSuccessor( const Reference
< XConnectable
>& xSucc
)
324 std::unique_lock
aGuard( m_aMutex
);
329 Reference
< XConnectable
> Pump::getSuccessor()
331 std::unique_lock
aGuard( m_aMutex
);
340 void Pump::addListener( const Reference
< XStreamListener
>& xListener
)
342 std::unique_lock
aGuard( m_aMutex
);
343 m_cnt
.addInterface( aGuard
, xListener
);
347 void Pump::removeListener( const Reference
< XStreamListener
>& xListener
)
349 std::unique_lock
aGuard( m_aMutex
);
350 m_cnt
.removeInterface( aGuard
, xListener
);
356 std::unique_lock
aGuard( m_aMutex
);
357 m_aThread
= osl_createSuspendedThread(Pump::static_run
,this);
360 throw RuntimeException(
361 "Pump::start Couldn't create worker thread",
365 // will be released by OPump::static_run
367 osl_resumeThread( m_aThread
);
372 void Pump::terminate()
376 // wait for the worker to die
378 osl_joinWithThread( m_aThread
);
389 void Pump::setInputStream( const Reference
< XInputStream
>& xStream
)
391 std::unique_lock
aGuard( m_aMutex
);
393 Reference
< XConnectable
> xConnect( xStream
, UNO_QUERY
);
395 xConnect
->setSuccessor( this );
396 // data transfer starts in XActiveDataControl::start
400 Reference
< XInputStream
> Pump::getInputStream()
402 std::unique_lock
aGuard( m_aMutex
);
411 void Pump::setOutputStream( const Reference
< XOutputStream
>& xOut
)
413 std::unique_lock
aGuard( m_aMutex
);
415 Reference
< XConnectable
> xConnect( xOut
, UNO_QUERY
);
417 xConnect
->setPredecessor( this );
418 // data transfer starts in XActiveDataControl::start
421 Reference
< XOutputStream
> Pump::getOutputStream()
423 std::unique_lock
aGuard( m_aMutex
);
428 OUString
Pump::getImplementationName()
430 return "com.sun.star.comp.io.Pump";
434 sal_Bool
Pump::supportsService(const OUString
& ServiceName
)
436 return cppu::supportsService(this, ServiceName
);
440 Sequence
< OUString
> Pump::getSupportedServiceNames()
442 return { "com.sun.star.io.Pump" };
447 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
448 io_Pump_get_implementation(
449 css::uno::XComponentContext
* , css::uno::Sequence
<css::uno::Any
> const&)
451 return cppu::acquire(new io_stm::Pump());
455 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */