/*
 * 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.
 */


#ifndef _OWLPS_POSITIONING_INPUTUDPSOCKET_HH_
#define _OWLPS_POSITIONING_INPUTUDPSOCKET_HH_

#include "inputmedium.hh"

/// Receives [requests](@ref Request) from a UDP socket
class InputUDPSocket: public InputMedium
{
protected:
  int sockfd ;
  uint_fast16_t listening_port ;

  /** @name Operations */
  //@{
  /// Initialises the socket
  bool init_socket(void) ;
  /// Reads data & fills the current request
  /**
   * This function fills the current Request from the data read from
   * the input medium.
   *
   * @returns `true` if #current_request was correctly filled.
   * @returns `false` in case of error.
   */
  bool fill_current_request(void) ;
  //@}

public:
  explicit InputUDPSocket(const uint_fast16_t _port);

  ~InputUDPSocket(void) ;

  /** @name Read accessors */
  //@{
  /// Always false
  bool eof(void) const ;
  //@}

  /** @name Operations */
  //@{
  /// Closes the socket
  bool close_socket(void) ;
  //@}
} ;



/* *** Read accessors *** */


/**
 * Note that by definition, the end of a socket is never reached.
 * @returns `true` if the socket is closed.
 * @returns `false` if the socket is opened.
 */
inline bool InputUDPSocket::eof() const
{
  return sockfd < 0 ;
}



#endif // _OWLPS_POSITIONING_INPUTUDPSOCKET_HH_
