xref: /trunk/main/sal/workben/testpipe.cxx (revision 87d2adbc)
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_sal.hxx"
26 
27 
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 
32 #include <osl/pipe.h>
33 #include <osl/process.h>
34 #include <rtl/ustring.h>
35 
36 // eindeutiger Name f�r die Pipe
37 const char pszPipeName[] = "TestPipe";
38 const char szTestString[] = "This is a test";
39 char       szBuffer[256];
40 
41 const char *  cp;
42 Size_t  n;
43 sSize_t nChars;
44 
45 // osl specific variables
46 oslPipe          Pipe;
47 oslPipe          C1Pipe;
48 oslProcess       Process = NULL;
49 oslProcessError  ProcessError;
50 oslProcessInfo   ProcessInfo;
51 
fail(const char * pszText,int retval)52 void fail( const char * pszText, int retval )
53 {
54     fprintf( stderr, "TestPipe Server: %s", pszText );
55     fprintf( stderr, "TestPipe Server: test failed, ErrNo: %d.\n", retval );
56 
57     if( Process ) osl_freeProcessHandle( Process );
58     exit( retval );
59 }
60 
61 
62 
63 /*
64  * Teste die Pipe-Implementation in osl
65  */
66 
main(int argc,const char * argv[])67 int main (int argc, const char *argv[])
68 {
69     // erzeuge die Pipe
70     rtl_uString* ustrPipeName=0;
71     rtl_uString* ustrExeName=0;
72 
73 
74     rtl_uString_newFromAscii(&ustrPipeName,pszPipeName);
75     rtl_uString_newFromAscii(&ustrExeName, "//./tmp/testpip2.exe");
76 
77     Pipe = osl_createPipe( ustrPipeName, osl_Pipe_CREATE, 0 );
78 
79     if( !Pipe )
80         fail( "unable to create Pipe.\n",
81               osl_getLastPipeError(NULL));
82 
83     // starte client process
84     ProcessError = osl_executeProcess( ustrExeName,
85                                        NULL,
86                                        0,
87                                         osl_Process_NORMAL,
88                                         0,
89                                         NULL,
90                                        NULL,
91                                        0,
92                                         NULL,
93                                         &Process );
94 
95     if( ProcessError != osl_Process_E_None )
96         fail( "unable to start client.\n", ProcessError );
97 
98     // wait for connection
99     C1Pipe = osl_acceptPipe( Pipe );
100 
101     if( !C1Pipe )
102         fail( "unable to connect to client.\n",
103             osl_getLastPipeError( Pipe ));
104 
105 
106     if( argc > 1 )
107     {
108         cp = argv[1];
109         n  = strlen( cp ) + 1;
110     }
111     else
112     {
113         cp = szTestString;
114         n  = sizeof(szTestString);
115     }
116 
117     // sende TestString zum Client
118     nChars = osl_sendPipe( C1Pipe, cp, n );
119 
120     if( nChars < 0 )
121         fail( "unable to write on pipe.\n",
122               osl_getLastPipeError( Pipe ) );
123 
124     // empfange Daten vom Server
125     nChars = osl_receivePipe( C1Pipe, szBuffer, 256 );
126 
127     if( nChars < 0 )
128         fail( "unable to read from pipe.\n",
129               osl_getLastPipeError( C1Pipe ) );
130 
131     printf( "TestPipe Server: received data: %s.\n", szBuffer );
132 
133     // warte bis das Client-Programm sich beendet
134     ProcessError = osl_joinProcess( Process );
135 
136     if( ProcessError != osl_Process_E_None )
137         fail( "unable to wait for client.\n",
138               ProcessError );
139 
140     // ermittle den R�ckgabewert des Client-Programms
141     ProcessInfo.Size = sizeof( ProcessInfo );
142 
143     ProcessError = osl_getProcessInfo( Process, osl_Process_EXITCODE, &ProcessInfo );
144 
145     if( ProcessError != osl_Process_E_None )
146         fail( "unable to receive return value of client process.\n",
147               ProcessError );
148 
149     if( ProcessInfo.Code != 0 )
150         fail( "client aborted.\n", ProcessInfo.Code );
151 
152     // gib das Handle fuer den Client-Prozess frei
153     osl_freeProcessHandle( Process );
154 
155     // schliesse die Pipes
156     osl_destroyPipe( C1Pipe );
157     osl_destroyPipe( Pipe );
158 
159     printf( "TestPipe Server: test passed.\n" );
160     return 0;
161 }
162 
163 
164 
165