Branch libreoffice-5-0-4
[LibreOffice.git] / odk / examples / cpp / remoteclient / remoteclient.cxx
blob373efaee8a24e10c5d69735a92d7a308232bd2f5
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * The Contents of this file are made available subject to the terms of
5 * the BSD license.
7 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
29 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
31 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
32 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *************************************************************************/
36 #include <stdio.h>
37 #include <osl/mutex.hxx>
38 #include <uno/lbnames.h>
39 #include <cppuhelper/factory.hxx>
41 #include <com/sun/star/uno/XNamingService.hpp>
43 #include <com/sun/star/connection/XConnector.hpp>
45 #include <com/sun/star/bridge/XUnoUrlResolver.hpp>
47 #include <com/sun/star/lang/XMain.hpp>
48 #include <com/sun/star/lang/XComponent.hpp>
50 #include <com/sun/star/io/XOutputStream.hpp>
51 #include <com/sun/star/io/XInputStream.hpp>
53 #include <cppuhelper/implbase1.hxx>
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::connection;
61 using namespace ::com::sun::star::bridge;
62 using namespace ::com::sun::star::io;
65 namespace remotebridges_officeclient {
67 class PipeClientMain : public WeakImplHelper1< XMain >
69 public:
70 PipeClientMain( const Reference< XMultiServiceFactory > &r ) :
71 m_xSMgr( r )
73 public: // Methods
76 virtual sal_Int32 SAL_CALL run( const Sequence< OUString >& aArguments )
77 throw(RuntimeException);
80 private: // helper methods
81 void testPipe( const Reference < XInterface > & rComponent );
82 Reference< XMultiServiceFactory > m_xSMgr;
85 void PipeClientMain::testPipe( const Reference< XInterface > & rxInterface )
87 // ask for the input stream
88 Reference< XInputStream > rInputStream( rxInterface, UNO_QUERY );
90 // ask for the output stream
91 Reference< XOutputStream > rOutputStream( rxInterface, UNO_QUERY );
93 if( rInputStream.is() && rOutputStream.is() )
95 printf( "got inputstream and outputstream from remote process\n" );
97 sal_Int8 a[] = { 5,4,3,2,1,0 };
98 Sequence< sal_Int8 > seq( a , 6 );
99 rOutputStream->writeBytes( seq );
100 rOutputStream->closeOutput();
102 Sequence< sal_Int8> seqRead;
103 if( rInputStream->readBytes( seqRead ,3 ) != 3 )
105 printf( "error : Couldn't read the expected number of bytes\n" );
106 return;
109 if( seqRead.getConstArray()[0] != 5 ||
110 seqRead.getConstArray()[1] != 4 ||
111 seqRead.getConstArray()[2] != 3 )
113 printf( "error : The array doesn't contain the expected values\n" );
114 return;
117 // try to read more bytes than written
118 if( rInputStream->readBytes( seqRead , 4 ) != 3 )
120 printf( "error : Got an unexpected number of bytes\n" );
121 return;
124 if( seqRead.getConstArray()[0] != 2 ||
125 seqRead.getConstArray()[1] != 1 ||
126 seqRead.getConstArray()[2] != 0 )
128 printf( "error : The array doesn't contain the expected values\n" );
129 return;
132 printf( "pipe test worked perfect\n" );
134 else
136 printf( "Couldn't get inputstream and outputstream\n" );
141 sal_Int32 PipeClientMain::run( const Sequence< OUString > & aArguments ) throw ( RuntimeException )
143 printf( "Connecting ....\n" );
145 if( aArguments.getLength() == 1 )
147 try {
148 Reference < XInterface > r =
149 m_xSMgr->createInstance("com.sun.star.bridge.UnoUrlResolver");
150 Reference < XUnoUrlResolver > rResolver( r , UNO_QUERY );
152 // connect to the remote process and retrieve the initial object
153 r = rResolver->resolve( aArguments.getConstArray()[0] );
155 if( r.is() )
157 printf( "got the remote initial object\n" );
158 testPipe( r );
160 else
162 printf( "error : didn't get the initial object\n" );
165 catch( ConnectionSetupException &e )
167 OString o = OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US );
168 printf( "%s\n", o.pData->buffer );
169 printf( "couldn't access local resource ( possible security resons )\n" );
171 catch( NoConnectException &e )
173 OString o = OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US );
174 printf( "%s\n", o.pData->buffer );
175 printf( "no server listening on the resource\n" );
177 catch( IllegalArgumentException &e )
179 OString o = OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US );
180 printf( "%s\n", o.pData->buffer );
181 printf( "uno url invalid\n" );
183 catch( RuntimeException & e )
185 OString o = OUStringToOString( e.Message, RTL_TEXTENCODING_ASCII_US );
186 printf( "%s\n", o.pData->buffer );
187 printf( "a remote call was aborted\n" );
190 else
192 printf( "usage: (uno remoteclient-component --) uno-url\n"
193 "e.g.: uno:socket,host=localhost,port=2083;urp;MyPipe\n" );
194 return 1;
196 return 0;
199 Reference< XInterface > SAL_CALL CreateInstance( const Reference< XMultiServiceFactory > &r)
201 return Reference< XInterface > ( ( OWeakObject * ) new PipeClientMain(r) );
204 Sequence< OUString > getSupportedServiceNames()
206 static Sequence < OUString > *pNames = 0;
207 if( ! pNames )
209 MutexGuard guard( Mutex::getGlobalMutex() );
210 if( !pNames )
212 static Sequence< OUString > seqNames(1);
213 seqNames[0] = "com.sun.star.bridge.example.RemoteClientSample";
214 pNames = &seqNames;
217 return *pNames;
222 using namespace remotebridges_officeclient;
223 #define IMPLEMENTATION_NAME "com.sun.star.comp.product.example.RemoteClientSample"
226 extern "C"
228 SAL_DLLPUBLIC_EXPORT void * SAL_CALL component_getFactory(
229 const sal_Char * pImplName, void * pServiceManager, void * pRegistryKey )
231 void * pRet = 0;
233 if (pServiceManager && rtl_str_compare( pImplName, IMPLEMENTATION_NAME ) == 0)
235 Reference< XSingleServiceFactory > xFactory( createSingleFactory(
236 reinterpret_cast< XMultiServiceFactory * >( pServiceManager ),
237 OUString::createFromAscii( pImplName ),
238 CreateInstance, getSupportedServiceNames() ) );
240 if (xFactory.is())
242 xFactory->acquire();
243 pRet = xFactory.get();
247 return pRet;
250 SAL_DLLPUBLIC_EXPORT void SAL_CALL component_getImplementationEnvironment(
251 char const ** ppEnvTypeName, uno_Environment **)
253 *ppEnvTypeName = CPPU_CURRENT_LANGUAGE_BINDING_NAME;
258 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */