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