Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 :
21 : #include <stdio.h>
22 :
23 : #include <sal/log.hxx>
24 :
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>
33 :
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 <cppuhelper/supportsservice.hxx>
40 : #include <osl/mutex.hxx>
41 : #include <osl/thread.h>
42 :
43 :
44 : using namespace osl;
45 : using namespace std;
46 : using namespace cppu;
47 : using namespace com::sun::star::uno;
48 : using namespace com::sun::star::lang;
49 : using namespace com::sun::star::registry;
50 : using namespace com::sun::star::io;
51 :
52 : #include "services.hxx"
53 :
54 : namespace io_stm {
55 :
56 : class Pump : public WeakImplHelper5<
57 : XActiveDataSource, XActiveDataSink, XActiveDataControl, XConnectable, XServiceInfo >
58 : {
59 : Mutex m_aMutex;
60 : oslThread m_aThread;
61 :
62 : Reference< XConnectable > m_xPred;
63 : Reference< XConnectable > m_xSucc;
64 : Reference< XInputStream > m_xInput;
65 : Reference< XOutputStream > m_xOutput;
66 : OInterfaceContainerHelper m_cnt;
67 : sal_Bool m_closeFired;
68 :
69 : void run();
70 : static void static_run( void* pObject );
71 :
72 : void close();
73 : void fireClose();
74 : void fireStarted();
75 : void fireTerminated();
76 : void fireError( const Any &a );
77 :
78 : public:
79 : Pump();
80 : virtual ~Pump();
81 :
82 : // XActiveDataSource
83 : virtual void SAL_CALL setOutputStream( const Reference< ::com::sun::star::io::XOutputStream >& xOutput ) throw(std::exception) SAL_OVERRIDE;
84 : virtual Reference< ::com::sun::star::io::XOutputStream > SAL_CALL getOutputStream() throw(std::exception) SAL_OVERRIDE;
85 :
86 : // XActiveDataSink
87 : virtual void SAL_CALL setInputStream( const Reference< ::com::sun::star::io::XInputStream >& xStream ) throw(std::exception) SAL_OVERRIDE;
88 : virtual Reference< ::com::sun::star::io::XInputStream > SAL_CALL getInputStream() throw(std::exception) SAL_OVERRIDE;
89 :
90 : // XActiveDataControl
91 : virtual void SAL_CALL addListener( const Reference< ::com::sun::star::io::XStreamListener >& xListener ) throw(std::exception) SAL_OVERRIDE;
92 : virtual void SAL_CALL removeListener( const Reference< ::com::sun::star::io::XStreamListener >& xListener ) throw(std::exception) SAL_OVERRIDE;
93 : virtual void SAL_CALL start() throw( RuntimeException, std::exception ) SAL_OVERRIDE;
94 : virtual void SAL_CALL terminate() throw(std::exception) SAL_OVERRIDE;
95 :
96 : // XConnectable
97 : virtual void SAL_CALL setPredecessor( const Reference< ::com::sun::star::io::XConnectable >& xPred ) throw(std::exception) SAL_OVERRIDE;
98 : virtual Reference< ::com::sun::star::io::XConnectable > SAL_CALL getPredecessor() throw(std::exception) SAL_OVERRIDE;
99 : virtual void SAL_CALL setSuccessor( const Reference< ::com::sun::star::io::XConnectable >& xSucc ) throw(std::exception) SAL_OVERRIDE;
100 : virtual Reference< ::com::sun::star::io::XConnectable > SAL_CALL getSuccessor() throw(std::exception) SAL_OVERRIDE;
101 :
102 : public: // XServiceInfo
103 : virtual OUString SAL_CALL getImplementationName() throw(std::exception ) SAL_OVERRIDE;
104 : virtual Sequence< OUString > SAL_CALL getSupportedServiceNames(void) throw(std::exception ) SAL_OVERRIDE;
105 : virtual sal_Bool SAL_CALL supportsService(const OUString& ServiceName) throw(std::exception ) SAL_OVERRIDE;
106 : };
107 :
108 0 : Pump::Pump() : m_aThread( 0 ),
109 : m_cnt( m_aMutex ),
110 0 : m_closeFired( sal_False )
111 : {
112 0 : }
113 :
114 0 : Pump::~Pump()
115 : {
116 : // exit gracefully
117 0 : if( m_aThread )
118 : {
119 0 : osl_joinWithThread( m_aThread );
120 0 : osl_destroyThread( m_aThread );
121 : }
122 0 : }
123 :
124 0 : void Pump::fireError( const Any & exception )
125 : {
126 0 : OInterfaceIteratorHelper iter( m_cnt );
127 0 : while( iter.hasMoreElements() )
128 : {
129 : try
130 : {
131 0 : static_cast< XStreamListener * > ( iter.next() )->error( exception );
132 : }
133 0 : catch ( const RuntimeException &e )
134 : {
135 : SAL_WARN("io.streams","com.sun.star.comp.stoc.Pump: unexpected exception during calling listeners" << e.Message);
136 : }
137 0 : }
138 0 : }
139 :
140 0 : void Pump::fireClose()
141 : {
142 0 : sal_Bool bFire = sal_False;
143 : {
144 0 : MutexGuard guard( m_aMutex );
145 0 : if( ! m_closeFired )
146 : {
147 0 : m_closeFired = sal_True;
148 0 : bFire = sal_True;
149 0 : }
150 : }
151 :
152 0 : if( bFire )
153 : {
154 0 : OInterfaceIteratorHelper iter( m_cnt );
155 0 : while( iter.hasMoreElements() )
156 : {
157 : try
158 : {
159 0 : static_cast< XStreamListener * > ( iter.next() )->closed( );
160 : }
161 0 : catch ( const RuntimeException &e )
162 : {
163 : SAL_WARN("io.streams","com.sun.star.comp.stoc.Pump: unexpected exception during calling listeners" << e.Message);
164 : }
165 0 : }
166 : }
167 0 : }
168 :
169 0 : void Pump::fireStarted()
170 : {
171 0 : OInterfaceIteratorHelper iter( m_cnt );
172 0 : while( iter.hasMoreElements() )
173 : {
174 : try
175 : {
176 0 : static_cast< XStreamListener * > ( iter.next() )->started( );
177 : }
178 0 : catch ( const RuntimeException &e )
179 : {
180 : SAL_WARN("io.streams","com.sun.star.comp.stoc.Pump: unexpected exception during calling listeners" << e.Message);
181 : }
182 0 : }
183 0 : }
184 :
185 0 : void Pump::fireTerminated()
186 : {
187 0 : OInterfaceIteratorHelper iter( m_cnt );
188 0 : while( iter.hasMoreElements() )
189 : {
190 : try
191 : {
192 0 : static_cast< XStreamListener * > ( iter.next() )->terminated();
193 : }
194 0 : catch ( const RuntimeException &e )
195 : {
196 : SAL_WARN("io.streams","com.sun.star.comp.stoc.Pump: unexpected exception during calling listeners" << e.Message);
197 : }
198 0 : }
199 0 : }
200 :
201 :
202 :
203 0 : void Pump::close()
204 : {
205 : // close streams and release references
206 0 : Reference< XInputStream > rInput;
207 0 : Reference< XOutputStream > rOutput;
208 : {
209 0 : MutexGuard guard( m_aMutex );
210 0 : rInput = m_xInput;
211 0 : m_xInput.clear();
212 :
213 0 : rOutput = m_xOutput;
214 0 : m_xOutput.clear();
215 0 : m_xSucc.clear();
216 0 : m_xPred.clear();
217 : }
218 0 : if( rInput.is() )
219 : {
220 : try
221 : {
222 0 : rInput->closeInput();
223 : }
224 0 : catch( Exception & )
225 : {
226 : // go down calm
227 : }
228 : }
229 0 : if( rOutput.is() )
230 : {
231 : try
232 : {
233 0 : rOutput->closeOutput();
234 : }
235 0 : catch( Exception & )
236 : {
237 : // go down calm
238 : }
239 0 : }
240 0 : }
241 :
242 0 : void Pump::static_run( void* pObject )
243 : {
244 0 : ((Pump*)pObject)->run();
245 0 : ((Pump*)pObject)->release();
246 0 : }
247 :
248 0 : void Pump::run()
249 : {
250 : try
251 : {
252 0 : fireStarted();
253 : try
254 : {
255 0 : Reference< XInputStream > rInput;
256 0 : Reference< XOutputStream > rOutput;
257 : {
258 0 : Guard< Mutex > aGuard( m_aMutex );
259 0 : rInput = m_xInput;
260 0 : rOutput = m_xOutput;
261 : }
262 :
263 0 : if( ! rInput.is() )
264 : {
265 : NotConnectedException exception(
266 0 : OUString("no input stream set") , Reference<XInterface>((OWeakObject*)this) );
267 0 : throw exception;
268 : }
269 0 : Sequence< sal_Int8 > aData;
270 0 : while( rInput->readSomeBytes( aData, 65536 ) )
271 : {
272 0 : if( ! rOutput.is() )
273 : {
274 : NotConnectedException exception(
275 0 : OUString("no output stream set") , Reference<XInterface>( (OWeakObject*)this) );
276 0 : throw exception;
277 : }
278 0 : rOutput->writeBytes( aData );
279 0 : osl_yieldThread();
280 0 : }
281 : }
282 0 : catch ( const IOException & e )
283 : {
284 0 : fireError( makeAny( e ) );
285 : }
286 0 : catch ( const RuntimeException & e )
287 : {
288 0 : fireError( makeAny( e ) );
289 : }
290 0 : catch ( const Exception & e )
291 : {
292 0 : fireError( makeAny( e ) );
293 : }
294 :
295 0 : close();
296 0 : fireClose();
297 : }
298 0 : catch ( const com::sun::star::uno::Exception &e )
299 : {
300 : // we are the last on the stack.
301 : // this is to avoid crashing the program, when e.g. a bridge crashes
302 : SAL_WARN("io.streams","com.sun.star.comp.stoc.Pump: unexpected exception during calling listeners" << e.Message);
303 : }
304 0 : }
305 :
306 :
307 :
308 : /*
309 : * XConnectable
310 : */
311 :
312 0 : void Pump::setPredecessor( const Reference< XConnectable >& xPred ) throw(std::exception)
313 : {
314 0 : Guard< Mutex > aGuard( m_aMutex );
315 0 : m_xPred = xPred;
316 0 : }
317 :
318 :
319 :
320 0 : Reference< XConnectable > Pump::getPredecessor() throw(std::exception)
321 : {
322 0 : Guard< Mutex > aGuard( m_aMutex );
323 0 : return m_xPred;
324 : }
325 :
326 :
327 :
328 0 : void Pump::setSuccessor( const Reference< XConnectable >& xSucc ) throw(std::exception)
329 : {
330 0 : Guard< Mutex > aGuard( m_aMutex );
331 0 : m_xSucc = xSucc;
332 0 : }
333 :
334 :
335 :
336 0 : Reference< XConnectable > Pump::getSuccessor() throw(std::exception)
337 : {
338 0 : Guard< Mutex > aGuard( m_aMutex );
339 0 : return m_xSucc;
340 : }
341 :
342 :
343 :
344 : /*
345 : * XActiveDataControl
346 : */
347 :
348 0 : void Pump::addListener( const Reference< XStreamListener >& xListener ) throw(std::exception)
349 : {
350 0 : m_cnt.addInterface( xListener );
351 0 : }
352 :
353 :
354 :
355 0 : void Pump::removeListener( const Reference< XStreamListener >& xListener ) throw(std::exception)
356 : {
357 0 : m_cnt.removeInterface( xListener );
358 0 : }
359 :
360 :
361 :
362 0 : void Pump::start() throw( RuntimeException, std::exception )
363 : {
364 0 : Guard< Mutex > aGuard( m_aMutex );
365 0 : m_aThread = osl_createSuspendedThread((oslWorkerFunction)Pump::static_run,this);
366 0 : if( m_aThread )
367 : {
368 : // will be released by OPump::static_run
369 0 : acquire();
370 0 : osl_resumeThread( m_aThread );
371 : }
372 : else
373 : {
374 : throw RuntimeException(
375 : OUString("Pump::start Couldn't create worker thread"),
376 0 : *this);
377 0 : }
378 0 : }
379 :
380 :
381 :
382 0 : void Pump::terminate() throw(std::exception)
383 : {
384 0 : close();
385 :
386 : // wait for the worker to die
387 0 : if( m_aThread )
388 0 : osl_joinWithThread( m_aThread );
389 :
390 0 : fireTerminated();
391 0 : fireClose();
392 0 : }
393 :
394 :
395 :
396 : /*
397 : * XActiveDataSink
398 : */
399 :
400 0 : void Pump::setInputStream( const Reference< XInputStream >& xStream ) throw(std::exception)
401 : {
402 0 : Guard< Mutex > aGuard( m_aMutex );
403 0 : m_xInput = xStream;
404 0 : Reference< XConnectable > xConnect( xStream, UNO_QUERY );
405 0 : if( xConnect.is() )
406 0 : xConnect->setSuccessor( this );
407 : // data transfer starts in XActiveDataControl::start
408 0 : }
409 :
410 :
411 :
412 0 : Reference< XInputStream > Pump::getInputStream() throw(std::exception)
413 : {
414 0 : Guard< Mutex > aGuard( m_aMutex );
415 0 : return m_xInput;
416 : }
417 :
418 :
419 :
420 : /*
421 : * XActiveDataSource
422 : */
423 :
424 0 : void Pump::setOutputStream( const Reference< XOutputStream >& xOut ) throw(std::exception)
425 : {
426 0 : Guard< Mutex > aGuard( m_aMutex );
427 0 : m_xOutput = xOut;
428 0 : Reference< XConnectable > xConnect( xOut, UNO_QUERY );
429 0 : if( xConnect.is() )
430 0 : xConnect->setPredecessor( this );
431 : // data transfer starts in XActiveDataControl::start
432 0 : }
433 :
434 0 : Reference< XOutputStream > Pump::getOutputStream() throw(std::exception)
435 : {
436 0 : Guard< Mutex > aGuard( m_aMutex );
437 0 : return m_xOutput;
438 : }
439 :
440 : // XServiceInfo
441 0 : OUString Pump::getImplementationName() throw(std::exception )
442 : {
443 0 : return OPumpImpl_getImplementationName();
444 : }
445 :
446 : // XServiceInfo
447 0 : sal_Bool Pump::supportsService(const OUString& ServiceName) throw(std::exception )
448 : {
449 0 : return cppu::supportsService(this, ServiceName);
450 : }
451 :
452 : // XServiceInfo
453 0 : Sequence< OUString > Pump::getSupportedServiceNames(void) throw(std::exception )
454 : {
455 0 : return OPumpImpl_getSupportedServiceNames();
456 : }
457 :
458 :
459 0 : Reference< XInterface > SAL_CALL OPumpImpl_CreateInstance(
460 : SAL_UNUSED_PARAMETER const Reference< XComponentContext > & )
461 : throw (Exception)
462 : {
463 0 : return Reference< XInterface >( *new Pump );
464 : }
465 :
466 0 : OUString OPumpImpl_getImplementationName()
467 : {
468 0 : return OUString("com.sun.star.comp.io.Pump");
469 : }
470 :
471 0 : Sequence<OUString> OPumpImpl_getSupportedServiceNames(void)
472 : {
473 0 : OUString s("com.sun.star.io.Pump");
474 0 : Sequence< OUString > seq( &s , 1 );
475 0 : return seq;
476 : }
477 :
478 : }
479 :
480 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|