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
7 * Copyright 2000, 2010 Oracle and/or its affiliates.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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 *************************************************************************/
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>
49 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
50 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
52 #include <com/sun/star/io/XOutputStream.hpp>
53 #include <com/sun/star/io/XInputStream.hpp>
55 #include <cppuhelper/implbase1.hxx>
57 using namespace ::rtl
;
58 using namespace ::cppu
;
59 using namespace ::osl
;
60 using namespace ::com::sun::star::uno
;
61 using namespace ::com::sun::star::lang
;
62 using namespace ::com::sun::star::connection
;
63 using namespace ::com::sun::star::bridge
;
64 using namespace ::com::sun::star::io
;
67 namespace remotebridges_officeclient
{
69 class PipeClientMain
: public WeakImplHelper1
< XMain
>
72 PipeClientMain( const Reference
< XMultiServiceFactory
> &r
) :
78 virtual sal_Int32 SAL_CALL
run( const Sequence
< OUString
>& aArguments
);
81 private: // helper methods
82 void testPipe( const Reference
< XInterface
> & rComponent
);
83 Reference
< XMultiServiceFactory
> m_xSMgr
;
86 void PipeClientMain::testPipe( const Reference
< XInterface
> & rxInterface
)
88 // ask for the input stream
89 Reference
< XInputStream
> rInputStream( rxInterface
, UNO_QUERY
);
91 // ask for the output stream
92 Reference
< XOutputStream
> rOutputStream( rxInterface
, UNO_QUERY
);
94 if( rInputStream
.is() && rOutputStream
.is() )
96 printf( "got inputstream and outputstream from remote process\n" );
98 sal_Int8 a
[] = { 5,4,3,2,1,0 };
99 Sequence
< sal_Int8
> seq( a
, 6 );
100 rOutputStream
->writeBytes( seq
);
101 rOutputStream
->closeOutput();
103 Sequence
< sal_Int8
> seqRead
;
104 if( rInputStream
->readBytes( seqRead
,3 ) != 3 )
106 printf( "error : Couldn't read the expected number of bytes\n" );
110 if( seqRead
.getConstArray()[0] != 5 ||
111 seqRead
.getConstArray()[1] != 4 ||
112 seqRead
.getConstArray()[2] != 3 )
114 printf( "error : The array doesn't contain the expected values\n" );
118 // try to read more bytes than written
119 if( rInputStream
->readBytes( seqRead
, 4 ) != 3 )
121 printf( "error : Got an unexpected number of bytes\n" );
125 if( seqRead
.getConstArray()[0] != 2 ||
126 seqRead
.getConstArray()[1] != 1 ||
127 seqRead
.getConstArray()[2] != 0 )
129 printf( "error : The array doesn't contain the expected values\n" );
133 printf( "pipe test worked perfect\n" );
137 printf( "Couldn't get inputstream and outputstream\n" );
142 sal_Int32
PipeClientMain::run( const Sequence
< OUString
> & aArguments
)
144 printf( "Connecting...\n" );
146 if( aArguments
.getLength() == 1 )
149 Reference
< XInterface
> r
=
150 m_xSMgr
->createInstance("com.sun.star.bridge.UnoUrlResolver");
151 Reference
< XUnoUrlResolver
> rResolver( r
, UNO_QUERY
);
153 // connect to the remote process and retrieve the initial object
154 r
= rResolver
->resolve( aArguments
.getConstArray()[0] );
158 printf( "got the remote initial object\n" );
163 printf( "error : didn't get the initial object\n" );
166 catch( ConnectionSetupException
&e
)
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 reasons)\n" );
172 catch( NoConnectException
&e
)
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" );
178 catch( IllegalArgumentException
&e
)
180 OString o
= OUStringToOString( e
.Message
, RTL_TEXTENCODING_ASCII_US
);
181 printf( "%s\n", o
.pData
->buffer
);
182 printf( "uno url invalid\n" );
184 catch( RuntimeException
& e
)
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" );
193 printf( "usage: (uno remoteclient-component --) uno-url\n"
194 "e.g.: uno:socket,host=localhost,port=2083;urp;MyPipe\n" );
200 Reference
< XInterface
> SAL_CALL
CreateInstance( const Reference
< XMultiServiceFactory
> &r
)
202 return Reference
< XInterface
> ( ( OWeakObject
* ) new PipeClientMain(r
) );
205 Sequence
< OUString
> getSupportedServiceNames()
207 static Sequence
< OUString
> *pNames
= 0;
210 MutexGuard
guard( Mutex::getGlobalMutex() );
213 static Sequence
< OUString
> seqNames(1);
214 seqNames
[0] = "com.sun.star.bridge.example.RemoteClientSample";
223 using namespace remotebridges_officeclient
;
224 #define IMPLEMENTATION_NAME "com.sun.star.comp.product.example.RemoteClientSample"
229 SAL_DLLPUBLIC_EXPORT
void * SAL_CALL
component_getFactory(
230 const char * pImplName
, void * pServiceManager
, void * pRegistryKey
)
234 if (pServiceManager
&& rtl_str_compare( pImplName
, IMPLEMENTATION_NAME
) == 0)
236 Reference
< XSingleServiceFactory
> xFactory( createSingleFactory(
237 reinterpret_cast< XMultiServiceFactory
* >( pServiceManager
),
238 OUString::createFromAscii( pImplName
),
239 CreateInstance
, getSupportedServiceNames() ) );
244 pRet
= xFactory
.get();
251 SAL_DLLPUBLIC_EXPORT
void SAL_CALL
component_getImplementationEnvironment(
252 char const ** ppEnvTypeName
, uno_Environment
**)
254 *ppEnvTypeName
= CPPU_CURRENT_LANGUAGE_BINDING_NAME
;
259 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */