os/ossrv/glib/tsrc/BC/tests/gio-test.c
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/* GLIB - Library of useful routines for C programming
sl@0
     2
 * Copyright (C) 2000  Tor Lillqvist
sl@0
     3
 * Portion Copyright © 2008-09 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
sl@0
     4
 * This library is free software; you can redistribute it and/or
sl@0
     5
 * modify it under the terms of the GNU Lesser General Public
sl@0
     6
 * License as published by the Free Software Foundation; either
sl@0
     7
 * version 2 of the License, or (at your option) any later version.
sl@0
     8
 *
sl@0
     9
 * This library is distributed in the hope that it will be useful,
sl@0
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
sl@0
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
sl@0
    12
 * Lesser General Public License for more details.
sl@0
    13
 *
sl@0
    14
 * You should have received a copy of the GNU Lesser General Public
sl@0
    15
 * License along with this library; if not, write to the
sl@0
    16
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
sl@0
    17
 * Boston, MA 02111-1307, USA.
sl@0
    18
 */
sl@0
    19
sl@0
    20
/* A test program for the main loop and IO channel code.
sl@0
    21
 * Just run it. Optional parameter is number of sub-processes.
sl@0
    22
 */
sl@0
    23
sl@0
    24
sl@0
    25
#undef G_DISABLE_ASSERT
sl@0
    26
#undef G_LOG_DOMAIN
sl@0
    27
sl@0
    28
#include "config.h"
sl@0
    29
sl@0
    30
#include <glib.h>
sl@0
    31
#include <unistd.h>
sl@0
    32
#include <stdio.h>
sl@0
    33
#include <stdlib.h>
sl@0
    34
#include <math.h>
sl@0
    35
#include <time.h>
sl@0
    36
sl@0
    37
#ifdef SYMBIAN
sl@0
    38
  //#define VERBOSE
sl@0
    39
  #include <pthread.h>
sl@0
    40
  #include "mrt2_glib2_test.h"
sl@0
    41
  void *child_function(void*);
sl@0
    42
  gboolean child_terminated = FALSE;
sl@0
    43
  int from_descriptor, to_descriptor;
sl@0
    44
#endif /*SYMBIAN*/
sl@0
    45
sl@0
    46
#ifdef G_OS_WIN32
sl@0
    47
  #include <io.h>
sl@0
    48
  #include <fcntl.h>
sl@0
    49
  #include <process.h>
sl@0
    50
  #define STRICT
sl@0
    51
  #include <windows.h>
sl@0
    52
#else
sl@0
    53
  #ifdef HAVE_UNISTD_H
sl@0
    54
    #include <unistd.h>
sl@0
    55
  #endif
sl@0
    56
#endif
sl@0
    57
sl@0
    58
sl@0
    59
static int nrunning;
sl@0
    60
static GMainLoop *main_loop;
sl@0
    61
sl@0
    62
#ifdef SYMBIAN
sl@0
    63
#define BUFSIZE 1000
sl@0
    64
#else
sl@0
    65
#define BUFSIZE 5000		/* Larger than the circular buffer in giowin32.c on purpose.*/
sl@0
    66
#endif
sl@0
    67
sl@0
    68
static int nkiddies;
sl@0
    69
sl@0
    70
static struct {
sl@0
    71
  int fd;
sl@0
    72
  int seq;
sl@0
    73
} *seqtab;
sl@0
    74
sl@0
    75
sl@0
    76
sl@0
    77
static GIOError
sl@0
    78
read_all (int         fd,
sl@0
    79
	  GIOChannel *channel,
sl@0
    80
	  char       *buffer,
sl@0
    81
	  guint       nbytes,
sl@0
    82
	  guint      *bytes_read)
sl@0
    83
{
sl@0
    84
  guint left = nbytes;
sl@0
    85
  gsize nb;
sl@0
    86
  GIOError error = G_IO_ERROR_NONE;
sl@0
    87
  char *bufp = buffer;
sl@0
    88
sl@0
    89
  /* g_io_channel_read() doesn't necessarily return all the
sl@0
    90
   * data we want at once.
sl@0
    91
   */
sl@0
    92
  *bytes_read = 0;
sl@0
    93
  while (left)
sl@0
    94
    {
sl@0
    95
      error = g_io_channel_read (channel, bufp, left, &nb);
sl@0
    96
      
sl@0
    97
      if (error != G_IO_ERROR_NONE)
sl@0
    98
	{	  
sl@0
    99
	  g_print ("gio-test: ...from %d: G_IO_ERROR_%s\n", fd,
sl@0
   100
		   (error == G_IO_ERROR_AGAIN ? "AGAIN" :
sl@0
   101
		    (error == G_IO_ERROR_INVAL ? "INVAL" :
sl@0
   102
		     (error == G_IO_ERROR_UNKNOWN ? "UNKNOWN" : "???"))));
sl@0
   103
	  if (error == G_IO_ERROR_AGAIN)
sl@0
   104
	    continue;
sl@0
   105
	  break;
sl@0
   106
	}
sl@0
   107
      if (nb == 0)
sl@0
   108
	return error;
sl@0
   109
      left -= nb;
sl@0
   110
      bufp += nb;
sl@0
   111
      *bytes_read += nb;
sl@0
   112
    }
sl@0
   113
  return error;
sl@0
   114
}
sl@0
   115
sl@0
   116
static void
sl@0
   117
shutdown_source (gpointer data)
sl@0
   118
{
sl@0
   119
  if (g_source_remove (*(guint *) data))
sl@0
   120
    {
sl@0
   121
      nrunning--;
sl@0
   122
      if (nrunning == 0)
sl@0
   123
	g_main_loop_quit (main_loop);
sl@0
   124
    }
sl@0
   125
}
sl@0
   126
sl@0
   127
static gboolean
sl@0
   128
recv_message (GIOChannel  *channel,
sl@0
   129
	      GIOCondition cond,
sl@0
   130
	      gpointer    data)
sl@0
   131
{
sl@0
   132
  gint fd = g_io_channel_unix_get_fd (channel);
sl@0
   133
  gboolean retval = TRUE;
sl@0
   134
sl@0
   135
#ifdef VERBOSE
sl@0
   136
  g_print ("gio-test: ...from %d:%s%s%s%s\n", fd,
sl@0
   137
	   (cond & G_IO_ERR) ? " ERR" : "",
sl@0
   138
	   (cond & G_IO_HUP) ? " HUP" : "",
sl@0
   139
	   (cond & G_IO_IN)  ? " IN"  : "",
sl@0
   140
	   (cond & G_IO_PRI) ? " PRI" : "");
sl@0
   141
#endif
sl@0
   142
sl@0
   143
  if ((cond & (G_IO_ERR | G_IO_HUP)))
sl@0
   144
    {
sl@0
   145
      shutdown_source (data);
sl@0
   146
      retval = FALSE;
sl@0
   147
    }
sl@0
   148
sl@0
   149
  if (cond & G_IO_IN)
sl@0
   150
    {
sl@0
   151
      char buf[BUFSIZE];
sl@0
   152
      guint nbytes;
sl@0
   153
      guint nb;
sl@0
   154
      int i, j, seq;
sl@0
   155
      GIOError error;
sl@0
   156
      
sl@0
   157
      error = read_all (fd, channel, (gchar *) &seq, sizeof (seq), &nb);
sl@0
   158
      if (error == G_IO_ERROR_NONE)
sl@0
   159
	{
sl@0
   160
	  if (nb == 0)
sl@0
   161
	    {
sl@0
   162
#ifdef VERBOSE
sl@0
   163
	      g_print ("gio-test: ...from %d: EOF\n", fd);
sl@0
   164
#endif
sl@0
   165
	      shutdown_source (data);
sl@0
   166
	      return FALSE;
sl@0
   167
	    }
sl@0
   168
	  
sl@0
   169
	  g_assert (nb == sizeof (nbytes));
sl@0
   170
sl@0
   171
	  for (i = 0; i < nkiddies; i++)
sl@0
   172
	    if (seqtab[i].fd == fd)
sl@0
   173
	      {
sl@0
   174
		if (seq != seqtab[i].seq)
sl@0
   175
		  {
sl@0
   176
		    g_print ("gio-test: ...from %d: invalid sequence number %d, expected %d\n",
sl@0
   177
			     fd, seq, seqtab[i].seq);
sl@0
   178
		    g_assert_not_reached ();
sl@0
   179
		    
sl@0
   180
		    g_assert(FALSE && "gio-test failed");
sl@0
   181
		  }
sl@0
   182
		seqtab[i].seq++;
sl@0
   183
		break;
sl@0
   184
	      }
sl@0
   185
sl@0
   186
sl@0
   187
	  error = read_all (fd, channel, (gchar *) &nbytes, sizeof (nbytes), &nb);
sl@0
   188
	}
sl@0
   189
sl@0
   190
      if (error != G_IO_ERROR_NONE)
sl@0
   191
	return FALSE;
sl@0
   192
      
sl@0
   193
      if (nb == 0)
sl@0
   194
	{
sl@0
   195
#ifdef VERBOSE
sl@0
   196
	  g_print ("gio-test: ...from %d: EOF\n", fd);
sl@0
   197
#endif
sl@0
   198
	  shutdown_source (data);
sl@0
   199
	  return FALSE;
sl@0
   200
	}
sl@0
   201
      
sl@0
   202
      g_assert (nb == sizeof (nbytes));
sl@0
   203
sl@0
   204
      if (nbytes >= BUFSIZE)
sl@0
   205
	{
sl@0
   206
	  g_print ("gio-test: ...from %d: nbytes = %d (%#x)!\n", fd, nbytes, nbytes);
sl@0
   207
	  g_assert_not_reached ();
sl@0
   208
	  g_assert(FALSE && "gio-test failed");
sl@0
   209
	}
sl@0
   210
      g_assert (nbytes >= 0 && nbytes < BUFSIZE);
sl@0
   211
#ifdef VERBOSE      
sl@0
   212
      g_print ("gio-test: ...from %d: %d bytes\n", fd, nbytes);
sl@0
   213
#endif      
sl@0
   214
      if (nbytes > 0)
sl@0
   215
	{
sl@0
   216
	  error = read_all (fd, channel, buf, nbytes, &nb);
sl@0
   217
	  if(child_terminated)
sl@0
   218
	  	shutdown_source (data);
sl@0
   219
	  	
sl@0
   220
sl@0
   221
	  if (error != G_IO_ERROR_NONE)
sl@0
   222
	    return FALSE;
sl@0
   223
sl@0
   224
	  if (nb == 0)
sl@0
   225
	    {
sl@0
   226
#ifdef VERBOSE
sl@0
   227
	      g_print ("gio-test: ...from %d: EOF\n", fd);
sl@0
   228
#endif
sl@0
   229
	      shutdown_source (data);
sl@0
   230
	      return FALSE;
sl@0
   231
	    }
sl@0
   232
      
sl@0
   233
	  for (j = 0; j < nbytes; j++)
sl@0
   234
	    if (buf[j] != ' ' + ((nbytes + j) % 95))
sl@0
   235
	      {
sl@0
   236
		g_print ("gio-test: ...from %d: buf[%d] == '%c', should be '%c'\n",
sl@0
   237
			 fd, j, buf[j], 'a' + ((nbytes + j) % 32));
sl@0
   238
		g_assert_not_reached ();
sl@0
   239
		g_assert(FALSE && "gio-test failed");
sl@0
   240
	      }
sl@0
   241
#ifdef VERBOSE
sl@0
   242
	  g_print ("gio-test: ...from %d: OK\n", fd);
sl@0
   243
#endif
sl@0
   244
	}
sl@0
   245
    }
sl@0
   246
  return retval;
sl@0
   247
}
sl@0
   248
sl@0
   249
#ifdef G_OS_WIN32
sl@0
   250
sl@0
   251
static gboolean
sl@0
   252
recv_windows_message (GIOChannel  *channel,
sl@0
   253
		      GIOCondition cond,
sl@0
   254
		      gpointer    data)
sl@0
   255
{
sl@0
   256
  GIOError error;
sl@0
   257
  MSG msg;
sl@0
   258
  guint nb;
sl@0
   259
  
sl@0
   260
  while (1)
sl@0
   261
    {
sl@0
   262
      error = g_io_channel_read (channel, &msg, sizeof (MSG), &nb);
sl@0
   263
      
sl@0
   264
      if (error != G_IO_ERROR_NONE)
sl@0
   265
	{
sl@0
   266
	  g_print ("gio-test: ...reading Windows message: G_IO_ERROR_%s\n",
sl@0
   267
		   (error == G_IO_ERROR_AGAIN ? "AGAIN" :
sl@0
   268
		    (error == G_IO_ERROR_INVAL ? "INVAL" :
sl@0
   269
		     (error == G_IO_ERROR_UNKNOWN ? "UNKNOWN" : "???"))));
sl@0
   270
	  if (error == G_IO_ERROR_AGAIN)
sl@0
   271
	    continue;
sl@0
   272
	}
sl@0
   273
      break;
sl@0
   274
    }
sl@0
   275
sl@0
   276
  g_print ("gio-test: ...Windows message for %#x: %d,%d,%d\n",
sl@0
   277
	   msg.hwnd, msg.message, msg.wParam, msg.lParam);
sl@0
   278
sl@0
   279
  return TRUE;
sl@0
   280
}
sl@0
   281
sl@0
   282
LRESULT CALLBACK 
sl@0
   283
window_procedure (HWND hwnd,
sl@0
   284
		  UINT message,
sl@0
   285
		  WPARAM wparam,
sl@0
   286
		  LPARAM lparam)
sl@0
   287
{
sl@0
   288
  g_print ("gio-test: window_procedure for %#x: %d,%d,%d\n",
sl@0
   289
	   hwnd, message, wparam, lparam);
sl@0
   290
  return DefWindowProc (hwnd, message, wparam, lparam);
sl@0
   291
}
sl@0
   292
sl@0
   293
#endif
sl@0
   294
sl@0
   295
int
sl@0
   296
main (int    argc,
sl@0
   297
      char **argv)
sl@0
   298
{
sl@0
   299
   
sl@0
   300
  #ifdef SYMBIAN
sl@0
   301
  /*GLIB_INIT();*/
sl@0
   302
  pthread_t thread_child;
sl@0
   303
  int rc,t;
sl@0
   304
  g_log_set_handler (NULL,  G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG, &mrtLogHandler, NULL);
sl@0
   305
  g_set_print_handler(mrtPrintHandler);
sl@0
   306
  #endif /*SYMBIAN*/
sl@0
   307
 
sl@0
   308
sl@0
   309
  if (argc < 3)
sl@0
   310
    {
sl@0
   311
      /* Parent */
sl@0
   312
      
sl@0
   313
      GIOChannel *my_read_channel;
sl@0
   314
      gchar *cmdline;
sl@0
   315
      guint *id;
sl@0
   316
      int i;
sl@0
   317
#ifdef G_OS_WIN32
sl@0
   318
      GTimeVal start, end;
sl@0
   319
      GPollFD pollfd;
sl@0
   320
      int pollresult;
sl@0
   321
      ATOM klass;
sl@0
   322
      static WNDCLASS wcl;
sl@0
   323
      HWND hwnd;
sl@0
   324
      GIOChannel *windows_messages_channel;
sl@0
   325
#endif
sl@0
   326
sl@0
   327
      nkiddies = (argc == 1 ? 1 : atoi(argv[1]));
sl@0
   328
      seqtab = g_malloc (nkiddies * 2 * sizeof (int));
sl@0
   329
sl@0
   330
#ifdef G_OS_WIN32
sl@0
   331
      wcl.style = 0;
sl@0
   332
      wcl.lpfnWndProc = window_procedure;
sl@0
   333
      wcl.cbClsExtra = 0;
sl@0
   334
      wcl.cbWndExtra = 0;
sl@0
   335
      wcl.hInstance = GetModuleHandle (NULL);
sl@0
   336
      wcl.hIcon = NULL;
sl@0
   337
      wcl.hCursor = NULL;
sl@0
   338
      wcl.hbrBackground = NULL;
sl@0
   339
      wcl.lpszMenuName = NULL;
sl@0
   340
      wcl.lpszClassName = "gio-test";
sl@0
   341
sl@0
   342
      klass = RegisterClass (&wcl);
sl@0
   343
sl@0
   344
      if (!klass)
sl@0
   345
	{
sl@0
   346
	  g_print ("gio-test: RegisterClass failed\n");
sl@0
   347
	  exit (1);
sl@0
   348
	}
sl@0
   349
sl@0
   350
      hwnd = CreateWindow (MAKEINTATOM(klass), "gio-test", 0, 0, 0, 10, 10,
sl@0
   351
			   NULL, NULL, wcl.hInstance, NULL);
sl@0
   352
      if (!hwnd)
sl@0
   353
	{
sl@0
   354
	  g_print ("gio-test: CreateWindow failed\n");
sl@0
   355
	  exit (1);
sl@0
   356
	}
sl@0
   357
sl@0
   358
      windows_messages_channel = g_io_channel_win32_new_messages ((guint)hwnd);
sl@0
   359
      g_io_add_watch (windows_messages_channel, G_IO_IN, recv_windows_message, 0);
sl@0
   360
#endif
sl@0
   361
sl@0
   362
      for (i = 0; i < nkiddies; i++)
sl@0
   363
	{
sl@0
   364
	  int pipe_to_sub[2], pipe_from_sub[2];
sl@0
   365
	  
sl@0
   366
	  if (pipe (pipe_to_sub) == -1 ||
sl@0
   367
	      pipe (pipe_from_sub) == -1)
sl@0
   368
	    perror ("pipe"), exit (1);
sl@0
   369
	  
sl@0
   370
	  seqtab[i].fd = pipe_from_sub[0];
sl@0
   371
	  seqtab[i].seq = 0;
sl@0
   372
sl@0
   373
	  my_read_channel = g_io_channel_unix_new (pipe_from_sub[0]);
sl@0
   374
	  
sl@0
   375
	  id = g_new (guint, 1);
sl@0
   376
	  *id =
sl@0
   377
	    g_io_add_watch (my_read_channel,
sl@0
   378
			    G_IO_IN | G_IO_PRI | G_IO_ERR | G_IO_HUP,
sl@0
   379
			    recv_message,
sl@0
   380
			    id);
sl@0
   381
	  
sl@0
   382
	  nrunning++;
sl@0
   383
	  
sl@0
   384
#ifdef G_OS_WIN32
sl@0
   385
	  cmdline = g_strdup_g_print ("%d:%d:%d",
sl@0
   386
				     pipe_to_sub[0],
sl@0
   387
				     pipe_from_sub[1],
sl@0
   388
				     hwnd);
sl@0
   389
	  _spawnl (_P_NOWAIT, argv[0], argv[0], "--child", cmdline, NULL);
sl@0
   390
#else
sl@0
   391
	  cmdline = g_strdup_printf ("%s --child %d:%d &", argv[0],
sl@0
   392
				     pipe_to_sub[0], pipe_from_sub[1]);
sl@0
   393
				     
sl@0
   394
	  
sl@0
   395
#ifndef SYMBIAN
sl@0
   396
	  system (cmdline);
sl@0
   397
#else
sl@0
   398
	 from_descriptor = pipe_to_sub[0];
sl@0
   399
     to_descriptor = pipe_from_sub[1]; 
sl@0
   400
	  rc = pthread_create(&thread_child,NULL,child_function,NULL);
sl@0
   401
     
sl@0
   402
sl@0
   403
	  if(rc)
sl@0
   404
	  {
sl@0
   405
	  	g_print("Failed to create thread.\n");
sl@0
   406
	  	exit(1);
sl@0
   407
	  }
sl@0
   408
#endif /*SYMBIAN*/	  
sl@0
   409
	  
sl@0
   410
#endif
sl@0
   411
sl@0
   412
#ifndef SYMBIAN
sl@0
   413
	  close (pipe_to_sub[0]);
sl@0
   414
	  close (pipe_from_sub [1]);
sl@0
   415
#endif	  
sl@0
   416
sl@0
   417
#ifdef G_OS_WIN32
sl@0
   418
	  g_get_current_time (&start);
sl@0
   419
	  g_io_channel_win32_make_pollfd (my_read_channel, G_IO_IN, &pollfd);
sl@0
   420
	  pollresult = g_io_channel_win32_poll (&pollfd, 1, 100);
sl@0
   421
	  g_get_current_time (&end);
sl@0
   422
	  if (end.tv_usec < start.tv_usec)
sl@0
   423
	    end.tv_sec--, end.tv_usec += 1000000;
sl@0
   424
	  g_print ("gio-test: had to wait %ld.%03ld s, result:%d\n",
sl@0
   425
		   end.tv_sec - start.tv_sec,
sl@0
   426
		   (end.tv_usec - start.tv_usec) / 1000,
sl@0
   427
		   pollresult);
sl@0
   428
#endif
sl@0
   429
	}
sl@0
   430
      
sl@0
   431
      main_loop = g_main_loop_new (NULL, FALSE);
sl@0
   432
      g_main_loop_run (main_loop);
sl@0
   433
sl@0
   434
    }
sl@0
   435
  else if (argc == 3)
sl@0
   436
    {
sl@0
   437
      /* Child */
sl@0
   438
      
sl@0
   439
      int readfd, writefd;
sl@0
   440
#ifdef G_OS_WIN32
sl@0
   441
      HWND hwnd;
sl@0
   442
#endif
sl@0
   443
      int i, j;
sl@0
   444
      char buf[BUFSIZE];
sl@0
   445
      int buflen;
sl@0
   446
      GTimeVal tv;
sl@0
   447
      int n;
sl@0
   448
  
sl@0
   449
      g_get_current_time (&tv);
sl@0
   450
      
sl@0
   451
      sscanf (argv[2], "%d:%d%n", &readfd, &writefd, &n);
sl@0
   452
sl@0
   453
#ifdef G_OS_WIN32
sl@0
   454
      sscanf (argv[2] + n, ":%d", &hwnd);
sl@0
   455
#endif
sl@0
   456
      
sl@0
   457
      srand (tv.tv_sec ^ (tv.tv_usec / 1000) ^ readfd ^ (writefd << 4));
sl@0
   458
  
sl@0
   459
      for (i = 0; i < 20 + rand() % 20; i++)
sl@0
   460
	{
sl@0
   461
	  g_usleep (100 + (rand() % 10) * 5000);
sl@0
   462
	  buflen = rand() % BUFSIZE;
sl@0
   463
	  for (j = 0; j < buflen; j++)
sl@0
   464
	    buf[j] = ' ' + ((buflen + j) % 95);
sl@0
   465
#ifdef VERBOSE
sl@0
   466
	  g_print ("gio-test: child writing %d+%d bytes to %d\n",
sl@0
   467
		   (int)(sizeof(i) + sizeof(buflen)), buflen, writefd);
sl@0
   468
#endif
sl@0
   469
	  write (writefd, &i, sizeof (i));
sl@0
   470
	  write (writefd, &buflen, sizeof (buflen));
sl@0
   471
	  write (writefd, buf, buflen);
sl@0
   472
sl@0
   473
#ifdef G_OS_WIN32
sl@0
   474
	  if (rand() % 100 < 5)
sl@0
   475
	    {
sl@0
   476
	      int msg = WM_USER + (rand() % 100);
sl@0
   477
	      WPARAM wparam = rand ();
sl@0
   478
	      LPARAM lparam = rand ();
sl@0
   479
	      g_print ("gio-test: child posting message %d,%d,%d to %#x\n",
sl@0
   480
		       msg, wparam, lparam, hwnd);
sl@0
   481
	      PostMessage (hwnd, msg, wparam, lparam);
sl@0
   482
	    }
sl@0
   483
#endif
sl@0
   484
	}
sl@0
   485
#ifdef VERBOSE
sl@0
   486
      g_print ("gio-test: child exiting, closing %d\n", writefd);
sl@0
   487
#endif
sl@0
   488
      close (writefd);
sl@0
   489
    }
sl@0
   490
  else
sl@0
   491
    g_print ("Huh?\n");
sl@0
   492
  
sl@0
   493
sl@0
   494
#ifdef SYMBIAN
sl@0
   495
  close(from_descriptor);
sl@0
   496
  close(to_descriptor);
sl@0
   497
#endif
sl@0
   498
sl@0
   499
#ifdef VERBOSE
sl@0
   500
printf("Completed tests\n");
sl@0
   501
#endif
sl@0
   502
  
sl@0
   503
  #if SYMBIAN
sl@0
   504
  testResultXml("gio-test");
sl@0
   505
  #endif /* EMULATOR */
sl@0
   506
  
sl@0
   507
  return 0;
sl@0
   508
}
sl@0
   509
sl@0
   510
#ifdef SYMBIAN
sl@0
   511
void *child_function(void* t)
sl@0
   512
{
sl@0
   513
      int readfd, writefd;
sl@0
   514
sl@0
   515
      int i, j;
sl@0
   516
      char buf[BUFSIZE];
sl@0
   517
      int buflen;
sl@0
   518
      GTimeVal tv;
sl@0
   519
      int n;
sl@0
   520
  
sl@0
   521
      g_get_current_time (&tv);
sl@0
   522
sl@0
   523
sl@0
   524
      readfd=from_descriptor;
sl@0
   525
      writefd=to_descriptor;
sl@0
   526
sl@0
   527
      srand (tv.tv_sec ^ (tv.tv_usec / 1000) ^ readfd ^ (writefd << 4));
sl@0
   528
  
sl@0
   529
      for (i = 0; i < 5; i++)
sl@0
   530
	  {
sl@0
   531
	    if(i==4)
sl@0
   532
	    	child_terminated = TRUE;
sl@0
   533
	    g_usleep (100 + (rand() % 10) * 5000);
sl@0
   534
	    buflen = rand() % BUFSIZE;
sl@0
   535
	    for (j = 0; j < buflen; j++)
sl@0
   536
	      buf[j] = ' ' + ((buflen + j) % 95);
sl@0
   537
#ifdef VERBOSE
sl@0
   538
	    g_print ("gio-test: child writing %d+%d bytes to %d\n",
sl@0
   539
		   (int)(sizeof(i) + sizeof(buflen)), buflen, writefd);
sl@0
   540
#endif
sl@0
   541
	    write (writefd, &i, sizeof (i));
sl@0
   542
	    write (writefd, &buflen, sizeof (buflen));
sl@0
   543
	    write (writefd, buf, buflen);
sl@0
   544
	  }
sl@0
   545
#ifdef VERBOSE
sl@0
   546
      g_print ("gio-test: child exiting, closing %d\n", writefd);
sl@0
   547
#endif
sl@0
   548
      close (writefd);
sl@0
   549
      
sl@0
   550
}
sl@0
   551
#endif