xref: /aoo42x/main/bridges/test/testclient.cxx (revision 61dff127)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_bridges.hxx"
26 #include <string.h>
27 
28 #include <osl/time.h>
29 
30 #include <osl/mutex.hxx>
31 #include <osl/module.h>
32 #include <osl/thread.h>
33 #include <osl/conditn.h>
34 #include <osl/diagnose.h>
35 
36 #include <uno/mapping.hxx>
37 
38 #include <cppuhelper/servicefactory.hxx>
39 
40 #include <com/sun/star/connection/XConnector.hpp>
41 
42 #include <com/sun/star/bridge/XBridgeFactory.hpp>
43 
44 #include <com/sun/star/lang/XComponent.hpp>
45 #include <com/sun/star/lang/DisposedException.hpp>
46 #include <com/sun/star/lang/XMain.hpp>
47 
48 #include <com/sun/star/test/performance/XPerformanceTest.hpp>
49 
50 #include <cppuhelper/weak.hxx>
51 #include <cppuhelper/factory.hxx>
52 
53 #include <test/XTestFactory.hpp>
54 
55 
56 using namespace ::test;
57 using namespace ::rtl;
58 using namespace ::cppu;
59 using namespace ::com::sun::star::uno;
60 using namespace ::com::sun::star::lang;
61 using namespace ::com::sun::star::bridge;
62 using namespace ::com::sun::star::registry;
63 using namespace ::com::sun::star::connection;
64 using namespace ::com::sun::star::test::performance;
65 
66 #include "testcomp.h"
67 
68 
doPerformanceTest(const Reference<XPerformanceTest> &)69 void doPerformanceTest( const Reference < XPerformanceTest > & /* xBench */)
70 {
71 	printf( "not implemented\n" );
72 //  	sal_Int32 i,nLoop = 2000;
73 //  	sal_Int32 tStart, tEnd , tEnd2;
74 //  	//------------------------------------
75 //  	// oneway calls
76 //  	i = nLoop;
77 //  	tStart = GetTickCount();
78 //  	while (i--)
79 //  		xBench->async();
80 //  	tEnd = GetTickCount();
81 //    	xBench->sync();
82 //  	tEnd2 = GetTickCount();
83 //  	printf( "%d %d %d\n" , nLoop, tEnd - tStart , tEnd2 -tStart );
84 //  	// synchron calls
85 //  	i = nLoop;
86 //  	tStart = GetTickCount();
87 //  	while (i--)
88 //  		xBench->sync();
89 //  	tEnd = GetTickCount();
90 //  	printf( "%d %d \n" , nLoop, tEnd - tStart );
91 
92 }
93 
testLatency(const Reference<XConnection> & r,sal_Bool)94 void testLatency( const Reference < XConnection > &r , sal_Bool /* bReply */)
95 {
96 	sal_Int32 nLoop = 10000;
97 	TimeValue aStartTime, aEndTime;
98 	osl_getSystemTime( &aStartTime );
99 
100 	sal_Int32 i;
101 	for( i = 0 ; i < nLoop ; i++ )
102 	{
103 		Sequence< sal_Int8 >  s1( 200 );
104 		r->write( s1 );
105 		r->read( s1 , 12 );
106 		r->read( s1 , 48 );
107 	}
108 	osl_getSystemTime( &aEndTime );
109 
110 	double fStart = (double)aStartTime.Seconds + ((double)aStartTime.Nanosec / 1000000000.0);
111 	double fEnd = (double)aEndTime.Seconds + ((double)aEndTime.Nanosec / 1000000000.0);
112 
113 	printf( "System latency per call : %g\n" , (( fEnd-fStart )/2.) / ((double)(nLoop)) );
114 }
115 
main(int argc,char * argv[])116 int main( int argc, char *argv[] )
117 {
118 	if( argc < 2 )
119 	{
120 		printf(
121 			"usage : testclient [-r] connectionstring\n"
122 			"        -r reverse call me test (server calls client)"
123 			 );
124 		return 0;
125 	}
126 
127 	OUString sConnectionString;
128 	OUString sProtocol;
129 	sal_Bool bLatency = sal_False;
130 	sal_Bool bReverse = sal_False;
131 
132 	parseCommandLine( argv , &sConnectionString , &sProtocol , &bLatency , &bReverse );
133 
134 	{
135 	    Reference< XMultiServiceFactory > rSMgr = createRegistryServiceFactory(
136 			OUString( RTL_CONSTASCII_USTRINGPARAM("client.rdb")) );
137 
138 
139   		Reference < XConnector > rConnector(
140     			createComponent( OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.connection.Connector")),
141     							 OUString( RTL_CONSTASCII_USTRINGPARAM("connector.uno" SAL_DLLEXTENSION)),
142     							 rSMgr ),
143     			UNO_QUERY );
144 
145 
146     		try
147     		{
148     			Reference < XConnection > rConnection =
149     				rConnector->connect( sConnectionString );
150 
151     			printf( "%s\n" , OUStringToOString( rConnection->getDescription(),
152     												RTL_TEXTENCODING_ASCII_US ).pData->buffer );
153 
154 
155     			if( bLatency )
156     			{
157     				testLatency( rConnection , sal_False );
158     				testLatency( rConnection , sal_True );
159     			}
160     			else
161     			{
162     				// just ensure that it is registered
163     				createComponent( OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.bridge.Bridge.iiop")),
164     								 OUString( RTL_CONSTASCII_USTRINGPARAM("remotebridge.uno" SAL_DLLEXTENSION)),
165     								 rSMgr );
166 
167     				Reference < XBridgeFactory > rFactory(
168     					createComponent( OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.bridge.BridgeFactory")),
169     									 OUString( RTL_CONSTASCII_USTRINGPARAM("bridgefac.uno" SAL_DLLEXTENSION)),
170     									 rSMgr ),
171     					UNO_QUERY );
172 
173     				if( rFactory.is() )
174     				{
175 
176     					Reference < XBridge > rBridge = rFactory->createBridge(
177     						OUString( RTL_CONSTASCII_USTRINGPARAM("bla blub")),
178     						sProtocol,
179     						rConnection,
180     						new OInstanceProvider );
181     					{
182     						// test the factory
183       						Reference < XBridge > rBridge2 = rFactory->getBridge( OUString( RTL_CONSTASCII_USTRINGPARAM("bla blub")) );
184       						OSL_ASSERT( rBridge2.is() );
185       						OSL_ASSERT( rBridge2->getDescription() == rBridge->getDescription( ) );
186       						OSL_ASSERT( rBridge2->getName() == rBridge->getName() );
187       						OSL_ASSERT( rBridge2 == rBridge );
188     					}
189 
190 
191     					Reference < XInterface > rInitialObject	= rBridge->getInstance(
192     						OUString( RTL_CONSTASCII_USTRINGPARAM("bridges-testobject")) );
193 
194     					if( rInitialObject.is() )
195     					{
196     						printf( "got the remote object\n" );
197     						if( ! bReverse )
198     						{
199   //    							Reference < XComponent > rPerfTest( rInitialObject , UNO_QUERY );
200 //        							if( rPerfTest.is() )
201 //        							{
202 //  //      								doPerformanceTest( rPerfTest );
203 //        							}
204 //        							else
205 //        							{
206 									testRemote( rInitialObject );
207 //        							}
208     						}
209     					}
210 //    						Reference < XComponent > rComp( rBridge , UNO_QUERY );
211 //    						rComp->dispose();
212 
213 						rInitialObject = Reference < XInterface > ();
214     					printf( "Waiting...\n" );
215     					TimeValue value={bReverse ?1000 :2,0};
216     					osl_waitThread( &value );
217     					printf( "Closing...\n" );
218     				}
219 
220     				Reference < XBridge > rBridge = rFactory->getBridge( OUString( RTL_CONSTASCII_USTRINGPARAM("bla blub")) );
221 //      				OSL_ASSERT( ! rBridge.is() );
222     			}
223 
224     		}
225             catch( DisposedException & e )
226             {
227     			OString o = OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US );
228     			printf( "A remote object reference became invalid\n%s\n" , o.pData->buffer );
229             }
230     		catch( Exception &e )
231     		{
232     			OString o = OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US );
233     			printf( "Login failed, got an Exception !\n%s\n" , o.pData->buffer );
234     		}
235 
236 
237 		Reference < XComponent > rComp( rSMgr , UNO_QUERY );
238 		rComp->dispose();
239 	}
240 	printf( "Closed\n" );
241     return 0;
242 }
243 
244