Bump version to 6.4-15
[LibreOffice.git] / io / source / stm / opump.cxx
blobfc6ba14c84883882847812eccf45b58a992d61a2
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 .
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>
38 using namespace osl;
39 using namespace std;
40 using namespace cppu;
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>
47 namespace io_stm {
49 class Pump : public WeakImplHelper<
50 XActiveDataSource, XActiveDataSink, XActiveDataControl, XConnectable, XServiceInfo >
52 Mutex m_aMutex;
53 oslThread m_aThread;
55 Reference< XConnectable > m_xPred;
56 Reference< XConnectable > m_xSucc;
57 Reference< XInputStream > m_xInput;
58 Reference< XOutputStream > m_xOutput;
59 OInterfaceContainerHelper m_cnt;
60 bool m_closeFired;
62 void run();
63 static void static_run( void* pObject );
65 void close();
66 void fireClose();
67 void fireStarted();
68 void fireTerminated();
69 void fireError( const Any &a );
71 public:
72 Pump();
73 virtual ~Pump() override;
75 // XActiveDataSource
76 virtual void SAL_CALL setOutputStream( const Reference< css::io::XOutputStream >& xOutput ) override;
77 virtual Reference< css::io::XOutputStream > SAL_CALL getOutputStream() override;
79 // XActiveDataSink
80 virtual void SAL_CALL setInputStream( const Reference< css::io::XInputStream >& xStream ) override;
81 virtual Reference< css::io::XInputStream > SAL_CALL getInputStream() override;
83 // XActiveDataControl
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;
89 // XConnectable
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 ),
102 m_cnt( m_aMutex ),
103 m_closeFired( false )
107 Pump::~Pump()
109 // exit gracefully
110 if( m_aThread )
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()
135 bool bFire = false;
137 MutexGuard guard( m_aMutex );
138 if( ! m_closeFired )
140 m_closeFired = true;
141 bFire = true;
145 if( bFire )
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");
195 void Pump::close()
197 // close streams and release references
198 Reference< XInputStream > rInput;
199 Reference< XOutputStream > rOutput;
201 MutexGuard guard( m_aMutex );
202 rInput = m_xInput;
203 m_xInput.clear();
205 rOutput = m_xOutput;
206 m_xOutput.clear();
207 m_xSucc.clear();
208 m_xPred.clear();
210 if( rInput.is() )
214 rInput->closeInput();
216 catch( Exception & )
218 // go down calm
221 if( rOutput.is() )
225 rOutput->closeOutput();
227 catch( Exception & )
229 // go down calm
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();
241 void Pump::run()
245 fireStarted();
248 Reference< XInputStream > rInput;
249 Reference< XOutputStream > rOutput;
251 Guard< Mutex > aGuard( m_aMutex );
252 rInput = m_xInput;
253 rOutput = m_xOutput;
256 if( ! rInput.is() )
258 throw NotConnectedException( "no input stream set", static_cast<OWeakObject*>(this) );
260 Sequence< sal_Int8 > aData;
261 while( rInput->readSomeBytes( aData, 65536 ) )
263 if( ! rOutput.is() )
265 throw NotConnectedException( "no output stream set", static_cast<OWeakObject*>(this) );
267 rOutput->writeBytes( aData );
268 osl_yieldThread();
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 ) );
284 close();
285 fireClose();
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");
297 * XConnectable
300 void Pump::setPredecessor( const Reference< XConnectable >& xPred )
302 Guard< Mutex > aGuard( m_aMutex );
303 m_xPred = xPred;
307 Reference< XConnectable > Pump::getPredecessor()
309 Guard< Mutex > aGuard( m_aMutex );
310 return m_xPred;
314 void Pump::setSuccessor( const Reference< XConnectable >& xSucc )
316 Guard< Mutex > aGuard( m_aMutex );
317 m_xSucc = xSucc;
321 Reference< XConnectable > Pump::getSuccessor()
323 Guard< Mutex > aGuard( m_aMutex );
324 return m_xSucc;
329 * XActiveDataControl
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 );
344 void Pump::start()
346 Guard< Mutex > aGuard( m_aMutex );
347 m_aThread = osl_createSuspendedThread(Pump::static_run,this);
348 if( !m_aThread )
350 throw RuntimeException(
351 "Pump::start Couldn't create worker thread",
352 *this);
355 // will be released by OPump::static_run
356 acquire();
357 osl_resumeThread( m_aThread );
362 void Pump::terminate()
364 close();
366 // wait for the worker to die
367 if( m_aThread )
368 osl_joinWithThread( m_aThread );
370 fireTerminated();
371 fireClose();
376 * XActiveDataSink
379 void Pump::setInputStream( const Reference< XInputStream >& xStream )
381 Guard< Mutex > aGuard( m_aMutex );
382 m_xInput = xStream;
383 Reference< XConnectable > xConnect( xStream, UNO_QUERY );
384 if( xConnect.is() )
385 xConnect->setSuccessor( this );
386 // data transfer starts in XActiveDataControl::start
390 Reference< XInputStream > Pump::getInputStream()
392 Guard< Mutex > aGuard( m_aMutex );
393 return m_xInput;
398 * XActiveDataSource
401 void Pump::setOutputStream( const Reference< XOutputStream >& xOut )
403 Guard< Mutex > aGuard( m_aMutex );
404 m_xOutput = xOut;
405 Reference< XConnectable > xConnect( xOut, UNO_QUERY );
406 if( xConnect.is() )
407 xConnect->setPredecessor( this );
408 // data transfer starts in XActiveDataControl::start
411 Reference< XOutputStream > Pump::getOutputStream()
413 Guard< Mutex > aGuard( m_aMutex );
414 return m_xOutput;
417 // XServiceInfo
418 OUString Pump::getImplementationName()
420 return OPumpImpl_getImplementationName();
423 // XServiceInfo
424 sal_Bool Pump::supportsService(const OUString& ServiceName)
426 return cppu::supportsService(this, ServiceName);
429 // XServiceInfo
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: */