#include <stdio.h>

#include "System.h"

int main(int argc, char **argv)
{
   if (argc < 2)
      return(1);

   printf("testping.cc was last compiled on %s, %s\n",__DATE__, __TIME__);

   for (int i = 0; i < 2; i++)
   {
        // Ping host with timeout of 5 seconds
        //
        printf("Pinging %s using System::ping and wait 5 seconds\n", argv[1]);
	int status = System::ping(argv[1], 5000);

        switch (status) {
          case 0:
            printf("%s is there!\n", argv[1]);
            break;

          case 1:
            printf("%s is not responding\n", argv[1]);
            break;

          case -2:
            printf("Failed to create ICMP socket\n");
            break;

          case -1:
            printf("You don't have privileges to create a ICMP socket\n");
            break;

          case -3:
            printf("ping doesn't know anything about %s\n", argv[1]);
            break;

          case -4:
            printf("ping couldn't send a message to %s\n", argv[1]);
            break;

          default:
            printf("Unknown response\n");
            break;
        }

        printf("Pinging %s using System::auvping and wait 5 seconds\n", argv[1]);
	status = System::auvping(argv[1], 5000);

        switch (status) {
          case 0:
            printf("%s is there!\n", argv[1]);
            break;

          case 1:
            printf("%s is not responding\n", argv[1]);
            break;

          case -2:
            printf("Failed to create ICMP socket\n");
            break;

          case -1:
            printf("You don't have privileges to create a ICMP socket\n");
            break;

          case -3:
            printf("ping doesn't know anything about %s\n", argv[1]);
            break;

          case -4:
            printf("ping couldn't send a message to %s\n", argv[1]);
            break;

          default:
            printf("Unknown response\n");
            break;
        }
   }

   char commandstr[100];
   sprintf(commandstr, "auvping %s 5000", argv[1]);
   for (i = 0; i < 2; i++)
   {
        // Ping host with timeout of 5 seconds
        //
        printf("Executing '%s' \n", commandstr);
        FILE *p = popen(commandstr, "r");

        if (!p)
           printf("auvping command failed\n");
        else
        {
           char result[20];
           if (fgets(result, 20, p))
              printf("result is %s", result);

           fclose(p);
        }
   }

   return(0);
}
