update dev300-m57
[ooovba.git] / sal / workben / testpipe.cxx
blob6dfed76d371849eed6c0b176bf65ee2553e0ab8b
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: testpipe.cxx,v $
10 * $Revision: 1.4 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sal.hxx"
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
39 #include <osl/pipe.h>
40 #include <osl/process.h>
41 #include <rtl/ustring.h>
43 // eindeutiger Name f�r die Pipe
44 const char pszPipeName[] = "TestPipe";
45 const char szTestString[] = "This is a test";
46 char szBuffer[256];
48 const char * cp;
49 Size_t n;
50 sSize_t nChars;
52 // osl specific variables
53 oslPipe Pipe;
54 oslPipe C1Pipe;
55 oslProcess Process = NULL;
56 oslProcessError ProcessError;
57 oslProcessInfo ProcessInfo;
59 void fail( const char * pszText, int retval )
61 fprintf( stderr, "TestPipe Server: %s", pszText );
62 fprintf( stderr, "TestPipe Server: test failed, ErrNo: %d.\n", retval );
64 if( Process ) osl_freeProcessHandle( Process );
65 exit( retval );
71 * Teste die Pipe-Implementation in osl
74 int main (int argc, const char *argv[])
76 // erzeuge die Pipe
77 rtl_uString* ustrPipeName=0;
78 rtl_uString* ustrExeName=0;
81 rtl_uString_newFromAscii(&ustrPipeName,pszPipeName);
82 rtl_uString_newFromAscii(&ustrExeName, "//./tmp/testpip2.exe");
84 Pipe = osl_createPipe( ustrPipeName, osl_Pipe_CREATE, 0 );
86 if( !Pipe )
87 fail( "unable to create Pipe.\n",
88 osl_getLastPipeError(NULL));
90 // starte client process
91 ProcessError = osl_executeProcess( ustrExeName,
92 NULL,
94 osl_Process_NORMAL,
96 NULL,
97 NULL,
99 NULL,
100 &Process );
102 if( ProcessError != osl_Process_E_None )
103 fail( "unable to start client.\n", ProcessError );
105 // wait for connection
106 C1Pipe = osl_acceptPipe( Pipe );
108 if( !C1Pipe )
109 fail( "unable to connect to client.\n",
110 osl_getLastPipeError( Pipe ));
113 if( argc > 1 )
115 cp = argv[1];
116 n = strlen( cp ) + 1;
118 else
120 cp = szTestString;
121 n = sizeof(szTestString);
124 // sende TestString zum Client
125 nChars = osl_sendPipe( C1Pipe, cp, n );
127 if( nChars < 0 )
128 fail( "unable to write on pipe.\n",
129 osl_getLastPipeError( Pipe ) );
131 // empfange Daten vom Server
132 nChars = osl_receivePipe( C1Pipe, szBuffer, 256 );
134 if( nChars < 0 )
135 fail( "unable to read from pipe.\n",
136 osl_getLastPipeError( C1Pipe ) );
138 printf( "TestPipe Server: received data: %s.\n", szBuffer );
140 // warte bis das Client-Programm sich beendet
141 ProcessError = osl_joinProcess( Process );
143 if( ProcessError != osl_Process_E_None )
144 fail( "unable to wait for client.\n",
145 ProcessError );
147 // ermittle den R�ckgabewert des Client-Programms
148 ProcessInfo.Size = sizeof( ProcessInfo );
150 ProcessError = osl_getProcessInfo( Process, osl_Process_EXITCODE, &ProcessInfo );
152 if( ProcessError != osl_Process_E_None )
153 fail( "unable to receive return value of client process.\n",
154 ProcessError );
156 if( ProcessInfo.Code != 0 )
157 fail( "client aborted.\n", ProcessInfo.Code );
159 // gib das Handle fuer den Client-Prozess frei
160 osl_freeProcessHandle( Process );
162 // schliesse die Pipes
163 osl_destroyPipe( C1Pipe );
164 osl_destroyPipe( Pipe );
166 printf( "TestPipe Server: test passed.\n" );
167 return 0;