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