xref: /aoo41x/main/bridges/test/testserver.cxx (revision 61dff127)
1*61dff127SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*61dff127SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*61dff127SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*61dff127SAndrew Rist  * distributed with this work for additional information
6*61dff127SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*61dff127SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*61dff127SAndrew Rist  * "License"); you may not use this file except in compliance
9*61dff127SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*61dff127SAndrew Rist  *
11*61dff127SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*61dff127SAndrew Rist  *
13*61dff127SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*61dff127SAndrew Rist  * software distributed under the License is distributed on an
15*61dff127SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*61dff127SAndrew Rist  * KIND, either express or implied.  See the License for the
17*61dff127SAndrew Rist  * specific language governing permissions and limitations
18*61dff127SAndrew Rist  * under the License.
19*61dff127SAndrew Rist  *
20*61dff127SAndrew Rist  *************************************************************/
21*61dff127SAndrew Rist 
22*61dff127SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_bridges.hxx"
26cdf0e10cSrcweir #include <string.h>
27cdf0e10cSrcweir #include <osl/time.h>
28cdf0e10cSrcweir 
29cdf0e10cSrcweir #include <osl/mutex.hxx>
30cdf0e10cSrcweir #include <osl/conditn.h>
31cdf0e10cSrcweir 
32cdf0e10cSrcweir #include <osl/thread.hxx>
33cdf0e10cSrcweir 
34cdf0e10cSrcweir #include <cppuhelper/servicefactory.hxx>
35cdf0e10cSrcweir #include <cppuhelper/implbase1.hxx>
36cdf0e10cSrcweir 
37cdf0e10cSrcweir #include <com/sun/star/connection/XAcceptor.hpp>
38cdf0e10cSrcweir #include <com/sun/star/connection/XConnection.hpp>
39cdf0e10cSrcweir 
40cdf0e10cSrcweir #include <com/sun/star/bridge/XInstanceProvider.hpp>
41cdf0e10cSrcweir #include <com/sun/star/bridge/XBridgeFactory.hpp>
42cdf0e10cSrcweir 
43cdf0e10cSrcweir #include <com/sun/star/lang/XComponent.hpp>
44cdf0e10cSrcweir #include <com/sun/star/lang/XInitialization.hpp>
45cdf0e10cSrcweir 
46cdf0e10cSrcweir 
47cdf0e10cSrcweir #include <test/XTestFactory.hpp>
48cdf0e10cSrcweir 
49cdf0e10cSrcweir #include <cppuhelper/weak.hxx>
50cdf0e10cSrcweir 
51cdf0e10cSrcweir using namespace ::test;
52cdf0e10cSrcweir using namespace ::rtl;
53cdf0e10cSrcweir using namespace ::osl;
54cdf0e10cSrcweir using namespace ::cppu;
55cdf0e10cSrcweir using namespace ::com::sun::star::uno;
56cdf0e10cSrcweir using namespace ::com::sun::star::lang;
57cdf0e10cSrcweir using namespace ::com::sun::star::bridge;
58cdf0e10cSrcweir using namespace ::com::sun::star::connection;
59cdf0e10cSrcweir #include "testcomp.h"
60cdf0e10cSrcweir #ifdef SAL_W32
61cdf0e10cSrcweir #include <conio.h>
62cdf0e10cSrcweir #endif
63cdf0e10cSrcweir 
64cdf0e10cSrcweir /*********
65cdf0e10cSrcweir  *
66cdf0e10cSrcweir  ********/
67cdf0e10cSrcweir 
68cdf0e10cSrcweir 
69cdf0e10cSrcweir 
70cdf0e10cSrcweir class MyThread :
71cdf0e10cSrcweir 	public Thread
72cdf0e10cSrcweir {
73cdf0e10cSrcweir public:
MyThread(const Reference<XAcceptor> & r,const Reference<XBridgeFactory> & rFactory,const Reference<XMultiServiceFactory> & rSMgr,const OUString & sConnectionDescription,const OUString & sProtocol,sal_Bool bReverse,sal_Bool bLatency)74cdf0e10cSrcweir 	MyThread( const Reference< XAcceptor > &r ,
75cdf0e10cSrcweir 			  const Reference< XBridgeFactory > &rFactory,
76cdf0e10cSrcweir 			  const Reference< XMultiServiceFactory > &rSMgr,
77cdf0e10cSrcweir 			  const OUString &sConnectionDescription,
78cdf0e10cSrcweir 			  const OUString &sProtocol,
79cdf0e10cSrcweir 			  sal_Bool bReverse,
80cdf0e10cSrcweir 			  sal_Bool bLatency ) :
81cdf0e10cSrcweir 		m_rAcceptor( r ),
82cdf0e10cSrcweir 		m_rBridgeFactory ( rFactory ),
83cdf0e10cSrcweir 		m_rSMgr( rSMgr ),
84cdf0e10cSrcweir 		m_sConnectionDescription( sConnectionDescription ),
85cdf0e10cSrcweir 		m_sProtocol( sProtocol ),
86cdf0e10cSrcweir 		m_bReverse( bReverse ),
87cdf0e10cSrcweir 		m_bLatency( bLatency )
88cdf0e10cSrcweir 		{}
89cdf0e10cSrcweir 	virtual void SAL_CALL run();
90cdf0e10cSrcweir 
91cdf0e10cSrcweir 	void latencyTest( const Reference< XConnection > &r );
92cdf0e10cSrcweir 
93cdf0e10cSrcweir private:
94cdf0e10cSrcweir 	Reference < XAcceptor > m_rAcceptor;
95cdf0e10cSrcweir 	Reference < XBridgeFactory > m_rBridgeFactory;
96cdf0e10cSrcweir 	Reference < XMultiServiceFactory > m_rSMgr;
97cdf0e10cSrcweir 	OUString m_sConnectionDescription;
98cdf0e10cSrcweir 	OUString m_sProtocol;
99cdf0e10cSrcweir 	sal_Bool m_bReverse;
100cdf0e10cSrcweir 	sal_Bool m_bLatency;
101cdf0e10cSrcweir };
102cdf0e10cSrcweir 
103cdf0e10cSrcweir 
latencyTest(const Reference<XConnection> & r)104cdf0e10cSrcweir void MyThread::latencyTest( const Reference< XConnection > &r )
105cdf0e10cSrcweir {
106cdf0e10cSrcweir 	Sequence < sal_Int8 > s;
107cdf0e10cSrcweir 	while( 12 == r->read( s , 12 ) )
108cdf0e10cSrcweir 	{
109cdf0e10cSrcweir 		r->read( s , 188 );
110cdf0e10cSrcweir 		s = Sequence < sal_Int8 >(60);
111cdf0e10cSrcweir 		r->write( s );
112cdf0e10cSrcweir 	}
113cdf0e10cSrcweir }
114cdf0e10cSrcweir 
run()115cdf0e10cSrcweir void MyThread::run()
116cdf0e10cSrcweir {
117cdf0e10cSrcweir 
118cdf0e10cSrcweir 	while ( sal_True )
119cdf0e10cSrcweir 	{
120cdf0e10cSrcweir 		try
121cdf0e10cSrcweir 		{
122cdf0e10cSrcweir 			Reference < XConnection > rConnection =
123cdf0e10cSrcweir 				m_rAcceptor->accept( m_sConnectionDescription );
124cdf0e10cSrcweir 
125cdf0e10cSrcweir 			if( ! rConnection.is() )
126cdf0e10cSrcweir 			{
127cdf0e10cSrcweir 				break;
128cdf0e10cSrcweir 			}
129cdf0e10cSrcweir 			if( m_bLatency )
130cdf0e10cSrcweir 			{
131cdf0e10cSrcweir 				latencyTest( rConnection );
132cdf0e10cSrcweir 			}
133cdf0e10cSrcweir 			else
134cdf0e10cSrcweir 			{
135cdf0e10cSrcweir 
136cdf0e10cSrcweir 				Reference < XBridge > rBridge =
137cdf0e10cSrcweir 					m_rBridgeFactory->createBridge(
138cdf0e10cSrcweir 						OUString() ,
139cdf0e10cSrcweir 						m_sProtocol,
140cdf0e10cSrcweir 						rConnection ,
141cdf0e10cSrcweir 						(XInstanceProvider * ) new OInstanceProvider(m_rSMgr) );
142cdf0e10cSrcweir 
143cdf0e10cSrcweir 
144cdf0e10cSrcweir 				if( m_bReverse )
145cdf0e10cSrcweir 				{
146cdf0e10cSrcweir 					printf( "doing reverse callme test (test is ok, when on each line a +- appears\n" );
147cdf0e10cSrcweir 					Reference < XInterface > r = rBridge->getInstance(
148cdf0e10cSrcweir 						OUString( RTL_CONSTASCII_USTRINGPARAM("blubber"  )));
149cdf0e10cSrcweir 					Reference < XTestFactory > rFactory( r , UNO_QUERY );
150cdf0e10cSrcweir 					Reference < XCallMe > rCallMe = rFactory->createCallMe();
151cdf0e10cSrcweir 
152cdf0e10cSrcweir 					for( sal_Int32 i = 0 ; i < 1  ; i ++ )
153cdf0e10cSrcweir 					{
154cdf0e10cSrcweir 						rCallMe->callOneway(
155cdf0e10cSrcweir 							OUString( RTL_CONSTASCII_USTRINGPARAM("my test string")) , 2 );
156cdf0e10cSrcweir 					}
157cdf0e10cSrcweir 					printf( "all oneway are send\n" );
158cdf0e10cSrcweir 					rCallMe->call( OUString::createFromAscii( "reverse call me test finished" ) , 0 );
159cdf0e10cSrcweir 				printf( "revers callme test finished\n" );
160cdf0e10cSrcweir 				}
161cdf0e10cSrcweir 			}
162cdf0e10cSrcweir 		}
163cdf0e10cSrcweir 		catch ( Exception & e )
164cdf0e10cSrcweir 		{
165cdf0e10cSrcweir 			printf( "Exception was thrown by acceptor \n" );
166cdf0e10cSrcweir 			OString o = OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US );
167cdf0e10cSrcweir 			printf( "%s\n" , o.getStr() );
168cdf0e10cSrcweir 			break;
169cdf0e10cSrcweir 		}
170cdf0e10cSrcweir 		catch ( ... )
171cdf0e10cSrcweir 		{
172cdf0e10cSrcweir 			printf( "Exception was thrown by acceptor thread\n" );
173cdf0e10cSrcweir 			break;
174cdf0e10cSrcweir 		}
175cdf0e10cSrcweir 	}
176cdf0e10cSrcweir }
177cdf0e10cSrcweir 
178cdf0e10cSrcweir 
main(int argc,char * argv[])179cdf0e10cSrcweir int main( int argc, char *argv[] )
180cdf0e10cSrcweir {
181cdf0e10cSrcweir //	testserver();
182cdf0e10cSrcweir 
183cdf0e10cSrcweir 	if( argc < 2 )
184cdf0e10cSrcweir 	{
185cdf0e10cSrcweir 		printf( "usage : testserver [-r] connectionstring\n"
186cdf0e10cSrcweir 				"        -r does a reverse test (server calls client)\n" );
187cdf0e10cSrcweir 		return 0;
188cdf0e10cSrcweir 	}
189cdf0e10cSrcweir 
190cdf0e10cSrcweir 	OUString sConnectionString;
191cdf0e10cSrcweir 	OUString sProtocol;
192cdf0e10cSrcweir 	sal_Bool bReverse = sal_False;
193cdf0e10cSrcweir 	sal_Bool bLatency = sal_False;
194cdf0e10cSrcweir 
195cdf0e10cSrcweir 	parseCommandLine( argv , &sConnectionString , &sProtocol , &bLatency , &bReverse );
196cdf0e10cSrcweir 
197cdf0e10cSrcweir 	{
198cdf0e10cSrcweir 	    Reference< XMultiServiceFactory > rSMgr = createRegistryServiceFactory(
199cdf0e10cSrcweir 			OUString( RTL_CONSTASCII_USTRINGPARAM( "server.rdb" )  ) );
200cdf0e10cSrcweir 
201cdf0e10cSrcweir 		Reference < XBridgeFactory > rBridgeFactory ( createComponent(
202cdf0e10cSrcweir 			OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.bridge.BridgeFactory")),
203cdf0e10cSrcweir 			OUString( RTL_CONSTASCII_USTRINGPARAM("bridgefac.uno" SAL_DLLEXTENSION )),
204cdf0e10cSrcweir 			rSMgr ),
205cdf0e10cSrcweir 													 UNO_QUERY );
206cdf0e10cSrcweir 
207cdf0e10cSrcweir 
208cdf0e10cSrcweir 		createComponent( OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.bridge.Bridge.iiop")),
209cdf0e10cSrcweir 						 OUString( RTL_CONSTASCII_USTRINGPARAM("remotebridge.uno" SAL_DLLEXTENSION)),
210cdf0e10cSrcweir 						 rSMgr );
211cdf0e10cSrcweir 
212cdf0e10cSrcweir 
213cdf0e10cSrcweir 		Reference < XAcceptor > rAcceptor(
214cdf0e10cSrcweir 			createComponent( OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.connection.Acceptor")),
215cdf0e10cSrcweir 							 OUString( RTL_CONSTASCII_USTRINGPARAM("acceptor.uno" SAL_DLLEXTENSION)),
216cdf0e10cSrcweir 							 rSMgr ) ,
217cdf0e10cSrcweir 			UNO_QUERY );
218cdf0e10cSrcweir 
219cdf0e10cSrcweir 		MyThread thread( rAcceptor ,
220cdf0e10cSrcweir 						 rBridgeFactory,
221cdf0e10cSrcweir 						 rSMgr,
222cdf0e10cSrcweir 						 sConnectionString,
223cdf0e10cSrcweir 						 sProtocol,
224cdf0e10cSrcweir 						 bReverse,
225cdf0e10cSrcweir 						 bLatency);
226cdf0e10cSrcweir 		thread.create();
227cdf0e10cSrcweir 
228cdf0e10cSrcweir #ifdef SAL_W32
229cdf0e10cSrcweir 		_getch();
230cdf0e10cSrcweir #elif  SOLARIS
231cdf0e10cSrcweir 		getchar();
232cdf0e10cSrcweir #elif LINUX
233cdf0e10cSrcweir 		TimeValue value={360,0};
234cdf0e10cSrcweir 		osl_waitThread( &value );
235cdf0e10cSrcweir #endif
236cdf0e10cSrcweir 		printf( "Closing...\n" );
237cdf0e10cSrcweir 
238cdf0e10cSrcweir 		rAcceptor->stopAccepting();
239cdf0e10cSrcweir 		thread.join();
240cdf0e10cSrcweir 
241cdf0e10cSrcweir 		printf( "Closed\n" );
242cdf0e10cSrcweir 
243cdf0e10cSrcweir 		Reference < XComponent > rComp2( rBridgeFactory , UNO_QUERY );
244cdf0e10cSrcweir 		rComp2->dispose();
245cdf0e10cSrcweir 		Reference < XComponent > rComp( rSMgr, UNO_QUERY );
246cdf0e10cSrcweir 		rComp->dispose();
247cdf0e10cSrcweir 	}
248cdf0e10cSrcweir     return 0;
249cdf0e10cSrcweir }
250