Branch libreoffice-5-0-4
[LibreOffice.git] / bridges / test / testserver.cxx
blob68cc3b69b40dba94354514cd964afc45c0e33c6d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
20 #include <string.h>
21 #include <osl/time.h>
23 #include <osl/mutex.hxx>
24 #include <osl/conditn.h>
26 #include <osl/thread.hxx>
28 #include <cppuhelper/servicefactory.hxx>
29 #include <cppuhelper/implbase1.hxx>
31 #include <com/sun/star/connection/XAcceptor.hpp>
32 #include <com/sun/star/connection/XConnection.hpp>
34 #include <com/sun/star/bridge/XInstanceProvider.hpp>
35 #include <com/sun/star/bridge/XBridgeFactory.hpp>
37 #include <com/sun/star/lang/XComponent.hpp>
38 #include <com/sun/star/lang/XInitialization.hpp>
41 #include <test/XTestFactory.hpp>
43 #include <cppuhelper/weak.hxx>
45 using namespace ::test;
46 using namespace ::osl;
47 using namespace ::cppu;
48 using namespace ::com::sun::star::uno;
49 using namespace ::com::sun::star::lang;
50 using namespace ::com::sun::star::bridge;
51 using namespace ::com::sun::star::connection;
52 #include "testcomp.h"
53 #ifdef SAL_W32
54 #include <conio.h>
55 #endif
57 /*********
59 ********/
63 class MyThread :
64 public Thread
66 public:
67 MyThread( const Reference< XAcceptor > &r ,
68 const Reference< XBridgeFactory > &rFactory,
69 const Reference< XMultiServiceFactory > &rSMgr,
70 const OUString &sConnectionDescription,
71 const OUString &sProtocol,
72 sal_Bool bReverse,
73 sal_Bool bLatency ) :
74 m_rAcceptor( r ),
75 m_rBridgeFactory ( rFactory ),
76 m_rSMgr( rSMgr ),
77 m_sConnectionDescription( sConnectionDescription ),
78 m_sProtocol( sProtocol ),
79 m_bReverse( bReverse ),
80 m_bLatency( bLatency )
82 virtual void SAL_CALL run();
84 void latencyTest( const Reference< XConnection > &r );
86 private:
87 Reference < XAcceptor > m_rAcceptor;
88 Reference < XBridgeFactory > m_rBridgeFactory;
89 Reference < XMultiServiceFactory > m_rSMgr;
90 OUString m_sConnectionDescription;
91 OUString m_sProtocol;
92 sal_Bool m_bReverse;
93 sal_Bool m_bLatency;
97 void MyThread::latencyTest( const Reference< XConnection > &r )
99 Sequence < sal_Int8 > s;
100 while( 12 == r->read( s , 12 ) )
102 r->read( s , 188 );
103 s = Sequence < sal_Int8 >(60);
104 r->write( s );
108 void MyThread::run()
111 while ( true )
115 Reference < XConnection > rConnection =
116 m_rAcceptor->accept( m_sConnectionDescription );
118 if( ! rConnection.is() )
120 break;
122 if( m_bLatency )
124 latencyTest( rConnection );
126 else
129 Reference < XBridge > rBridge =
130 m_rBridgeFactory->createBridge(
131 OUString() ,
132 m_sProtocol,
133 rConnection ,
134 (XInstanceProvider * ) new OInstanceProvider(m_rSMgr) );
137 if( m_bReverse )
139 printf( "doing reverse callme test (test is ok, when on each line a +- appears\n" );
140 Reference < XInterface > r = rBridge->getInstance(
141 OUString("blubber" ));
142 Reference < XTestFactory > rFactory( r , UNO_QUERY );
143 Reference < XCallMe > rCallMe = rFactory->createCallMe();
145 for( sal_Int32 i = 0 ; i < 1 ; i ++ )
147 rCallMe->callOneway(
148 OUString("my test string") , 2 );
150 printf( "all oneway are send\n" );
151 rCallMe->call( OUString( "reverse call me test finished" ) , 0 );
152 printf( "revers callme test finished\n" );
156 catch ( Exception & e )
158 printf( "Exception was thrown by acceptor \n" );
159 OString o = OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US );
160 printf( "%s\n" , o.getStr() );
161 break;
163 catch ( ... )
165 printf( "Exception was thrown by acceptor thread\n" );
166 break;
172 int main( int argc, char *argv[] )
174 // testserver();
176 if( argc < 2 )
178 printf( "usage : testserver [-r] connectionstring\n"
179 " -r does a reverse test (server calls client)\n" );
180 return 0;
183 OUString sConnectionString;
184 OUString sProtocol;
185 sal_Bool bReverse = sal_False;
186 sal_Bool bLatency = sal_False;
188 parseCommandLine( argv , &sConnectionString , &sProtocol , &bLatency , &bReverse );
191 Reference< XMultiServiceFactory > rSMgr = createRegistryServiceFactory(
192 OUString( "server.rdb" ) );
194 Reference < XBridgeFactory > rBridgeFactory ( createComponent(
195 OUString("com.sun.star.bridge.BridgeFactory"),
196 OUString( "bridgefac.uno" SAL_DLLEXTENSION ),
197 rSMgr ),
198 UNO_QUERY );
201 createComponent( OUString("com.sun.star.bridge.Bridge.iiop"),
202 OUString( "remotebridge.uno" SAL_DLLEXTENSION),
203 rSMgr );
206 Reference < XAcceptor > rAcceptor(
207 createComponent( OUString("com.sun.star.connection.Acceptor"),
208 OUString( "acceptor.uno" SAL_DLLEXTENSION),
209 rSMgr ) ,
210 UNO_QUERY );
212 MyThread thread( rAcceptor ,
213 rBridgeFactory,
214 rSMgr,
215 sConnectionString,
216 sProtocol,
217 bReverse,
218 bLatency);
219 thread.create();
221 #ifdef SAL_W32
222 _getch();
223 #elif SOLARIS
224 getchar();
225 #elif LINUX
226 TimeValue value={360,0};
227 osl_waitThread( &value );
228 #endif
229 printf( "Closing...\n" );
231 rAcceptor->stopAccepting();
232 thread.join();
234 printf( "Closed\n" );
236 Reference < XComponent > rComp2( rBridgeFactory , UNO_QUERY );
237 rComp2->dispose();
238 Reference < XComponent > rComp( rSMgr, UNO_QUERY );
239 rComp->dispose();
241 return 0;
244 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */