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 : #include "Transmitter.hxx"
10 :
11 : using namespace std;
12 : using namespace osl; // Sockets etc.
13 : using namespace sd;
14 :
15 0 : Transmitter::Transmitter( IBluetoothSocket* aSocket )
16 : : pStreamSocket( aSocket ),
17 : mQueuesNotEmpty(),
18 : mFinishRequested(),
19 : mQueueMutex(),
20 : mLowPriority(),
21 0 : mHighPriority()
22 : {
23 0 : }
24 :
25 0 : void SAL_CALL Transmitter::run()
26 : {
27 : while ( true )
28 : {
29 0 : mQueuesNotEmpty.wait();
30 :
31 0 : if ( mFinishRequested.check() )
32 0 : return;
33 :
34 0 : ::osl::MutexGuard aQueueGuard( mQueueMutex );
35 0 : if ( !mHighPriority.empty() )
36 : {
37 0 : OString aMessage( mHighPriority.front() );
38 0 : mHighPriority.pop();
39 : SAL_INFO( "sdremote.bluetooth", "write high prio line '" << aMessage << "'" );
40 0 : pStreamSocket->write( aMessage.getStr(), aMessage.getLength() );
41 : }
42 0 : else if ( !mLowPriority.empty() )
43 : {
44 0 : OString aMessage( mLowPriority.front() );
45 0 : mLowPriority.pop();
46 : SAL_INFO( "sdremote.bluetooth", "write normal line '" << aMessage << "'" );
47 0 : pStreamSocket->write( aMessage.getStr(), aMessage.getLength() );
48 : }
49 :
50 0 : if ( mLowPriority.empty() && mHighPriority.empty() )
51 : {
52 0 : mQueuesNotEmpty.reset();
53 : }
54 0 : }
55 : }
56 :
57 0 : void Transmitter::notifyFinished()
58 : {
59 0 : mFinishRequested.set();
60 0 : mQueuesNotEmpty.set();
61 0 : }
62 :
63 0 : Transmitter::~Transmitter()
64 : {
65 0 : }
66 :
67 0 : void Transmitter::addMessage( const OString& aMessage, const Priority aPriority )
68 : {
69 0 : ::osl::MutexGuard aQueueGuard( mQueueMutex );
70 0 : switch ( aPriority )
71 : {
72 : case PRIORITY_LOW:
73 0 : mLowPriority.push( aMessage );
74 0 : break;
75 : case PRIORITY_HIGH:
76 0 : mHighPriority.push( aMessage );
77 0 : break;
78 : }
79 0 : if ( !mQueuesNotEmpty.check() )
80 : {
81 0 : mQueuesNotEmpty.set();
82 0 : }
83 0 : }
84 :
85 :
86 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|