1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
32 #include <osl/diagnose.h>
33 #include <osl/thread.hxx>
35 #include <cppuhelper/servicefactory.hxx>
37 #include <com/sun/star/lang/XComponent.hpp>
39 #include <com/sun/star/registry/XImplementationRegistration.hpp>
41 #include <com/sun/star/connection/XConnector.hpp>
42 #include <com/sun/star/connection/XAcceptor.hpp>
44 using namespace ::osl
;
45 using namespace ::rtl
;
46 using namespace ::cppu
;
47 using namespace ::com::sun::star::uno
;
48 using namespace ::com::sun::star::io
;
49 using namespace ::com::sun::star::lang
;
50 using namespace ::com::sun::star::registry
;
51 using namespace ::com::sun::star::connection
;
58 MyThread( const Reference
< XAcceptor
> &r
, const OUString
& sConnectionDescription
) :
60 m_sConnectionDescription( sConnectionDescription
)
62 virtual void SAL_CALL
run();
64 Reference
< XAcceptor
> m_rAcceptor
;
66 Reference
< XConnection
> m_rConnection
;
67 OUString m_sConnectionDescription
;
70 void doWrite( const Reference
< XConnection
> &r
)
72 Sequence
< sal_Int8
> seq(10);
73 for( sal_Int32 i
= 0 ; i
< 10 ; i
++ )
75 seq
.getArray()[i
] = i
;
81 void doRead( const Reference
< XConnection
> &r
)
83 Sequence
< sal_Int8
> seq(10);
85 OSL_ASSERT( 10 == r
->read( seq
, 10 ) );
87 for( sal_Int32 i
= 0 ; i
< 10 ; i
++ )
89 OSL_ASSERT( seq
.getConstArray()[i
] == i
);
98 m_rConnection
= m_rAcceptor
->accept( m_sConnectionDescription
);
100 catch ( const Exception
&e
)
102 OString tmp
= OUStringToOString( e
.Message
, RTL_TEXTENCODING_ASCII_US
);
103 printf( "Exception was thrown by acceptor thread: %s\n", tmp
.getStr() );
106 if( m_rConnection
.is() )
108 Sequence
< sal_Int8
> seq(12);
111 doWrite( m_rConnection
);
112 doRead( m_rConnection
);
116 printf( "unknown exception was thrown\n" );
127 void testConnection( const OUString
&sConnectionDescription
,
128 const Reference
< XAcceptor
> &rAcceptor
,
129 const Reference
< XConnector
> &rConnector
)
132 MyThread
thread( rAcceptor
, sConnectionDescription
);
135 sal_Bool bGotit
= sal_False
;
136 Reference
< XConnection
> r
;
142 // Why is this wait necessary ????
143 TimeValue value
= {1,0};
144 osl_waitThread( &value
);
145 r
= rConnector
->connect( sConnectionDescription
);
146 OSL_ASSERT( r
.is() );
153 printf( "Couldn't connect, retrying ...\n" );
162 Sequence
< sal_Int8
> seq(10);
164 OSL_FAIL( "expected exception not thrown" );
166 catch ( IOException
& )
172 OSL_FAIL( "wrong exception was thrown" );
180 int SAL_CALL
main( int argc
, char * argv
[] )
182 Reference
< XMultiServiceFactory
> xMgr(
183 createRegistryServiceFactory( OUString( "applicat.rdb") ) );
185 Reference
< XImplementationRegistration
> xImplReg(
186 xMgr
->createInstance( OUString("com.sun.star.registry.ImplementationRegistration") ), UNO_QUERY
);
187 OSL_ENSURE( xImplReg
.is(), "### no impl reg!" );
190 OUString( "connector.uno" SAL_DLLEXTENSION
);
191 xImplReg
->registerImplementation(
192 OUString("com.sun.star.loader.SharedLibrary"), aLibName
, Reference
< XSimpleRegistry
>() );
194 aLibName
= OUString( "acceptor.uno" SAL_DLLEXTENSION
);
195 xImplReg
->registerImplementation(
196 OUString("com.sun.star.loader.SharedLibrary"), aLibName
, Reference
< XSimpleRegistry
>() );
198 Reference
< XAcceptor
> rAcceptor(
199 xMgr
->createInstance(
200 OUString("com.sun.star.connection.Acceptor") ) , UNO_QUERY
);
202 Reference
< XAcceptor
> rAcceptorPipe(
203 xMgr
->createInstance(
204 OUString("com.sun.star.connection.Acceptor") ) , UNO_QUERY
);
206 Reference
< XConnector
> rConnector(
207 xMgr
->createInstance( OUString("com.sun.star.connection.Connector") ) , UNO_QUERY
);
210 printf( "Testing sockets" );
212 testConnection( OUString("socket,host=localhost,port=2001"), rAcceptor
, rConnector
);
215 printf( "Testing pipe" );
217 testConnection( OUString("pipe,name=bla") , rAcceptorPipe
, rConnector
);
220 // check, if errornous strings make any problem
221 rAcceptor
= Reference
< XAcceptor
> (
222 xMgr
->createInstance( OUString("com.sun.star.connection.Acceptor") ),
227 rAcceptor
->accept( OUString() );
228 OSL_FAIL( "empty connection string" );
230 catch( IllegalArgumentException
& )
232 // everything is fine
236 OSL_FAIL( "unexpected akexception with empty connection string" );
241 rConnector
->connect( OUString() );
242 OSL_FAIL( "empty connection string" );
244 catch( ConnectionSetupException
& )
246 // everything is fine
250 OSL_FAIL( "unexpected exception with empty connection string" );
254 MyThread
thread( rAcceptor
, OUString("socket,host=localhost,port=2001") );
257 TimeValue value
= {0,1};
258 osl_waitThread( &value
);
261 rAcceptor
->accept( OUString("socket,host=localhost,port=2001") );
262 OSL_FAIL( "already existing exception expected" );
264 catch( AlreadyAcceptingException
& )
266 // everything is fine
270 OSL_FAIL( "unknown exception, already existing existing expected" );
273 rAcceptor
->stopAccepting();
276 Reference
< XComponent
> rComp( xMgr
, UNO_QUERY
);
283 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */