/* Network-Integrated Multimedia Middleware (NMM)
 * http://www.motama.com/nmm.html
 *  
 * Copyright (C) 2006-2007
 * Motama GmbH, Saarbruecken, Germany
 * http://www.motama.com
 *  
 * This file is part of NMM. 
 * 
 * NMM 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; only version 2 of the License.      
 * 
 * NMM 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 NMM; if not, write to the Free Software Foundation,    
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */
 
#ifndef NMM_INPUT_PRODUCER_LINUX_HPP
#define NMM_INPUT_PRODUCER_LINUX_HPP NMM_INPUT_PRODUCER_LINUX_HPP

#include "nmm/nmm_prefix.hpp"

#include "GenericProducer.hpp"

#include "nmm/utils/thread/ThreadedObject.hpp"

#include "visibility.hpp"

namespace NMM {

  /** Input producer.
   * This producer polls a file descriptor and sends events when input is available.
   * It is only available and only meaningful on Linux.
   *
   * The producer polls a file descriptor for available input in a separate thread
   * using the Linux poll() function. 
   */
  class DLLEXPORT InputProducer :
    public GenericProducer,
    private ThreadedObject
  {
  public:
    /** Default constructor.
     */
    InputProducer();

    /** Virtual destructor.
     */
    virtual ~InputProducer();

    /** Set the poll time-out.
     * This method sets the time-out for the internal polling on the file descriptor.
     * The timeout is only useful for debugging and has no significant effect.
     * A value of -1 means infinite timeout. This is the default and should not be changed.
     *
     * @param timeout Time-out in milliseconds
     */
    void setTimeout(int timeout);

    /** Start producing events.
     */
    void start();

    /** Stop producing events.
     */
    void stop();

  protected:
    /** Handle input.
     * This method is called when the file descriptor is readable.
     * The implementation must be provided by a subclass.
     */
    virtual void handleInput() = 0;

    /** Set the file descriptor.
     * This method sets the file descriptor that should be polled for input.
     * Subclasses use this method to initialize the producer.
     *
     * @param fd An opened file descriptor
     */
    void setFileDescriptor(int fd);

  private:
    /** Main input handling method.
     * This method implements the event polling loop which invokes handleInput().
     * The loop is started in a separate thread when start() is called and exits when stop() is called.
     */
    void run();

  private:
    int fd;           ///< File descriptor
    int timeout;      ///< poll time-out

    int pipefd[2];    ///< Internal pipe for interrupting the poll() call
  };

}

#include "nmm/nmm_suffix.hpp"

#endif
