Stop leaking all ScPostIt instances.
[LibreOffice.git] / io / source / stm / opump.cxx
blob72f1570f23cd667a033899bd15e1780ea136ac19
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 <stdio.h>
23 #include <sal/log.hxx>
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/XSingleServiceFactory.hpp>
30 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
31 #include <com/sun/star/lang/XServiceInfo.hpp>
32 #include <com/sun/star/registry/XRegistryKey.hpp>
34 #include <uno/dispatcher.h>
35 #include <uno/mapping.hxx>
36 #include <cppuhelper/implbase5.hxx>
37 #include <cppuhelper/factory.hxx>
38 #include <cppuhelper/interfacecontainer.hxx>
39 #include <osl/mutex.hxx>
40 #include <osl/thread.h>
43 using namespace osl;
44 using namespace std;
45 using namespace cppu;
46 using namespace com::sun::star::uno;
47 using namespace com::sun::star::lang;
48 using namespace com::sun::star::registry;
49 using namespace com::sun::star::io;
51 #include "services.hxx"
53 namespace io_stm {
55 class Pump : public WeakImplHelper5<
56 XActiveDataSource, XActiveDataSink, XActiveDataControl, XConnectable, XServiceInfo >
58 Mutex m_aMutex;
59 oslThread m_aThread;
61 Reference< XConnectable > m_xPred;
62 Reference< XConnectable > m_xSucc;
63 Reference< XInputStream > m_xInput;
64 Reference< XOutputStream > m_xOutput;
65 OInterfaceContainerHelper m_cnt;
66 sal_Bool m_closeFired;
68 void run();
69 static void static_run( void* pObject );
71 void close();
72 void fireClose();
73 void fireStarted();
74 void fireTerminated();
75 void fireError( const Any &a );
77 public:
78 Pump();
79 virtual ~Pump();
81 // XActiveDataSource
82 virtual void SAL_CALL setOutputStream( const Reference< ::com::sun::star::io::XOutputStream >& xOutput ) throw();
83 virtual Reference< ::com::sun::star::io::XOutputStream > SAL_CALL getOutputStream() throw();
85 // XActiveDataSink
86 virtual void SAL_CALL setInputStream( const Reference< ::com::sun::star::io::XInputStream >& xStream ) throw();
87 virtual Reference< ::com::sun::star::io::XInputStream > SAL_CALL getInputStream() throw();
89 // XActiveDataControl
90 virtual void SAL_CALL addListener( const Reference< ::com::sun::star::io::XStreamListener >& xListener ) throw();
91 virtual void SAL_CALL removeListener( const Reference< ::com::sun::star::io::XStreamListener >& xListener ) throw();
92 virtual void SAL_CALL start() throw( RuntimeException );
93 virtual void SAL_CALL terminate() throw();
95 // XConnectable
96 virtual void SAL_CALL setPredecessor( const Reference< ::com::sun::star::io::XConnectable >& xPred ) throw();
97 virtual Reference< ::com::sun::star::io::XConnectable > SAL_CALL getPredecessor() throw();
98 virtual void SAL_CALL setSuccessor( const Reference< ::com::sun::star::io::XConnectable >& xSucc ) throw();
99 virtual Reference< ::com::sun::star::io::XConnectable > SAL_CALL getSuccessor() throw();
101 public: // XServiceInfo
102 virtual OUString SAL_CALL getImplementationName() throw( );
103 virtual Sequence< OUString > SAL_CALL getSupportedServiceNames(void) throw( );
104 virtual sal_Bool SAL_CALL supportsService(const OUString& ServiceName) throw( );
107 Pump::Pump() : m_aThread( 0 ),
108 m_cnt( m_aMutex ),
109 m_closeFired( sal_False )
113 Pump::~Pump()
115 // exit gracefully
116 if( m_aThread )
118 osl_joinWithThread( m_aThread );
119 osl_destroyThread( m_aThread );
123 void Pump::fireError( const Any & exception )
125 OInterfaceIteratorHelper iter( m_cnt );
126 while( iter.hasMoreElements() )
130 static_cast< XStreamListener * > ( iter.next() )->error( exception );
132 catch ( const RuntimeException &e )
134 SAL_WARN("io.streams","com.sun.star.comp.stoc.Pump: unexpected exception during calling listeners" << e.Message);
139 void Pump::fireClose()
141 sal_Bool bFire = sal_False;
143 MutexGuard guard( m_aMutex );
144 if( ! m_closeFired )
146 m_closeFired = sal_True;
147 bFire = sal_True;
151 if( bFire )
153 OInterfaceIteratorHelper iter( m_cnt );
154 while( iter.hasMoreElements() )
158 static_cast< XStreamListener * > ( iter.next() )->closed( );
160 catch ( const RuntimeException &e )
162 SAL_WARN("io.streams","com.sun.star.comp.stoc.Pump: unexpected exception during calling listeners" << e.Message);
168 void Pump::fireStarted()
170 OInterfaceIteratorHelper iter( m_cnt );
171 while( iter.hasMoreElements() )
175 static_cast< XStreamListener * > ( 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.Message);
184 void Pump::fireTerminated()
186 OInterfaceIteratorHelper iter( m_cnt );
187 while( iter.hasMoreElements() )
191 static_cast< XStreamListener * > ( iter.next() )->terminated();
193 catch ( const RuntimeException &e )
195 SAL_WARN("io.streams","com.sun.star.comp.stoc.Pump: unexpected exception during calling listeners" << e.Message);
202 void Pump::close()
204 // close streams and release references
205 Reference< XInputStream > rInput;
206 Reference< XOutputStream > rOutput;
208 MutexGuard guard( m_aMutex );
209 rInput = m_xInput;
210 m_xInput.clear();
212 rOutput = m_xOutput;
213 m_xOutput.clear();
214 m_xSucc.clear();
215 m_xPred.clear();
217 if( rInput.is() )
221 rInput->closeInput();
223 catch( Exception & )
225 // go down calm
228 if( rOutput.is() )
232 rOutput->closeOutput();
234 catch( Exception & )
236 // go down calm
241 void Pump::static_run( void* pObject )
243 ((Pump*)pObject)->run();
244 ((Pump*)pObject)->release();
247 void Pump::run()
251 fireStarted();
254 Reference< XInputStream > rInput;
255 Reference< XOutputStream > rOutput;
257 Guard< Mutex > aGuard( m_aMutex );
258 rInput = m_xInput;
259 rOutput = m_xOutput;
262 if( ! rInput.is() )
264 NotConnectedException exception(
265 OUString("no input stream set") , Reference<XInterface>((OWeakObject*)this) );
266 throw exception;
268 Sequence< sal_Int8 > aData;
269 while( rInput->readSomeBytes( aData, 65536 ) )
271 if( ! rOutput.is() )
273 NotConnectedException exception(
274 OUString("no output stream set") , Reference<XInterface>( (OWeakObject*)this) );
275 throw exception;
277 rOutput->writeBytes( aData );
278 osl_yieldThread();
281 catch ( const IOException & e )
283 fireError( makeAny( e ) );
285 catch ( const RuntimeException & e )
287 fireError( makeAny( e ) );
289 catch ( const Exception & e )
291 fireError( makeAny( e ) );
294 close();
295 fireClose();
297 catch ( const com::sun::star::uno::Exception &e )
299 // we are the last on the stack.
300 // this is to avoid crashing the program, when e.g. a bridge crashes
301 SAL_WARN("io.streams","com.sun.star.comp.stoc.Pump: unexpected exception during calling listeners" << e.Message);
305 // ------------------------------------------------------------
308 * XConnectable
311 void Pump::setPredecessor( const Reference< XConnectable >& xPred ) throw()
313 Guard< Mutex > aGuard( m_aMutex );
314 m_xPred = xPred;
317 // ------------------------------------------------------------
319 Reference< XConnectable > Pump::getPredecessor() throw()
321 Guard< Mutex > aGuard( m_aMutex );
322 return m_xPred;
325 // ------------------------------------------------------------
327 void Pump::setSuccessor( const Reference< XConnectable >& xSucc ) throw()
329 Guard< Mutex > aGuard( m_aMutex );
330 m_xSucc = xSucc;
333 // ------------------------------------------------------------
335 Reference< XConnectable > Pump::getSuccessor() throw()
337 Guard< Mutex > aGuard( m_aMutex );
338 return m_xSucc;
341 // -----------------------------------------------------------------
344 * XActiveDataControl
347 void Pump::addListener( const Reference< XStreamListener >& xListener ) throw()
349 m_cnt.addInterface( xListener );
352 // ------------------------------------------------------------
354 void Pump::removeListener( const Reference< XStreamListener >& xListener ) throw()
356 m_cnt.removeInterface( xListener );
359 // ------------------------------------------------------------
361 void Pump::start() throw( RuntimeException )
363 Guard< Mutex > aGuard( m_aMutex );
364 m_aThread = osl_createSuspendedThread((oslWorkerFunction)Pump::static_run,this);
365 if( m_aThread )
367 // will be released by OPump::static_run
368 acquire();
369 osl_resumeThread( m_aThread );
371 else
373 throw RuntimeException(
374 OUString("Pump::start Couldn't create worker thread"),
375 *this);
379 // ------------------------------------------------------------
381 void Pump::terminate() throw()
383 close();
385 // wait for the worker to die
386 if( m_aThread )
387 osl_joinWithThread( m_aThread );
389 fireTerminated();
390 fireClose();
393 // ------------------------------------------------------------
396 * XActiveDataSink
399 void Pump::setInputStream( const Reference< XInputStream >& xStream ) throw()
401 Guard< Mutex > aGuard( m_aMutex );
402 m_xInput = xStream;
403 Reference< XConnectable > xConnect( xStream, UNO_QUERY );
404 if( xConnect.is() )
405 xConnect->setSuccessor( this );
406 // data transfer starts in XActiveDataControl::start
409 // ------------------------------------------------------------
411 Reference< XInputStream > Pump::getInputStream() throw()
413 Guard< Mutex > aGuard( m_aMutex );
414 return m_xInput;
417 // ------------------------------------------------------------
420 * XActiveDataSource
423 void Pump::setOutputStream( const Reference< XOutputStream >& xOut ) throw()
425 Guard< Mutex > aGuard( m_aMutex );
426 m_xOutput = xOut;
427 Reference< XConnectable > xConnect( xOut, UNO_QUERY );
428 if( xConnect.is() )
429 xConnect->setPredecessor( this );
430 // data transfer starts in XActiveDataControl::start
433 // ------------------------------------------------------------
435 Reference< XOutputStream > Pump::getOutputStream() throw()
437 Guard< Mutex > aGuard( m_aMutex );
438 return m_xOutput;
442 // XServiceInfo
443 OUString Pump::getImplementationName() throw( )
445 return OPumpImpl_getImplementationName();
448 // XServiceInfo
449 sal_Bool Pump::supportsService(const OUString& ServiceName) throw( )
451 Sequence< OUString > aSNL = getSupportedServiceNames();
452 const OUString * pArray = aSNL.getConstArray();
454 for( sal_Int32 i = 0; i < aSNL.getLength(); i++ )
455 if( pArray[i] == ServiceName )
456 return sal_True;
458 return sal_False;
461 // XServiceInfo
462 Sequence< OUString > Pump::getSupportedServiceNames(void) throw( )
464 return OPumpImpl_getSupportedServiceNames();
468 Reference< XInterface > SAL_CALL OPumpImpl_CreateInstance(
469 SAL_UNUSED_PARAMETER const Reference< XComponentContext > & )
470 throw (Exception)
472 return Reference< XInterface >( *new Pump );
475 OUString OPumpImpl_getImplementationName()
477 return OUString("com.sun.star.comp.io.Pump");
480 Sequence<OUString> OPumpImpl_getSupportedServiceNames(void)
482 OUString s("com.sun.star.io.Pump");
483 Sequence< OUString > seq( &s , 1 );
484 return seq;
489 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */