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