/*
 * This file is part of the Owl Positioning System (OwlPS) project.
 * It is subject to the copyright notice and license terms in the
 * COPYRIGHT.t2t file found in the top-level directory of this
 * distribution and at
 *   https://code.lm7.fr/mcy/owlps/src/master/COPYRIGHT.t2t
 * No part of the OwlPS Project, including this file, may be copied,
 * modified, propagated, or distributed except according to the terms
 * contained in the COPYRIGHT.t2t file; the COPYRIGHT.t2t file must be
 * distributed along with this file, either separately or by replacing
 * this notice by the COPYRIGHT.t2t file's contents.
 *
 ***********************************************************************
 *
 * This is the main source file of OwlPS ResultReader-UDP, a simple
 * program demonstrating the use of libowlps-resultreader-udp.
 * The received results are printed on the standard output.
 *
 * This program must be linked against libowlps-resultreader and
 * libowlps.
 */


#include "owlps-resultreader.h"
#include <owlps.h>

#include <signal.h>
#include <unistd.h>

int main(void)
{
  struct sigaction action ; // Signal handler structure
  owl_result *result ;
  int sockfd ;

  /* Set up signal handlers */
  action.sa_flags = 0 ;
  sigemptyset(&action.sa_mask) ;
  action.sa_handler = owl_sigint_handler ;
  sigaction(SIGINT, &action, NULL) ;
  action.sa_handler = owl_sigterm_handler ;
  sigaction(SIGTERM, &action, NULL) ;

  /* Open the socket */
  sockfd = owl_create_udp_listening_socket(OWL_DEFAULT_RESULT_PORT) ;
  if (sockfd < 0)
    return 1 ;

  /* Read loop */
  owl_run = true ;
  while (owl_run)
    {
      if (! (result = owl_receive_position(sockfd)))
        return 1 ;
      owl_print_result(result) ;
      owl_free_result(result) ;
      printf("--------------\n") ;
    }

  /* Cleaning */
  close(sockfd) ;

  return 0 ;
}
