1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
23 #include <osl/diagnose.h>
24 #include <osl/thread.hxx>
26 #include <cppuhelper/servicefactory.hxx>
28 #include <com/sun/star/lang/XComponent.hpp>
30 #include <com/sun/star/registry/XImplementationRegistration.hpp>
32 #include <com/sun/star/connection/XConnector.hpp>
33 #include <com/sun/star/connection/XAcceptor.hpp>
35 using namespace ::osl
;
36 using namespace ::rtl
;
37 using namespace ::cppu
;
38 using namespace ::com::sun::star::uno
;
39 using namespace ::com::sun::star::io
;
40 using namespace ::com::sun::star::lang
;
41 using namespace ::com::sun::star::registry
;
42 using namespace ::com::sun::star::connection
;
49 MyThread( const Reference
< XAcceptor
> &r
, const OUString
& sConnectionDescription
) :
51 m_sConnectionDescription( sConnectionDescription
)
53 virtual void SAL_CALL
run();
55 Reference
< XAcceptor
> m_rAcceptor
;
57 Reference
< XConnection
> m_rConnection
;
58 OUString m_sConnectionDescription
;
61 void doWrite( const Reference
< XConnection
> &r
)
63 Sequence
< sal_Int8
> seq(10);
64 for( sal_Int32 i
= 0 ; i
< 10 ; i
++ )
66 seq
.getArray()[i
] = i
;
72 void doRead( const Reference
< XConnection
> &r
)
74 Sequence
< sal_Int8
> seq(10);
76 OSL_ASSERT( 10 == r
->read( seq
, 10 ) );
78 for( sal_Int32 i
= 0 ; i
< 10 ; i
++ )
80 OSL_ASSERT( seq
.getConstArray()[i
] == i
);
89 m_rConnection
= m_rAcceptor
->accept( m_sConnectionDescription
);
91 catch ( const Exception
&e
)
93 OString tmp
= OUStringToOString( e
.Message
, RTL_TEXTENCODING_ASCII_US
);
94 printf( "Exception was thrown by acceptor thread: %s\n", tmp
.getStr() );
97 if( m_rConnection
.is() )
99 Sequence
< sal_Int8
> seq(12);
102 doWrite( m_rConnection
);
103 doRead( m_rConnection
);
107 printf( "unknown exception was thrown\n" );
118 void testConnection( const OUString
&sConnectionDescription
,
119 const Reference
< XAcceptor
> &rAcceptor
,
120 const Reference
< XConnector
> &rConnector
)
123 MyThread
thread( rAcceptor
, sConnectionDescription
);
126 sal_Bool bGotit
= sal_False
;
127 Reference
< XConnection
> r
;
133 // Why is this wait necessary ????
134 TimeValue value
= {1,0};
135 osl_waitThread( &value
);
136 r
= rConnector
->connect( sConnectionDescription
);
137 OSL_ASSERT( r
.is() );
144 printf( "Couldn't connect, retrying ...\n" );
153 Sequence
< sal_Int8
> seq(10);
155 OSL_FAIL( "expected exception not thrown" );
157 catch ( IOException
& )
163 OSL_FAIL( "wrong exception was thrown" );
171 int SAL_CALL
main( int argc
, char * argv
[] )
173 Reference
< XMultiServiceFactory
> xMgr(
174 createRegistryServiceFactory( OUString( "applicat.rdb") ) );
176 Reference
< XImplementationRegistration
> xImplReg(
177 xMgr
->createInstance( OUString("com.sun.star.registry.ImplementationRegistration") ), UNO_QUERY
);
178 OSL_ENSURE( xImplReg
.is(), "### no impl reg!" );
181 OUString( "connector.uno" SAL_DLLEXTENSION
);
182 xImplReg
->registerImplementation(
183 OUString("com.sun.star.loader.SharedLibrary"), aLibName
, Reference
< XSimpleRegistry
>() );
185 aLibName
= OUString( "acceptor.uno" SAL_DLLEXTENSION
);
186 xImplReg
->registerImplementation(
187 OUString("com.sun.star.loader.SharedLibrary"), aLibName
, Reference
< XSimpleRegistry
>() );
189 Reference
< XAcceptor
> rAcceptor(
190 xMgr
->createInstance(
191 OUString("com.sun.star.connection.Acceptor") ) , UNO_QUERY
);
193 Reference
< XAcceptor
> rAcceptorPipe(
194 xMgr
->createInstance(
195 OUString("com.sun.star.connection.Acceptor") ) , UNO_QUERY
);
197 Reference
< XConnector
> rConnector(
198 xMgr
->createInstance( OUString("com.sun.star.connection.Connector") ) , UNO_QUERY
);
201 printf( "Testing sockets" );
203 testConnection( OUString("socket,host=localhost,port=2001"), rAcceptor
, rConnector
);
206 printf( "Testing pipe" );
208 testConnection( OUString("pipe,name=bla") , rAcceptorPipe
, rConnector
);
211 // check, if errornous strings make any problem
212 rAcceptor
= Reference
< XAcceptor
> (
213 xMgr
->createInstance( OUString("com.sun.star.connection.Acceptor") ),
218 rAcceptor
->accept( OUString() );
219 OSL_FAIL( "empty connection string" );
221 catch( IllegalArgumentException
& )
223 // everything is fine
227 OSL_FAIL( "unexpected akexception with empty connection string" );
232 rConnector
->connect( OUString() );
233 OSL_FAIL( "empty connection string" );
235 catch( ConnectionSetupException
& )
237 // everything is fine
241 OSL_FAIL( "unexpected exception with empty connection string" );
245 MyThread
thread( rAcceptor
, OUString("socket,host=localhost,port=2001") );
248 TimeValue value
= {0,1};
249 osl_waitThread( &value
);
252 rAcceptor
->accept( OUString("socket,host=localhost,port=2001") );
253 OSL_FAIL( "already existing exception expected" );
255 catch( AlreadyAcceptingException
& )
257 // everything is fine
261 OSL_FAIL( "unknown exception, already existing existing expected" );
264 rAcceptor
->stopAccepting();
267 Reference
< XComponent
> rComp( xMgr
, UNO_QUERY
);
274 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */