Branch data 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 <stdlib.h>
10 : : #include <algorithm>
11 : : #include <vector>
12 : :
13 : : #include "officecfg/Office/Common.hxx"
14 : : #include <comphelper/processfactory.hxx>
15 : :
16 : : #include "sddll.hxx"
17 : :
18 : : #include "DiscoveryService.hxx"
19 : : #include "ImagePreparer.hxx"
20 : : #include "Listener.hxx"
21 : : #include "Receiver.hxx"
22 : : #include "RemoteServer.hxx"
23 : : #include "BluetoothServer.hxx"
24 : :
25 : : using namespace std;
26 : : using namespace sd;
27 : : using namespace ::com::sun::star;
28 : : using rtl::OString;
29 : : using namespace ::osl;
30 : :
31 : : // struct ClientInfoInternal:
32 : : // ClientInfo
33 : : // {
34 : : // osl::StreamSocket mStreamSocket;
35 : : // rtl::OUString mPin;
36 : : // ClientInfoInternal( const rtl::OUString rName,
37 : : // const rtl::OUString rAddress,
38 : : // osl::StreamSocket &rSocket, rtl::OUString rPin ):
39 : : // ClientInfo( rName, rAddress ),
40 : : // mStreamSocket( rSocket ),
41 : : // mPin( rPin ) {}
42 : : // };
43 : :
44 : 0 : RemoteServer::RemoteServer() :
45 : : Thread( "RemoteServerThread" ),
46 : : mSocket(),
47 : : mDataMutex(),
48 : : mCommunicators(),
49 [ # # ][ # # ]: 0 : mAvailableClients()
[ # # ][ # # ]
50 : : {
51 : 0 : }
52 : :
53 [ # # ][ # # ]: 0 : RemoteServer::~RemoteServer()
54 : : {
55 [ # # ]: 0 : }
56 : :
57 : 0 : void RemoteServer::execute()
58 : : {
59 [ # # ]: 0 : osl::SocketAddr aAddr( "0", PORT );
60 [ # # ]: 0 : if ( !mSocket.bind( aAddr ) )
61 : : {
62 : : // Error binding
63 : : }
64 : :
65 [ # # ]: 0 : if ( !mSocket.listen(3) )
66 : : {
67 : : // Error listening
68 : : }
69 : 0 : while ( true )
70 : : {
71 [ # # ]: 0 : StreamSocket aSocket;
72 [ # # ][ # # ]: 0 : if ( mSocket.acceptConnection( aSocket ) == osl_Socket_Error )
73 : : {
74 : 0 : return; // Closed, or other issue.
75 : : }
76 [ # # ][ # # ]: 0 : BufferedStreamSocket *pSocket = new BufferedStreamSocket( aSocket);
77 : 0 : OString aLine;
78 [ # # # # ]: 0 : if ( pSocket->readLine( aLine)
[ # # ][ # # ]
[ # # ]
79 [ # # ][ # # ]: 0 : && aLine.equals( "LO_SERVER_CLIENT_PAIR" ) &&
[ # # # # ]
80 [ # # ]: 0 : pSocket->readLine( aLine ) )
81 : : {
82 : 0 : OString aName( aLine );
83 : :
84 [ # # ][ # # ]: 0 : if ( ! pSocket->readLine( aLine ) ) delete pSocket;
[ # # ][ # # ]
85 : 0 : OString aPin( aLine );
86 : :
87 [ # # ]: 0 : SocketAddr aClientAddr;
88 [ # # ]: 0 : pSocket->getPeerAddr( aClientAddr );
89 [ # # ]: 0 : OUString aAddress = aClientAddr.getHostname();
90 : :
91 [ # # ]: 0 : MutexGuard aGuard( mDataMutex );
92 : : mAvailableClients.push_back( new ClientInfoInternal(
93 : : OStringToOUString( aName, RTL_TEXTENCODING_UTF8 ),
94 : : aAddress, pSocket, OStringToOUString( aPin,
95 [ # # ][ # # ]: 0 : RTL_TEXTENCODING_UTF8 ) ) );
[ # # ][ # # ]
96 : :
97 : : // Read off any additional non-empty lines
98 [ # # ]: 0 : do
99 : : {
100 [ # # ]: 0 : pSocket->readLine( aLine );
101 : : }
102 [ # # ][ # # ]: 0 : while ( aLine.getLength() > 0 );
103 : : } else {
104 [ # # ][ # # ]: 0 : delete pSocket;
105 : : }
106 [ # # ][ # # ]: 0 : }
[ # # ]
107 : :
108 : : }
109 : :
110 : : RemoteServer *sd::RemoteServer::spServer = NULL;
111 : :
112 : 0 : void RemoteServer::setup()
113 : : {
114 [ # # ]: 0 : if (spServer)
115 : 0 : return;
116 : :
117 [ # # ]: 0 : spServer = new RemoteServer();
118 : 0 : spServer->launch();
119 : :
120 : 0 : sd::BluetoothServer::setup( &(spServer->mCommunicators) );
121 : : }
122 : :
123 : :
124 : 0 : void RemoteServer::presentationStarted( const css::uno::Reference<
125 : : css::presentation::XSlideShowController > &rController )
126 : : {
127 [ # # ]: 0 : if ( !spServer )
128 : 0 : return;
129 [ # # ]: 0 : MutexGuard aGuard( spServer->mDataMutex );
130 [ # # ][ # # ]: 0 : for ( vector<Communicator*>::const_iterator aIt = spServer->mCommunicators.begin();
[ # # ][ # # ]
131 : 0 : aIt < spServer->mCommunicators.end(); aIt++ )
132 : : {
133 [ # # ]: 0 : (*aIt)->presentationStarted( rController );
134 [ # # ]: 0 : }
135 : : }
136 : 0 : void RemoteServer::presentationStopped()
137 : : {
138 [ # # ]: 0 : if ( !spServer )
139 : 0 : return;
140 [ # # ]: 0 : MutexGuard aGuard( spServer->mDataMutex );
141 [ # # ][ # # ]: 0 : for ( vector<Communicator*>::const_iterator aIt = spServer->mCommunicators.begin();
[ # # ][ # # ]
142 : 0 : aIt < spServer->mCommunicators.end(); aIt++ )
143 : : {
144 [ # # ]: 0 : (*aIt)->disposeListener();
145 [ # # ]: 0 : }
146 : : }
147 : :
148 : 0 : void RemoteServer::removeCommunicator( Communicator* mCommunicator )
149 : : {
150 [ # # ]: 0 : if ( !spServer )
151 : 0 : return;
152 [ # # ]: 0 : MutexGuard aGuard( spServer->mDataMutex );
153 [ # # ][ # # ]: 0 : for ( vector<Communicator*>::iterator aIt = spServer->mCommunicators.begin();
[ # # ]
154 : 0 : aIt < spServer->mCommunicators.end(); aIt++ )
155 : : {
156 [ # # ]: 0 : if ( mCommunicator == *aIt )
157 : : {
158 [ # # ]: 0 : spServer->mCommunicators.erase( aIt );
159 : 0 : break;
160 : : }
161 [ # # ]: 0 : }
162 : : }
163 : :
164 : 0 : std::vector<ClientInfo*> RemoteServer::getClients()
165 : : {
166 [ # # ]: 0 : if ( !spServer )
167 [ # # ]: 0 : std::vector<ClientInfo*>();
168 [ # # ]: 0 : MutexGuard aGuard( spServer->mDataMutex );
169 [ # # ]: 0 : std::vector<ClientInfo*> aClients;
170 : : aClients.assign( spServer->mAvailableClients.begin(),
171 [ # # ]: 0 : spServer->mAvailableClients.end() );
172 [ # # ]: 0 : return aClients;
173 : : }
174 : :
175 : 0 : sal_Bool RemoteServer::connectClient( ClientInfo* pClient, rtl::OUString aPin )
176 : : {
177 [ # # ]: 0 : if ( !spServer )
178 : 0 : return false;
179 : :
180 : 0 : ClientInfoInternal *apClient = (ClientInfoInternal*) pClient;
181 [ # # ]: 0 : if ( apClient->mPin.equals( aPin ) )
182 : : {
183 [ # # ][ # # ]: 0 : Communicator* pCommunicator = new Communicator( apClient->mpStreamSocket );
184 [ # # ]: 0 : MutexGuard aGuard( spServer->mDataMutex );
185 : :
186 [ # # ]: 0 : spServer->mCommunicators.push_back( pCommunicator );
187 : :
188 [ # # ][ # # ]: 0 : for ( vector<ClientInfoInternal*>::iterator aIt = spServer->mAvailableClients.begin();
[ # # ]
189 : 0 : aIt < spServer->mAvailableClients.end(); aIt++ )
190 : : {
191 [ # # ]: 0 : if ( pClient == *aIt )
192 : : {
193 [ # # ]: 0 : spServer->mAvailableClients.erase( aIt );
194 : 0 : break;
195 : : }
196 : : }
197 [ # # ]: 0 : pCommunicator->launch();
198 [ # # ]: 0 : return true;
199 : : }
200 : : else
201 : : {
202 : 0 : return false;
203 : : }
204 : : }
205 : :
206 : 25 : void SdDLL::RegisterRemotes()
207 : : {
208 : : // Disable unless in experimental mode for now
209 [ + - ]: 25 : uno::Reference< uno::XComponentContext > xContext = comphelper::getProcessComponentContext();
210 [ + - ][ + - ]: 25 : if (!xContext.is() || !officecfg::Office::Common::Misc::ExperimentalMode::get(xContext))
[ + - ][ + - ]
211 : 25 : return;
212 : :
213 [ # # ]: 0 : sd::RemoteServer::setup();
214 [ # # ][ - + ]: 25 : sd::DiscoveryService::setup();
215 : :
216 : : }
217 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|