1*34dd1e25SAndrew Rist /**************************************************************
2*34dd1e25SAndrew Rist  *
3*34dd1e25SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*34dd1e25SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*34dd1e25SAndrew Rist  * distributed with this work for additional information
6*34dd1e25SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*34dd1e25SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*34dd1e25SAndrew Rist  * "License"); you may not use this file except in compliance
9*34dd1e25SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*34dd1e25SAndrew Rist  *
11*34dd1e25SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*34dd1e25SAndrew Rist  *
13*34dd1e25SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*34dd1e25SAndrew Rist  * software distributed under the License is distributed on an
15*34dd1e25SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*34dd1e25SAndrew Rist  * KIND, either express or implied.  See the License for the
17*34dd1e25SAndrew Rist  * specific language governing permissions and limitations
18*34dd1e25SAndrew Rist  * under the License.
19*34dd1e25SAndrew Rist  *
20*34dd1e25SAndrew Rist  *************************************************************/
21*34dd1e25SAndrew Rist 
22*34dd1e25SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include <stdio.h>
25cdf0e10cSrcweir #include <osl/mutex.hxx>
26cdf0e10cSrcweir #include <cppuhelper/factory.hxx>
27cdf0e10cSrcweir 
28cdf0e10cSrcweir #include <com/sun/star/uno/XNamingService.hpp>
29cdf0e10cSrcweir 
30cdf0e10cSrcweir #include <com/sun/star/registry/XImplementationRegistration.hpp>
31cdf0e10cSrcweir 
32cdf0e10cSrcweir #include <com/sun/star/connection/XConnector.hpp>
33cdf0e10cSrcweir 
34cdf0e10cSrcweir #include <com/sun/star/bridge/XUnoUrlResolver.hpp>
35cdf0e10cSrcweir 
36cdf0e10cSrcweir #include <com/sun/star/lang/XMain.hpp>
37cdf0e10cSrcweir #include <com/sun/star/lang/XComponent.hpp>
38cdf0e10cSrcweir 
39cdf0e10cSrcweir #include <com/sun/star/io/XOutputStream.hpp>
40cdf0e10cSrcweir #include <com/sun/star/io/XInputStream.hpp>
41cdf0e10cSrcweir 
42cdf0e10cSrcweir #include <cppuhelper/implbase1.hxx>
43cdf0e10cSrcweir 
44cdf0e10cSrcweir using namespace ::rtl;
45cdf0e10cSrcweir using namespace ::cppu;
46cdf0e10cSrcweir using namespace ::osl;
47cdf0e10cSrcweir using namespace ::com::sun::star::uno;
48cdf0e10cSrcweir using namespace ::com::sun::star::lang;
49cdf0e10cSrcweir using namespace ::com::sun::star::registry;
50cdf0e10cSrcweir using namespace ::com::sun::star::connection;
51cdf0e10cSrcweir using namespace ::com::sun::star::bridge;
52cdf0e10cSrcweir using namespace ::com::sun::star::io;
53cdf0e10cSrcweir 
54cdf0e10cSrcweir 
55cdf0e10cSrcweir namespace remotebridges_officeclient {
56cdf0e10cSrcweir 
57cdf0e10cSrcweir class PipeClientMain : public WeakImplHelper1< XMain >
58cdf0e10cSrcweir {
59cdf0e10cSrcweir public:
PipeClientMain(const Reference<XMultiServiceFactory> & r)60cdf0e10cSrcweir 	PipeClientMain( const Reference< XMultiServiceFactory > &r ) :
61cdf0e10cSrcweir 		m_xSMgr( r )
62cdf0e10cSrcweir 		{}
63cdf0e10cSrcweir public:     // Methods
64cdf0e10cSrcweir 
65cdf0e10cSrcweir 
66cdf0e10cSrcweir     virtual sal_Int32 SAL_CALL run( const Sequence< OUString >& aArguments )
67cdf0e10cSrcweir 		throw(RuntimeException);
68cdf0e10cSrcweir 
69cdf0e10cSrcweir 
70cdf0e10cSrcweir private: // helper methods
71cdf0e10cSrcweir 	void testPipe( const Reference < XInterface > & rComponent );
72cdf0e10cSrcweir 	Reference< XMultiServiceFactory > m_xSMgr;
73cdf0e10cSrcweir };
74cdf0e10cSrcweir 
testPipe(const Reference<XInterface> & rxInterface)75cdf0e10cSrcweir void PipeClientMain::testPipe( const Reference< XInterface > & rxInterface )
76cdf0e10cSrcweir {
77cdf0e10cSrcweir 	// ask for the input stream
78cdf0e10cSrcweir 	Reference< XInputStream > rInputStream( rxInterface, UNO_QUERY );
79cdf0e10cSrcweir 
80cdf0e10cSrcweir 	// ask for the output stream
81cdf0e10cSrcweir 	Reference< XOutputStream > rOutputStream( rxInterface, UNO_QUERY );
82cdf0e10cSrcweir 
83cdf0e10cSrcweir 	if( rInputStream.is() && rOutputStream.is() )
84cdf0e10cSrcweir 	{
85cdf0e10cSrcweir 		printf( "got inputstream and outputstream from remote process\n" );
86cdf0e10cSrcweir 
87cdf0e10cSrcweir 		sal_Int8 a[] = { 5,4,3,2,1,0 };
88cdf0e10cSrcweir 		Sequence< sal_Int8 > seq( a , 6 );
89cdf0e10cSrcweir 		rOutputStream->writeBytes( seq );
90cdf0e10cSrcweir 		rOutputStream->closeOutput();
91cdf0e10cSrcweir 
92cdf0e10cSrcweir 		Sequence< sal_Int8> seqRead;
93cdf0e10cSrcweir 		if( rInputStream->readBytes( seqRead ,3 ) != 3 )
94cdf0e10cSrcweir 		{
95cdf0e10cSrcweir 			printf( "error : Couldn't read the expected number of bytes\n" );
96cdf0e10cSrcweir 			return;
97cdf0e10cSrcweir 		}
98cdf0e10cSrcweir 
99cdf0e10cSrcweir 		if( seqRead.getConstArray()[0] != 5 ||
100cdf0e10cSrcweir 			seqRead.getConstArray()[1] != 4 ||
101cdf0e10cSrcweir 			seqRead.getConstArray()[2] != 3 )
102cdf0e10cSrcweir 		{
103cdf0e10cSrcweir 			printf( "error : The array doesn't contain the expected values\n" );
104cdf0e10cSrcweir 			return;
105cdf0e10cSrcweir 		}
106cdf0e10cSrcweir 
107cdf0e10cSrcweir 		// try to read more bytes than written
108cdf0e10cSrcweir 		if( rInputStream->readBytes( seqRead , 4 ) != 3 )
109cdf0e10cSrcweir 		{
110cdf0e10cSrcweir 			printf( "error : Got an unexpected number of bytes\n" );
111cdf0e10cSrcweir 			return;
112cdf0e10cSrcweir 		}
113cdf0e10cSrcweir 
114cdf0e10cSrcweir 		if( seqRead.getConstArray()[0] != 2 ||
115cdf0e10cSrcweir 			seqRead.getConstArray()[1] != 1 ||
116cdf0e10cSrcweir 			seqRead.getConstArray()[2] != 0 )
117cdf0e10cSrcweir 		{
118cdf0e10cSrcweir 			printf( "error : The array doesn't contain the expected values\n" );
119cdf0e10cSrcweir 			return;
120cdf0e10cSrcweir 		}
121cdf0e10cSrcweir 
122cdf0e10cSrcweir 		printf( "pipe test worked perfect\n" );
123cdf0e10cSrcweir 	}
124cdf0e10cSrcweir 	else
125cdf0e10cSrcweir 	{
126cdf0e10cSrcweir 		printf( "Couldn't get inputstream and outputstream\n" );
127cdf0e10cSrcweir 	}
128cdf0e10cSrcweir }
129cdf0e10cSrcweir 
130cdf0e10cSrcweir 
run(const Sequence<OUString> & aArguments)131cdf0e10cSrcweir sal_Int32 PipeClientMain::run( const Sequence< OUString > & aArguments ) throw ( RuntimeException )
132cdf0e10cSrcweir {
133cdf0e10cSrcweir 	printf( "Connecting ....\n" );
134cdf0e10cSrcweir 
135cdf0e10cSrcweir 	if( aArguments.getLength() == 1 )
136cdf0e10cSrcweir 	{
137cdf0e10cSrcweir 		try {
138cdf0e10cSrcweir 			Reference < XInterface > r =
139cdf0e10cSrcweir 				m_xSMgr->createInstance( OUString::createFromAscii( "com.sun.star.bridge.UnoUrlResolver" ) );
140cdf0e10cSrcweir 			Reference < XUnoUrlResolver > rResolver( r , UNO_QUERY );
141cdf0e10cSrcweir 
142cdf0e10cSrcweir 			// connect to the remote process and retrieve the initial object
143cdf0e10cSrcweir 			r = rResolver->resolve( aArguments.getConstArray()[0] );
144cdf0e10cSrcweir 
145cdf0e10cSrcweir 			if( r.is() )
146cdf0e10cSrcweir 			{
147cdf0e10cSrcweir 				printf( "got the remote initial object\n" );
148cdf0e10cSrcweir 				testPipe( r );
149cdf0e10cSrcweir 			}
150cdf0e10cSrcweir 			else
151cdf0e10cSrcweir 			{
152cdf0e10cSrcweir 				printf( "error : didn't get the initial object\n" );
153cdf0e10cSrcweir 			}
154cdf0e10cSrcweir 		}
155cdf0e10cSrcweir 		catch( ConnectionSetupException &e )
156cdf0e10cSrcweir 		{
157cdf0e10cSrcweir 			OString o = OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US );
158cdf0e10cSrcweir 			printf( "%s\n", o.pData->buffer );
159cdf0e10cSrcweir 			printf( "couldn't access local resource ( possible security resons )\n" );
160cdf0e10cSrcweir 		}
161cdf0e10cSrcweir 		catch( NoConnectException &e )
162cdf0e10cSrcweir 		{
163cdf0e10cSrcweir 			OString o = OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US );
164cdf0e10cSrcweir 			printf( "%s\n", o.pData->buffer );
165cdf0e10cSrcweir 			printf( "no server listening on the resource\n" );
166cdf0e10cSrcweir 		}
167cdf0e10cSrcweir 		catch( IllegalArgumentException &e )
168cdf0e10cSrcweir 		{
169cdf0e10cSrcweir 			OString o = OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US );
170cdf0e10cSrcweir 			printf( "%s\n", o.pData->buffer );
171cdf0e10cSrcweir 			printf( "uno url invalid\n" );
172cdf0e10cSrcweir 		}
173cdf0e10cSrcweir 		catch( RuntimeException & e )
174cdf0e10cSrcweir 		{
175cdf0e10cSrcweir 			OString o = OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US );
176cdf0e10cSrcweir 			printf( "%s\n", o.pData->buffer );
177cdf0e10cSrcweir 			printf( "a remote call was aborted\n" );
178cdf0e10cSrcweir 		}
179cdf0e10cSrcweir 	}
180cdf0e10cSrcweir 	else
181cdf0e10cSrcweir 	{
182cdf0e10cSrcweir 		printf( "usage: (uno remoteclient-component --) uno-url\n"
183cdf0e10cSrcweir 		        "e.g.:  uno:socket,host=localhost,port=2083;urp;MyPipe\n" );
184cdf0e10cSrcweir 		return 1;
185cdf0e10cSrcweir 	}
186cdf0e10cSrcweir 	return 0;
187cdf0e10cSrcweir }
188cdf0e10cSrcweir 
CreateInstance(const Reference<XMultiServiceFactory> & r)189cdf0e10cSrcweir Reference< XInterface > SAL_CALL CreateInstance( const Reference< XMultiServiceFactory > &r)
190cdf0e10cSrcweir {
191cdf0e10cSrcweir 	return Reference< XInterface > ( ( OWeakObject * ) new PipeClientMain(r) );
192cdf0e10cSrcweir }
193cdf0e10cSrcweir 
getSupportedServiceNames()194cdf0e10cSrcweir Sequence< OUString > getSupportedServiceNames()
195cdf0e10cSrcweir {
196cdf0e10cSrcweir 	static Sequence < OUString > *pNames = 0;
197cdf0e10cSrcweir 	if( ! pNames )
198cdf0e10cSrcweir 	{
199cdf0e10cSrcweir 		MutexGuard guard( Mutex::getGlobalMutex() );
200cdf0e10cSrcweir 		if( !pNames )
201cdf0e10cSrcweir 		{
202cdf0e10cSrcweir 			static Sequence< OUString > seqNames(1);
203cdf0e10cSrcweir 			seqNames.getArray()[0] = OUString::createFromAscii( "com.sun.star.bridge.example.RemoteClientSample" );
204cdf0e10cSrcweir 			pNames = &seqNames;
205cdf0e10cSrcweir 		}
206cdf0e10cSrcweir 	}
207cdf0e10cSrcweir 	return *pNames;
208cdf0e10cSrcweir }
209cdf0e10cSrcweir 
210cdf0e10cSrcweir }
211cdf0e10cSrcweir 
212cdf0e10cSrcweir using namespace remotebridges_officeclient;
213cdf0e10cSrcweir #define IMPLEMENTATION_NAME "com.sun.star.comp.product.example.RemoteClientSample"
214cdf0e10cSrcweir 
215cdf0e10cSrcweir 
216cdf0e10cSrcweir extern "C"
217cdf0e10cSrcweir {
218cdf0e10cSrcweir //==================================================================================================
component_getImplementationEnvironment(const sal_Char ** ppEnvTypeName,uno_Environment ** ppEnv)219cdf0e10cSrcweir SAL_DLLPUBLIC_EXPORT void SAL_CALL component_getImplementationEnvironment(
220cdf0e10cSrcweir 	const sal_Char ** ppEnvTypeName, uno_Environment ** ppEnv )
221cdf0e10cSrcweir {
222cdf0e10cSrcweir 	*ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
223cdf0e10cSrcweir }
224cdf0e10cSrcweir 
225cdf0e10cSrcweir //==================================================================================================
226cdf0e10cSrcweir // SAL_DLLPUBLIC_EXPORT sal_Bool SAL_CALL component_writeInfo(
227cdf0e10cSrcweir // 	void * pServiceManager, void * pRegistryKey )
228cdf0e10cSrcweir // {
229cdf0e10cSrcweir // 	if (pRegistryKey)
230cdf0e10cSrcweir // 	{
231cdf0e10cSrcweir // 		try
232cdf0e10cSrcweir // 		{
233cdf0e10cSrcweir // 			Reference< XRegistryKey > xNewKey(
234cdf0e10cSrcweir // 				reinterpret_cast< XRegistryKey * >( pRegistryKey )->createKey(
235cdf0e10cSrcweir // 					OUString::createFromAscii( "/" IMPLEMENTATION_NAME "/UNO/SERVICES" ) ) );
236cdf0e10cSrcweir 
237cdf0e10cSrcweir // 			const Sequence< OUString > & rSNL = getSupportedServiceNames();
238cdf0e10cSrcweir // 			const OUString * pArray = rSNL.getConstArray();
239cdf0e10cSrcweir // 			for ( sal_Int32 nPos = rSNL.getLength(); nPos--; )
240cdf0e10cSrcweir // 				xNewKey->createKey( pArray[nPos] );
241cdf0e10cSrcweir 
242cdf0e10cSrcweir // 			return sal_True;
243cdf0e10cSrcweir // 		}
244cdf0e10cSrcweir // 		catch (InvalidRegistryException &)
245cdf0e10cSrcweir // 		{
246cdf0e10cSrcweir // 			OSL_ENSURE( sal_False, "### InvalidRegistryException!" );
247cdf0e10cSrcweir // 		}
248cdf0e10cSrcweir // 	}
249cdf0e10cSrcweir // 	return sal_False;
250cdf0e10cSrcweir // }
251cdf0e10cSrcweir 
252cdf0e10cSrcweir //==================================================================================================
component_getFactory(const sal_Char * pImplName,void * pServiceManager,void * pRegistryKey)253cdf0e10cSrcweir SAL_DLLPUBLIC_EXPORT void * SAL_CALL component_getFactory(
254cdf0e10cSrcweir 	const sal_Char * pImplName, void * pServiceManager, void * pRegistryKey )
255cdf0e10cSrcweir {
256cdf0e10cSrcweir 	void * pRet = 0;
257cdf0e10cSrcweir 
258cdf0e10cSrcweir 	if (pServiceManager && rtl_str_compare( pImplName, IMPLEMENTATION_NAME ) == 0)
259cdf0e10cSrcweir 	{
260cdf0e10cSrcweir 		Reference< XSingleServiceFactory > xFactory( createSingleFactory(
261cdf0e10cSrcweir 			reinterpret_cast< XMultiServiceFactory * >( pServiceManager ),
262cdf0e10cSrcweir 			OUString::createFromAscii( pImplName ),
263cdf0e10cSrcweir 			CreateInstance, getSupportedServiceNames() ) );
264cdf0e10cSrcweir 
265cdf0e10cSrcweir 		if (xFactory.is())
266cdf0e10cSrcweir 		{
267cdf0e10cSrcweir 			xFactory->acquire();
268cdf0e10cSrcweir 			pRet = xFactory.get();
269cdf0e10cSrcweir 		}
270cdf0e10cSrcweir 	}
271cdf0e10cSrcweir 
272cdf0e10cSrcweir 	return pRet;
273cdf0e10cSrcweir }
274cdf0e10cSrcweir }
275