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_io.hxx" 26 #include <stdio.h> 27 #include <osl/time.h> 28 29 #include <osl/diagnose.h> 30 #include <osl/thread.hxx> 31 32 #include <cppuhelper/servicefactory.hxx> 33 34 #include <com/sun/star/lang/XComponent.hpp> 35 36 #include <com/sun/star/registry/XImplementationRegistration.hpp> 37 38 #include <com/sun/star/connection/XConnector.hpp> 39 #include <com/sun/star/connection/XAcceptor.hpp> 40 41 using namespace ::osl; 42 using namespace ::rtl; 43 using namespace ::cppu; 44 using namespace ::com::sun::star::uno; 45 using namespace ::com::sun::star::io; 46 using namespace ::com::sun::star::lang; 47 using namespace ::com::sun::star::registry; 48 using namespace ::com::sun::star::connection; 49 50 51 class MyThread : 52 public Thread 53 { 54 public: 55 MyThread( const Reference< XAcceptor > &r , const OUString & sConnectionDescription) : 56 m_rAcceptor( r ), 57 m_sConnectionDescription( sConnectionDescription ) 58 {} 59 virtual void SAL_CALL run(); 60 61 Reference < XAcceptor > m_rAcceptor; 62 private: 63 Reference < XConnection > m_rConnection; 64 OUString m_sConnectionDescription; 65 }; 66 67 void doWrite( const Reference < XConnection > &r ) 68 { 69 Sequence < sal_Int8 > seq(10); 70 for( sal_Int32 i = 0 ; i < 10 ; i ++ ) 71 { 72 seq.getArray()[i] = i; 73 } 74 75 r->write( seq ); 76 } 77 78 void doRead( const Reference < XConnection > &r ) 79 { 80 Sequence < sal_Int8 > seq(10); 81 82 OSL_ASSERT( 10 == r->read( seq , 10 ) ); 83 84 for( sal_Int32 i = 0 ; i < 10 ; i ++ ) 85 { 86 OSL_ASSERT( seq.getConstArray()[i] == i ); 87 } 88 } 89 90 91 void MyThread::run() 92 { 93 try 94 { 95 m_rConnection = m_rAcceptor->accept( m_sConnectionDescription ); 96 } 97 catch ( Exception &e) 98 { 99 OString tmp= OUStringToOString( e.Message , RTL_TEXTENCODING_ASCII_US ); 100 printf( "Exception was thrown by acceptor thread: %s\n", tmp.getStr() ); 101 } 102 103 if( m_rConnection.is() ) 104 { 105 Sequence < sal_Int8 > seq(12); 106 try 107 { 108 doWrite( m_rConnection ); 109 doRead( m_rConnection ); 110 } 111 catch (... ) 112 { 113 printf( "unknown exception was thrown\n" ); 114 throw; 115 } 116 } 117 118 } 119 120 121 122 123 124 void testConnection( const OUString &sConnectionDescription , 125 const Reference < XAcceptor > &rAcceptor, 126 const Reference < XConnector > &rConnector ) 127 { 128 { 129 MyThread thread( rAcceptor , sConnectionDescription ); 130 thread.create(); 131 132 sal_Bool bGotit = sal_False; 133 Reference < XConnection > r; 134 135 while( ! bGotit ) 136 { 137 try 138 { 139 // Why is this wait necessary ???? 140 TimeValue value = {1,0}; 141 osl_waitThread( &value ); 142 r = rConnector->connect( sConnectionDescription ); 143 OSL_ASSERT( r.is() ); 144 doWrite( r ); 145 doRead( r ); 146 bGotit = sal_True; 147 } 148 catch( ... ) 149 { 150 printf( "Couldn't connect, retrying ...\n" ); 151 152 } 153 } 154 155 r->close(); 156 157 try 158 { 159 Sequence < sal_Int8 > seq(10); 160 r->write( seq ); 161 OSL_ENSURE( 0 , "expected exception not thrown" ); 162 } 163 catch ( IOException & ) 164 { 165 // everything is ok 166 } 167 catch ( ... ) 168 { 169 OSL_ENSURE( 0 , "wrong exception was thrown" ); 170 } 171 172 thread.join(); 173 } 174 } 175 176 177 #if (defined UNX) || (defined OS2) 178 int main( int argc, char * argv[] ) 179 #else 180 int __cdecl main( int argc, char * argv[] ) 181 #endif 182 { 183 Reference< XMultiServiceFactory > xMgr( 184 createRegistryServiceFactory( OUString( RTL_CONSTASCII_USTRINGPARAM("applicat.rdb")) ) ); 185 186 Reference< XImplementationRegistration > xImplReg( 187 xMgr->createInstance( OUString::createFromAscii("com.sun.star.registry.ImplementationRegistration") ), UNO_QUERY ); 188 OSL_ENSURE( xImplReg.is(), "### no impl reg!" ); 189 190 OUString aLibName = 191 OUString::createFromAscii( "connector.uno" SAL_DLLEXTENSION ); 192 xImplReg->registerImplementation( 193 OUString::createFromAscii("com.sun.star.loader.SharedLibrary"), aLibName, Reference< XSimpleRegistry >() ); 194 195 aLibName = OUString::createFromAscii( "acceptor.uno" SAL_DLLEXTENSION ); 196 xImplReg->registerImplementation( 197 OUString::createFromAscii("com.sun.star.loader.SharedLibrary"), aLibName, Reference< XSimpleRegistry >() ); 198 199 Reference < XAcceptor > rAcceptor( 200 xMgr->createInstance( 201 OUString::createFromAscii("com.sun.star.connection.Acceptor" ) ) , UNO_QUERY ); 202 203 Reference < XAcceptor > rAcceptorPipe( 204 xMgr->createInstance( 205 OUString::createFromAscii("com.sun.star.connection.Acceptor" ) ) , UNO_QUERY ); 206 207 Reference < XConnector > rConnector( 208 xMgr->createInstance( OUString::createFromAscii("com.sun.star.connection.Connector") ) , UNO_QUERY ); 209 210 211 printf( "Testing sockets" ); 212 fflush( stdout ); 213 testConnection( OUString::createFromAscii("socket,host=localhost,port=2001"), rAcceptor , rConnector ); 214 printf( " Done\n" ); 215 216 printf( "Testing pipe" ); 217 fflush( stdout ); 218 testConnection( OUString::createFromAscii("pipe,name=bla") , rAcceptorPipe , rConnector ); 219 printf( " Done\n" ); 220 221 // check, if errornous strings make any problem 222 rAcceptor = Reference< XAcceptor > ( 223 xMgr->createInstance( OUString::createFromAscii( "com.sun.star.connection.Acceptor" ) ), 224 UNO_QUERY ); 225 226 try 227 { 228 rAcceptor->accept( OUString() ); 229 OSL_ENSURE( 0 , "empty connection string" ); 230 } 231 catch( IllegalArgumentException & ) 232 { 233 // everything is fine 234 } 235 catch( ... ) 236 { 237 OSL_ENSURE( 0, "unexpected akexception with empty connection string" ); 238 } 239 240 try 241 { 242 rConnector->connect( OUString() ); 243 OSL_ENSURE( 0 , "empty connection string" ); 244 } 245 catch( ConnectionSetupException & ) 246 { 247 // everything is fine 248 } 249 catch( ... ) 250 { 251 OSL_ENSURE( 0, "unexpected exception with empty connection string" ); 252 } 253 254 255 MyThread thread( rAcceptor , OUString::createFromAscii("socket,host=localhost,port=2001") ); 256 thread.create(); 257 258 TimeValue value = {0,1}; 259 osl_waitThread( &value ); 260 try 261 { 262 rAcceptor->accept( OUString::createFromAscii("socket,host=localhost,port=2001") ); 263 OSL_ENSURE( 0 , "already existing exception expected" ); 264 } 265 catch( AlreadyAcceptingException & e) 266 { 267 // everything is fine 268 } 269 catch( ... ) 270 { 271 OSL_ENSURE( 0, "unknown exception, already existing existing expected" ); 272 } 273 274 rAcceptor->stopAccepting(); 275 thread.join(); 276 277 Reference < XComponent > rComp( xMgr , UNO_QUERY ); 278 if( rComp.is() ) 279 { 280 rComp->dispose(); 281 } 282 } 283