1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_bridges.hxx"
30*cdf0e10cSrcweir #include <osl/time.h>
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir #include <osl/mutex.hxx>
33*cdf0e10cSrcweir #include <osl/thread.hxx>
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir #include <cppuhelper/servicefactory.hxx>
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir #include <com/sun/star/bridge/XBridgeFactory.hpp>
38*cdf0e10cSrcweir #include <com/sun/star/connection/XAcceptor.hpp>
39*cdf0e10cSrcweir #include <com/sun/star/connection/XConnector.hpp>
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir #include <com/sun/star/lang/XComponent.hpp>
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir #include <cppuhelper/weak.hxx>
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir #include <test/XTestFactory.hpp>
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir using namespace ::test;
49*cdf0e10cSrcweir using namespace ::rtl;
50*cdf0e10cSrcweir using namespace ::cppu;
51*cdf0e10cSrcweir using namespace ::osl;
52*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
53*cdf0e10cSrcweir using namespace ::com::sun::star::lang;
54*cdf0e10cSrcweir using namespace ::com::sun::star::bridge;
55*cdf0e10cSrcweir using namespace ::com::sun::star::connection;
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir #ifdef SAL_W32
58*cdf0e10cSrcweir #include <conio.h>
59*cdf0e10cSrcweir #endif
60*cdf0e10cSrcweir 
61*cdf0e10cSrcweir #include "testcomp.h"
62*cdf0e10cSrcweir #include "osl/mutex.h"
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir /*********
65*cdf0e10cSrcweir  *
66*cdf0e10cSrcweir  ********/
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir class MyThread :
69*cdf0e10cSrcweir 	public Thread
70*cdf0e10cSrcweir {
71*cdf0e10cSrcweir public:
72*cdf0e10cSrcweir 	MyThread( const Reference< XAcceptor > &r ,
73*cdf0e10cSrcweir 			  const Reference< XBridgeFactory > &rFactory,
74*cdf0e10cSrcweir 			  const OUString &sConnectionDescription) :
75*cdf0e10cSrcweir 		m_rAcceptor( r ),
76*cdf0e10cSrcweir 		m_rBridgeFactory ( rFactory ),
77*cdf0e10cSrcweir 		m_sConnectionDescription( sConnectionDescription )
78*cdf0e10cSrcweir 		{}
79*cdf0e10cSrcweir 	virtual void SAL_CALL run();
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir private:
82*cdf0e10cSrcweir 	Reference < XAcceptor > m_rAcceptor;
83*cdf0e10cSrcweir 	Reference < XBridgeFactory > m_rBridgeFactory;
84*cdf0e10cSrcweir 	OUString m_sConnectionDescription;
85*cdf0e10cSrcweir };
86*cdf0e10cSrcweir 
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir 
89*cdf0e10cSrcweir void MyThread::run()
90*cdf0e10cSrcweir {
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir 	while ( sal_True )
93*cdf0e10cSrcweir 	{
94*cdf0e10cSrcweir 		try
95*cdf0e10cSrcweir 		{
96*cdf0e10cSrcweir 			Reference < XConnection > rConnection =
97*cdf0e10cSrcweir 				m_rAcceptor->accept( m_sConnectionDescription );
98*cdf0e10cSrcweir 
99*cdf0e10cSrcweir 			if( ! rConnection.is() )
100*cdf0e10cSrcweir 			{
101*cdf0e10cSrcweir 				break;
102*cdf0e10cSrcweir 			}
103*cdf0e10cSrcweir 
104*cdf0e10cSrcweir 			Reference < XBridge > rBridge =
105*cdf0e10cSrcweir 				m_rBridgeFactory->createBridge(
106*cdf0e10cSrcweir 					OUString() ,
107*cdf0e10cSrcweir 					OUString( RTL_CONSTASCII_USTRINGPARAM("iiop")) ,
108*cdf0e10cSrcweir 					rConnection ,
109*cdf0e10cSrcweir 					(XInstanceProvider * ) new OInstanceProvider );
110*cdf0e10cSrcweir 
111*cdf0e10cSrcweir 
112*cdf0e10cSrcweir 		}
113*cdf0e10cSrcweir 		catch ( ... )
114*cdf0e10cSrcweir 		{
115*cdf0e10cSrcweir 			printf( "Exception was thrown by acceptor thread\n" );
116*cdf0e10cSrcweir 			break;
117*cdf0e10cSrcweir 		}
118*cdf0e10cSrcweir 	}
119*cdf0e10cSrcweir }
120*cdf0e10cSrcweir 
121*cdf0e10cSrcweir 
122*cdf0e10cSrcweir int main( int argc, char *argv[] )
123*cdf0e10cSrcweir {
124*cdf0e10cSrcweir 	if( argc < 2 )
125*cdf0e10cSrcweir 	{
126*cdf0e10cSrcweir 		printf( "usage : testsamprocess host:port\n" );
127*cdf0e10cSrcweir 		return 0;
128*cdf0e10cSrcweir 	}
129*cdf0e10cSrcweir 
130*cdf0e10cSrcweir 	{
131*cdf0e10cSrcweir 	    Reference< XMultiServiceFactory > rSMgr = createRegistryServiceFactory(
132*cdf0e10cSrcweir 			OUString( RTL_CONSTASCII_USTRINGPARAM( "client.rdb" )  ) );
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir 		Reference < XConnector > rConnector(
135*cdf0e10cSrcweir 			createComponent( OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.connection.Connector")),
136*cdf0e10cSrcweir 							 OUString( RTL_CONSTASCII_USTRINGPARAM("connector.uno" SAL_DLLEXTENSION)),
137*cdf0e10cSrcweir 							 rSMgr ),
138*cdf0e10cSrcweir 			UNO_QUERY );
139*cdf0e10cSrcweir 
140*cdf0e10cSrcweir 		Reference < XAcceptor > rAcceptor(
141*cdf0e10cSrcweir 			createComponent( OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.connection.Acceptor")),
142*cdf0e10cSrcweir 							 OUString( RTL_CONSTASCII_USTRINGPARAM("acceptor.uno" SAL_DLLEXTENSION)),
143*cdf0e10cSrcweir 							 rSMgr ),
144*cdf0e10cSrcweir 			UNO_QUERY );
145*cdf0e10cSrcweir 
146*cdf0e10cSrcweir 		// just ensure that it is registered
147*cdf0e10cSrcweir // 		createComponent( OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.bridge.Bridge.iiop")),
148*cdf0e10cSrcweir // 						 OUString( RTL_CONSTASCII_USTRINGPARAM("iiopbrdg" SAL_DLLEXTENSION)),
149*cdf0e10cSrcweir // 						 rSMgr );
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir 		Reference < XBridgeFactory > rFactory(
152*cdf0e10cSrcweir 			createComponent( OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.bridge.BridgeFactory")),
153*cdf0e10cSrcweir 							 OUString( RTL_CONSTASCII_USTRINGPARAM("bridgefac.uno" SAL_DLLEXTENSION)),
154*cdf0e10cSrcweir 							 rSMgr ),
155*cdf0e10cSrcweir 			UNO_QUERY );
156*cdf0e10cSrcweir 
157*cdf0e10cSrcweir 
158*cdf0e10cSrcweir 		MyThread threadAcceptor( rAcceptor , rFactory , OUString::createFromAscii( argv[1] ) );
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir 		threadAcceptor.create();
161*cdf0e10cSrcweir 		TimeValue value={2,0};
162*cdf0e10cSrcweir 		osl_waitThread( &value );
163*cdf0e10cSrcweir 
164*cdf0e10cSrcweir 		try
165*cdf0e10cSrcweir 		{
166*cdf0e10cSrcweir 			Reference < XConnection > rConnection =
167*cdf0e10cSrcweir 				rConnector->connect( OUString::createFromAscii( argv[1] ) );
168*cdf0e10cSrcweir 
169*cdf0e10cSrcweir 			printf( "%s\n" , OUStringToOString( rConnection->getDescription(),
170*cdf0e10cSrcweir 												RTL_TEXTENCODING_ASCII_US ).pData->buffer );
171*cdf0e10cSrcweir 
172*cdf0e10cSrcweir 			if( rFactory.is() )
173*cdf0e10cSrcweir 			{
174*cdf0e10cSrcweir 
175*cdf0e10cSrcweir 				Reference < XBridge > rBridge = rFactory->createBridge(
176*cdf0e10cSrcweir 					OUString( RTL_CONSTASCII_USTRINGPARAM("bla blub")),
177*cdf0e10cSrcweir 					OUString( RTL_CONSTASCII_USTRINGPARAM("iiop")),
178*cdf0e10cSrcweir 					rConnection,
179*cdf0e10cSrcweir 					Reference < XInstanceProvider > () );
180*cdf0e10cSrcweir 
181*cdf0e10cSrcweir 				Reference < XInterface > rInitialObject
182*cdf0e10cSrcweir 					= rBridge->getInstance( OUString( RTL_CONSTASCII_USTRINGPARAM("bla")) );
183*cdf0e10cSrcweir 
184*cdf0e10cSrcweir 				if( rInitialObject.is() )
185*cdf0e10cSrcweir 				{
186*cdf0e10cSrcweir 					printf( "got the remote object\n" );
187*cdf0e10cSrcweir 				    testRemote( rInitialObject );
188*cdf0e10cSrcweir 				}
189*cdf0e10cSrcweir 				printf( "Closing...\n" );
190*cdf0e10cSrcweir 				TimeValue timeValue={2,0};
191*cdf0e10cSrcweir 				osl_waitThread( &timeValue );
192*cdf0e10cSrcweir 			}
193*cdf0e10cSrcweir 
194*cdf0e10cSrcweir 			Reference < XBridge > rBridge = rFactory->getBridge(
195*cdf0e10cSrcweir 				OUString( RTL_CONSTASCII_USTRINGPARAM("bla blub")) );
196*cdf0e10cSrcweir 			OSL_ASSERT( ! rBridge.is() );
197*cdf0e10cSrcweir 
198*cdf0e10cSrcweir 		}
199*cdf0e10cSrcweir 		catch( Exception & )
200*cdf0e10cSrcweir 		{
201*cdf0e10cSrcweir 			printf( "Login failed, got an Exception !\n" );
202*cdf0e10cSrcweir 		}
203*cdf0e10cSrcweir 
204*cdf0e10cSrcweir 		rAcceptor->stopAccepting();
205*cdf0e10cSrcweir 		threadAcceptor.join();
206*cdf0e10cSrcweir 
207*cdf0e10cSrcweir 		Reference < XComponent > rComp( rFactory , UNO_QUERY );
208*cdf0e10cSrcweir 		rComp->dispose();
209*cdf0e10cSrcweir 
210*cdf0e10cSrcweir 
211*cdf0e10cSrcweir 		rComp = Reference < XComponent > ( rSMgr , UNO_QUERY );
212*cdf0e10cSrcweir 		rComp->dispose();
213*cdf0e10cSrcweir 	}
214*cdf0e10cSrcweir     return 0;
215*cdf0e10cSrcweir }
216