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>
39 using namespace com::sun::star::uno
;
40 using namespace com::sun::star::lang
;
41 using namespace com::sun::star::io
;
47 class Pump
: public WeakImplHelper
<
48 XActiveDataSource
, XActiveDataSink
, XActiveDataControl
, XConnectable
, XServiceInfo
>
53 Reference
< XConnectable
> m_xPred
;
54 Reference
< XConnectable
> m_xSucc
;
55 Reference
< XInputStream
> m_xInput
;
56 Reference
< XOutputStream
> m_xOutput
;
57 comphelper::OInterfaceContainerHelper4
<XStreamListener
> m_cnt
;
61 static void static_run( void* pObject
);
66 void fireTerminated();
67 void fireError( const Any
&a
);
71 virtual ~Pump() override
;
74 virtual void SAL_CALL
setOutputStream( const Reference
< css::io::XOutputStream
>& xOutput
) override
;
75 virtual Reference
< css::io::XOutputStream
> SAL_CALL
getOutputStream() override
;
78 virtual void SAL_CALL
setInputStream( const Reference
< css::io::XInputStream
>& xStream
) override
;
79 virtual Reference
< css::io::XInputStream
> SAL_CALL
getInputStream() override
;
82 virtual void SAL_CALL
addListener( const Reference
< css::io::XStreamListener
>& xListener
) override
;
83 virtual void SAL_CALL
removeListener( const Reference
< css::io::XStreamListener
>& xListener
) override
;
84 virtual void SAL_CALL
start() override
;
85 virtual void SAL_CALL
terminate() override
;
88 virtual void SAL_CALL
setPredecessor( const Reference
< css::io::XConnectable
>& xPred
) override
;
89 virtual Reference
< css::io::XConnectable
> SAL_CALL
getPredecessor() override
;
90 virtual void SAL_CALL
setSuccessor( const Reference
< css::io::XConnectable
>& xSucc
) override
;
91 virtual Reference
< css::io::XConnectable
> SAL_CALL
getSuccessor() override
;
93 public: // XServiceInfo
94 virtual OUString SAL_CALL
getImplementationName() override
;
95 virtual Sequence
< OUString
> SAL_CALL
getSupportedServiceNames() override
;
96 virtual sal_Bool SAL_CALL
supportsService(const OUString
& ServiceName
) override
;
101 Pump::Pump() : m_aThread( nullptr ),
102 m_closeFired( false )
111 osl_joinWithThread( m_aThread
);
112 osl_destroyThread( m_aThread
);
116 void Pump::fireError( const Any
& exception
)
118 std::unique_lock
guard( m_aMutex
);
119 comphelper::OInterfaceIteratorHelper4
<XStreamListener
> iter( guard
, m_cnt
);
121 while( iter
.hasMoreElements() )
125 iter
.next()->error( exception
);
127 catch ( const RuntimeException
&e
)
129 SAL_WARN("io.streams","com.sun.star.comp.stoc.Pump: unexpected exception during calling listeners" << e
);
134 void Pump::fireClose()
138 std::unique_lock
guard( m_aMutex
);
149 std::unique_lock
guard( m_aMutex
);
150 comphelper::OInterfaceIteratorHelper4
<XStreamListener
> iter( guard
, m_cnt
);
152 while( iter
.hasMoreElements() )
156 iter
.next()->closed( );
158 catch ( const RuntimeException
&e
)
160 SAL_WARN("io.streams","com.sun.star.comp.stoc.Pump: unexpected exception during calling listeners" << e
);
165 void Pump::fireStarted()
167 std::unique_lock
guard( m_aMutex
);
168 comphelper::OInterfaceIteratorHelper4
<XStreamListener
> iter( guard
, m_cnt
);
170 while( iter
.hasMoreElements() )
174 iter
.next()->started( );
176 catch ( const RuntimeException
&e
)
178 SAL_WARN("io.streams","com.sun.star.comp.stoc.Pump: unexpected exception during calling listeners" << e
);
183 void Pump::fireTerminated()
185 std::unique_lock
guard( m_aMutex
);
186 comphelper::OInterfaceIteratorHelper4
<XStreamListener
> iter( guard
, m_cnt
);
188 while( iter
.hasMoreElements() )
192 iter
.next()->terminated();
194 catch ( const RuntimeException
&e
)
196 SAL_WARN("io.streams","com.sun.star.comp.stoc.Pump: unexpected exception during calling listeners" << e
);
204 // close streams and release references
205 Reference
< XInputStream
> rInput
;
206 Reference
< XOutputStream
> rOutput
;
208 std::unique_lock
guard( m_aMutex
);
221 rInput
->closeInput();
232 rOutput
->closeOutput();
241 void Pump::static_run( void* pObject
)
243 osl_setThreadName("io_stm::Pump::run()");
244 static_cast<Pump
*>(pObject
)->run();
245 static_cast<Pump
*>(pObject
)->release();
255 Reference
< XInputStream
> rInput
;
256 Reference
< XOutputStream
> rOutput
;
258 std::unique_lock
aGuard( m_aMutex
);
265 throw NotConnectedException( u
"no input stream set"_ustr
, getXWeak() );
267 Sequence
< sal_Int8
> aData
;
268 while( rInput
->readSomeBytes( aData
, 65536 ) )
272 throw NotConnectedException( u
"no output stream set"_ustr
, getXWeak() );
274 rOutput
->writeBytes( aData
);
278 catch ( const IOException
& e
)
280 fireError( Any( e
) );
282 catch ( const RuntimeException
& e
)
284 fireError( Any( e
) );
286 catch ( const Exception
& e
)
288 fireError( Any( e
) );
294 catch ( const css::uno::Exception
&e
)
296 // we are the last on the stack.
297 // this is to avoid crashing the program, when e.g. a bridge crashes
298 SAL_WARN("io.streams","com.sun.star.comp.stoc.Pump: unexpected exception during calling listeners" << e
);
307 void Pump::setPredecessor( const Reference
< XConnectable
>& xPred
)
309 std::unique_lock
aGuard( m_aMutex
);
314 Reference
< XConnectable
> Pump::getPredecessor()
316 std::unique_lock
aGuard( m_aMutex
);
321 void Pump::setSuccessor( const Reference
< XConnectable
>& xSucc
)
323 std::unique_lock
aGuard( m_aMutex
);
328 Reference
< XConnectable
> Pump::getSuccessor()
330 std::unique_lock
aGuard( m_aMutex
);
339 void Pump::addListener( const Reference
< XStreamListener
>& xListener
)
341 std::unique_lock
aGuard( m_aMutex
);
342 m_cnt
.addInterface( aGuard
, xListener
);
346 void Pump::removeListener( const Reference
< XStreamListener
>& xListener
)
348 std::unique_lock
aGuard( m_aMutex
);
349 m_cnt
.removeInterface( aGuard
, xListener
);
355 std::unique_lock
aGuard( m_aMutex
);
356 m_aThread
= osl_createSuspendedThread(Pump::static_run
,this);
359 throw RuntimeException(
360 u
"Pump::start Couldn't create worker thread"_ustr
,
364 // will be released by OPump::static_run
366 osl_resumeThread( m_aThread
);
371 void Pump::terminate()
375 // wait for the worker to die
377 osl_joinWithThread( m_aThread
);
388 void Pump::setInputStream( const Reference
< XInputStream
>& xStream
)
390 std::unique_lock
aGuard( m_aMutex
);
392 Reference
< XConnectable
> xConnect( xStream
, UNO_QUERY
);
394 xConnect
->setSuccessor( this );
395 // data transfer starts in XActiveDataControl::start
399 Reference
< XInputStream
> Pump::getInputStream()
401 std::unique_lock
aGuard( m_aMutex
);
410 void Pump::setOutputStream( const Reference
< XOutputStream
>& xOut
)
412 std::unique_lock
aGuard( m_aMutex
);
414 Reference
< XConnectable
> xConnect( xOut
, UNO_QUERY
);
416 xConnect
->setPredecessor( this );
417 // data transfer starts in XActiveDataControl::start
420 Reference
< XOutputStream
> Pump::getOutputStream()
422 std::unique_lock
aGuard( m_aMutex
);
427 OUString
Pump::getImplementationName()
429 return u
"com.sun.star.comp.io.Pump"_ustr
;
433 sal_Bool
Pump::supportsService(const OUString
& ServiceName
)
435 return cppu::supportsService(this, ServiceName
);
439 Sequence
< OUString
> Pump::getSupportedServiceNames()
441 return { u
"com.sun.star.io.Pump"_ustr
};
446 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
447 io_Pump_get_implementation(
448 css::uno::XComponentContext
* , css::uno::Sequence
<css::uno::Any
> const&)
450 return cppu::acquire(new io_stm::Pump());
454 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */