update dev300-m58
[ooovba.git] / bridges / test / testserver.cxx
blobf968372c25d9c9d61ef7bc6f433d0d5be3594d83
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: testserver.cxx,v $
10 * $Revision: 1.14 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_bridges.hxx"
33 #include <string.h>
34 #include <osl/time.h>
36 #include <osl/mutex.hxx>
37 #include <osl/conditn.h>
39 #include <osl/thread.hxx>
41 #include <cppuhelper/servicefactory.hxx>
42 #include <cppuhelper/implbase1.hxx>
44 #include <com/sun/star/connection/XAcceptor.hpp>
45 #include <com/sun/star/connection/XConnection.hpp>
47 #include <com/sun/star/bridge/XInstanceProvider.hpp>
48 #include <com/sun/star/bridge/XBridgeFactory.hpp>
50 #include <com/sun/star/lang/XComponent.hpp>
51 #include <com/sun/star/lang/XInitialization.hpp>
54 #include <test/XTestFactory.hpp>
56 #include <cppuhelper/weak.hxx>
58 using namespace ::test;
59 using namespace ::rtl;
60 using namespace ::osl;
61 using namespace ::cppu;
62 using namespace ::com::sun::star::uno;
63 using namespace ::com::sun::star::lang;
64 using namespace ::com::sun::star::bridge;
65 using namespace ::com::sun::star::connection;
66 #include "testcomp.h"
67 #ifdef SAL_W32
68 #include <conio.h>
69 #endif
71 /*********
73 ********/
77 class MyThread :
78 public Thread
80 public:
81 MyThread( const Reference< XAcceptor > &r ,
82 const Reference< XBridgeFactory > &rFactory,
83 const Reference< XMultiServiceFactory > &rSMgr,
84 const OUString &sConnectionDescription,
85 const OUString &sProtocol,
86 sal_Bool bReverse,
87 sal_Bool bLatency ) :
88 m_rAcceptor( r ),
89 m_rBridgeFactory ( rFactory ),
90 m_rSMgr( rSMgr ),
91 m_sConnectionDescription( sConnectionDescription ),
92 m_sProtocol( sProtocol ),
93 m_bReverse( bReverse ),
94 m_bLatency( bLatency )
96 virtual void SAL_CALL run();
98 void latencyTest( const Reference< XConnection > &r );
100 private:
101 Reference < XAcceptor > m_rAcceptor;
102 Reference < XBridgeFactory > m_rBridgeFactory;
103 Reference < XMultiServiceFactory > m_rSMgr;
104 OUString m_sConnectionDescription;
105 OUString m_sProtocol;
106 sal_Bool m_bReverse;
107 sal_Bool m_bLatency;
111 void MyThread::latencyTest( const Reference< XConnection > &r )
113 Sequence < sal_Int8 > s;
114 while( 12 == r->read( s , 12 ) )
116 r->read( s , 188 );
117 s = Sequence < sal_Int8 >(60);
118 r->write( s );
122 void MyThread::run()
125 while ( sal_True )
129 Reference < XConnection > rConnection =
130 m_rAcceptor->accept( m_sConnectionDescription );
132 if( ! rConnection.is() )
134 break;
136 if( m_bLatency )
138 latencyTest( rConnection );
140 else
143 Reference < XBridge > rBridge =
144 m_rBridgeFactory->createBridge(
145 OUString() ,
146 m_sProtocol,
147 rConnection ,
148 (XInstanceProvider * ) new OInstanceProvider(m_rSMgr) );
151 if( m_bReverse )
153 printf( "doing reverse callme test (test is ok, when on each line a +- appears\n" );
154 Reference < XInterface > r = rBridge->getInstance(
155 OUString( RTL_CONSTASCII_USTRINGPARAM("blubber" )));
156 Reference < XTestFactory > rFactory( r , UNO_QUERY );
157 Reference < XCallMe > rCallMe = rFactory->createCallMe();
159 for( sal_Int32 i = 0 ; i < 1 ; i ++ )
161 rCallMe->callOneway(
162 OUString( RTL_CONSTASCII_USTRINGPARAM("my test string")) , 2 );
164 printf( "all oneway are send\n" );
165 rCallMe->call( OUString::createFromAscii( "reverse call me test finished" ) , 0 );
166 printf( "revers callme test finished\n" );
170 catch ( Exception & e )
172 printf( "Exception was thrown by acceptor \n" );
173 OString o = OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US );
174 printf( "%s\n" , o.getStr() );
175 break;
177 catch ( ... )
179 printf( "Exception was thrown by acceptor thread\n" );
180 break;
186 int main( int argc, char *argv[] )
188 // testserver();
190 if( argc < 2 )
192 printf( "usage : testserver [-r] connectionstring\n"
193 " -r does a reverse test (server calls client)\n" );
194 return 0;
197 OUString sConnectionString;
198 OUString sProtocol;
199 sal_Bool bReverse = sal_False;
200 sal_Bool bLatency = sal_False;
202 parseCommandLine( argv , &sConnectionString , &sProtocol , &bLatency , &bReverse );
205 Reference< XMultiServiceFactory > rSMgr = createRegistryServiceFactory(
206 OUString( RTL_CONSTASCII_USTRINGPARAM( "server.rdb" ) ) );
208 Reference < XBridgeFactory > rBridgeFactory ( createComponent(
209 OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.bridge.BridgeFactory")),
210 OUString( RTL_CONSTASCII_USTRINGPARAM("bridgefac.uno" SAL_DLLEXTENSION )),
211 rSMgr ),
212 UNO_QUERY );
215 createComponent( OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.bridge.Bridge.iiop")),
216 OUString( RTL_CONSTASCII_USTRINGPARAM("remotebridge.uno" SAL_DLLEXTENSION)),
217 rSMgr );
220 Reference < XAcceptor > rAcceptor(
221 createComponent( OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.connection.Acceptor")),
222 OUString( RTL_CONSTASCII_USTRINGPARAM("acceptor.uno" SAL_DLLEXTENSION)),
223 rSMgr ) ,
224 UNO_QUERY );
226 MyThread thread( rAcceptor ,
227 rBridgeFactory,
228 rSMgr,
229 sConnectionString,
230 sProtocol,
231 bReverse,
232 bLatency);
233 thread.create();
235 #ifdef SAL_W32
236 _getch();
237 #elif SOLARIS
238 getchar();
239 #elif LINUX
240 TimeValue value={360,0};
241 osl_waitThread( &value );
242 #endif
243 printf( "Closing...\n" );
245 rAcceptor->stopAccepting();
246 thread.join();
248 printf( "Closed\n" );
250 Reference < XComponent > rComp2( rBridgeFactory , UNO_QUERY );
251 rComp2->dispose();
252 Reference < XComponent > rComp( rSMgr, UNO_QUERY );
253 rComp->dispose();
255 return 0;