xref: /trunk/main/odk/examples/cpp/remoteclient/remoteclient.cxx (revision 45c5a00162811bd03b87fcb4fe9996782f2b59ec)
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:
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 
68 
69 private: // helper methods
70     void testPipe( const Reference < XInterface > & rComponent );
71     Reference< XMultiServiceFactory > m_xSMgr;
72 };
73 
74 void PipeClientMain::testPipe( const Reference< XInterface > & rxInterface )
75 {
76     // ask for the input stream
77     Reference< XInputStream > rInputStream( rxInterface, UNO_QUERY );
78 
79     // ask for the output stream
80     Reference< XOutputStream > rOutputStream( rxInterface, UNO_QUERY );
81 
82     if( rInputStream.is() && rOutputStream.is() )
83     {
84         printf( "got inputstream and outputstream from remote process\n" );
85 
86         sal_Int8 a[] = { 5,4,3,2,1,0 };
87         Sequence< sal_Int8 > seq( a , 6 );
88         rOutputStream->writeBytes( seq );
89         rOutputStream->closeOutput();
90 
91         Sequence< sal_Int8> seqRead;
92         if( rInputStream->readBytes( seqRead ,3 ) != 3 )
93         {
94             printf( "error : Couldn't read the expected number of bytes\n" );
95             return;
96         }
97 
98         if( seqRead.getConstArray()[0] != 5 ||
99             seqRead.getConstArray()[1] != 4 ||
100             seqRead.getConstArray()[2] != 3 )
101         {
102             printf( "error : The array doesn't contain the expected values\n" );
103             return;
104         }
105 
106         // try to read more bytes than written
107         if( rInputStream->readBytes( seqRead , 4 ) != 3 )
108         {
109             printf( "error : Got an unexpected number of bytes\n" );
110             return;
111         }
112 
113         if( seqRead.getConstArray()[0] != 2 ||
114             seqRead.getConstArray()[1] != 1 ||
115             seqRead.getConstArray()[2] != 0 )
116         {
117             printf( "error : The array doesn't contain the expected values\n" );
118             return;
119         }
120 
121         printf( "pipe test worked perfect\n" );
122     }
123     else
124     {
125         printf( "Couldn't get inputstream and outputstream\n" );
126     }
127 }
128 
129 
130 sal_Int32 PipeClientMain::run( const Sequence< OUString > & aArguments )
131 {
132     printf( "Connecting ....\n" );
133 
134     if( aArguments.getLength() == 1 )
135     {
136         try {
137             Reference < XInterface > r =
138                 m_xSMgr->createInstance( OUString::createFromAscii( "com.sun.star.bridge.UnoUrlResolver" ) );
139             Reference < XUnoUrlResolver > rResolver( r , UNO_QUERY );
140 
141             // connect to the remote process and retrieve the initial object
142             r = rResolver->resolve( aArguments.getConstArray()[0] );
143 
144             if( r.is() )
145             {
146                 printf( "got the remote initial object\n" );
147                 testPipe( r );
148             }
149             else
150             {
151                 printf( "error : didn't get the initial object\n" );
152             }
153         }
154         catch( ConnectionSetupException &e )
155         {
156             OString o = OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US );
157             printf( "%s\n", o.pData->buffer );
158             printf( "couldn't access local resource ( possible security resons )\n" );
159         }
160         catch( NoConnectException &e )
161         {
162             OString o = OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US );
163             printf( "%s\n", o.pData->buffer );
164             printf( "no server listening on the resource\n" );
165         }
166         catch( IllegalArgumentException &e )
167         {
168             OString o = OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US );
169             printf( "%s\n", o.pData->buffer );
170             printf( "uno url invalid\n" );
171         }
172         catch( RuntimeException & e )
173         {
174             OString o = OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US );
175             printf( "%s\n", o.pData->buffer );
176             printf( "a remote call was aborted\n" );
177         }
178     }
179     else
180     {
181         printf( "usage: (uno remoteclient-component --) uno-url\n"
182                 "e.g.:  uno:socket,host=localhost,port=2083;urp;MyPipe\n" );
183         return 1;
184     }
185     return 0;
186 }
187 
188 Reference< XInterface > SAL_CALL CreateInstance( const Reference< XMultiServiceFactory > &r)
189 {
190     return Reference< XInterface > ( ( OWeakObject * ) new PipeClientMain(r) );
191 }
192 
193 Sequence< OUString > getSupportedServiceNames()
194 {
195     static Sequence < OUString > *pNames = 0;
196     if( ! pNames )
197     {
198         MutexGuard guard( Mutex::getGlobalMutex() );
199         if( !pNames )
200         {
201             static Sequence< OUString > seqNames(1);
202             seqNames.getArray()[0] = OUString::createFromAscii( "com.sun.star.bridge.example.RemoteClientSample" );
203             pNames = &seqNames;
204         }
205     }
206     return *pNames;
207 }
208 
209 }
210 
211 using namespace remotebridges_officeclient;
212 #define IMPLEMENTATION_NAME "com.sun.star.comp.product.example.RemoteClientSample"
213 
214 
215 extern "C"
216 {
217 //==================================================================================================
218 SAL_DLLPUBLIC_EXPORT void SAL_CALL component_getImplementationEnvironment(
219     const sal_Char ** ppEnvTypeName, uno_Environment ** ppEnv )
220 {
221     *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
222 }
223 
224 //==================================================================================================
225 // SAL_DLLPUBLIC_EXPORT sal_Bool SAL_CALL component_writeInfo(
226 //  void * pServiceManager, void * pRegistryKey )
227 // {
228 //  if (pRegistryKey)
229 //  {
230 //      try
231 //      {
232 //          Reference< XRegistryKey > xNewKey(
233 //              reinterpret_cast< XRegistryKey * >( pRegistryKey )->createKey(
234 //                  OUString::createFromAscii( "/" IMPLEMENTATION_NAME "/UNO/SERVICES" ) ) );
235 
236 //          const Sequence< OUString > & rSNL = getSupportedServiceNames();
237 //          const OUString * pArray = rSNL.getConstArray();
238 //          for ( sal_Int32 nPos = rSNL.getLength(); nPos--; )
239 //              xNewKey->createKey( pArray[nPos] );
240 
241 //          return sal_True;
242 //      }
243 //      catch (InvalidRegistryException &)
244 //      {
245 //          OSL_ENSURE( sal_False, "### InvalidRegistryException!" );
246 //      }
247 //  }
248 //  return sal_False;
249 // }
250 
251 //==================================================================================================
252 SAL_DLLPUBLIC_EXPORT void * SAL_CALL component_getFactory(
253     const sal_Char * pImplName, void * pServiceManager, void * pRegistryKey )
254 {
255     void * pRet = 0;
256 
257     if (pServiceManager && rtl_str_compare( pImplName, IMPLEMENTATION_NAME ) == 0)
258     {
259         Reference< XSingleServiceFactory > xFactory( createSingleFactory(
260             reinterpret_cast< XMultiServiceFactory * >( pServiceManager ),
261             OUString::createFromAscii( pImplName ),
262             CreateInstance, getSupportedServiceNames() ) );
263 
264         if (xFactory.is())
265         {
266             xFactory->acquire();
267             pRet = xFactory.get();
268         }
269     }
270 
271     return pRet;
272 }
273 }
274