1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
|
/*************************************************************************
*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* Copyright 2000, 2010 Oracle and/or its affiliates.
*
* OpenOffice.org - a multi-platform office productivity suite
*
* This file is part of OpenOffice.org.
*
* OpenOffice.org is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3
* only, as published by the Free Software Foundation.
*
* OpenOffice.org is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License version 3 for more details
* (a copy is included in the LICENSE file that accompanied this code).
*
* You should have received a copy of the GNU Lesser General Public License
* version 3 along with OpenOffice.org. If not, see
* <http://www.openoffice.org/license.html>
* for a copy of the LGPLv3 License.
*
************************************************************************/
// MARKER(update_precomp.py): autogen include statement, do not remove
#include "precompiled_bridges.hxx"
#include <stdio.h>
#include <osl/interlck.h>
#include <osl/diagnose.h>
#include <osl/conditn.h>
#include <osl/mutex.hxx>
#include <osl/process.h>
#include <rtl/alloc.h>
#include <rtl/uuid.h>
#include <rtl/unload.h>
#include <uno/environment.h>
#include <uno/lbnames.h>
#include <uno/mapping.hxx>
#include <uno/threadpool.h>
#include <com/sun/star/uno/Sequence.hxx>
#include <bridges/remote/proxy.hxx>
#include <bridges/remote/stub.hxx>
#include <bridges/remote/context.h>
#include <bridges/remote/mapping.hxx>
#include <bridges/remote/counter.hxx>
#include <bridges/remote/bridgeimpl.hxx>
#include <bridges/remote/helper.hxx>
#include "urp_bridgeimpl.hxx"
#include "urp_writer.hxx"
#include "urp_reader.hxx"
#include "urp_dispatch.hxx"
#include "urp_log.hxx"
#include "urp_propertyobject.hxx"
using namespace ::rtl;
using namespace ::osl;
using namespace ::com::sun::star::uno;
namespace bridges_urp
{
rtl_StandardModuleCount g_moduleCount = MODULE_COUNT_INIT;
// static void dumpProperties( struct Properties *p )
// {
// fprintf( stderr , "FlushBlockSize : %d\n" , p->nFlushBlockSize );
// fprintf( stderr , "OnewayTimeoutMUSEC : %d\n" , p->nOnewayTimeoutMUSEC );
// fprintf( stderr , "OidCacheSize : %d\n" , p->nOidCacheSize );
// fprintf( stderr , "TypeCacheSize : %d\n" , p->nTypeCacheSize );
// fprintf( stderr , "TidCacheSize : %d\n" , p->nTidCacheSize );
// OString o = OUStringToOString( p->sSupportedVersions , RTL_TEXTENCODING_ASCII_US );
// fprintf( stderr , "SupportedVersions : %s\n" , o.pData->buffer );
// o = OUStringToOString( p->sVersion , RTL_TEXTENCODING_ASCII_US );
// fprintf( stderr , "Version : %s\n" , o.pData->buffer );
// fprintf( stderr , "SupportsMultipleSynchronous : %d\n" , p->bSupportsMultipleSynchronous );
// fprintf( stderr , "SupportsMustReply : %d\n" , p->bSupportsMustReply );
// fprintf( stderr , "SupportsSynchronous : %d\n" , p->bSupportsSynchronous );
// }
// PropertySetterThread
//------------------------------------
class PropertySetterThread : public ::osl::Thread
{
urp_BridgeImpl *m_pImpl;
::rtl::OUString m_sProps;
uno_Environment *m_pEnvRemote;
public:
PropertySetterThread( uno_Environment *pEnvRemote,
urp_BridgeImpl *pImpl,
const ::rtl::OUString & props )
: m_pImpl( pImpl )
, m_sProps( props )
, m_pEnvRemote( pEnvRemote )
{
if (m_sProps.getLength() > 0) {
m_sProps += rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(","));
}
m_sProps += rtl::OUString(
RTL_CONSTASCII_USTRINGPARAM("CurrentContext="));
// hold the environment in case all references are released before this
// thread terminates
m_pEnvRemote->acquire( pEnvRemote );
}
~PropertySetterThread()
{
m_pEnvRemote->release( m_pEnvRemote );
}
virtual void SAL_CALL run()
{
for (;;)
{
switch ( m_pImpl->m_pPropertyObject->localRequestChange( ) )
{
case 1:
sal_Bool bExceptionThrown;
m_pImpl->m_pPropertyObject->localCommitChange( m_sProps , &bExceptionThrown );
OSL_ENSURE( !bExceptionThrown, "properties were not set\n" );
goto done;
case 0:
OSL_TRACE( "urp-bridge : remote-counterpart won the changing-the-protocol-race\n" );
goto done;
}
}
done:
m_pImpl->m_initialized.set();
}
virtual void SAL_CALL onTerminated()
{
delete this;
}
};
//------------------------------------
void test_cache()
{
OUString a = OUString( RTL_CONSTASCII_USTRINGPARAM( "a" ) );
OUString b = OUString( RTL_CONSTASCII_USTRINGPARAM( "b" ) );
OUString c = OUString( RTL_CONSTASCII_USTRINGPARAM( "c" ) );
Cache < OUString , equalOUString > cache ( 2 );
sal_Int32 n = cache.put( a );
if (cache.seek( a ) != n)
{
OSL_ASSERT( false );
}
OSL_ASSERT( 0 == n );
n = cache.put( b );
OSL_ASSERT( 1 == n );
cache.put( c );
OSL_ASSERT( 0xffff == cache.seek( a ) );
OSL_ASSERT( 1 == cache.seek( b ) );
OSL_ASSERT( 0 == cache.seek( c ) );
OSL_ASSERT( 1 == cache.put( a ) );
OSL_ASSERT( 0xffff == cache.seek( b) );
OSL_ASSERT( 1 == cache.seek( a ) );
OSL_ASSERT( 0 == cache.seek( c ) );
}
/*******************
* Are we within thread, that calls destructors of static objects ?
*******************/
sal_Bool g_bStaticDestructorsCalled = sal_False;
struct StaticSingleton
{
~StaticSingleton()
{
::osl::MutexGuard guard( ::osl::Mutex::getGlobalMutex() );
g_bStaticDestructorsCalled = sal_True;
}
};
StaticSingleton singleton;
#if OSL_DEBUG_LEVEL > 1
static MyCounter thisCounter( "Remote Environment" );
#endif
void SAL_CALL allThreadsAreGone( uno_Environment * pEnvRemote )
{
remote_Context *pContext = (remote_Context *) pEnvRemote->pContext;
urp_BridgeImpl *pImpl = ( urp_BridgeImpl *) pContext->m_pBridgeImpl;
// if the current thread is not the writer thread, the writer thread
// object is not destroyed up to now, though it may already have run out.
// In both cases, it must be safe to cal pImpl->m_pWriter->getIdentifier()
OSL_ASSERT( pImpl->m_pWriter );
if( pImpl->m_pWriter->getIdentifier() == osl_getThreadIdentifier(0) )
{
// This is the writer thread. It has done some release calls,
// and is now the last one, that was active. Because the writer
// thread holds the environment weakly, there may also be a thread within
// the dispose of the bridge ( because the enviroment may have a refcount == 0 ).
// However, this thread MUST wait for the writer thread, so it is perfectly ok,
// not to set m_cndWaitForThreads. ( The check for m_nRemoteThreads is done
// after the join of the writer thread ).
}
else
{
pImpl->m_cndWaitForThreads.set();
}
}
void SAL_CALL releaseStubs( uno_Environment *pEnvRemote )
{
((remote_Context * ) pEnvRemote->pContext )->m_pBridgeImpl->m_bReleaseStubsCalled = sal_True;
remote_Interface **ppInterfaces = 0;
sal_Int32 nCount;
pEnvRemote->pExtEnv->getRegisteredInterfaces( pEnvRemote->pExtEnv,
(void***)&ppInterfaces,
&nCount,
rtl_allocateMemory );
sal_Int32 i;
for( i = 0 ; i < nCount ; i ++ )
{
if( ppInterfaces[i]->acquire == bridges_remote::acquireUno2RemoteStub )
{
// these are freed by the environment, so no release necessary
pEnvRemote->pExtEnv->revokeInterface( pEnvRemote->pExtEnv, ppInterfaces[i] );
}
else
{
ppInterfaces[i]->release( ppInterfaces[i] );
}
}
rtl_freeMemory( (void*) ppInterfaces );
}
} // end namespace bridges_urp
using namespace bridges_urp;
extern "C" {
static void SAL_CALL RemoteEnvironment_thisDispose( uno_Environment *pEnvRemote )
{
remote_Context *pContext = (remote_Context *) pEnvRemote->pContext;
urp_BridgeImpl *pImpl = ( urp_BridgeImpl *) pContext->m_pBridgeImpl;
::osl::ClearableMutexGuard guard( pImpl->m_disposingMutex );
if( pContext->m_pBridgeImpl->m_bDisposed &&
( ! pImpl->m_pReader ||
osl_getThreadIdentifier(0) ==
(oslThreadIdentifier) pImpl->m_pReader->getIdentifier() ) )
{
return;
}
// in case, that the static destructors already have been called, no
// tiding up is done.
bool tidyUp;
{
::osl::MutexGuard guard2( ::osl::Mutex::getGlobalMutex() );
tidyUp = ! g_bStaticDestructorsCalled &&
! pContext->m_pBridgeImpl->m_bDisposed;
}
if( tidyUp )
{
// TODO : not threadsafe
// synchronization with dispatch methods needed !
pImpl->m_bDisposed = sal_True;
// close the connection
uno_threadpool_dispose( pImpl->m_hThreadPool );
pImpl->m_pWriter->abortThread();
pContext->m_pConnection->close( pContext->m_pConnection );
if( osl_getThreadIdentifier(0) ==
(oslThreadIdentifier) pImpl->m_pReader->getIdentifier() )
{
// This is the reader thread. Let the thread destroy itself
// the reader thread object must also release the connection at this stage !
pImpl->m_pReader->destroyYourself();
pImpl->m_pReader = 0;
}
else
{
// wait for the reader thread
// the reader thread object must also release the connection,
// when terminating !!!!
pImpl->m_pReader->join();
}
// wait for the writer thread
pImpl->m_pWriter->join();
// now let the context go !
pContext->dispose( pContext );
if( 0 != pImpl->m_nRemoteThreads )
{
// Wait for all threads
guard.clear();
pImpl->m_cndWaitForThreads.wait();
OSL_ASSERT( ! pImpl->m_nRemoteThreads );
}
else
{
guard.clear();
}
#ifdef BRIDGES_URP_PROT
if( pImpl->m_pLogFile )
{
fclose( pImpl->m_pLogFile );
pImpl->m_pLogFile = 0;
}
#endif
#if OSL_DEBUG_LEVEL > 1
pImpl->dumpErrors( stderr );
#endif
// destroy the threads
delete pImpl->m_pWriter;
pImpl->m_pWriter = 0;
if( pImpl->m_pReader != 0 )
{
// This is not the reader thread, so the thread object is deleted
delete pImpl->m_pReader;
pImpl->m_pReader = 0;
}
bool bReleaseStubs = false;
{
::osl::MutexGuard guard2( ::osl::Mutex::getGlobalMutex() );
bReleaseStubs = !g_bStaticDestructorsCalled;
}
if( bReleaseStubs )
{
releaseStubs( pEnvRemote );
}
}
}
static void SAL_CALL RemoteEnvironment_thisDisposing(
uno_Environment *pEnvRemote )
{
remote_Context *pContext = (remote_Context * )pEnvRemote->pContext;
urp_BridgeImpl *pImpl = ((urp_BridgeImpl*) pContext->m_pBridgeImpl);
{
::osl::ClearableMutexGuard guard( pImpl->m_disposingMutex );
if( ! pImpl->m_bDisposed )
{
guard.clear();
urp_sendCloseConnection( pEnvRemote );
RemoteEnvironment_thisDispose( pEnvRemote );
}
}
pImpl->m_pPropertyObject->thisRelease();
pImpl->m_pPropertyObject = 0;
uno_threadpool_destroy( pImpl->m_hThreadPool );
delete pImpl;
pContext->aBase.release( (uno_Context * ) pContext );
g_moduleCount.modCnt.release( &g_moduleCount.modCnt );
#if OSL_DEBUG_LEVEL > 1
thisCounter.release();
#endif
}
static void SAL_CALL RemoteEnvironment_thisComputeObjectIdentifier(
uno_ExtEnvironment *, rtl_uString **, void *)
{
OSL_ENSURE( 0, "RemoteEnvironment_thisComputeObjectIdentifier should never be called" );
}
static void SAL_CALL RemoteEnvironment_thisAcquireInterface(
uno_ExtEnvironment *, void *pInterface )
{
((remote_Interface *)pInterface)->acquire( ( remote_Interface *) pInterface );
}
static void SAL_CALL RemoteEnvironment_thisReleaseInterface(
uno_ExtEnvironment *, void *pInterface )
{
((remote_Interface *)pInterface)->release( ( remote_Interface *) pInterface );
}
//##################################################################################################
void SAL_CALL uno_initEnvironment( uno_Environment * pEnvRemote )
{
g_moduleCount.modCnt.acquire( &g_moduleCount.modCnt );
// set C-virtual methods
pEnvRemote->environmentDisposing = RemoteEnvironment_thisDisposing;
pEnvRemote->pExtEnv->computeObjectIdentifier = RemoteEnvironment_thisComputeObjectIdentifier;
pEnvRemote->pExtEnv->acquireInterface = RemoteEnvironment_thisAcquireInterface;
pEnvRemote->pExtEnv->releaseInterface = RemoteEnvironment_thisReleaseInterface;
pEnvRemote->dispose = RemoteEnvironment_thisDispose;
remote_Context *pContext = ( remote_Context * ) pEnvRemote->pContext;
pContext->aBase.acquire( ( uno_Context * ) pContext );
pContext->getRemoteInstance = ::bridges_remote::remote_sendQueryInterface;
// Initialize impl struct urp_BridgeImpl
urp_BridgeImpl *pImpl = new ::bridges_urp::urp_BridgeImpl( 256, 8192 );
pContext->m_pBridgeImpl = pImpl;
// Initialize threadpool
pImpl->m_hThreadPool = uno_threadpool_create();
// take the bridgepointer as id
pImpl->m_properties.seqBridgeID = ByteSequence( (sal_Int8*)&pEnvRemote , sizeof( pEnvRemote ) );
pImpl->m_allThreadsAreGone = allThreadsAreGone;
pImpl->m_sendRequest = urp_sendRequest;
pImpl->m_nRemoteThreads = 0;
pImpl->m_bDisposed = sal_False;
pImpl->m_bReleaseStubsCalled = sal_False;
pImpl->m_pPropertyObject = new PropertyObject( &(pImpl->m_properties ), pEnvRemote, pImpl );
pImpl->m_pPropertyObject->thisAcquire();
OUString sProtocolProperties;
if( pContext->m_pProtocol->length > 3 )
{
sProtocolProperties = OUString( pContext->m_pProtocol ).copy( 4, pContext->m_pProtocol->length-4);
}
if( sProtocolProperties.getLength() )
{
struct Properties props = pImpl->m_properties;
assignFromStringToStruct( sProtocolProperties , &props );
if( ! props.bNegotiate )
{
// no negotiation takes place, the creator of the bridge knows the parameter
// of the other side !
pImpl->applyProtocolChanges( props );
sProtocolProperties = OUString();
}
}
#ifdef BRIDGES_URP_PROT
char *p = getenv( "PROT_REMOTE" );
pImpl->m_pLogFile = 0;
if( p )
{
MutexGuard guard( Mutex::getGlobalMutex() );
static int counter;
oslProcessInfo data;
data.Size = sizeof( data );
osl_getProcessInfo( 0 , osl_Process_HEAPUSAGE | osl_Process_IDENTIFIER , &data );
OString s(p);
s += "_pid";
s += OString::valueOf( (sal_Int32) data.Ident );
s += "_";
s += OString::valueOf( (sal_Int32) counter );
pImpl->m_sLogFileName = s;
// clear the file
FILE *f = fopen( s.getStr() , "w" );
OSL_ASSERT( f );
if( getenv( "PROT_REMOTE_FAST") )
{
pImpl->m_pLogFile = f;
}
else
{
fclose( f );
}
counter++;
}
#endif
// start reader and writer threads
pImpl->m_pWriter = new ::bridges_urp::OWriterThread( pContext->m_pConnection , pImpl,
pEnvRemote);
pImpl->m_pWriter->create();
pImpl->m_pReader = new ::bridges_urp::OReaderThread( pContext->m_pConnection ,
pEnvRemote,
pImpl->m_pWriter );
pImpl->m_pReader->create();
PropertySetterThread *pPropsSetterThread =
new PropertySetterThread( pEnvRemote, pImpl , sProtocolProperties );
pPropsSetterThread->create();
#if OSL_DEBUG_LEVEL > 1
thisCounter.acquire();
#endif
}
//##################################################################################################
void SAL_CALL uno_ext_getMapping(
uno_Mapping ** ppMapping,
uno_Environment * pFrom,
uno_Environment * pTo )
{
OSL_ASSERT( ppMapping && pFrom && pTo );
if (ppMapping && pFrom && pTo)
{
if (*ppMapping)
((*ppMapping)->release)( *ppMapping );
bridges_remote::RemoteMapping * pMapping = 0;
::rtl::OUString sFromName = pFrom->pTypeName;
::rtl::OUString sToName = pTo->pTypeName;
::rtl::OUString sUno = OUString::createFromAscii( UNO_LB_UNO );
::rtl::OUString sRemote = OUString::createFromAscii( "urp" );
if ( sFromName.equalsIgnoreAsciiCase( sRemote ) &&
sToName.equalsIgnoreAsciiCase( sUno ) )
{
pMapping = new bridges_remote::RemoteMapping( pTo, /* Uno */
pFrom, /*remote*/
bridges_remote::remoteToUno,
OUString() );
}
else if ( sFromName.equalsIgnoreAsciiCase( sUno ) &&
sToName.equalsIgnoreAsciiCase( sRemote ) )
{
pMapping = new bridges_remote::RemoteMapping( pFrom ,
pTo ,
bridges_remote::unoToRemote,
OUString() );
}
*ppMapping = (uno_Mapping * )pMapping;
OUString dummy;
uno_registerMapping( ppMapping ,
bridges_remote::freeRemoteMapping,
pFrom ,
pTo ,
dummy.pData );
}
}
sal_Bool SAL_CALL component_canUnload( TimeValue *pTime )
{
return g_moduleCount.canUnload( &g_moduleCount , pTime );
}
}
|