xref: /AOO41X/main/bridges/test/testserver.cxx (revision 61dff127b6698e0bae836c8aedd6ec62111483d1)
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 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_bridges.hxx"
26 #include <string.h>
27 #include <osl/time.h>
28 
29 #include <osl/mutex.hxx>
30 #include <osl/conditn.h>
31 
32 #include <osl/thread.hxx>
33 
34 #include <cppuhelper/servicefactory.hxx>
35 #include <cppuhelper/implbase1.hxx>
36 
37 #include <com/sun/star/connection/XAcceptor.hpp>
38 #include <com/sun/star/connection/XConnection.hpp>
39 
40 #include <com/sun/star/bridge/XInstanceProvider.hpp>
41 #include <com/sun/star/bridge/XBridgeFactory.hpp>
42 
43 #include <com/sun/star/lang/XComponent.hpp>
44 #include <com/sun/star/lang/XInitialization.hpp>
45 
46 
47 #include <test/XTestFactory.hpp>
48 
49 #include <cppuhelper/weak.hxx>
50 
51 using namespace ::test;
52 using namespace ::rtl;
53 using namespace ::osl;
54 using namespace ::cppu;
55 using namespace ::com::sun::star::uno;
56 using namespace ::com::sun::star::lang;
57 using namespace ::com::sun::star::bridge;
58 using namespace ::com::sun::star::connection;
59 #include "testcomp.h"
60 #ifdef SAL_W32
61 #include <conio.h>
62 #endif
63 
64 /*********
65  *
66  ********/
67 
68 
69 
70 class MyThread :
71     public Thread
72 {
73 public:
MyThread(const Reference<XAcceptor> & r,const Reference<XBridgeFactory> & rFactory,const Reference<XMultiServiceFactory> & rSMgr,const OUString & sConnectionDescription,const OUString & sProtocol,sal_Bool bReverse,sal_Bool bLatency)74     MyThread( const Reference< XAcceptor > &r ,
75               const Reference< XBridgeFactory > &rFactory,
76               const Reference< XMultiServiceFactory > &rSMgr,
77               const OUString &sConnectionDescription,
78               const OUString &sProtocol,
79               sal_Bool bReverse,
80               sal_Bool bLatency ) :
81         m_rAcceptor( r ),
82         m_rBridgeFactory ( rFactory ),
83         m_rSMgr( rSMgr ),
84         m_sConnectionDescription( sConnectionDescription ),
85         m_sProtocol( sProtocol ),
86         m_bReverse( bReverse ),
87         m_bLatency( bLatency )
88         {}
89     virtual void SAL_CALL run();
90 
91     void latencyTest( const Reference< XConnection > &r );
92 
93 private:
94     Reference < XAcceptor > m_rAcceptor;
95     Reference < XBridgeFactory > m_rBridgeFactory;
96     Reference < XMultiServiceFactory > m_rSMgr;
97     OUString m_sConnectionDescription;
98     OUString m_sProtocol;
99     sal_Bool m_bReverse;
100     sal_Bool m_bLatency;
101 };
102 
103 
latencyTest(const Reference<XConnection> & r)104 void MyThread::latencyTest( const Reference< XConnection > &r )
105 {
106     Sequence < sal_Int8 > s;
107     while( 12 == r->read( s , 12 ) )
108     {
109         r->read( s , 188 );
110         s = Sequence < sal_Int8 >(60);
111         r->write( s );
112     }
113 }
114 
run()115 void MyThread::run()
116 {
117 
118     while ( sal_True )
119     {
120         try
121         {
122             Reference < XConnection > rConnection =
123                 m_rAcceptor->accept( m_sConnectionDescription );
124 
125             if( ! rConnection.is() )
126             {
127                 break;
128             }
129             if( m_bLatency )
130             {
131                 latencyTest( rConnection );
132             }
133             else
134             {
135 
136                 Reference < XBridge > rBridge =
137                     m_rBridgeFactory->createBridge(
138                         OUString() ,
139                         m_sProtocol,
140                         rConnection ,
141                         (XInstanceProvider * ) new OInstanceProvider(m_rSMgr) );
142 
143 
144                 if( m_bReverse )
145                 {
146                     printf( "doing reverse callme test (test is ok, when on each line a +- appears\n" );
147                     Reference < XInterface > r = rBridge->getInstance(
148                         OUString( RTL_CONSTASCII_USTRINGPARAM("blubber"  )));
149                     Reference < XTestFactory > rFactory( r , UNO_QUERY );
150                     Reference < XCallMe > rCallMe = rFactory->createCallMe();
151 
152                     for( sal_Int32 i = 0 ; i < 1  ; i ++ )
153                     {
154                         rCallMe->callOneway(
155                             OUString( RTL_CONSTASCII_USTRINGPARAM("my test string")) , 2 );
156                     }
157                     printf( "all oneway are send\n" );
158                     rCallMe->call( OUString::createFromAscii( "reverse call me test finished" ) , 0 );
159                 printf( "revers callme test finished\n" );
160                 }
161             }
162         }
163         catch ( Exception & e )
164         {
165             printf( "Exception was thrown by acceptor \n" );
166             OString o = OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US );
167             printf( "%s\n" , o.getStr() );
168             break;
169         }
170         catch ( ... )
171         {
172             printf( "Exception was thrown by acceptor thread\n" );
173             break;
174         }
175     }
176 }
177 
178 
main(int argc,char * argv[])179 int main( int argc, char *argv[] )
180 {
181 //  testserver();
182 
183     if( argc < 2 )
184     {
185         printf( "usage : testserver [-r] connectionstring\n"
186                 "        -r does a reverse test (server calls client)\n" );
187         return 0;
188     }
189 
190     OUString sConnectionString;
191     OUString sProtocol;
192     sal_Bool bReverse = sal_False;
193     sal_Bool bLatency = sal_False;
194 
195     parseCommandLine( argv , &sConnectionString , &sProtocol , &bLatency , &bReverse );
196 
197     {
198         Reference< XMultiServiceFactory > rSMgr = createRegistryServiceFactory(
199             OUString( RTL_CONSTASCII_USTRINGPARAM( "server.rdb" )  ) );
200 
201         Reference < XBridgeFactory > rBridgeFactory ( createComponent(
202             OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.bridge.BridgeFactory")),
203             OUString( RTL_CONSTASCII_USTRINGPARAM("bridgefac.uno" SAL_DLLEXTENSION )),
204             rSMgr ),
205                                                      UNO_QUERY );
206 
207 
208         createComponent( OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.bridge.Bridge.iiop")),
209                          OUString( RTL_CONSTASCII_USTRINGPARAM("remotebridge.uno" SAL_DLLEXTENSION)),
210                          rSMgr );
211 
212 
213         Reference < XAcceptor > rAcceptor(
214             createComponent( OUString( RTL_CONSTASCII_USTRINGPARAM("com.sun.star.connection.Acceptor")),
215                              OUString( RTL_CONSTASCII_USTRINGPARAM("acceptor.uno" SAL_DLLEXTENSION)),
216                              rSMgr ) ,
217             UNO_QUERY );
218 
219         MyThread thread( rAcceptor ,
220                          rBridgeFactory,
221                          rSMgr,
222                          sConnectionString,
223                          sProtocol,
224                          bReverse,
225                          bLatency);
226         thread.create();
227 
228 #ifdef SAL_W32
229         _getch();
230 #elif  SOLARIS
231         getchar();
232 #elif LINUX
233         TimeValue value={360,0};
234         osl_waitThread( &value );
235 #endif
236         printf( "Closing...\n" );
237 
238         rAcceptor->stopAccepting();
239         thread.join();
240 
241         printf( "Closed\n" );
242 
243         Reference < XComponent > rComp2( rBridgeFactory , UNO_QUERY );
244         rComp2->dispose();
245         Reference < XComponent > rComp( rSMgr, UNO_QUERY );
246         rComp->dispose();
247     }
248     return 0;
249 }
250