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 <com/sun/star/io/IOException.hpp>
27 #include <com/sun/star/lang/IllegalArgumentException.hpp>
28 #include <com/sun/star/lang/XComponent.hpp>
29 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
30 #include <com/sun/star/uno/XComponentContext.hpp>
32 #include <com/sun/star/connection/AlreadyAcceptingException.hpp>
33 #include <com/sun/star/connection/ConnectionSetupException.hpp>
34 #include <com/sun/star/connection/XConnector.hpp>
35 #include <com/sun/star/connection/XAcceptor.hpp>
37 #include <cppuhelper/bootstrap.hxx>
39 using namespace ::osl
;
40 using namespace ::cppu
;
41 using namespace ::com::sun::star::uno
;
42 using namespace ::com::sun::star::io
;
43 using namespace ::com::sun::star::lang
;
44 using namespace ::com::sun::star::connection
;
53 MyThread( const Reference
< XAcceptor
> &r
, const OUString
& sConnectionDescription
) :
55 m_sConnectionDescription( sConnectionDescription
)
57 virtual void SAL_CALL
run();
59 Reference
< XAcceptor
> m_rAcceptor
;
61 Reference
< XConnection
> m_rConnection
;
62 OUString m_sConnectionDescription
;
65 void doWrite( const Reference
< XConnection
> &r
)
67 Sequence
< sal_Int8
> seq(10);
68 for( sal_Int32 i
= 0 ; i
< 10 ; i
++ )
70 seq
.getArray()[i
] = i
;
76 void doRead( const Reference
< XConnection
> &r
)
78 Sequence
< sal_Int8
> seq(10);
80 OSL_ASSERT( 10 == r
->read( seq
, 10 ) );
82 for( sal_Int32 i
= 0 ; i
< 10 ; i
++ )
84 OSL_ASSERT( seq
.getConstArray()[i
] == i
);
93 m_rConnection
= m_rAcceptor
->accept( m_sConnectionDescription
);
95 catch ( const Exception
&e
)
97 OString tmp
= OUStringToOString( e
.Message
, RTL_TEXTENCODING_ASCII_US
);
98 printf( "Exception was thrown by acceptor thread: %s\n", tmp
.getStr() );
101 if( m_rConnection
.is() )
105 doWrite( m_rConnection
);
106 doRead( m_rConnection
);
110 printf( "unknown exception was thrown\n" );
118 void testConnection( const OUString
&sConnectionDescription
,
119 const Reference
< XAcceptor
> &rAcceptor
,
120 const Reference
< XConnector
> &rConnector
)
122 MyThread
thread( rAcceptor
, sConnectionDescription
);
126 Reference
< XConnection
> r
;
132 // Why is this wait necessary ????
133 osl::Thread::wait(std::chrono::seconds(1));
134 r
= rConnector
->connect( sConnectionDescription
);
135 OSL_ASSERT( r
.is() );
142 printf( "Couldn't connect, retrying ...\n" );
151 Sequence
< sal_Int8
> seq(10);
153 OSL_FAIL( "expected exception not thrown" );
155 catch ( IOException
& )
161 OSL_FAIL( "wrong exception was thrown" );
172 Reference
< XMultiServiceFactory
> xMgr(
173 defaultBootstrap_InitialComponentContext()->getServiceManager(), UNO_QUERY
);
175 Reference
< XAcceptor
> rAcceptor(
176 xMgr
->createInstance( "com.sun.star.connection.Acceptor" ) , UNO_QUERY
);
178 Reference
< XAcceptor
> rAcceptorPipe(
179 xMgr
->createInstance( "com.sun.star.connection.Acceptor" ) , UNO_QUERY
);
181 Reference
< XConnector
> rConnector(
182 xMgr
->createInstance("com.sun.star.connection.Connector") , UNO_QUERY
);
185 printf( "Testing sockets" );
187 testConnection( "socket,host=localhost,port=2001", rAcceptor
, rConnector
);
190 printf( "Testing pipe" );
192 testConnection( "pipe,name=bla" , rAcceptorPipe
, rConnector
);
195 // check, if erroneous strings make any problem
197 xMgr
->createInstance("com.sun.star.connection.Acceptor"),
202 rAcceptor
->accept( OUString() );
203 OSL_FAIL( "empty connection string" );
205 catch( IllegalArgumentException
& )
207 // everything is fine
211 OSL_FAIL( "unexpected akexception with empty connection string" );
216 rConnector
->connect( OUString() );
217 OSL_FAIL( "empty connection string" );
219 catch( ConnectionSetupException
& )
221 // everything is fine
225 OSL_FAIL( "unexpected exception with empty connection string" );
229 MyThread
thread( rAcceptor
, "socket,host=localhost,port=2001" );
232 osl::Thread::wait(std::chrono::seconds(1));
235 rAcceptor
->accept( "socket,host=localhost,port=2001" );
236 OSL_FAIL( "already existing exception expected" );
238 catch( AlreadyAcceptingException
& )
240 // everything is fine
244 OSL_FAIL( "unknown exception, already existing exception expected" );
247 rAcceptor
->stopAccepting();
250 Reference
< XComponent
> rComp( xMgr
, UNO_QUERY
);
257 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */