1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 #include <stdio.h>
21 #include <osl/time.h>
22 
23 #include <osl/diagnose.h>
24 #include <osl/thread.hxx>
25 
26 #include <cppuhelper/servicefactory.hxx>
27 
28 #include <com/sun/star/lang/XComponent.hpp>
29 
30 #include <com/sun/star/registry/XImplementationRegistration.hpp>
31 
32 #include <com/sun/star/connection/XConnector.hpp>
33 #include <com/sun/star/connection/XAcceptor.hpp>
34 
35 using namespace ::osl;
36 using namespace ::cppu;
37 using namespace ::com::sun::star::uno;
38 using namespace ::com::sun::star::io;
39 using namespace ::com::sun::star::lang;
40 using namespace ::com::sun::star::registry;
41 using namespace ::com::sun::star::connection;
42 
43 
44 class MyThread :
45     public Thread
46 {
47 public:
MyThread(const Reference<XAcceptor> & r,const OUString & sConnectionDescription)48     MyThread( const Reference< XAcceptor > &r , const OUString & sConnectionDescription) :
49         m_rAcceptor( r ),
50         m_sConnectionDescription( sConnectionDescription )
51         {}
52     virtual void SAL_CALL run();
53 
54     Reference < XAcceptor > m_rAcceptor;
55 private:
56     Reference < XConnection > m_rConnection;
57     OUString m_sConnectionDescription;
58 };
59 
doWrite(const Reference<XConnection> & r)60 void doWrite( const Reference < XConnection > &r )
61 {
62     Sequence < sal_Int8 > seq(10);
63     for( sal_Int32 i = 0 ; i < 10 ; i ++ )
64     {
65         seq.getArray()[i] = i;
66     }
67 
68     r->write( seq );
69 }
70 
doRead(const Reference<XConnection> & r)71 void doRead( const Reference < XConnection > &r )
72 {
73     Sequence < sal_Int8 > seq(10);
74 
75     OSL_ASSERT( 10 == r->read( seq , 10 ) );
76 
77     for( sal_Int32 i = 0 ; i < 10 ; i ++ )
78     {
79         OSL_ASSERT( seq.getConstArray()[i] == i );
80     }
81 }
82 
83 
run()84 void MyThread::run()
85 {
86     try
87     {
88         m_rConnection = m_rAcceptor->accept( m_sConnectionDescription );
89     }
90     catch ( const Exception &e)
91     {
92         OString tmp= OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US );
93         printf( "Exception was thrown by acceptor thread: %s\n", tmp.getStr() );
94     }
95 
96     if( m_rConnection.is() )
97     {
98         Sequence < sal_Int8 > seq(12);
99         try
100         {
101             doWrite( m_rConnection );
102             doRead( m_rConnection );
103         }
104         catch (... )
105         {
106             printf( "unknown exception was thrown\n" );
107             throw;
108         }
109     }
110 
111 }
112 
113 
testConnection(const OUString & sConnectionDescription,const Reference<XAcceptor> & rAcceptor,const Reference<XConnector> & rConnector)114 void testConnection( const OUString &sConnectionDescription  ,
115                      const Reference < XAcceptor > &rAcceptor,
116                      const Reference < XConnector > &rConnector )
117 {
118     {
119         MyThread thread( rAcceptor , sConnectionDescription );
120         thread.create();
121 
122         sal_Bool bGotit = sal_False;
123         Reference < XConnection > r;
124 
125         while( ! bGotit )
126         {
127             try
128             {
129                 // Why is this wait necessary ????
130                 osl::Thread::wait(std::chrono::seconds(1));
131                 r = rConnector->connect( sConnectionDescription );
132                 OSL_ASSERT( r.is() );
133                 doWrite( r );
134                 doRead( r );
135                 bGotit = sal_True;
136             }
137             catch( ... )
138             {
139                 printf( "Couldn't connect, retrying ...\n" );
140 
141             }
142         }
143 
144         r->close();
145 
146         try
147         {
148             Sequence < sal_Int8 > seq(10);
149             r->write( seq );
150             OSL_FAIL( "expected exception not thrown" );
151         }
152         catch ( IOException & )
153         {
154             // everything is ok
155         }
156         catch ( ... )
157         {
158             OSL_FAIL( "wrong exception was thrown" );
159         }
160 
161         thread.join();
162     }
163 }
164 
165 
main(int argc,char * argv[])166 int SAL_CALL main( int argc, char * argv[] )
167 {
168     Reference< XMultiServiceFactory > xMgr(
169         createRegistryServiceFactory( OUString( "applicat.rdb") ) );
170 
171     Reference< XImplementationRegistration > xImplReg(
172         xMgr->createInstance("com.sun.star.registry.ImplementationRegistration"), UNO_QUERY );
173     OSL_ENSURE( xImplReg.is(), "### no impl reg!" );
174 
175     OUString aLibName = "connector.uno" SAL_DLLEXTENSION;
176     xImplReg->registerImplementation(
177         OUString("com.sun.star.loader.SharedLibrary"), aLibName, Reference< XSimpleRegistry >() );
178 
179     aLibName = "acceptor.uno" SAL_DLLEXTENSION;
180     xImplReg->registerImplementation(
181         OUString("com.sun.star.loader.SharedLibrary"), aLibName, Reference< XSimpleRegistry >() );
182 
183     Reference < XAcceptor >  rAcceptor(
184         xMgr->createInstance( "com.sun.star.connection.Acceptor" ) , UNO_QUERY );
185 
186     Reference < XAcceptor >  rAcceptorPipe(
187         xMgr->createInstance( "com.sun.star.connection.Acceptor" ) , UNO_QUERY );
188 
189     Reference < XConnector >  rConnector(
190         xMgr->createInstance("com.sun.star.connection.Connector") , UNO_QUERY );
191 
192 
193     printf( "Testing sockets" );
194     fflush( stdout );
195     testConnection( OUString("socket,host=localhost,port=2001"), rAcceptor , rConnector );
196     printf( " Done\n" );
197 
198     printf( "Testing pipe" );
199     fflush( stdout );
200     testConnection( OUString("pipe,name=bla") , rAcceptorPipe , rConnector );
201     printf( " Done\n" );
202 
203     // check, if erroneous strings make any problem
204     rAcceptor.set(
205         xMgr->createInstance("com.sun.star.connection.Acceptor"),
206         UNO_QUERY );
207 
208     try
209     {
210         rAcceptor->accept( OUString() );
211         OSL_FAIL( "empty connection string" );
212     }
213     catch( IllegalArgumentException & )
214     {
215         // everything is fine
216     }
217     catch( ... )
218     {
219         OSL_FAIL( "unexpected akexception with empty connection string" );
220     }
221 
222     try
223     {
224         rConnector->connect( OUString() );
225         OSL_FAIL( "empty connection string" );
226     }
227     catch( ConnectionSetupException & )
228     {
229         // everything is fine
230     }
231     catch( ... )
232     {
233         OSL_FAIL( "unexpected exception with empty connection string" );
234     }
235 
236 
237     MyThread thread( rAcceptor , OUString("socket,host=localhost,port=2001") );
238     thread.create();
239 
240     osl::Thread::wait(std::chrono::nanoseconds(1));
241     try
242     {
243         rAcceptor->accept( OUString("socket,host=localhost,port=2001") );
244         OSL_FAIL( "already existing exception expected" );
245     }
246     catch( AlreadyAcceptingException & )
247     {
248         // everything is fine
249     }
250     catch( ... )
251     {
252         OSL_FAIL( "unknown exception, already existing exception expected" );
253     }
254 
255     rAcceptor->stopAccepting();
256     thread.join();
257 
258     Reference < XComponent > rComp( xMgr , UNO_QUERY );
259     if( rComp.is() )
260     {
261         rComp->dispose();
262     }
263 }
264 
265 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
266