Branch libreoffice-5-0-4
[LibreOffice.git] / bridges / test / testsameprocess.cxx
blob8009e2b1965bedf55bc3c5fc94c6159533bdc964
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <osl/time.h>
22 #include <osl/mutex.hxx>
23 #include <osl/thread.hxx>
25 #include <cppuhelper/servicefactory.hxx>
27 #include <com/sun/star/bridge/XBridgeFactory.hpp>
28 #include <com/sun/star/connection/XAcceptor.hpp>
29 #include <com/sun/star/connection/XConnector.hpp>
31 #include <com/sun/star/lang/XComponent.hpp>
33 #include <cppuhelper/weak.hxx>
35 #include <test/XTestFactory.hpp>
38 using namespace ::test;
39 using namespace ::cppu;
40 using namespace ::osl;
41 using namespace ::com::sun::star::uno;
42 using namespace ::com::sun::star::lang;
43 using namespace ::com::sun::star::bridge;
44 using namespace ::com::sun::star::connection;
46 #ifdef SAL_W32
47 #include <conio.h>
48 #endif
50 #include "testcomp.h"
51 #include "osl/mutex.h"
53 /*********
55 ********/
57 class MyThread :
58 public Thread
60 public:
61 MyThread( const Reference< XAcceptor > &r ,
62 const Reference< XBridgeFactory > &rFactory,
63 const OUString &sConnectionDescription) :
64 m_rAcceptor( r ),
65 m_rBridgeFactory ( rFactory ),
66 m_sConnectionDescription( sConnectionDescription )
68 virtual void SAL_CALL run();
70 private:
71 Reference < XAcceptor > m_rAcceptor;
72 Reference < XBridgeFactory > m_rBridgeFactory;
73 OUString m_sConnectionDescription;
78 void MyThread::run()
81 while ( true )
83 try
85 Reference < XConnection > rConnection =
86 m_rAcceptor->accept( m_sConnectionDescription );
88 if( ! rConnection.is() )
90 break;
93 Reference < XBridge > rBridge =
94 m_rBridgeFactory->createBridge(
95 OUString() ,
96 OUString("iiop") ,
97 rConnection ,
98 (XInstanceProvider * ) new OInstanceProvider );
102 catch ( ... )
104 printf( "Exception was thrown by acceptor thread\n" );
105 break;
111 int main( int argc, char *argv[] )
113 if( argc < 2 )
115 printf( "usage : testsamprocess host:port\n" );
116 return 0;
120 Reference< XMultiServiceFactory > rSMgr = createRegistryServiceFactory(
121 OUString( "client.rdb" ) );
123 Reference < XConnector > rConnector(
124 createComponent( OUString("com.sun.star.connection.Connector"),
125 OUString( "connector.uno" SAL_DLLEXTENSION),
126 rSMgr ),
127 UNO_QUERY );
129 Reference < XAcceptor > rAcceptor(
130 createComponent( OUString("com.sun.star.connection.Acceptor"),
131 OUString( "acceptor.uno" SAL_DLLEXTENSION),
132 rSMgr ),
133 UNO_QUERY );
135 // just ensure that it is registered
136 // createComponent( OUString("com.sun.star.bridge.Bridge.iiop"),
137 // OUString( "iiopbrdg" SAL_DLLEXTENSION),
138 // rSMgr );
140 Reference < XBridgeFactory > rFactory(
141 createComponent( OUString("com.sun.star.bridge.BridgeFactory"),
142 OUString( "bridgefac.uno" SAL_DLLEXTENSION),
143 rSMgr ),
144 UNO_QUERY );
147 MyThread threadAcceptor( rAcceptor , rFactory , OUString::createFromAscii( argv[1] ) );
149 threadAcceptor.create();
150 TimeValue value={2,0};
151 osl_waitThread( &value );
155 Reference < XConnection > rConnection =
156 rConnector->connect( OUString::createFromAscii( argv[1] ) );
158 printf( "%s\n" , OUStringToOString( rConnection->getDescription(),
159 RTL_TEXTENCODING_ASCII_US ).pData->buffer );
161 if( rFactory.is() )
164 Reference < XBridge > rBridge = rFactory->createBridge(
165 OUString("bla blub"),
166 OUString("iiop"),
167 rConnection,
168 Reference < XInstanceProvider > () );
170 Reference < XInterface > rInitialObject
171 = rBridge->getInstance( OUString("bla") );
173 if( rInitialObject.is() )
175 printf( "got the remote object\n" );
176 testRemote( rInitialObject );
178 printf( "Closing...\n" );
179 TimeValue timeValue={2,0};
180 osl_waitThread( &timeValue );
183 Reference < XBridge > rBridge = rFactory->getBridge(
184 OUString("bla blub") );
185 OSL_ASSERT( ! rBridge.is() );
188 catch( Exception & )
190 printf( "Login failed, got an Exception !\n" );
193 rAcceptor->stopAccepting();
194 threadAcceptor.join();
196 Reference < XComponent > rComp( rFactory , UNO_QUERY );
197 rComp->dispose();
200 rComp = Reference < XComponent > ( rSMgr , UNO_QUERY );
201 rComp->dispose();
203 return 0;
206 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */