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 ************************************************************************/
29 // MARKER(update_precomp.py): autogen include statement, do not remove
30 #include "precompiled_io.hxx"
34 #include <osl/diagnose.h>
35 #include <osl/thread.hxx>
37 #include <cppuhelper/servicefactory.hxx>
39 #include <com/sun/star/lang/XComponent.hpp>
41 #include <com/sun/star/registry/XImplementationRegistration.hpp>
43 #include <com/sun/star/connection/XConnector.hpp>
44 #include <com/sun/star/connection/XAcceptor.hpp>
46 using namespace ::osl
;
47 using namespace ::rtl
;
48 using namespace ::cppu
;
49 using namespace ::com::sun::star::uno
;
50 using namespace ::com::sun::star::io
;
51 using namespace ::com::sun::star::lang
;
52 using namespace ::com::sun::star::registry
;
53 using namespace ::com::sun::star::connection
;
60 MyThread( const Reference
< XAcceptor
> &r
, const OUString
& sConnectionDescription
) :
62 m_sConnectionDescription( sConnectionDescription
)
64 virtual void SAL_CALL
run();
66 Reference
< XAcceptor
> m_rAcceptor
;
68 Reference
< XConnection
> m_rConnection
;
69 OUString m_sConnectionDescription
;
72 void doWrite( const Reference
< XConnection
> &r
)
74 Sequence
< sal_Int8
> seq(10);
75 for( sal_Int32 i
= 0 ; i
< 10 ; i
++ )
77 seq
.getArray()[i
] = i
;
83 void doRead( const Reference
< XConnection
> &r
)
85 Sequence
< sal_Int8
> seq(10);
87 OSL_ASSERT( 10 == r
->read( seq
, 10 ) );
89 for( sal_Int32 i
= 0 ; i
< 10 ; i
++ )
91 OSL_ASSERT( seq
.getConstArray()[i
] == i
);
100 m_rConnection
= m_rAcceptor
->accept( m_sConnectionDescription
);
102 catch ( Exception
&e
)
104 OString tmp
= OUStringToOString( e
.Message
, RTL_TEXTENCODING_ASCII_US
);
105 printf( "Exception was thrown by acceptor thread: %s\n", tmp
.getStr() );
108 if( m_rConnection
.is() )
110 Sequence
< sal_Int8
> seq(12);
113 doWrite( m_rConnection
);
114 doRead( m_rConnection
);
118 printf( "unknown exception was thrown\n" );
129 void testConnection( const OUString
&sConnectionDescription
,
130 const Reference
< XAcceptor
> &rAcceptor
,
131 const Reference
< XConnector
> &rConnector
)
134 MyThread
thread( rAcceptor
, sConnectionDescription
);
137 sal_Bool bGotit
= sal_False
;
138 Reference
< XConnection
> r
;
144 // Why is this wait necessary ????
145 TimeValue value
= {1,0};
146 osl_waitThread( &value
);
147 r
= rConnector
->connect( sConnectionDescription
);
148 OSL_ASSERT( r
.is() );
155 printf( "Couldn't connect, retrying ...\n" );
164 Sequence
< sal_Int8
> seq(10);
166 OSL_FAIL( "expected exception not thrown" );
168 catch ( IOException
& )
174 OSL_FAIL( "wrong exception was thrown" );
182 int SAL_CALL
main( int argc
, char * argv
[] )
184 Reference
< XMultiServiceFactory
> xMgr(
185 createRegistryServiceFactory( OUString( RTL_CONSTASCII_USTRINGPARAM("applicat.rdb")) ) );
187 Reference
< XImplementationRegistration
> xImplReg(
188 xMgr
->createInstance( OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.registry.ImplementationRegistration")) ), UNO_QUERY
);
189 OSL_ENSURE( xImplReg
.is(), "### no impl reg!" );
192 OUString(RTL_CONSTASCII_USTRINGPARAM( "connector.uno" SAL_DLLEXTENSION
));
193 xImplReg
->registerImplementation(
194 OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.loader.SharedLibrary")), aLibName
, Reference
< XSimpleRegistry
>() );
196 aLibName
= OUString(RTL_CONSTASCII_USTRINGPARAM( "acceptor.uno" SAL_DLLEXTENSION
));
197 xImplReg
->registerImplementation(
198 OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.loader.SharedLibrary")), aLibName
, Reference
< XSimpleRegistry
>() );
200 Reference
< XAcceptor
> rAcceptor(
201 xMgr
->createInstance(
202 OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.connection.Acceptor")) ) , UNO_QUERY
);
204 Reference
< XAcceptor
> rAcceptorPipe(
205 xMgr
->createInstance(
206 OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.connection.Acceptor")) ) , UNO_QUERY
);
208 Reference
< XConnector
> rConnector(
209 xMgr
->createInstance( OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.connection.Connector")) ) , UNO_QUERY
);
212 printf( "Testing sockets" );
214 testConnection( OUString(RTL_CONSTASCII_USTRINGPARAM("socket,host=localhost,port=2001")), rAcceptor
, rConnector
);
217 printf( "Testing pipe" );
219 testConnection( OUString(RTL_CONSTASCII_USTRINGPARAM("pipe,name=bla")) , rAcceptorPipe
, rConnector
);
222 // check, if errornous strings make any problem
223 rAcceptor
= Reference
< XAcceptor
> (
224 xMgr
->createInstance( OUString(RTL_CONSTASCII_USTRINGPARAM("com.sun.star.connection.Acceptor")) ),
229 rAcceptor
->accept( OUString() );
230 OSL_FAIL( "empty connection string" );
232 catch( IllegalArgumentException
& )
234 // everything is fine
238 OSL_FAIL( "unexpected akexception with empty connection string" );
243 rConnector
->connect( OUString() );
244 OSL_FAIL( "empty connection string" );
246 catch( ConnectionSetupException
& )
248 // everything is fine
252 OSL_FAIL( "unexpected exception with empty connection string" );
256 MyThread
thread( rAcceptor
, OUString(RTL_CONSTASCII_USTRINGPARAM("socket,host=localhost,port=2001")) );
259 TimeValue value
= {0,1};
260 osl_waitThread( &value
);
263 rAcceptor
->accept( OUString(RTL_CONSTASCII_USTRINGPARAM("socket,host=localhost,port=2001")) );
264 OSL_FAIL( "already existing exception expected" );
266 catch( AlreadyAcceptingException
& e
)
268 // everything is fine
272 OSL_FAIL( "unknown exception, already existing existing expected" );
275 rAcceptor
->stopAccepting();
278 Reference
< XComponent
> rComp( xMgr
, UNO_QUERY
);
285 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */