summaryrefslogtreecommitdiff
path: root/io/source/acceptor/acceptor.cxx
blob: 6b1d6840b2aee9a4c13d8be42da4d7cf6d0d712a (plain)
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
/*************************************************************************
 *
 *  $RCSfile: acceptor.cxx,v $
 *
 *  $Revision: 1.7 $
 *
 *  last change: $Author: jbu $ $Date: 2001-04-11 15:43:48 $
 *
 *  The Contents of this file are made available subject to the terms of
 *  either of the following licenses
 *
 *         - GNU Lesser General Public License Version 2.1
 *         - Sun Industry Standards Source License Version 1.1
 *
 *  Sun Microsystems Inc., October, 2000
 *
 *  GNU Lesser General Public License Version 2.1
 *  =============================================
 *  Copyright 2000 by Sun Microsystems, Inc.
 *  901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License version 2.1, as published by the Free Software Foundation.
 *
 *  This library 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 for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *  MA  02111-1307  USA
 *
 *
 *  Sun Industry Standards Source License Version 1.1
 *  =================================================
 *  The contents of this file are subject to the Sun Industry Standards
 *  Source License Version 1.1 (the "License"); You may not use this file
 *  except in compliance with the License. You may obtain a copy of the
 *  License at http://www.openoffice.org/license.html.
 *
 *  Software provided under this License is provided on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 *  See the License for the specific provisions governing your rights and
 *  obligations concerning the Software.
 *
 *  The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 *
 *  Copyright: 2000 by Sun Microsystems, Inc.
 *
 *  All Rights Reserved.
 *
 *  Contributor(s): _______________________________________
 *
 *
 ************************************************************************/
#include <osl/mutex.hxx>

#include <uno/mapping.hxx>

#include <cppuhelper/factory.hxx>
#include <cppuhelper/implbase1.hxx>

#include <com/sun/star/connection/XAcceptor.hpp>

#include "acceptor.hxx"

#define IMPLEMENTATION_NAME "com.sun.star.comp.io.Acceptor"
#define SERVICE_NAME "com.sun.star.connection.Acceptor"

using namespace ::osl;
using namespace ::rtl;
using namespace ::cppu;
using namespace ::com::sun::star::uno;
using namespace ::com::sun::star::lang;
using namespace ::com::sun::star::registry;
using namespace ::com::sun::star::connection;


namespace io_acceptor
{
    typedef WeakImplHelper1< XAcceptor > MyImplHelper;

    class OAcceptor : public MyImplHelper
    {
    public:
        OAcceptor(Reference< XMultiServiceFactory > xMultiServiceFactory);
        ~OAcceptor();

    public:
        // Methods
        virtual Reference< XConnection > SAL_CALL accept( const OUString& sConnectionDescription )
            throw( AlreadyAcceptingException,
                   ConnectionSetupException,
                   IllegalArgumentException,
                   RuntimeException);
        virtual void SAL_CALL stopAccepting(  ) throw( RuntimeException);

        PipeAcceptor *m_pPipe;
        SocketAcceptor *m_pSocket;
        Mutex m_mutex;
        OUString m_sLastDescription;
        sal_Bool m_bInAccept;

        Reference<XMultiServiceFactory> _xMultiServiceFactory;
        Reference<XAcceptor>            _xAcceptor;
    };


    OAcceptor::OAcceptor(Reference< XMultiServiceFactory > xMultiServiceFactory)
        : m_pPipe( 0 )
        ,m_pSocket( 0 )
        ,_xMultiServiceFactory(xMultiServiceFactory)
        , m_bInAccept( sal_False )
    {}

    OAcceptor::~OAcceptor()
    {
        if( m_pPipe )
        {
            delete m_pPipe;
        }
        if( m_pSocket )
        {
            delete m_pSocket;
        }
    }

    // helper class
    class TokenContainer
    {
    public:
        TokenContainer( const OUString &sString );

        ~TokenContainer()
        {
            delete [] m_aTokens;
        }

        inline OUString & getToken( sal_Int32 nElement )
        {
            return m_aTokens[nElement];
        }

        inline sal_Int32 getTokenCount()
        {
            return m_nTokenCount;
        }

        OUString *m_aTokens;
        sal_Int32 m_nTokenCount;
    };

    TokenContainer::TokenContainer( const OUString & sString ) :
        m_nTokenCount( 0 ),
        m_aTokens( 0 )
    {
        // split into separate tokens
        sal_Int32 i = 0,nMax;

        nMax = sString.getLength();
        for( i = 0 ; i < nMax ; i ++ )
        {
            if( ',' == sString.pData->buffer[i] )
            {
                m_nTokenCount ++;
            }
        }

        if( sString.getLength() )
        {
            m_nTokenCount ++;
        }
        if( m_nTokenCount )
        {
            m_aTokens = new OUString[m_nTokenCount];
            sal_Int32 nIndex = 0;
            for( i = 0 ; i < m_nTokenCount ; i ++ )
            {
                sal_Int32 nLastIndex = nIndex;
                nIndex = sString.indexOf( ( sal_Unicode ) ',' , nIndex );
                if( -1 == nIndex )
                {
                    m_aTokens[i] = sString.copy( nLastIndex );
                    break;
                }
                else
                {
                    m_aTokens[i] = sString.copy( nLastIndex , nIndex-nLastIndex );
                }
                m_aTokens[i] = m_aTokens[i].trim();
                nIndex ++;
            }
        }
    }

    struct BeingInAccept
    {
        BeingInAccept( sal_Bool *pFlag,const OUString & sConnectionDescription  ) throw( AlreadyAcceptingException)
            : m_pFlag( pFlag )
            {
                  if( *m_pFlag )
                  {
                      OUString sMessage( RTL_CONSTASCII_USTRINGPARAM( "AlreadyAcceptingException :" ) );
                      sMessage += sConnectionDescription;
                      throw AlreadyAcceptingException( sMessage , Reference< XInterface > () );
                  }
                  *m_pFlag = sal_True;
            }
        ~BeingInAccept()
            {
                *m_pFlag = sal_False;
            }
        sal_Bool *m_pFlag;
    };

    Reference< XConnection > OAcceptor::accept( const OUString &sConnectionDescription )
        throw( AlreadyAcceptingException,
               ConnectionSetupException,
               IllegalArgumentException,
               RuntimeException)
    {
#ifdef DEBUG
        OString tmp = OUStringToOString(sConnectionDescription, RTL_TEXTENCODING_ASCII_US);
        OSL_TRACE("acceptor %s\n", tmp.getStr());
#endif
        // if there is a thread alread accepting in this object, throw an exception.
        struct BeingInAccept guard( &m_bInAccept, sConnectionDescription );

        Reference< XConnection > r;
        if( m_sLastDescription.getLength() &&
            m_sLastDescription != sConnectionDescription )
        {
            // instantiate another acceptor for different ports
            OUString sMessage = OUString( RTL_CONSTASCII_USTRINGPARAM(
                "acceptor::accept called multiple times with different conncetion strings\n" ) );
            throw ConnectionSetupException( sMessage, Reference< XInterface > () );
        }

        if( ! m_sLastDescription.getLength() )
        {
            // setup the acceptor
            TokenContainer container( sConnectionDescription );
            if( ! container.getTokenCount() )
            {
                throw IllegalArgumentException(
                    OUString( RTL_CONSTASCII_USTRINGPARAM( "empty connection string" ) ),
                    Reference< XInterface > (),
                    0 );
            }

            if( 0 == container.getToken(0).compareToAscii("pipe") )
            {
                OUString sName;
                sal_Int32 i;
                for( i = 1 ; i < container.getTokenCount() ; i ++ )
                {
                    sal_Int32 nIndex = container.getToken(i).indexOf( '=' );
                    if( -1 != nIndex )
                    {
                        OUString aName = container.getToken(i).copy( 0 , nIndex ).trim().toLowerCase();
                        if( nIndex < container.getToken(i).getLength() )
                        {
                            OUString oValue = container.getToken(i).copy(
                                nIndex+1 , container.getToken(i).getLength() - nIndex -1 ).trim();
                            if ( 0 == aName.compareToAscii("name") )
                            {
                                sName = oValue;
                            }
                        }
                    }
                }
                m_pPipe = new PipeAcceptor(sName , sConnectionDescription );

                try
                {
                    m_pPipe->init();
                }
                catch( ... )
                {
                    {
                        MutexGuard guard( m_mutex );
                        delete m_pPipe;
                        m_pPipe = 0;
                    }
                    throw;
                }
            }
            else if ( 0 == container.getToken(0).compareToAscii("socket") )
            {
                OUString sHost = OUString( RTL_CONSTASCII_USTRINGPARAM("localhost"));
                sal_uInt16 nPort = 0;
                sal_Bool bTcpNoDelay = sal_False;
                sal_Int32 i;

                for( i = 1 ;i < container.getTokenCount() ; i ++ )
                {
                    sal_Int32 nIndex = container.getToken(i).indexOf( '=' );
                    if( -1 != nIndex )
                    {
                        OUString aName = container.getToken(i).copy( 0 , nIndex ).trim().toLowerCase();
                        if( nIndex < container.getToken(i).getLength() )
                        {
                            OUString oValue = container.getToken(i).copy( nIndex+1 , container.getToken(i).getLength() - nIndex -1 ).trim();
                            if( aName.compareToAscii("host") == 0 )
                            {
                                sHost = oValue;
                            }
                            else if( aName.compareToAscii("port") == 0 )
                            {
                                nPort = (sal_uInt16) oValue.toInt32();
                            }
                            else if( aName.compareToAscii("tcpnodelay") == 0 )
                            {
                                bTcpNoDelay = oValue.toInt32() ? sal_True : sal_False;
                            }
                        }
                    }
                }

                m_pSocket = new SocketAcceptor(
                    sHost , nPort, bTcpNoDelay, sConnectionDescription );

                try
                {
                    m_pSocket->init();
                }
                catch( ... )
                {
                    {
                        MutexGuard guard( m_mutex );
                        delete m_pSocket;
                        m_pSocket = 0;
                    }
                    throw;
                }
            }
            else
            {
                OUString delegatee = OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.connection.Acceptor."));
                delegatee += container.getToken(0);

#ifdef DEBUG
                OString tmp = OUStringToOString(delegatee, RTL_TEXTENCODING_ASCII_US);
                OSL_TRACE("trying to get service %s\n", tmp.getStr());
#endif
                _xAcceptor = Reference<XAcceptor>(_xMultiServiceFactory->createInstance(delegatee), UNO_QUERY);

                if(!_xAcceptor.is())
                {
                    OUString message(RTL_CONSTASCII_USTRINGPARAM("Acceptor: unknown delegatee "));
                    message += delegatee;

                    throw ConnectionSetupException(message, Reference<XInterface>());
                }
            }
            m_sLastDescription = sConnectionDescription;
        }

        if( m_pPipe )
        {
            r = m_pPipe->accept();
        }
        else if( m_pSocket )
        {
            r = m_pSocket->accept();
        }
        else
        {
            sal_Int32 index = sConnectionDescription.indexOf((sal_Unicode) ',');

            r = _xAcceptor->accept(m_sLastDescription.copy(index + 1).trim());
        }

        return r;
    }

    void SAL_CALL OAcceptor::stopAccepting(  ) throw( RuntimeException)
    {
        MutexGuard guard( m_mutex );

        if( m_pPipe )
        {
            m_pPipe->stopAccepting();
        }
        else if ( m_pSocket )
        {
            m_pSocket->stopAccepting();
        }
        else if( _xAcceptor.is() )
        {
            _xAcceptor->stopAccepting();
        }

    }


    Reference< XInterface > SAL_CALL acceptor_CreateInstance( const Reference< XMultiServiceFactory > & xMultiServiceFactory)
    {
        return Reference < XInterface >( ( OWeakObject * ) new OAcceptor(xMultiServiceFactory) );
    }

    Sequence< OUString > acceptor_getSupportedServiceNames()
    {
        static Sequence < OUString > *pNames = 0;
        if( ! pNames )
        {
            MutexGuard guard( Mutex::getGlobalMutex() );
            if( !pNames )
            {
                static Sequence< OUString > seqNames(1);
                seqNames.getArray()[0] = OUString::createFromAscii( SERVICE_NAME );
                pNames = &seqNames;
            }
        }
        return *pNames;
    }

}

using namespace io_acceptor;



extern "C"
{
//==================================================================================================
void SAL_CALL component_getImplementationEnvironment(
    const sal_Char ** ppEnvTypeName, uno_Environment ** ppEnv )
{
    *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
}
//==================================================================================================
sal_Bool SAL_CALL component_writeInfo(
    void * pServiceManager, void * pRegistryKey )
{
    if (pRegistryKey)
    {
        try
        {
            Reference< XRegistryKey > xNewKey(
                reinterpret_cast< XRegistryKey * >( pRegistryKey )->createKey(
                    OUString::createFromAscii("/" IMPLEMENTATION_NAME "/UNO/SERVICES" ) ) );

            const Sequence< OUString > & rSNL = acceptor_getSupportedServiceNames();
            const OUString * pArray = rSNL.getConstArray();
            for ( sal_Int32 nPos = rSNL.getLength(); nPos--; )
                xNewKey->createKey( pArray[nPos] );

            return sal_True;
        }
        catch (InvalidRegistryException &)
        {
            OSL_ENSURE( sal_False, "### InvalidRegistryException!" );
        }
    }
    return sal_False;
}
//==================================================================================================
void * SAL_CALL component_getFactory(
    const sal_Char * pImplName, void * pServiceManager, void * pRegistryKey )
{
    void * pRet = 0;

    if (pServiceManager && rtl_str_compare( pImplName, IMPLEMENTATION_NAME ) == 0)
    {
        Reference< XSingleServiceFactory > xFactory( createSingleFactory(
            reinterpret_cast< XMultiServiceFactory * >( pServiceManager ),
            OUString::createFromAscii( pImplName ),
            acceptor_CreateInstance, acceptor_getSupportedServiceNames() ) );

        if (xFactory.is())
        {
            xFactory->acquire();
            pRet = xFactory.get();
        }
    }

    return pRet;
}
}