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