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