bump product version to 4.1.6.2
[LibreOffice.git] / io / test / testconnection.cxx
blob2b6369674c05cb619796ef18243a20865006fbd9
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
20 #include <stdio.h>
21 #include <osl/time.h>
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;
45 class MyThread :
46 public Thread
48 public:
49 MyThread( const Reference< XAcceptor > &r , const OUString & sConnectionDescription) :
50 m_rAcceptor( r ),
51 m_sConnectionDescription( sConnectionDescription )
53 virtual void SAL_CALL run();
55 Reference < XAcceptor > m_rAcceptor;
56 private:
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;
69 r->write( seq );
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 );
85 void MyThread::run()
87 try
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 );
105 catch (... )
107 printf( "unknown exception was thrown\n" );
108 throw;
118 void testConnection( const OUString &sConnectionDescription ,
119 const Reference < XAcceptor > &rAcceptor,
120 const Reference < XConnector > &rConnector )
123 MyThread thread( rAcceptor , sConnectionDescription );
124 thread.create();
126 sal_Bool bGotit = sal_False;
127 Reference < XConnection > r;
129 while( ! bGotit )
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() );
138 doWrite( r );
139 doRead( r );
140 bGotit = sal_True;
142 catch( ... )
144 printf( "Couldn't connect, retrying ...\n" );
149 r->close();
153 Sequence < sal_Int8 > seq(10);
154 r->write( seq );
155 OSL_FAIL( "expected exception not thrown" );
157 catch ( IOException & )
159 // everything is ok
161 catch ( ... )
163 OSL_FAIL( "wrong exception was thrown" );
166 thread.join();
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!" );
180 OUString aLibName =
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" );
202 fflush( stdout );
203 testConnection( OUString("socket,host=localhost,port=2001"), rAcceptor , rConnector );
204 printf( " Done\n" );
206 printf( "Testing pipe" );
207 fflush( stdout );
208 testConnection( OUString("pipe,name=bla") , rAcceptorPipe , rConnector );
209 printf( " Done\n" );
211 // check, if errornous strings make any problem
212 rAcceptor = Reference< XAcceptor > (
213 xMgr->createInstance( OUString("com.sun.star.connection.Acceptor") ),
214 UNO_QUERY );
218 rAcceptor->accept( OUString() );
219 OSL_FAIL( "empty connection string" );
221 catch( IllegalArgumentException & )
223 // everything is fine
225 catch( ... )
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
239 catch( ... )
241 OSL_FAIL( "unexpected exception with empty connection string" );
245 MyThread thread( rAcceptor , OUString("socket,host=localhost,port=2001") );
246 thread.create();
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
259 catch( ... )
261 OSL_FAIL( "unknown exception, already existing existing expected" );
264 rAcceptor->stopAccepting();
265 thread.join();
267 Reference < XComponent > rComp( xMgr , UNO_QUERY );
268 if( rComp.is() )
270 rComp->dispose();
274 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */