xref: /aoo42x/main/sal/workben/testpipe.cxx (revision 87d2adbc)
1*87d2adbcSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*87d2adbcSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*87d2adbcSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*87d2adbcSAndrew Rist  * distributed with this work for additional information
6*87d2adbcSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*87d2adbcSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*87d2adbcSAndrew Rist  * "License"); you may not use this file except in compliance
9*87d2adbcSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*87d2adbcSAndrew Rist  *
11*87d2adbcSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*87d2adbcSAndrew Rist  *
13*87d2adbcSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*87d2adbcSAndrew Rist  * software distributed under the License is distributed on an
15*87d2adbcSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*87d2adbcSAndrew Rist  * KIND, either express or implied.  See the License for the
17*87d2adbcSAndrew Rist  * specific language governing permissions and limitations
18*87d2adbcSAndrew Rist  * under the License.
19*87d2adbcSAndrew Rist  *
20*87d2adbcSAndrew Rist  *************************************************************/
21*87d2adbcSAndrew Rist 
22*87d2adbcSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sal.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir 
28cdf0e10cSrcweir #include <stdio.h>
29cdf0e10cSrcweir #include <stdlib.h>
30cdf0e10cSrcweir #include <string.h>
31cdf0e10cSrcweir 
32cdf0e10cSrcweir #include <osl/pipe.h>
33cdf0e10cSrcweir #include <osl/process.h>
34cdf0e10cSrcweir #include <rtl/ustring.h>
35cdf0e10cSrcweir 
36cdf0e10cSrcweir // eindeutiger Name f�r die Pipe
37cdf0e10cSrcweir const char pszPipeName[] = "TestPipe";
38cdf0e10cSrcweir const char szTestString[] = "This is a test";
39cdf0e10cSrcweir char       szBuffer[256];
40cdf0e10cSrcweir 
41cdf0e10cSrcweir const char *  cp;
42cdf0e10cSrcweir Size_t  n;
43cdf0e10cSrcweir sSize_t nChars;
44cdf0e10cSrcweir 
45cdf0e10cSrcweir // osl specific variables
46cdf0e10cSrcweir oslPipe          Pipe;
47cdf0e10cSrcweir oslPipe          C1Pipe;
48cdf0e10cSrcweir oslProcess       Process = NULL;
49cdf0e10cSrcweir oslProcessError  ProcessError;
50cdf0e10cSrcweir oslProcessInfo   ProcessInfo;
51cdf0e10cSrcweir 
fail(const char * pszText,int retval)52cdf0e10cSrcweir void fail( const char * pszText, int retval )
53cdf0e10cSrcweir {
54cdf0e10cSrcweir     fprintf( stderr, "TestPipe Server: %s", pszText );
55cdf0e10cSrcweir     fprintf( stderr, "TestPipe Server: test failed, ErrNo: %d.\n", retval );
56cdf0e10cSrcweir 
57cdf0e10cSrcweir     if( Process ) osl_freeProcessHandle( Process );
58cdf0e10cSrcweir     exit( retval );
59cdf0e10cSrcweir }
60cdf0e10cSrcweir 
61cdf0e10cSrcweir 
62cdf0e10cSrcweir 
63cdf0e10cSrcweir /*
64cdf0e10cSrcweir  * Teste die Pipe-Implementation in osl
65cdf0e10cSrcweir  */
66cdf0e10cSrcweir 
main(int argc,const char * argv[])67cdf0e10cSrcweir int main (int argc, const char *argv[])
68cdf0e10cSrcweir {
69cdf0e10cSrcweir     // erzeuge die Pipe
70cdf0e10cSrcweir     rtl_uString* ustrPipeName=0;
71cdf0e10cSrcweir     rtl_uString* ustrExeName=0;
72cdf0e10cSrcweir 
73cdf0e10cSrcweir 
74cdf0e10cSrcweir     rtl_uString_newFromAscii(&ustrPipeName,pszPipeName);
75cdf0e10cSrcweir     rtl_uString_newFromAscii(&ustrExeName, "//./tmp/testpip2.exe");
76cdf0e10cSrcweir 
77cdf0e10cSrcweir     Pipe = osl_createPipe( ustrPipeName, osl_Pipe_CREATE, 0 );
78cdf0e10cSrcweir 
79cdf0e10cSrcweir     if( !Pipe )
80cdf0e10cSrcweir         fail( "unable to create Pipe.\n",
81cdf0e10cSrcweir               osl_getLastPipeError(NULL));
82cdf0e10cSrcweir 
83cdf0e10cSrcweir     // starte client process
84cdf0e10cSrcweir     ProcessError = osl_executeProcess( ustrExeName,
85cdf0e10cSrcweir                                        NULL,
86cdf0e10cSrcweir                                        0,
87cdf0e10cSrcweir                                         osl_Process_NORMAL,
88cdf0e10cSrcweir                                         0,
89cdf0e10cSrcweir                                         NULL,
90cdf0e10cSrcweir                                        NULL,
91cdf0e10cSrcweir                                        0,
92cdf0e10cSrcweir                                         NULL,
93cdf0e10cSrcweir                                         &Process );
94cdf0e10cSrcweir 
95cdf0e10cSrcweir     if( ProcessError != osl_Process_E_None )
96cdf0e10cSrcweir         fail( "unable to start client.\n", ProcessError );
97cdf0e10cSrcweir 
98cdf0e10cSrcweir     // wait for connection
99cdf0e10cSrcweir     C1Pipe = osl_acceptPipe( Pipe );
100cdf0e10cSrcweir 
101cdf0e10cSrcweir     if( !C1Pipe )
102cdf0e10cSrcweir         fail( "unable to connect to client.\n",
103cdf0e10cSrcweir             osl_getLastPipeError( Pipe ));
104cdf0e10cSrcweir 
105cdf0e10cSrcweir 
106cdf0e10cSrcweir     if( argc > 1 )
107cdf0e10cSrcweir     {
108cdf0e10cSrcweir         cp = argv[1];
109cdf0e10cSrcweir         n  = strlen( cp ) + 1;
110cdf0e10cSrcweir     }
111cdf0e10cSrcweir     else
112cdf0e10cSrcweir     {
113cdf0e10cSrcweir         cp = szTestString;
114cdf0e10cSrcweir         n  = sizeof(szTestString);
115cdf0e10cSrcweir     }
116cdf0e10cSrcweir 
117cdf0e10cSrcweir     // sende TestString zum Client
118cdf0e10cSrcweir     nChars = osl_sendPipe( C1Pipe, cp, n );
119cdf0e10cSrcweir 
120cdf0e10cSrcweir     if( nChars < 0 )
121cdf0e10cSrcweir         fail( "unable to write on pipe.\n",
122cdf0e10cSrcweir               osl_getLastPipeError( Pipe ) );
123cdf0e10cSrcweir 
124cdf0e10cSrcweir     // empfange Daten vom Server
125cdf0e10cSrcweir     nChars = osl_receivePipe( C1Pipe, szBuffer, 256 );
126cdf0e10cSrcweir 
127cdf0e10cSrcweir     if( nChars < 0 )
128cdf0e10cSrcweir         fail( "unable to read from pipe.\n",
129cdf0e10cSrcweir               osl_getLastPipeError( C1Pipe ) );
130cdf0e10cSrcweir 
131cdf0e10cSrcweir     printf( "TestPipe Server: received data: %s.\n", szBuffer );
132cdf0e10cSrcweir 
133cdf0e10cSrcweir     // warte bis das Client-Programm sich beendet
134cdf0e10cSrcweir     ProcessError = osl_joinProcess( Process );
135cdf0e10cSrcweir 
136cdf0e10cSrcweir     if( ProcessError != osl_Process_E_None )
137cdf0e10cSrcweir         fail( "unable to wait for client.\n",
138cdf0e10cSrcweir               ProcessError );
139cdf0e10cSrcweir 
140cdf0e10cSrcweir     // ermittle den R�ckgabewert des Client-Programms
141cdf0e10cSrcweir     ProcessInfo.Size = sizeof( ProcessInfo );
142cdf0e10cSrcweir 
143cdf0e10cSrcweir     ProcessError = osl_getProcessInfo( Process, osl_Process_EXITCODE, &ProcessInfo );
144cdf0e10cSrcweir 
145cdf0e10cSrcweir     if( ProcessError != osl_Process_E_None )
146cdf0e10cSrcweir         fail( "unable to receive return value of client process.\n",
147cdf0e10cSrcweir               ProcessError );
148cdf0e10cSrcweir 
149cdf0e10cSrcweir     if( ProcessInfo.Code != 0 )
150cdf0e10cSrcweir         fail( "client aborted.\n", ProcessInfo.Code );
151cdf0e10cSrcweir 
152cdf0e10cSrcweir     // gib das Handle fuer den Client-Prozess frei
153cdf0e10cSrcweir     osl_freeProcessHandle( Process );
154cdf0e10cSrcweir 
155cdf0e10cSrcweir     // schliesse die Pipes
156cdf0e10cSrcweir     osl_destroyPipe( C1Pipe );
157cdf0e10cSrcweir     osl_destroyPipe( Pipe );
158cdf0e10cSrcweir 
159cdf0e10cSrcweir     printf( "TestPipe Server: test passed.\n" );
160cdf0e10cSrcweir     return 0;
161cdf0e10cSrcweir }
162cdf0e10cSrcweir 
163cdf0e10cSrcweir 
164cdf0e10cSrcweir 
165