/* Blinker
   Copyright 2011-2019 Stefan Schuermans <stefan@blinkenarea.org>
   Copyleft GNU public license - http://www.gnu.org/copyleft/gpl.html
   a blinkenarea.org project */

#ifndef BLINKER_RATELIMITER_H
#define BLINKER_RATELIMITER_H

#include <string>

#include <BlinkenLib/BlinkenFrame.h>

#include "Directory.h"
#include "File.h"
#include "InStreamFile.h"
#include "Mgrs.h"
#include "Module.h"
#include "OutStreamFile.h"
#include "RateFile.h"
#include "StreamRecv.h"
#include "Time.h"
#include "TimeCallee.h"

namespace Blinker {

/// a stream frame rate limiter
class RateLimiter: public Module, public StreamRecv, public TimeCallee
{
public:
  /**
   * @brief constructor
   * @param[in] name module name
   * @param[in] mgrs managers
   * @param[in] dirBase base directory
   */
  RateLimiter(const std::string &name, Mgrs &mgrs, const Directory &dirBase);

  /// virtual destructor
  virtual ~RateLimiter();

private:
  /// copy constructor disabled
  RateLimiter(const RateLimiter &that);

  /// assignment operator disabled
  const RateLimiter & operator=(const RateLimiter &that);

public:
  /// check for update of configuration
  virtual void updateConfig();

  /// callback when requested time reached
  virtual void timeCall();

  /**
   * @brief set current frame
   * @param[in] stream stream name
   * @param[in] pFrame current frame (NULL for none)
   */
  virtual void setFrame(const std::string &stream, stBlinkenFrame *pFrame);

protected:
  /// (re-)get maximum frame rate to limit to
  void getMaxRate();

  /// send current frame to output stream
  void sendFrame();

  /**
   * @brief process frame
   * @param[in] pFrame frame to process (NULL for none)
   */
  void procFrame(stBlinkenFrame *pFrame);

protected:
  InStreamFile  m_fileInStream;  ///< input stream name file
  RateFile      m_fileMaxRate;   ///< maximum frame rate file
  OutStreamFile m_fileOutStream; ///< output stream name file

  Time m_last; ///< time at which the last frame was forwarded
}; // class RateLimiter

} // namespace Blinker

#endif // #ifndef BLINKER_RATELIMITER_H

