/****************************************************************************/
/* Copyright (c) 2012 MBARI                                                 */
/* MBARI Proprietary Information. All rights reserved.                      */
/****************************************************************************/
/* Summary  :                                                               */
/* Filename : multibeamerTest.cc                                            */
/* Author   :                                                               */
/* Project  :                                                               */
/* Version  : 1.0                                                           */
/* Created  : 09/17/2012                                                    */
/* Modified :                                                               */
/* Archived :                                                               */
/****************************************************************************/
/* Modification History:                                                    */
/****************************************************************************/
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <conio.h>
#include <process.h>
#include <signal.h>
#include <stdlib.h>
#include "Exception.h"
#include "StringConverter.h"
#include "MathP.h"
#include "System.h"
#include "Task.h"
#include "Syslog.h"
#include "MultibeamerIF.h"

int main(int argc, char **argv)
{
  Boolean debug = True;
  Boolean error = False;

  if (argc < 2)
  {
    printf("multibeamerTest requires the ip address of the 7kCenter...\n");
    return -1;
  }

  printf("Starting multibeamerServer...\n");

  char *program = "multibeamerServer";

  char errorBuf[256];
  char buf[256];
 
  pid_t pid;
 
  switch ((pid = fork())) {
     
  case 0:
    // In child
    // Execute driver program
    execlp(program, program, argv[1], (char *)0);

    sprintf(errorBuf, 
	    "Task::spawnServer() - exec() of \"%s\" failed", program);

    perror(errorBuf);
 
    break;

  case -1:
    perror("Task::spawnServer() - fork() failed");
    return -1;
  }

  MultibeamerIF *mbs;
  
  try {
    mbs = new MultibeamerIF("MultibeamerServer");
  }
  catch (Exception e) {
    fprintf(stderr, "Caught exception: %s\n", e.msg);
    error = True;
  }
  catch (...) {
    fprintf(stderr, "Caught some exception...");
    error = True;
  }

  for (int i = 0; i < 10; i++)
  {
    sleep(1);
    double range = -1.;
    Boolean comms = False, ready = False;
    TimeIF::TimeSpec updateTime;
  }

  // Signal all children
  kill(0, SIGTERM);
  Syslog::write("OK, LEAVING!!!\n");

  printf("Ending multibeamer...\n");

  if (error)
    return 1;
  else
    return 0;
}

