xref: /AOO41X/main/odk/examples/cpp/remoteclient/remoteclient.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  *  The Contents of this file are made available subject to the terms of
4*cdf0e10cSrcweir  *  the BSD license.
5*cdf0e10cSrcweir  *
6*cdf0e10cSrcweir  *  Copyright 2000, 2010 Oracle and/or its affiliates.
7*cdf0e10cSrcweir  *  All rights reserved.
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  *  Redistribution and use in source and binary forms, with or without
10*cdf0e10cSrcweir  *  modification, are permitted provided that the following conditions
11*cdf0e10cSrcweir  *  are met:
12*cdf0e10cSrcweir  *  1. Redistributions of source code must retain the above copyright
13*cdf0e10cSrcweir  *     notice, this list of conditions and the following disclaimer.
14*cdf0e10cSrcweir  *  2. Redistributions in binary form must reproduce the above copyright
15*cdf0e10cSrcweir  *     notice, this list of conditions and the following disclaimer in the
16*cdf0e10cSrcweir  *     documentation and/or other materials provided with the distribution.
17*cdf0e10cSrcweir  *  3. Neither the name of Sun Microsystems, Inc. nor the names of its
18*cdf0e10cSrcweir  *     contributors may be used to endorse or promote products derived
19*cdf0e10cSrcweir  *     from this software without specific prior written permission.
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22*cdf0e10cSrcweir  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23*cdf0e10cSrcweir  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24*cdf0e10cSrcweir  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25*cdf0e10cSrcweir  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26*cdf0e10cSrcweir  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27*cdf0e10cSrcweir  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28*cdf0e10cSrcweir  *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29*cdf0e10cSrcweir  *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30*cdf0e10cSrcweir  *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31*cdf0e10cSrcweir  *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*cdf0e10cSrcweir  *
33*cdf0e10cSrcweir  *************************************************************************/
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir #include <stdio.h>
36*cdf0e10cSrcweir #include <osl/mutex.hxx>
37*cdf0e10cSrcweir #include <cppuhelper/factory.hxx>
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir #include <com/sun/star/uno/XNamingService.hpp>
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir #include <com/sun/star/registry/XImplementationRegistration.hpp>
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir #include <com/sun/star/connection/XConnector.hpp>
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir #include <com/sun/star/bridge/XUnoUrlResolver.hpp>
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir #include <com/sun/star/lang/XMain.hpp>
48*cdf0e10cSrcweir #include <com/sun/star/lang/XComponent.hpp>
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir #include <com/sun/star/io/XOutputStream.hpp>
51*cdf0e10cSrcweir #include <com/sun/star/io/XInputStream.hpp>
52*cdf0e10cSrcweir 
53*cdf0e10cSrcweir #include <cppuhelper/implbase1.hxx>
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir using namespace ::rtl;
56*cdf0e10cSrcweir using namespace ::cppu;
57*cdf0e10cSrcweir using namespace ::osl;
58*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
59*cdf0e10cSrcweir using namespace ::com::sun::star::lang;
60*cdf0e10cSrcweir using namespace ::com::sun::star::registry;
61*cdf0e10cSrcweir using namespace ::com::sun::star::connection;
62*cdf0e10cSrcweir using namespace ::com::sun::star::bridge;
63*cdf0e10cSrcweir using namespace ::com::sun::star::io;
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir namespace remotebridges_officeclient {
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir class PipeClientMain : public WeakImplHelper1< XMain >
69*cdf0e10cSrcweir {
70*cdf0e10cSrcweir public:
71*cdf0e10cSrcweir 	PipeClientMain( const Reference< XMultiServiceFactory > &r ) :
72*cdf0e10cSrcweir 		m_xSMgr( r )
73*cdf0e10cSrcweir 		{}
74*cdf0e10cSrcweir public:     // Methods
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir     virtual sal_Int32 SAL_CALL run( const Sequence< OUString >& aArguments )
78*cdf0e10cSrcweir 		throw(RuntimeException);
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir 
81*cdf0e10cSrcweir private: // helper methods
82*cdf0e10cSrcweir 	void testPipe( const Reference < XInterface > & rComponent );
83*cdf0e10cSrcweir 	Reference< XMultiServiceFactory > m_xSMgr;
84*cdf0e10cSrcweir };
85*cdf0e10cSrcweir 
86*cdf0e10cSrcweir void PipeClientMain::testPipe( const Reference< XInterface > & rxInterface )
87*cdf0e10cSrcweir {
88*cdf0e10cSrcweir 	// ask for the input stream
89*cdf0e10cSrcweir 	Reference< XInputStream > rInputStream( rxInterface, UNO_QUERY );
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir 	// ask for the output stream
92*cdf0e10cSrcweir 	Reference< XOutputStream > rOutputStream( rxInterface, UNO_QUERY );
93*cdf0e10cSrcweir 
94*cdf0e10cSrcweir 	if( rInputStream.is() && rOutputStream.is() )
95*cdf0e10cSrcweir 	{
96*cdf0e10cSrcweir 		printf( "got inputstream and outputstream from remote process\n" );
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir 		sal_Int8 a[] = { 5,4,3,2,1,0 };
99*cdf0e10cSrcweir 		Sequence< sal_Int8 > seq( a , 6 );
100*cdf0e10cSrcweir 		rOutputStream->writeBytes( seq );
101*cdf0e10cSrcweir 		rOutputStream->closeOutput();
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir 		Sequence< sal_Int8> seqRead;
104*cdf0e10cSrcweir 		if( rInputStream->readBytes( seqRead ,3 ) != 3 )
105*cdf0e10cSrcweir 		{
106*cdf0e10cSrcweir 			printf( "error : Couldn't read the expected number of bytes\n" );
107*cdf0e10cSrcweir 			return;
108*cdf0e10cSrcweir 		}
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir 		if( seqRead.getConstArray()[0] != 5 ||
111*cdf0e10cSrcweir 			seqRead.getConstArray()[1] != 4 ||
112*cdf0e10cSrcweir 			seqRead.getConstArray()[2] != 3 )
113*cdf0e10cSrcweir 		{
114*cdf0e10cSrcweir 			printf( "error : The array doesn't contain the expected values\n" );
115*cdf0e10cSrcweir 			return;
116*cdf0e10cSrcweir 		}
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir 		// try to read more bytes than written
119*cdf0e10cSrcweir 		if( rInputStream->readBytes( seqRead , 4 ) != 3 )
120*cdf0e10cSrcweir 		{
121*cdf0e10cSrcweir 			printf( "error : Got an unexpected number of bytes\n" );
122*cdf0e10cSrcweir 			return;
123*cdf0e10cSrcweir 		}
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir 		if( seqRead.getConstArray()[0] != 2 ||
126*cdf0e10cSrcweir 			seqRead.getConstArray()[1] != 1 ||
127*cdf0e10cSrcweir 			seqRead.getConstArray()[2] != 0 )
128*cdf0e10cSrcweir 		{
129*cdf0e10cSrcweir 			printf( "error : The array doesn't contain the expected values\n" );
130*cdf0e10cSrcweir 			return;
131*cdf0e10cSrcweir 		}
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir 		printf( "pipe test worked perfect\n" );
134*cdf0e10cSrcweir 	}
135*cdf0e10cSrcweir 	else
136*cdf0e10cSrcweir 	{
137*cdf0e10cSrcweir 		printf( "Couldn't get inputstream and outputstream\n" );
138*cdf0e10cSrcweir 	}
139*cdf0e10cSrcweir }
140*cdf0e10cSrcweir 
141*cdf0e10cSrcweir 
142*cdf0e10cSrcweir sal_Int32 PipeClientMain::run( const Sequence< OUString > & aArguments ) throw ( RuntimeException )
143*cdf0e10cSrcweir {
144*cdf0e10cSrcweir 	printf( "Connecting ....\n" );
145*cdf0e10cSrcweir 
146*cdf0e10cSrcweir 	if( aArguments.getLength() == 1 )
147*cdf0e10cSrcweir 	{
148*cdf0e10cSrcweir 		try {
149*cdf0e10cSrcweir 			Reference < XInterface > r =
150*cdf0e10cSrcweir 				m_xSMgr->createInstance( OUString::createFromAscii( "com.sun.star.bridge.UnoUrlResolver" ) );
151*cdf0e10cSrcweir 			Reference < XUnoUrlResolver > rResolver( r , UNO_QUERY );
152*cdf0e10cSrcweir 
153*cdf0e10cSrcweir 			// connect to the remote process and retrieve the initial object
154*cdf0e10cSrcweir 			r = rResolver->resolve( aArguments.getConstArray()[0] );
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir 			if( r.is() )
157*cdf0e10cSrcweir 			{
158*cdf0e10cSrcweir 				printf( "got the remote initial object\n" );
159*cdf0e10cSrcweir 				testPipe( r );
160*cdf0e10cSrcweir 			}
161*cdf0e10cSrcweir 			else
162*cdf0e10cSrcweir 			{
163*cdf0e10cSrcweir 				printf( "error : didn't get the initial object\n" );
164*cdf0e10cSrcweir 			}
165*cdf0e10cSrcweir 		}
166*cdf0e10cSrcweir 		catch( ConnectionSetupException &e )
167*cdf0e10cSrcweir 		{
168*cdf0e10cSrcweir 			OString o = OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US );
169*cdf0e10cSrcweir 			printf( "%s\n", o.pData->buffer );
170*cdf0e10cSrcweir 			printf( "couldn't access local resource ( possible security resons )\n" );
171*cdf0e10cSrcweir 		}
172*cdf0e10cSrcweir 		catch( NoConnectException &e )
173*cdf0e10cSrcweir 		{
174*cdf0e10cSrcweir 			OString o = OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US );
175*cdf0e10cSrcweir 			printf( "%s\n", o.pData->buffer );
176*cdf0e10cSrcweir 			printf( "no server listening on the resource\n" );
177*cdf0e10cSrcweir 		}
178*cdf0e10cSrcweir 		catch( IllegalArgumentException &e )
179*cdf0e10cSrcweir 		{
180*cdf0e10cSrcweir 			OString o = OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US );
181*cdf0e10cSrcweir 			printf( "%s\n", o.pData->buffer );
182*cdf0e10cSrcweir 			printf( "uno url invalid\n" );
183*cdf0e10cSrcweir 		}
184*cdf0e10cSrcweir 		catch( RuntimeException & e )
185*cdf0e10cSrcweir 		{
186*cdf0e10cSrcweir 			OString o = OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US );
187*cdf0e10cSrcweir 			printf( "%s\n", o.pData->buffer );
188*cdf0e10cSrcweir 			printf( "a remote call was aborted\n" );
189*cdf0e10cSrcweir 		}
190*cdf0e10cSrcweir 	}
191*cdf0e10cSrcweir 	else
192*cdf0e10cSrcweir 	{
193*cdf0e10cSrcweir 		printf( "usage: (uno remoteclient-component --) uno-url\n"
194*cdf0e10cSrcweir 		        "e.g.:  uno:socket,host=localhost,port=2083;urp;MyPipe\n" );
195*cdf0e10cSrcweir 		return 1;
196*cdf0e10cSrcweir 	}
197*cdf0e10cSrcweir 	return 0;
198*cdf0e10cSrcweir }
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir Reference< XInterface > SAL_CALL CreateInstance( const Reference< XMultiServiceFactory > &r)
201*cdf0e10cSrcweir {
202*cdf0e10cSrcweir 	return Reference< XInterface > ( ( OWeakObject * ) new PipeClientMain(r) );
203*cdf0e10cSrcweir }
204*cdf0e10cSrcweir 
205*cdf0e10cSrcweir Sequence< OUString > getSupportedServiceNames()
206*cdf0e10cSrcweir {
207*cdf0e10cSrcweir 	static Sequence < OUString > *pNames = 0;
208*cdf0e10cSrcweir 	if( ! pNames )
209*cdf0e10cSrcweir 	{
210*cdf0e10cSrcweir 		MutexGuard guard( Mutex::getGlobalMutex() );
211*cdf0e10cSrcweir 		if( !pNames )
212*cdf0e10cSrcweir 		{
213*cdf0e10cSrcweir 			static Sequence< OUString > seqNames(1);
214*cdf0e10cSrcweir 			seqNames.getArray()[0] = OUString::createFromAscii( "com.sun.star.bridge.example.RemoteClientSample" );
215*cdf0e10cSrcweir 			pNames = &seqNames;
216*cdf0e10cSrcweir 		}
217*cdf0e10cSrcweir 	}
218*cdf0e10cSrcweir 	return *pNames;
219*cdf0e10cSrcweir }
220*cdf0e10cSrcweir 
221*cdf0e10cSrcweir }
222*cdf0e10cSrcweir 
223*cdf0e10cSrcweir using namespace remotebridges_officeclient;
224*cdf0e10cSrcweir #define IMPLEMENTATION_NAME "com.sun.star.comp.product.example.RemoteClientSample"
225*cdf0e10cSrcweir 
226*cdf0e10cSrcweir 
227*cdf0e10cSrcweir extern "C"
228*cdf0e10cSrcweir {
229*cdf0e10cSrcweir //==================================================================================================
230*cdf0e10cSrcweir SAL_DLLPUBLIC_EXPORT void SAL_CALL component_getImplementationEnvironment(
231*cdf0e10cSrcweir 	const sal_Char ** ppEnvTypeName, uno_Environment ** ppEnv )
232*cdf0e10cSrcweir {
233*cdf0e10cSrcweir 	*ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
234*cdf0e10cSrcweir }
235*cdf0e10cSrcweir 
236*cdf0e10cSrcweir //==================================================================================================
237*cdf0e10cSrcweir // SAL_DLLPUBLIC_EXPORT sal_Bool SAL_CALL component_writeInfo(
238*cdf0e10cSrcweir // 	void * pServiceManager, void * pRegistryKey )
239*cdf0e10cSrcweir // {
240*cdf0e10cSrcweir // 	if (pRegistryKey)
241*cdf0e10cSrcweir // 	{
242*cdf0e10cSrcweir // 		try
243*cdf0e10cSrcweir // 		{
244*cdf0e10cSrcweir // 			Reference< XRegistryKey > xNewKey(
245*cdf0e10cSrcweir // 				reinterpret_cast< XRegistryKey * >( pRegistryKey )->createKey(
246*cdf0e10cSrcweir // 					OUString::createFromAscii( "/" IMPLEMENTATION_NAME "/UNO/SERVICES" ) ) );
247*cdf0e10cSrcweir 
248*cdf0e10cSrcweir // 			const Sequence< OUString > & rSNL = getSupportedServiceNames();
249*cdf0e10cSrcweir // 			const OUString * pArray = rSNL.getConstArray();
250*cdf0e10cSrcweir // 			for ( sal_Int32 nPos = rSNL.getLength(); nPos--; )
251*cdf0e10cSrcweir // 				xNewKey->createKey( pArray[nPos] );
252*cdf0e10cSrcweir 
253*cdf0e10cSrcweir // 			return sal_True;
254*cdf0e10cSrcweir // 		}
255*cdf0e10cSrcweir // 		catch (InvalidRegistryException &)
256*cdf0e10cSrcweir // 		{
257*cdf0e10cSrcweir // 			OSL_ENSURE( sal_False, "### InvalidRegistryException!" );
258*cdf0e10cSrcweir // 		}
259*cdf0e10cSrcweir // 	}
260*cdf0e10cSrcweir // 	return sal_False;
261*cdf0e10cSrcweir // }
262*cdf0e10cSrcweir 
263*cdf0e10cSrcweir //==================================================================================================
264*cdf0e10cSrcweir SAL_DLLPUBLIC_EXPORT void * SAL_CALL component_getFactory(
265*cdf0e10cSrcweir 	const sal_Char * pImplName, void * pServiceManager, void * pRegistryKey )
266*cdf0e10cSrcweir {
267*cdf0e10cSrcweir 	void * pRet = 0;
268*cdf0e10cSrcweir 
269*cdf0e10cSrcweir 	if (pServiceManager && rtl_str_compare( pImplName, IMPLEMENTATION_NAME ) == 0)
270*cdf0e10cSrcweir 	{
271*cdf0e10cSrcweir 		Reference< XSingleServiceFactory > xFactory( createSingleFactory(
272*cdf0e10cSrcweir 			reinterpret_cast< XMultiServiceFactory * >( pServiceManager ),
273*cdf0e10cSrcweir 			OUString::createFromAscii( pImplName ),
274*cdf0e10cSrcweir 			CreateInstance, getSupportedServiceNames() ) );
275*cdf0e10cSrcweir 
276*cdf0e10cSrcweir 		if (xFactory.is())
277*cdf0e10cSrcweir 		{
278*cdf0e10cSrcweir 			xFactory->acquire();
279*cdf0e10cSrcweir 			pRet = xFactory.get();
280*cdf0e10cSrcweir 		}
281*cdf0e10cSrcweir 	}
282*cdf0e10cSrcweir 
283*cdf0e10cSrcweir 	return pRet;
284*cdf0e10cSrcweir }
285*cdf0e10cSrcweir }
286