/**
   @file Server- and Client-Testprogram for the linemonitor functions.

   @param server server to be connected (clientprogram only)

   @param port to be connected (client) or port to be listened (server)

   @param soft_msec (int) timeout in milliseconds which causes
   soft-real-time exception.

   @param hard_msec (int) timeout in milliseconds which causes
   hard-real-time exception.

   @param wait_msec (int) timeout for resent -- sending of the next
   ping.
*/

/*
  Copyright (c) Andreas Hofmeier
  (www.an-h.de, www.an-h.de.vu, www.lgut.uni-bremen.de/an-h/)
  
  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include <stdio.h>
#include "libcomm.h"


/** Exception Function, print exception code and meaning on the
    screen
*/
linemonitor_exception(char *server, int port, int type) {
  printf("linemonitor_exception(%s, %d, %d): ",
	  server, port, type);
  switch (type) {
  case 0:
    printf("Connecion Fault\n");
    break;
  case 1:
    printf("Soft Real Time Exception\n");
    break;
  case 2:
    printf("HARD Real Time Exception\n");
    break;
  case 3:
    printf("Transmission Fault\n");
    break;
  case 4:
    printf("Emergency Stop\n");
    break;
  } /* switch() */
}


main(int argc, char *argv[]) {
  char buf[255];
  int fd = 0;

  // Client Mode: Server Port soft_msec hard_msec wait_msec
  if (argc == 6) {
    printf("Client Mode\n");
    linemonitor(argv[1], atoi(argv[2]),
		atoi(argv[3]), atoi(argv[4]), atoi(argv[5]),
		linemonitor_exception);
  }


  // Server Mode: Port soft_msec hard_msec wait_msec
  if (argc == 5) {
    printf("Server Mode\n");
    fd = linemonitor_server(atoi(argv[1]),
		       atoi(argv[2]), atoi(argv[3]), atoi(argv[3]),
		       linemonitor_exception);
  }

  if ((argc != 5) && (argc != 6)) {
    printf("Client Mode: %s server port soft_msec hard_msec wait_msec\n" \
	   "Server Mode: %s port soft_msec hard_msec wait_msec\n\n", argv[0], argv[0]);
    exit(0);
  }

  while (1) {
    gets(buf);
    linemonitor_emergencystop(fd);
  }
}
