Exemple #1
0
/**
 * Call with the port number as the only argument.
 * Never terminates (other than by signals, such as CTRL-C).
 */
int
main (int argc, char *const *argv)
{
  struct MHD_Daemon *d;
  struct timeval tv;
  struct timeval *tvp;
  fd_set rs;
  fd_set ws;
  fd_set es;
  int max;
  MHD_UNSIGNED_LONG_LONG mhd_timeout;

  if (argc != 2)
    {
      printf ("%s PORT\n", argv[0]);
      return 1;
    }
  /* initialize PRNG */
  srand ((unsigned int) time (NULL));
  d = MHD_start_daemon (MHD_USE_DEBUG,
                        atoi (argv[1]),
                        NULL, NULL, 
			&create_response, NULL, 
			MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 15,
			MHD_OPTION_NOTIFY_COMPLETED, &request_completed_callback, NULL,
			MHD_OPTION_END);
  if (NULL == d)
    return 1;
  while (1)
    {
      expire_sessions ();
      max = 0;
      FD_ZERO (&rs);
      FD_ZERO (&ws);
      FD_ZERO (&es);
      if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &max))
	break; /* fatal internal error */
      if (MHD_get_timeout (d, &mhd_timeout) == MHD_YES)	
	{
	  tv.tv_sec = mhd_timeout / 1000;
	  tv.tv_usec = (mhd_timeout - (tv.tv_sec * 1000)) * 1000;
	  tvp = &tv;	  
	}
      else
	tvp = NULL;
      if (-1 == select (max + 1, &rs, &ws, &es, tvp))
	{
	  if (EINTR != errno)
	    fprintf (stderr, 
		     "Aborting due to error during select: %s\n",
		     strerror (errno));
	  break;
	}
      MHD_run (d);
    }
  MHD_stop_daemon (d);
  return 0;
}
/**
 * Call with the port number as the only argument.
 * Never terminates (other than by signals, such as CTRL-C).
 */
int
main (int argc, char *const *argv)
{
#ifdef TEST
	unsigned char buf[65];
	int GetBufferName(unsigned char *buf);
	int j;
	for ( j=0; j<20; ++j )
	{
	GetBufferName(buf);
	int i;
	for ( i=0; i<20; ++i )
		fprintf(stdout, " %d", buf[i]);
	fprintf(stdout, "\n");
	
#else
  struct MHD_Daemon *d;
  struct timeval tv;
  struct timeval *tvp;
  fd_set rs;
  fd_set ws;
  fd_set es;
  MHD_socket max;
  MHD_UNSIGNED_LONG_LONG mhd_timeout;
  int	 Port;

  if (argc != 2)
	  // No port supplied by command line: default it to 80
	  Port = 80;
  else
	  Port = atoi (argv[1]);
 	
  //MySQLInit();
	
  if ( hid_init() )
	return 1;
  CommandInit();
  // Emulate a Recognize command here
  CmdRecognize();
  
  /* initialize PRNG */
  srand ((unsigned int) time (NULL));
  d = MHD_start_daemon (MHD_USE_DEBUG,
                        Port,
                        NULL, NULL,
			&create_response, NULL,
			MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 15,
			MHD_OPTION_NOTIFY_COMPLETED, &request_completed_callback, NULL,
			MHD_OPTION_END);
  if (NULL == d)
    return 1;
  printf("HIDWebServer is listening on port %d\n", Port);
  while (1)
    {
      expire_sessions ();
      max = 0;
      FD_ZERO (&rs);
      FD_ZERO (&ws);
      FD_ZERO (&es);
      if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &max))
	break; /* fatal internal error */
      if (MHD_get_timeout (d, &mhd_timeout) == MHD_YES)
	{
	  tv.tv_sec = mhd_timeout / 1000;
	  tv.tv_usec = (mhd_timeout - (tv.tv_sec * 1000)) * 1000;
	  tvp = &tv;
	}
      else
	tvp = NULL;
      select (max + 1, &rs, &ws, &es, tvp);
      MHD_run (d);
    }
  MHD_stop_daemon (d);
  CommandQuit();
  hid_exit();
  return 0;
#endif
}