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>
31 #include <cppuhelper/implbase.hxx>
32 #include <cppuhelper/interfacecontainer.hxx>
33 #include <cppuhelper/supportsservice.hxx>
34 #include <osl/mutex.hxx>
35 #include <osl/thread.h>
36 #include <tools/diagnose_ex.h>
41 using namespace com::sun::star::uno
;
42 using namespace com::sun::star::lang
;
43 using namespace com::sun::star::io
;
45 #include <services.hxx>
49 class Pump
: public WeakImplHelper
<
50 XActiveDataSource
, XActiveDataSink
, XActiveDataControl
, XConnectable
, XServiceInfo
>
55 Reference
< XConnectable
> m_xPred
;
56 Reference
< XConnectable
> m_xSucc
;
57 Reference
< XInputStream
> m_xInput
;
58 Reference
< XOutputStream
> m_xOutput
;
59 OInterfaceContainerHelper m_cnt
;
63 static void static_run( void* pObject
);
68 void fireTerminated();
69 void fireError( const Any
&a
);
73 virtual ~Pump() override
;
76 virtual void SAL_CALL
setOutputStream( const Reference
< css::io::XOutputStream
>& xOutput
) override
;
77 virtual Reference
< css::io::XOutputStream
> SAL_CALL
getOutputStream() override
;
80 virtual void SAL_CALL
setInputStream( const Reference
< css::io::XInputStream
>& xStream
) override
;
81 virtual Reference
< css::io::XInputStream
> SAL_CALL
getInputStream() override
;
84 virtual void SAL_CALL
addListener( const Reference
< css::io::XStreamListener
>& xListener
) override
;
85 virtual void SAL_CALL
removeListener( const Reference
< css::io::XStreamListener
>& xListener
) override
;
86 virtual void SAL_CALL
start() override
;
87 virtual void SAL_CALL
terminate() override
;
90 virtual void SAL_CALL
setPredecessor( const Reference
< css::io::XConnectable
>& xPred
) override
;
91 virtual Reference
< css::io::XConnectable
> SAL_CALL
getPredecessor() override
;
92 virtual void SAL_CALL
setSuccessor( const Reference
< css::io::XConnectable
>& xSucc
) override
;
93 virtual Reference
< css::io::XConnectable
> SAL_CALL
getSuccessor() override
;
95 public: // XServiceInfo
96 virtual OUString SAL_CALL
getImplementationName() override
;
97 virtual Sequence
< OUString
> SAL_CALL
getSupportedServiceNames() override
;
98 virtual sal_Bool SAL_CALL
supportsService(const OUString
& ServiceName
) override
;
101 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 OInterfaceIteratorHelper
iter( m_cnt
);
120 while( iter
.hasMoreElements() )
124 static_cast< XStreamListener
* > ( iter
.next() )->error( exception
);
126 catch ( const RuntimeException
& )
128 TOOLS_WARN_EXCEPTION("io.streams","com.sun.star.comp.stoc.Pump: unexpected exception during calling listeners");
133 void Pump::fireClose()
137 MutexGuard
guard( m_aMutex
);
147 OInterfaceIteratorHelper
iter( m_cnt
);
148 while( iter
.hasMoreElements() )
152 static_cast< XStreamListener
* > ( iter
.next() )->closed( );
154 catch ( const RuntimeException
& )
156 TOOLS_WARN_EXCEPTION("io.streams","com.sun.star.comp.stoc.Pump: unexpected exception during calling listeners");
162 void Pump::fireStarted()
164 OInterfaceIteratorHelper
iter( m_cnt
);
165 while( iter
.hasMoreElements() )
169 static_cast< XStreamListener
* > ( iter
.next() )->started( );
171 catch ( const RuntimeException
& )
173 TOOLS_WARN_EXCEPTION("io.streams","com.sun.star.comp.stoc.Pump: unexpected exception during calling listeners");
178 void Pump::fireTerminated()
180 OInterfaceIteratorHelper
iter( m_cnt
);
181 while( iter
.hasMoreElements() )
185 static_cast< XStreamListener
* > ( iter
.next() )->terminated();
187 catch ( const RuntimeException
& )
189 TOOLS_WARN_EXCEPTION("io.streams","com.sun.star.comp.stoc.Pump: unexpected exception during calling listeners");
197 // close streams and release references
198 Reference
< XInputStream
> rInput
;
199 Reference
< XOutputStream
> rOutput
;
201 MutexGuard
guard( m_aMutex
);
214 rInput
->closeInput();
225 rOutput
->closeOutput();
234 void Pump::static_run( void* pObject
)
236 osl_setThreadName("io_stm::Pump::run()");
237 static_cast<Pump
*>(pObject
)->run();
238 static_cast<Pump
*>(pObject
)->release();
248 Reference
< XInputStream
> rInput
;
249 Reference
< XOutputStream
> rOutput
;
251 Guard
< Mutex
> aGuard( m_aMutex
);
258 throw NotConnectedException( "no input stream set", static_cast<OWeakObject
*>(this) );
260 Sequence
< sal_Int8
> aData
;
261 while( rInput
->readSomeBytes( aData
, 65536 ) )
265 throw NotConnectedException( "no output stream set", static_cast<OWeakObject
*>(this) );
267 rOutput
->writeBytes( aData
);
271 catch ( const IOException
& e
)
273 fireError( makeAny( e
) );
275 catch ( const RuntimeException
& e
)
277 fireError( makeAny( e
) );
279 catch ( const Exception
& e
)
281 fireError( makeAny( e
) );
287 catch ( const css::uno::Exception
& )
289 // we are the last on the stack.
290 // this is to avoid crashing the program, when e.g. a bridge crashes
291 TOOLS_WARN_EXCEPTION("io.streams","com.sun.star.comp.stoc.Pump: unexpected exception during calling listeners");
300 void Pump::setPredecessor( const Reference
< XConnectable
>& xPred
)
302 Guard
< Mutex
> aGuard( m_aMutex
);
307 Reference
< XConnectable
> Pump::getPredecessor()
309 Guard
< Mutex
> aGuard( m_aMutex
);
314 void Pump::setSuccessor( const Reference
< XConnectable
>& xSucc
)
316 Guard
< Mutex
> aGuard( m_aMutex
);
321 Reference
< XConnectable
> Pump::getSuccessor()
323 Guard
< Mutex
> aGuard( m_aMutex
);
332 void Pump::addListener( const Reference
< XStreamListener
>& xListener
)
334 m_cnt
.addInterface( xListener
);
338 void Pump::removeListener( const Reference
< XStreamListener
>& xListener
)
340 m_cnt
.removeInterface( xListener
);
346 Guard
< Mutex
> aGuard( m_aMutex
);
347 m_aThread
= osl_createSuspendedThread(Pump::static_run
,this);
350 throw RuntimeException(
351 "Pump::start Couldn't create worker thread",
355 // will be released by OPump::static_run
357 osl_resumeThread( m_aThread
);
362 void Pump::terminate()
366 // wait for the worker to die
368 osl_joinWithThread( m_aThread
);
379 void Pump::setInputStream( const Reference
< XInputStream
>& xStream
)
381 Guard
< Mutex
> aGuard( m_aMutex
);
383 Reference
< XConnectable
> xConnect( xStream
, UNO_QUERY
);
385 xConnect
->setSuccessor( this );
386 // data transfer starts in XActiveDataControl::start
390 Reference
< XInputStream
> Pump::getInputStream()
392 Guard
< Mutex
> aGuard( m_aMutex
);
401 void Pump::setOutputStream( const Reference
< XOutputStream
>& xOut
)
403 Guard
< Mutex
> aGuard( m_aMutex
);
405 Reference
< XConnectable
> xConnect( xOut
, UNO_QUERY
);
407 xConnect
->setPredecessor( this );
408 // data transfer starts in XActiveDataControl::start
411 Reference
< XOutputStream
> Pump::getOutputStream()
413 Guard
< Mutex
> aGuard( m_aMutex
);
418 OUString
Pump::getImplementationName()
420 return OPumpImpl_getImplementationName();
424 sal_Bool
Pump::supportsService(const OUString
& ServiceName
)
426 return cppu::supportsService(this, ServiceName
);
430 Sequence
< OUString
> Pump::getSupportedServiceNames()
432 return OPumpImpl_getSupportedServiceNames();
436 Reference
< XInterface
> OPumpImpl_CreateInstance(
437 SAL_UNUSED_PARAMETER
const Reference
< XComponentContext
> & )
439 return Reference
< XInterface
>( *new Pump
);
442 OUString
OPumpImpl_getImplementationName()
444 return "com.sun.star.comp.io.Pump";
447 Sequence
<OUString
> OPumpImpl_getSupportedServiceNames()
449 return Sequence
< OUString
> { "com.sun.star.io.Pump" };
454 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */