xref: /AOO41X/main/io/test/testconnection.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_io.hxx"
30*cdf0e10cSrcweir #include <stdio.h>
31*cdf0e10cSrcweir #include <osl/time.h>
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir #include <osl/diagnose.h>
34*cdf0e10cSrcweir #include <osl/thread.hxx>
35*cdf0e10cSrcweir 
36*cdf0e10cSrcweir #include <cppuhelper/servicefactory.hxx>
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir #include <com/sun/star/lang/XComponent.hpp>
39*cdf0e10cSrcweir 
40*cdf0e10cSrcweir #include <com/sun/star/registry/XImplementationRegistration.hpp>
41*cdf0e10cSrcweir 
42*cdf0e10cSrcweir #include <com/sun/star/connection/XConnector.hpp>
43*cdf0e10cSrcweir #include <com/sun/star/connection/XAcceptor.hpp>
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir using namespace ::osl;
46*cdf0e10cSrcweir using namespace ::rtl;
47*cdf0e10cSrcweir using namespace ::cppu;
48*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
49*cdf0e10cSrcweir using namespace ::com::sun::star::io;
50*cdf0e10cSrcweir using namespace ::com::sun::star::lang;
51*cdf0e10cSrcweir using namespace ::com::sun::star::registry;
52*cdf0e10cSrcweir using namespace ::com::sun::star::connection;
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir class MyThread :
56*cdf0e10cSrcweir 	public Thread
57*cdf0e10cSrcweir {
58*cdf0e10cSrcweir public:
59*cdf0e10cSrcweir 	MyThread( const Reference< XAcceptor > &r , const OUString & sConnectionDescription) :
60*cdf0e10cSrcweir 		m_rAcceptor( r ),
61*cdf0e10cSrcweir 		m_sConnectionDescription( sConnectionDescription )
62*cdf0e10cSrcweir 		{}
63*cdf0e10cSrcweir 	virtual void SAL_CALL run();
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir 	Reference < XAcceptor > m_rAcceptor;
66*cdf0e10cSrcweir private:
67*cdf0e10cSrcweir 	Reference < XConnection > m_rConnection;
68*cdf0e10cSrcweir 	OUString m_sConnectionDescription;
69*cdf0e10cSrcweir };
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir void doWrite( const Reference < XConnection > &r )
72*cdf0e10cSrcweir {
73*cdf0e10cSrcweir 	Sequence < sal_Int8 > seq(10);
74*cdf0e10cSrcweir 	for( sal_Int32 i = 0 ; i < 10 ; i ++ )
75*cdf0e10cSrcweir 	{
76*cdf0e10cSrcweir 		seq.getArray()[i] = i;
77*cdf0e10cSrcweir 	}
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir 	r->write( seq );
80*cdf0e10cSrcweir }
81*cdf0e10cSrcweir 
82*cdf0e10cSrcweir void doRead( const Reference < XConnection > &r )
83*cdf0e10cSrcweir {
84*cdf0e10cSrcweir 	Sequence < sal_Int8 > seq(10);
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir 	OSL_ASSERT( 10 == r->read( seq , 10 ) );
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir 	for( sal_Int32 i = 0 ; i < 10 ; i ++ )
89*cdf0e10cSrcweir 	{
90*cdf0e10cSrcweir 		OSL_ASSERT( seq.getConstArray()[i] == i );
91*cdf0e10cSrcweir 	}
92*cdf0e10cSrcweir }
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir void MyThread::run()
96*cdf0e10cSrcweir {
97*cdf0e10cSrcweir 	try
98*cdf0e10cSrcweir 	{
99*cdf0e10cSrcweir 		m_rConnection = m_rAcceptor->accept( m_sConnectionDescription );
100*cdf0e10cSrcweir 	}
101*cdf0e10cSrcweir 	catch ( Exception &e)
102*cdf0e10cSrcweir 	{
103*cdf0e10cSrcweir 		OString tmp= OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US );
104*cdf0e10cSrcweir 		printf( "Exception was thrown by acceptor thread: %s\n", tmp.getStr() );
105*cdf0e10cSrcweir 	}
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir 	if( m_rConnection.is() )
108*cdf0e10cSrcweir 	{
109*cdf0e10cSrcweir 		Sequence < sal_Int8 > seq(12);
110*cdf0e10cSrcweir 		try
111*cdf0e10cSrcweir 		{
112*cdf0e10cSrcweir 			doWrite( m_rConnection );
113*cdf0e10cSrcweir 			doRead( m_rConnection );
114*cdf0e10cSrcweir 		}
115*cdf0e10cSrcweir 		catch (... )
116*cdf0e10cSrcweir 		{
117*cdf0e10cSrcweir 			printf( "unknown exception was thrown\n" );
118*cdf0e10cSrcweir 			throw;
119*cdf0e10cSrcweir 		}
120*cdf0e10cSrcweir 	}
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir }
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir 
126*cdf0e10cSrcweir 
127*cdf0e10cSrcweir 
128*cdf0e10cSrcweir void testConnection( const OUString &sConnectionDescription  ,
129*cdf0e10cSrcweir 					 const Reference < XAcceptor > &rAcceptor,
130*cdf0e10cSrcweir 					 const Reference < XConnector > &rConnector )
131*cdf0e10cSrcweir {
132*cdf0e10cSrcweir 	{
133*cdf0e10cSrcweir 		MyThread thread( rAcceptor , sConnectionDescription );
134*cdf0e10cSrcweir 		thread.create();
135*cdf0e10cSrcweir 
136*cdf0e10cSrcweir 		sal_Bool bGotit = sal_False;
137*cdf0e10cSrcweir 		Reference < XConnection > r;
138*cdf0e10cSrcweir 
139*cdf0e10cSrcweir 		while( ! bGotit )
140*cdf0e10cSrcweir 		{
141*cdf0e10cSrcweir 			try
142*cdf0e10cSrcweir 			{
143*cdf0e10cSrcweir 				// Why is this wait necessary ????
144*cdf0e10cSrcweir 				TimeValue value = {1,0};
145*cdf0e10cSrcweir 				osl_waitThread( &value );
146*cdf0e10cSrcweir 				r = rConnector->connect( sConnectionDescription );
147*cdf0e10cSrcweir 				OSL_ASSERT( r.is() );
148*cdf0e10cSrcweir 				doWrite( r );
149*cdf0e10cSrcweir 				doRead( r );
150*cdf0e10cSrcweir 				bGotit = sal_True;
151*cdf0e10cSrcweir 			}
152*cdf0e10cSrcweir 			catch( ... )
153*cdf0e10cSrcweir 			{
154*cdf0e10cSrcweir 				printf( "Couldn't connect, retrying ...\n" );
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir 			}
157*cdf0e10cSrcweir 		}
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir 		r->close();
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir 		try
162*cdf0e10cSrcweir 		{
163*cdf0e10cSrcweir 			Sequence < sal_Int8 > seq(10);
164*cdf0e10cSrcweir 			r->write( seq );
165*cdf0e10cSrcweir 			OSL_ENSURE( 0 , "expected exception not thrown" );
166*cdf0e10cSrcweir 		}
167*cdf0e10cSrcweir 		catch ( IOException & )
168*cdf0e10cSrcweir 		{
169*cdf0e10cSrcweir 			// everything is ok
170*cdf0e10cSrcweir 		}
171*cdf0e10cSrcweir 		catch ( ... )
172*cdf0e10cSrcweir 		{
173*cdf0e10cSrcweir 			OSL_ENSURE( 0 , "wrong exception was thrown" );
174*cdf0e10cSrcweir 		}
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir 		thread.join();
177*cdf0e10cSrcweir 	}
178*cdf0e10cSrcweir }
179*cdf0e10cSrcweir 
180*cdf0e10cSrcweir 
181*cdf0e10cSrcweir #if (defined UNX) || (defined OS2)
182*cdf0e10cSrcweir int main( int argc, char * argv[] )
183*cdf0e10cSrcweir #else
184*cdf0e10cSrcweir int __cdecl main( int argc, char * argv[] )
185*cdf0e10cSrcweir #endif
186*cdf0e10cSrcweir {
187*cdf0e10cSrcweir 	Reference< XMultiServiceFactory > xMgr(
188*cdf0e10cSrcweir 		createRegistryServiceFactory( OUString( RTL_CONSTASCII_USTRINGPARAM("applicat.rdb")) ) );
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir 	Reference< XImplementationRegistration > xImplReg(
191*cdf0e10cSrcweir 		xMgr->createInstance( OUString::createFromAscii("com.sun.star.registry.ImplementationRegistration") ), UNO_QUERY );
192*cdf0e10cSrcweir 	OSL_ENSURE( xImplReg.is(), "### no impl reg!" );
193*cdf0e10cSrcweir 
194*cdf0e10cSrcweir 	OUString aLibName =
195*cdf0e10cSrcweir         OUString::createFromAscii( "connector.uno" SAL_DLLEXTENSION );
196*cdf0e10cSrcweir 	xImplReg->registerImplementation(
197*cdf0e10cSrcweir 		OUString::createFromAscii("com.sun.star.loader.SharedLibrary"), aLibName, Reference< XSimpleRegistry >() );
198*cdf0e10cSrcweir 
199*cdf0e10cSrcweir 	aLibName = OUString::createFromAscii( "acceptor.uno" SAL_DLLEXTENSION );
200*cdf0e10cSrcweir 	xImplReg->registerImplementation(
201*cdf0e10cSrcweir 		OUString::createFromAscii("com.sun.star.loader.SharedLibrary"), aLibName, Reference< XSimpleRegistry >() );
202*cdf0e10cSrcweir 
203*cdf0e10cSrcweir 	Reference < XAcceptor >  rAcceptor(
204*cdf0e10cSrcweir 		xMgr->createInstance(
205*cdf0e10cSrcweir 			OUString::createFromAscii("com.sun.star.connection.Acceptor" ) ) , UNO_QUERY );
206*cdf0e10cSrcweir 
207*cdf0e10cSrcweir 	Reference < XAcceptor >  rAcceptorPipe(
208*cdf0e10cSrcweir 		xMgr->createInstance(
209*cdf0e10cSrcweir 			OUString::createFromAscii("com.sun.star.connection.Acceptor" ) ) , UNO_QUERY );
210*cdf0e10cSrcweir 
211*cdf0e10cSrcweir 	Reference < XConnector >  rConnector(
212*cdf0e10cSrcweir 		xMgr->createInstance( OUString::createFromAscii("com.sun.star.connection.Connector") ) , UNO_QUERY );
213*cdf0e10cSrcweir 
214*cdf0e10cSrcweir 
215*cdf0e10cSrcweir 	printf( "Testing sockets" );
216*cdf0e10cSrcweir 	fflush( stdout );
217*cdf0e10cSrcweir 	testConnection( OUString::createFromAscii("socket,host=localhost,port=2001"), rAcceptor , rConnector );
218*cdf0e10cSrcweir 	printf( " Done\n" );
219*cdf0e10cSrcweir 
220*cdf0e10cSrcweir 	printf( "Testing pipe" );
221*cdf0e10cSrcweir 	fflush( stdout );
222*cdf0e10cSrcweir 	testConnection( OUString::createFromAscii("pipe,name=bla") , rAcceptorPipe , rConnector );
223*cdf0e10cSrcweir 	printf( " Done\n" );
224*cdf0e10cSrcweir 
225*cdf0e10cSrcweir 	// check, if errornous strings make any problem
226*cdf0e10cSrcweir 	rAcceptor = Reference< XAcceptor > (
227*cdf0e10cSrcweir 		xMgr->createInstance( OUString::createFromAscii( "com.sun.star.connection.Acceptor" ) ),
228*cdf0e10cSrcweir 		UNO_QUERY );
229*cdf0e10cSrcweir 
230*cdf0e10cSrcweir 	try
231*cdf0e10cSrcweir 	{
232*cdf0e10cSrcweir 		rAcceptor->accept( OUString() );
233*cdf0e10cSrcweir 		OSL_ENSURE( 0 , "empty connection string" );
234*cdf0e10cSrcweir 	}
235*cdf0e10cSrcweir 	catch( IllegalArgumentException & )
236*cdf0e10cSrcweir 	{
237*cdf0e10cSrcweir 		// everything is fine
238*cdf0e10cSrcweir 	}
239*cdf0e10cSrcweir 	catch( ... )
240*cdf0e10cSrcweir 	{
241*cdf0e10cSrcweir 		OSL_ENSURE( 0, "unexpected akexception with empty connection string" );
242*cdf0e10cSrcweir 	}
243*cdf0e10cSrcweir 
244*cdf0e10cSrcweir 	try
245*cdf0e10cSrcweir 	{
246*cdf0e10cSrcweir 		rConnector->connect( OUString() );
247*cdf0e10cSrcweir 		OSL_ENSURE( 0 , "empty connection string" );
248*cdf0e10cSrcweir 	}
249*cdf0e10cSrcweir 	catch( ConnectionSetupException & )
250*cdf0e10cSrcweir 	{
251*cdf0e10cSrcweir 		// everything is fine
252*cdf0e10cSrcweir 	}
253*cdf0e10cSrcweir 	catch( ... )
254*cdf0e10cSrcweir 	{
255*cdf0e10cSrcweir 		OSL_ENSURE( 0, "unexpected exception with empty connection string" );
256*cdf0e10cSrcweir 	}
257*cdf0e10cSrcweir 
258*cdf0e10cSrcweir 
259*cdf0e10cSrcweir 	MyThread thread( rAcceptor , OUString::createFromAscii("socket,host=localhost,port=2001") );
260*cdf0e10cSrcweir 	thread.create();
261*cdf0e10cSrcweir 
262*cdf0e10cSrcweir 	TimeValue value = {0,1};
263*cdf0e10cSrcweir 	osl_waitThread( &value );
264*cdf0e10cSrcweir 	try
265*cdf0e10cSrcweir 	{
266*cdf0e10cSrcweir 		rAcceptor->accept( OUString::createFromAscii("socket,host=localhost,port=2001") );
267*cdf0e10cSrcweir 		OSL_ENSURE( 0 , "already existing exception expected" );
268*cdf0e10cSrcweir 	}
269*cdf0e10cSrcweir 	catch( AlreadyAcceptingException & e)
270*cdf0e10cSrcweir 	{
271*cdf0e10cSrcweir 		// everything is fine
272*cdf0e10cSrcweir 	}
273*cdf0e10cSrcweir 	catch( ... )
274*cdf0e10cSrcweir 	{
275*cdf0e10cSrcweir 		OSL_ENSURE( 0, "unknown exception, already existing existing expected" );
276*cdf0e10cSrcweir 	}
277*cdf0e10cSrcweir 
278*cdf0e10cSrcweir 	rAcceptor->stopAccepting();
279*cdf0e10cSrcweir 	thread.join();
280*cdf0e10cSrcweir 
281*cdf0e10cSrcweir 	Reference < XComponent > rComp( xMgr , UNO_QUERY );
282*cdf0e10cSrcweir 	if( rComp.is() )
283*cdf0e10cSrcweir 	{
284*cdf0e10cSrcweir 		rComp->dispose();
285*cdf0e10cSrcweir 	}
286*cdf0e10cSrcweir }
287