merged tag ooo/DEV300_m102
[LibreOffice.git] / sal / workben / testpipe.cxx
blob6b860351d6b4788de1a217d08caebbec1d605d8e
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sal.hxx"
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
36 #include <osl/pipe.h>
37 #include <osl/process.h>
38 #include <rtl/ustring.h>
40 // eindeutiger Name f�r die Pipe
41 const char pszPipeName[] = "TestPipe";
42 const char szTestString[] = "This is a test";
43 char szBuffer[256];
45 const char * cp;
46 Size_t n;
47 sSize_t nChars;
49 // osl specific variables
50 oslPipe Pipe;
51 oslPipe C1Pipe;
52 oslProcess Process = NULL;
53 oslProcessError ProcessError;
54 oslProcessInfo ProcessInfo;
56 void fail( const char * pszText, int retval )
58 fprintf( stderr, "TestPipe Server: %s", pszText );
59 fprintf( stderr, "TestPipe Server: test failed, ErrNo: %d.\n", retval );
61 if( Process ) osl_freeProcessHandle( Process );
62 exit( retval );
68 * Teste die Pipe-Implementation in osl
71 int main (int argc, const char *argv[])
73 // erzeuge die Pipe
74 rtl_uString* ustrPipeName=0;
75 rtl_uString* ustrExeName=0;
78 rtl_uString_newFromAscii(&ustrPipeName,pszPipeName);
79 rtl_uString_newFromAscii(&ustrExeName, "//./tmp/testpip2.exe");
81 Pipe = osl_createPipe( ustrPipeName, osl_Pipe_CREATE, 0 );
83 if( !Pipe )
84 fail( "unable to create Pipe.\n",
85 osl_getLastPipeError(NULL));
87 // starte client process
88 ProcessError = osl_executeProcess( ustrExeName,
89 NULL,
91 osl_Process_NORMAL,
93 NULL,
94 NULL,
96 NULL,
97 &Process );
99 if( ProcessError != osl_Process_E_None )
100 fail( "unable to start client.\n", ProcessError );
102 // wait for connection
103 C1Pipe = osl_acceptPipe( Pipe );
105 if( !C1Pipe )
106 fail( "unable to connect to client.\n",
107 osl_getLastPipeError( Pipe ));
110 if( argc > 1 )
112 cp = argv[1];
113 n = strlen( cp ) + 1;
115 else
117 cp = szTestString;
118 n = sizeof(szTestString);
121 // sende TestString zum Client
122 nChars = osl_sendPipe( C1Pipe, cp, n );
124 if( nChars < 0 )
125 fail( "unable to write on pipe.\n",
126 osl_getLastPipeError( Pipe ) );
128 // empfange Daten vom Server
129 nChars = osl_receivePipe( C1Pipe, szBuffer, 256 );
131 if( nChars < 0 )
132 fail( "unable to read from pipe.\n",
133 osl_getLastPipeError( C1Pipe ) );
135 printf( "TestPipe Server: received data: %s.\n", szBuffer );
137 // warte bis das Client-Programm sich beendet
138 ProcessError = osl_joinProcess( Process );
140 if( ProcessError != osl_Process_E_None )
141 fail( "unable to wait for client.\n",
142 ProcessError );
144 // ermittle den R�ckgabewert des Client-Programms
145 ProcessInfo.Size = sizeof( ProcessInfo );
147 ProcessError = osl_getProcessInfo( Process, osl_Process_EXITCODE, &ProcessInfo );
149 if( ProcessError != osl_Process_E_None )
150 fail( "unable to receive return value of client process.\n",
151 ProcessError );
153 if( ProcessInfo.Code != 0 )
154 fail( "client aborted.\n", ProcessInfo.Code );
156 // gib das Handle fuer den Client-Prozess frei
157 osl_freeProcessHandle( Process );
159 // schliesse die Pipes
160 osl_destroyPipe( C1Pipe );
161 osl_destroyPipe( Pipe );
163 printf( "TestPipe Server: test passed.\n" );
164 return 0;