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