/* 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_CONTROLLER_HPP_
#define _NMM_CONTROLLER_HPP_

#include "nmm/nmm_prefix.hpp"

#include "nmm/multimedia/sync/interfaces/IControllerImpl.hpp"

#include "nmm/comm/StdNamedObject.hpp"
#include "nmm/multimedia/Node.hpp"
#include "nmm/utils/time/TimedElement.hpp"

namespace NMM {

  class Synchronizer;

  /** A controller for synchronization.
   * This is the abstract base class of all synchronization controller classes.
   */
  class Controller : public TimedElement,
                     public StdNamedObject,
		     virtual public IControllerImpl {
  public:
    static const IController::Mode DEFAULT_MODE = IController::OFFLINE; 
    //!< The default mode should be offline, so that no buffer can be lost. */
    
  private:
    IController::Mode mode;       ///< Controller mode

    /** Check if a timestamp is valid.
     * If the controller is in ONLINE mode, this method is called to determine if a
     * buffer with given timestamp can be presented in time. In this case, the buffer is "valid",
     * otherwise it is "invalid".
     * The implementation must be provided by a subclass.
     *
     * @param timestamp The timestamp of the buffer to check.
     * @return @c true if the buffer is valid, @c false if the buffer is invalid
     */
    virtual bool checkBufferValid(const Timestamp timestamp) = 0;
    
  protected:
    bool discard_buffers;               ///< Discard invalid buffers
    bool invalidate_buffers;            ///< Invalidate all buffers
    bool invalidate_buffers_changed;    ///< Invalidation of all buffers will be enabled or disabled at a given timestamp
    Timestamp invalidate_buffers_ts;    ///< Timestamp when invalidation of all buffers is enabled or disabled
    ThreadMutex invalidate_mutex;       ///< Mutex for synchronizing buffer invalidation state

  public:
    /** Create a controller.
     *
     * @param mode Initial mode of the controller
     */
    Controller(const IController::Mode mode);

    /** Create a controller for a node.
     *
     * @param parent The synchronized node to which this controller belongs
     * @param mode Initial mode of the controller
     */
    Controller(const Node* parent = 0, const IController::Mode mode = DEFAULT_MODE);

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

    /** Check if a buffer is valid.
     * A buffer is valid if it can be presented in time. This method is called by a node to check
     * whether an incoming buffer is valid. The actual implementation is provided by a subclass.
     * This method only implements the general logic of buffer validation.
     *
     * If the controller is in the OFFLINE mode, all buffers are valid.
     * If the controller is in ONLINE mode, the given timestamp is used to determine if the
     * buffer is valid. It is also possible to force invalidation of all buffers.
     *
     * @param timestamp Buffer timestamp
     * @return @c true if the buffer is valid, @c false if the buffer is invalid
     */
    bool isBufferValid(const Timestamp& timestamp);
    
  protected:
    // IController interface
    /** Set the controller mode.
     *
     * @param mode The controller mode
     */
    virtual Result setMode(const IController::Mode& mode);

    /** Enable or disable discarding of buffers.
     * If the controller is in @c ONLINE mode, this method controls whether the controller may discard buffers
     * which can not be presented in time. Enabled by default.
     *
     * @param enable @c true to enable, @c false to disable discarding of buffers
     */
    virtual Result permitDiscardBuffers(const nmm_bool& enable);

    /** Pause this controller.
     *
     * @return @c SUCCESS if the operation succeeded, @c FAILURE otherwise
     */
    virtual Result pause() = 0;

    /** Wake up this controller after pause.
     *
     * @return @c SUCCESS if the operation succeeded, @c FAILURE otherwise
     */
    virtual Result wakeup() = 0;

    /** Reset this controller.
     *
     * @return @c SUCCESS if the operation succeeded, @c FAILURE otherwise
     */
    virtual Result reset() = 0;

    /** Enable or disable invalidation of all buffers.
     * If invalidation of all buffers is enabled, then all buffers are flagged as invalid and are not presented.
     * Disabled by default.
     *
     * @param enable @c true to enable, @c false to disable invalidation of all buffers
     */
    virtual void setBuffersInvalid(const nmm_bool& status);

    /** Enable or disable invalidation of all buffers after a specific timestamp.
     * If invalidation of all buffers is enabled, then all buffers are flagged as invalid and are not presented.
     * Disabled by default.
     * This method enables or disables invalidation of all buffers after a specific buffer timestamp.
     *
     * @param enable @c true to enable, @c false to disable invalidation of all buffers
     * @param timestamp Buffer timestamp which provides the @c sync_time after which the change becomes active
     */
    virtual void setBuffersInvalidTimed(const nmm_bool& enable, const Timestamp& status_time);

    // IPing interface
    /** Measure the delay of a method call.
     * This method can be used to estimate the delay between method call and execution.
     * The clocks on the host which calls the method and the host which executes the method
     * must be synchronized.
     * @return the system time when the method call was executed
     */
    virtual Time ping();
    // END< IController interface

  };
}

#include "nmm/nmm_suffix.hpp"

#endif // _NMM_CONTROLLER_HPP_



