build: Add missing "static" keyword where it should be used
[platform/upstream/glib.git] / tests / gio-test.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 2000  Tor Lillqvist
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /* A test program for the main loop and IO channel code.
21  * Just run it. Optional parameter is number of sub-processes.
22  */
23
24 #undef G_DISABLE_ASSERT
25 #undef G_LOG_DOMAIN
26
27 #include "config.h"
28
29 #include <glib.h>
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <math.h>
34 #include <time.h>
35
36 #ifdef G_OS_WIN32
37   #include <io.h>
38   #include <fcntl.h>
39   #include <process.h>
40   #define STRICT
41   #include <windows.h>
42   #define pipe(fds) _pipe(fds, 4096, _O_BINARY)
43 #else
44   #ifdef HAVE_UNISTD_H
45     #include <unistd.h>
46   #endif
47 #endif
48
49 static int nrunning;
50 static GMainLoop *main_loop;
51
52 #define BUFSIZE 5000            /* Larger than the circular buffer in
53                                  * giowin32.c on purpose.
54                                  */
55
56 static int nkiddies;
57
58 static struct {
59   int fd;
60   int seq;
61 } *seqtab;
62
63 static GIOError
64 read_all (int         fd,
65           GIOChannel *channel,
66           char       *buffer,
67           guint       nbytes,
68           guint      *bytes_read)
69 {
70   guint left = nbytes;
71   gsize nb;
72   GIOError error = G_IO_ERROR_NONE;
73   char *bufp = buffer;
74
75   /* g_io_channel_read() doesn't necessarily return all the
76    * data we want at once.
77    */
78   *bytes_read = 0;
79   while (left)
80     {
81       error = g_io_channel_read (channel, bufp, left, &nb);
82       
83       if (error != G_IO_ERROR_NONE)
84         {
85           g_print ("gio-test: ...from %d: %d\n", fd, error);
86           if (error == G_IO_ERROR_AGAIN)
87             continue;
88           break;
89         }
90       if (nb == 0)
91         return error;
92       left -= nb;
93       bufp += nb;
94       *bytes_read += nb;
95     }
96   return error;
97 }
98
99 static void
100 shutdown_source (gpointer data)
101 {
102   if (g_source_remove (*(guint *) data))
103     {
104       nrunning--;
105       if (nrunning == 0)
106         g_main_loop_quit (main_loop);
107     }
108 }
109
110 static gboolean
111 recv_message (GIOChannel  *channel,
112               GIOCondition cond,
113               gpointer    data)
114 {
115   gint fd = g_io_channel_unix_get_fd (channel);
116   gboolean retval = TRUE;
117
118 #ifdef VERBOSE
119   g_print ("gio-test: ...from %d:%s%s%s%s\n", fd,
120            (cond & G_IO_ERR) ? " ERR" : "",
121            (cond & G_IO_HUP) ? " HUP" : "",
122            (cond & G_IO_IN)  ? " IN"  : "",
123            (cond & G_IO_PRI) ? " PRI" : "");
124 #endif
125
126   if (cond & (G_IO_ERR | G_IO_HUP))
127     {
128       shutdown_source (data);
129       retval = FALSE;
130     }
131
132   if (cond & G_IO_IN)
133     {
134       char buf[BUFSIZE];
135       guint nbytes;
136       guint nb;
137       int i, j, seq;
138       GIOError error;
139       
140       error = read_all (fd, channel, (gchar *) &seq, sizeof (seq), &nb);
141       if (error == G_IO_ERROR_NONE)
142         {
143           if (nb == 0)
144             {
145 #ifdef VERBOSE
146               g_print ("gio-test: ...from %d: EOF\n", fd);
147 #endif
148               shutdown_source (data);
149               return FALSE;
150             }
151           
152           g_assert (nb == sizeof (nbytes));
153
154           for (i = 0; i < nkiddies; i++)
155             if (seqtab[i].fd == fd)
156               {
157                 g_assert_cmpint (seq, ==, seqtab[i].seq);
158                 seqtab[i].seq++;
159                 break;
160               }
161
162           error = read_all (fd, channel, (gchar *) &nbytes, sizeof (nbytes), &nb);
163         }
164
165       if (error != G_IO_ERROR_NONE)
166         return FALSE;
167       
168       if (nb == 0)
169         {
170 #ifdef VERBOSE
171           g_print ("gio-test: ...from %d: EOF\n", fd);
172 #endif
173           shutdown_source (data);
174           return FALSE;
175         }
176       
177       g_assert (nb == sizeof (nbytes));
178
179       g_assert_cmpint (nbytes, <, BUFSIZE);
180       g_assert (nbytes >= 0 && nbytes < BUFSIZE);
181 #ifdef VERBOSE      
182       g_print ("gio-test: ...from %d: %d bytes\n", fd, nbytes);
183 #endif      
184       if (nbytes > 0)
185         {
186           error = read_all (fd, channel, buf, nbytes, &nb);
187
188           if (error != G_IO_ERROR_NONE)
189             return FALSE;
190
191           if (nb == 0)
192             {
193 #ifdef VERBOSE
194               g_print ("gio-test: ...from %d: EOF\n", fd);
195 #endif
196               shutdown_source (data);
197               return FALSE;
198             }
199       
200           for (j = 0; j < nbytes; j++)
201             g_assert (buf[j] == ' ' + ((nbytes + j) % 95));
202 #ifdef VERBOSE
203           g_print ("gio-test: ...from %d: OK\n", fd);
204 #endif
205         }
206     }
207   return retval;
208 }
209
210 #ifdef G_OS_WIN32
211
212 static gboolean
213 recv_windows_message (GIOChannel  *channel,
214                       GIOCondition cond,
215                       gpointer    data)
216 {
217   GIOError error;
218   MSG msg;
219   guint nb;
220   
221   while (1)
222     {
223       error = g_io_channel_read (channel, &msg, sizeof (MSG), &nb);
224       
225       if (error != G_IO_ERROR_NONE)
226         {
227           g_print ("gio-test: ...reading Windows message: G_IO_ERROR_%s\n",
228                    (error == G_IO_ERROR_AGAIN ? "AGAIN" :
229                     (error == G_IO_ERROR_INVAL ? "INVAL" :
230                      (error == G_IO_ERROR_UNKNOWN ? "UNKNOWN" : "???"))));
231           if (error == G_IO_ERROR_AGAIN)
232             continue;
233         }
234       break;
235     }
236
237   g_print ("gio-test: ...Windows message for %#x: %d,%d,%d\n",
238            msg.hwnd, msg.message, msg.wParam, msg.lParam);
239
240   return TRUE;
241 }
242
243 LRESULT CALLBACK 
244 window_procedure (HWND hwnd,
245                   UINT message,
246                   WPARAM wparam,
247                   LPARAM lparam)
248 {
249   g_print ("gio-test: window_procedure for %#x: %d,%d,%d\n",
250            hwnd, message, wparam, lparam);
251   return DefWindowProc (hwnd, message, wparam, lparam);
252 }
253
254 #endif
255
256 int
257 main (int    argc,
258       char **argv)
259 {
260   if (argc < 3)
261     {
262       /* Parent */
263       
264       GIOChannel *my_read_channel;
265       gchar *cmdline;
266       guint *id;
267       int i;
268 #ifdef G_OS_WIN32
269       GTimeVal start, end;
270       GPollFD pollfd;
271       int pollresult;
272       ATOM klass;
273       static WNDCLASS wcl;
274       HWND hwnd;
275       GIOChannel *windows_messages_channel;
276 #endif
277
278       nkiddies = (argc == 1 ? 1 : atoi(argv[1]));
279       seqtab = g_malloc (nkiddies * 2 * sizeof (int));
280
281 #ifdef G_OS_WIN32
282       wcl.style = 0;
283       wcl.lpfnWndProc = window_procedure;
284       wcl.cbClsExtra = 0;
285       wcl.cbWndExtra = 0;
286       wcl.hInstance = GetModuleHandle (NULL);
287       wcl.hIcon = NULL;
288       wcl.hCursor = NULL;
289       wcl.hbrBackground = NULL;
290       wcl.lpszMenuName = NULL;
291       wcl.lpszClassName = "gio-test";
292
293       klass = RegisterClass (&wcl);
294
295       if (!klass)
296         {
297           g_print ("gio-test: RegisterClass failed\n");
298           exit (1);
299         }
300
301       hwnd = CreateWindow (MAKEINTATOM(klass), "gio-test", 0, 0, 0, 10, 10,
302                            NULL, NULL, wcl.hInstance, NULL);
303       if (!hwnd)
304         {
305           g_print ("gio-test: CreateWindow failed\n");
306           exit (1);
307         }
308
309       windows_messages_channel = g_io_channel_win32_new_messages ((guint)hwnd);
310       g_io_add_watch (windows_messages_channel, G_IO_IN, recv_windows_message, 0);
311 #endif
312
313       for (i = 0; i < nkiddies; i++)
314         {
315           int pipe_to_sub[2], pipe_from_sub[2];
316           
317           if (pipe (pipe_to_sub) == -1 ||
318               pipe (pipe_from_sub) == -1)
319             perror ("pipe"), exit (1);
320           
321           seqtab[i].fd = pipe_from_sub[0];
322           seqtab[i].seq = 0;
323
324           my_read_channel = g_io_channel_unix_new (pipe_from_sub[0]);
325           
326           id = g_new (guint, 1);
327           *id =
328             g_io_add_watch (my_read_channel,
329                             G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP,
330                             recv_message,
331                             id);
332           
333           nrunning++;
334           
335 #ifdef G_OS_WIN32
336           cmdline = g_strdup_printf ("%d:%d:%d",
337                                      pipe_to_sub[0],
338                                      pipe_from_sub[1],
339                                      hwnd);
340           _spawnl (_P_NOWAIT, argv[0], argv[0], "--child", cmdline, NULL);
341 #else
342           cmdline = g_strdup_printf ("%s --child %d:%d &", argv[0],
343                                      pipe_to_sub[0], pipe_from_sub[1]);
344           
345           system (cmdline);
346 #endif
347           close (pipe_to_sub[0]);
348           close (pipe_from_sub [1]);
349
350 #ifdef G_OS_WIN32
351           g_get_current_time (&start);
352           g_io_channel_win32_make_pollfd (my_read_channel, G_IO_IN, &pollfd);
353           pollresult = g_io_channel_win32_poll (&pollfd, 1, 100);
354           g_get_current_time (&end);
355           if (end.tv_usec < start.tv_usec)
356             end.tv_sec--, end.tv_usec += 1000000;
357           g_print ("gio-test: had to wait %ld.%03ld s, result:%d\n",
358                    end.tv_sec - start.tv_sec,
359                    (end.tv_usec - start.tv_usec) / 1000,
360                    pollresult);
361 #endif
362         }
363       
364       main_loop = g_main_loop_new (NULL, FALSE);
365       
366       g_main_loop_run (main_loop);
367     }
368   else if (argc == 3)
369     {
370       /* Child */
371       
372       int readfd, writefd;
373 #ifdef G_OS_WIN32
374       HWND hwnd;
375 #endif
376       int i, j;
377       char buf[BUFSIZE];
378       int buflen;
379       GTimeVal tv;
380       int n;
381   
382       g_get_current_time (&tv);
383       
384       sscanf (argv[2], "%d:%d%n", &readfd, &writefd, &n);
385
386 #ifdef G_OS_WIN32
387       sscanf (argv[2] + n, ":%d", &hwnd);
388 #endif
389       
390       srand (tv.tv_sec ^ (tv.tv_usec / 1000) ^ readfd ^ (writefd << 4));
391   
392       for (i = 0; i < 20 + rand() % 20; i++)
393         {
394           g_usleep (100 + (rand() % 10) * 5000);
395           buflen = rand() % BUFSIZE;
396           for (j = 0; j < buflen; j++)
397             buf[j] = ' ' + ((buflen + j) % 95);
398 #ifdef VERBOSE
399           g_print ("gio-test: child writing %d+%d bytes to %d\n",
400                    (int)(sizeof(i) + sizeof(buflen)), buflen, writefd);
401 #endif
402           write (writefd, &i, sizeof (i));
403           write (writefd, &buflen, sizeof (buflen));
404           write (writefd, buf, buflen);
405
406 #ifdef G_OS_WIN32
407           if (rand() % 100 < 5)
408             {
409               int msg = WM_USER + (rand() % 100);
410               WPARAM wparam = rand ();
411               LPARAM lparam = rand ();
412               g_print ("gio-test: child posting message %d,%d,%d to %#x\n",
413                        msg, wparam, lparam, hwnd);
414               PostMessage (hwnd, msg, wparam, lparam);
415             }
416 #endif
417         }
418 #ifdef VERBOSE
419       g_print ("gio-test: child exiting, closing %d\n", writefd);
420 #endif
421       close (writefd);
422     }
423   else
424     g_print ("Huh?\n");
425   
426   return 0;
427 }
428