xref: /AOO41X/main/bridges/test/testserver.cxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_bridges.hxx"
30 #include <string.h>
31 #include <osl/time.h>
32 
33 #include <osl/mutex.hxx>
34 #include <osl/conditn.h>
35 
36 #include <osl/thread.hxx>
37 
38 #include <cppuhelper/servicefactory.hxx>
39 #include <cppuhelper/implbase1.hxx>
40 
41 #include <com/sun/star/connection/XAcceptor.hpp>
42 #include <com/sun/star/connection/XConnection.hpp>
43 
44 #include <com/sun/star/bridge/XInstanceProvider.hpp>
45 #include <com/sun/star/bridge/XBridgeFactory.hpp>
46 
47 #include <com/sun/star/lang/XComponent.hpp>
48 #include <com/sun/star/lang/XInitialization.hpp>
49 
50 
51 #include <test/XTestFactory.hpp>
52 
53 #include <cppuhelper/weak.hxx>
54 
55 using namespace ::test;
56 using namespace ::rtl;
57 using namespace ::osl;
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::connection;
63 #include "testcomp.h"
64 #ifdef SAL_W32
65 #include <conio.h>
66 #endif
67 
68 /*********
69  *
70  ********/
71 
72 
73 
74 class MyThread :
75 	public Thread
76 {
77 public:
78 	MyThread( const Reference< XAcceptor > &r ,
79 			  const Reference< XBridgeFactory > &rFactory,
80 			  const Reference< XMultiServiceFactory > &rSMgr,
81 			  const OUString &sConnectionDescription,
82 			  const OUString &sProtocol,
83 			  sal_Bool bReverse,
84 			  sal_Bool bLatency ) :
85 		m_rAcceptor( r ),
86 		m_rBridgeFactory ( rFactory ),
87 		m_rSMgr( rSMgr ),
88 		m_sConnectionDescription( sConnectionDescription ),
89 		m_sProtocol( sProtocol ),
90 		m_bReverse( bReverse ),
91 		m_bLatency( bLatency )
92 		{}
93 	virtual void SAL_CALL run();
94 
95 	void latencyTest( const Reference< XConnection > &r );
96 
97 private:
98 	Reference < XAcceptor > m_rAcceptor;
99 	Reference < XBridgeFactory > m_rBridgeFactory;
100 	Reference < XMultiServiceFactory > m_rSMgr;
101 	OUString m_sConnectionDescription;
102 	OUString m_sProtocol;
103 	sal_Bool m_bReverse;
104 	sal_Bool m_bLatency;
105 };
106 
107 
108 void MyThread::latencyTest( const Reference< XConnection > &r )
109 {
110 	Sequence < sal_Int8 > s;
111 	while( 12 == r->read( s , 12 ) )
112 	{
113 		r->read( s , 188 );
114 		s = Sequence < sal_Int8 >(60);
115 		r->write( s );
116 	}
117 }
118 
119 void MyThread::run()
120 {
121 
122 	while ( sal_True )
123 	{
124 		try
125 		{
126 			Reference < XConnection > rConnection =
127 				m_rAcceptor->accept( m_sConnectionDescription );
128 
129 			if( ! rConnection.is() )
130 			{
131 				break;
132 			}
133 			if( m_bLatency )
134 			{
135 				latencyTest( rConnection );
136 			}
137 			else
138 			{
139 
140 				Reference < XBridge > rBridge =
141 					m_rBridgeFactory->createBridge(
142 						OUString() ,
143 						m_sProtocol,
144 						rConnection ,
145 						(XInstanceProvider * ) new OInstanceProvider(m_rSMgr) );
146 
147 
148 				if( m_bReverse )
149 				{
150 					printf( "doing reverse callme test (test is ok, when on each line a +- appears\n" );
151 					Reference < XInterface > r = rBridge->getInstance(
152 						OUString( RTL_CONSTASCII_USTRINGPARAM("blubber"  )));
153 					Reference < XTestFactory > rFactory( r , UNO_QUERY );
154 					Reference < XCallMe > rCallMe = rFactory->createCallMe();
155 
156 					for( sal_Int32 i = 0 ; i < 1  ; i ++ )
157 					{
158 						rCallMe->callOneway(
159 							OUString( RTL_CONSTASCII_USTRINGPARAM("my test string")) , 2 );
160 					}
161 					printf( "all oneway are send\n" );
162 					rCallMe->call( OUString::createFromAscii( "reverse call me test finished" ) , 0 );
163 				printf( "revers callme test finished\n" );
164 				}
165 			}
166 		}
167 		catch ( Exception & e )
168 		{
169 			printf( "Exception was thrown by acceptor \n" );
170 			OString o = OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US );
171 			printf( "%s\n" , o.getStr() );
172 			break;
173 		}
174 		catch ( ... )
175 		{
176 			printf( "Exception was thrown by acceptor thread\n" );
177 			break;
178 		}
179 	}
180 }
181 
182 
183 int main( int argc, char *argv[] )
184 {
185 //	testserver();
186 
187 	if( argc < 2 )
188 	{
189 		printf( "usage : testserver [-r] connectionstring\n"
190 				"        -r does a reverse test (server calls client)\n" );
191 		return 0;
192 	}
193 
194 	OUString sConnectionString;
195 	OUString sProtocol;
196 	sal_Bool bReverse = sal_False;
197 	sal_Bool bLatency = sal_False;
198 
199 	parseCommandLine( argv , &sConnectionString , &sProtocol , &bLatency , &bReverse );
200 
201 	{
202 	    Reference< XMultiServiceFactory > rSMgr = createRegistryServiceFactory(
203 			OUString( RTL_CONSTASCII_USTRINGPARAM( "server.rdb" )  ) );
204 
205 		Reference < XBridgeFactory > rBridgeFactory ( createComponent(
206 			OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.bridge.BridgeFactory")),
207 			OUString( RTL_CONSTASCII_USTRINGPARAM("bridgefac.uno" SAL_DLLEXTENSION )),
208 			rSMgr ),
209 													 UNO_QUERY );
210 
211 
212 		createComponent( OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.bridge.Bridge.iiop")),
213 						 OUString( RTL_CONSTASCII_USTRINGPARAM("remotebridge.uno" SAL_DLLEXTENSION)),
214 						 rSMgr );
215 
216 
217 		Reference < XAcceptor > rAcceptor(
218 			createComponent( OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.connection.Acceptor")),
219 							 OUString( RTL_CONSTASCII_USTRINGPARAM("acceptor.uno" SAL_DLLEXTENSION)),
220 							 rSMgr ) ,
221 			UNO_QUERY );
222 
223 		MyThread thread( rAcceptor ,
224 						 rBridgeFactory,
225 						 rSMgr,
226 						 sConnectionString,
227 						 sProtocol,
228 						 bReverse,
229 						 bLatency);
230 		thread.create();
231 
232 #ifdef SAL_W32
233 		_getch();
234 #elif  SOLARIS
235 		getchar();
236 #elif LINUX
237 		TimeValue value={360,0};
238 		osl_waitThread( &value );
239 #endif
240 		printf( "Closing...\n" );
241 
242 		rAcceptor->stopAccepting();
243 		thread.join();
244 
245 		printf( "Closed\n" );
246 
247 		Reference < XComponent > rComp2( rBridgeFactory , UNO_QUERY );
248 		rComp2->dispose();
249 		Reference < XComponent > rComp( rSMgr, UNO_QUERY );
250 		rComp->dispose();
251 	}
252     return 0;
253 }
254