1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: testconnection.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_io.hxx"
36 #include <osl/diagnose.h>
37 #include <osl/thread.hxx>
39 #include <cppuhelper/servicefactory.hxx>
41 #include <com/sun/star/lang/XComponent.hpp>
43 #include <com/sun/star/registry/XImplementationRegistration.hpp>
45 #include <com/sun/star/connection/XConnector.hpp>
46 #include <com/sun/star/connection/XAcceptor.hpp>
48 using namespace ::osl
;
49 using namespace ::rtl
;
50 using namespace ::cppu
;
51 using namespace ::com::sun::star::uno
;
52 using namespace ::com::sun::star::io
;
53 using namespace ::com::sun::star::lang
;
54 using namespace ::com::sun::star::registry
;
55 using namespace ::com::sun::star::connection
;
62 MyThread( const Reference
< XAcceptor
> &r
, const OUString
& sConnectionDescription
) :
64 m_sConnectionDescription( sConnectionDescription
)
66 virtual void SAL_CALL
run();
68 Reference
< XAcceptor
> m_rAcceptor
;
70 Reference
< XConnection
> m_rConnection
;
71 OUString m_sConnectionDescription
;
74 void doWrite( const Reference
< XConnection
> &r
)
76 Sequence
< sal_Int8
> seq(10);
77 for( sal_Int32 i
= 0 ; i
< 10 ; i
++ )
79 seq
.getArray()[i
] = i
;
85 void doRead( const Reference
< XConnection
> &r
)
87 Sequence
< sal_Int8
> seq(10);
89 OSL_ASSERT( 10 == r
->read( seq
, 10 ) );
91 for( sal_Int32 i
= 0 ; i
< 10 ; i
++ )
93 OSL_ASSERT( seq
.getConstArray()[i
] == i
);
102 m_rConnection
= m_rAcceptor
->accept( m_sConnectionDescription
);
104 catch ( Exception
&e
)
106 OString tmp
= OUStringToOString( e
.Message
, RTL_TEXTENCODING_ASCII_US
);
107 printf( "Exception was thrown by acceptor thread: %s\n", tmp
.getStr() );
110 if( m_rConnection
.is() )
112 Sequence
< sal_Int8
> seq(12);
115 doWrite( m_rConnection
);
116 doRead( m_rConnection
);
120 printf( "unknown exception was thrown\n" );
131 void testConnection( const OUString
&sConnectionDescription
,
132 const Reference
< XAcceptor
> &rAcceptor
,
133 const Reference
< XConnector
> &rConnector
)
136 MyThread
thread( rAcceptor
, sConnectionDescription
);
139 sal_Bool bGotit
= sal_False
;
140 Reference
< XConnection
> r
;
146 // Why is this wait necessary ????
147 TimeValue value
= {1,0};
148 osl_waitThread( &value
);
149 r
= rConnector
->connect( sConnectionDescription
);
150 OSL_ASSERT( r
.is() );
157 printf( "Couldn't connect, retrying ...\n" );
166 Sequence
< sal_Int8
> seq(10);
168 OSL_ENSURE( 0 , "expected exception not thrown" );
170 catch ( IOException
& )
176 OSL_ENSURE( 0 , "wrong exception was thrown" );
184 #if (defined UNX) || (defined OS2)
185 int main( int argc
, char * argv
[] )
187 int __cdecl
main( int argc
, char * argv
[] )
190 Reference
< XMultiServiceFactory
> xMgr(
191 createRegistryServiceFactory( OUString( RTL_CONSTASCII_USTRINGPARAM("applicat.rdb")) ) );
193 Reference
< XImplementationRegistration
> xImplReg(
194 xMgr
->createInstance( OUString::createFromAscii("com.sun.star.registry.ImplementationRegistration") ), UNO_QUERY
);
195 OSL_ENSURE( xImplReg
.is(), "### no impl reg!" );
198 OUString::createFromAscii( "connector.uno" SAL_DLLEXTENSION
);
199 xImplReg
->registerImplementation(
200 OUString::createFromAscii("com.sun.star.loader.SharedLibrary"), aLibName
, Reference
< XSimpleRegistry
>() );
202 aLibName
= OUString::createFromAscii( "acceptor.uno" SAL_DLLEXTENSION
);
203 xImplReg
->registerImplementation(
204 OUString::createFromAscii("com.sun.star.loader.SharedLibrary"), aLibName
, Reference
< XSimpleRegistry
>() );
206 Reference
< XAcceptor
> rAcceptor(
207 xMgr
->createInstance(
208 OUString::createFromAscii("com.sun.star.connection.Acceptor" ) ) , UNO_QUERY
);
210 Reference
< XAcceptor
> rAcceptorPipe(
211 xMgr
->createInstance(
212 OUString::createFromAscii("com.sun.star.connection.Acceptor" ) ) , UNO_QUERY
);
214 Reference
< XConnector
> rConnector(
215 xMgr
->createInstance( OUString::createFromAscii("com.sun.star.connection.Connector") ) , UNO_QUERY
);
218 printf( "Testing sockets" );
220 testConnection( OUString::createFromAscii("socket,host=localhost,port=2001"), rAcceptor
, rConnector
);
223 printf( "Testing pipe" );
225 testConnection( OUString::createFromAscii("pipe,name=bla") , rAcceptorPipe
, rConnector
);
228 // check, if errornous strings make any problem
229 rAcceptor
= Reference
< XAcceptor
> (
230 xMgr
->createInstance( OUString::createFromAscii( "com.sun.star.connection.Acceptor" ) ),
235 rAcceptor
->accept( OUString() );
236 OSL_ENSURE( 0 , "empty connection string" );
238 catch( IllegalArgumentException
& )
240 // everything is fine
244 OSL_ENSURE( 0, "unexpected akexception with empty connection string" );
249 rConnector
->connect( OUString() );
250 OSL_ENSURE( 0 , "empty connection string" );
252 catch( ConnectionSetupException
& )
254 // everything is fine
258 OSL_ENSURE( 0, "unexpected exception with empty connection string" );
262 MyThread
thread( rAcceptor
, OUString::createFromAscii("socket,host=localhost,port=2001") );
265 TimeValue value
= {0,1};
266 osl_waitThread( &value
);
269 rAcceptor
->accept( OUString::createFromAscii("socket,host=localhost,port=2001") );
270 OSL_ENSURE( 0 , "already existing exception expected" );
272 catch( AlreadyAcceptingException
& e
)
274 // everything is fine
278 OSL_ENSURE( 0, "unknown exception, already existing existing expected" );
281 rAcceptor
->stopAccepting();
284 Reference
< XComponent
> rComp( xMgr
, UNO_QUERY
);