/* 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_MESSAGE_DURATION_HPP
#define NMM_MESSAGE_DURATION_HPP NMM_MESSAGE_DURATION_HPP

#include "nmm/nmm_prefix.hpp"

#include "CEvent.hpp"
#include "Event.hpp"

#include "nmm/utils/time/Interval.hpp"

namespace NMM {

  /** An accumulator function which computes the duration of a message in a stream.
   *
   * This class accepts a stream of messages and for each message computes the duration interval of the
   * previous message. It does this by computing timestamp differences. Only valid timestamps are used
   * for duration computation. Messages with invalid timestamps are assumed to have zero duration.
   *
   * Furthermore, this class detects syncReset events. A syncReset event is assumed to have zero duration,
   * and the duration of the last message with a valid timestamp before the syncReset also has zero duration.
   *
   * This class can not compute the duration of a single buffer. It can only determine the duration of a
   * buffer with a valid timestamp by being given the next subsequent buffer with a valid timestamp.
   */
  class MessageDuration
  {
    public:

    /** Default constructor.
     */
    MessageDuration();
    
    /** Resets last valid timestamp.
     */
    void syncReset();
    
    /** Returns difference between synchronization time of argument's and last valid timestamp.
     * 
     * If argument is an Event which contains a syncReset, reset last valid timestamp.
     *
     * @param message to calculate difference in synchronization time
     * @return difference in synchronization time
     */  
    Interval operator()(const Message* message);
    
  private:
    Timestamp last_valid_timestamp; ///< last valid timestamp processed
  };
  
  inline MessageDuration::MessageDuration()
    : last_valid_timestamp()
  {}
  
  inline void MessageDuration::syncReset()
  {
    // Invalidate last valid timestamp
    last_valid_timestamp = Timestamp();
  }

  inline Interval MessageDuration::operator()(const Message* message)
  {
    // Check for syncReset
    if(message->getType() == Message::EVENT || message->getType() == Message::FLUSH_EVENT)
      {
      const CEvent& cevent = static_cast<const CEvent&>(*message);
      for(size_t i=0; i<cevent.size(); ++i)
        {
          if(cevent[i].getKey() == "ISyncReset::syncReset")
            {
          // Found a syncReset
              syncReset();
              return Interval();
            }
      }
      }
    
    // Check if timestamp of incoming buffer is invalid
    if(!message->getTimestamp().is_valid) return Interval();
    
    // Store this timestamps as last valid timestamp
    Timestamp previous_timestamp = last_valid_timestamp;
    last_valid_timestamp = message->getTimestamp();
    
    // Check if a previous valid timestamp is known
    if(!previous_timestamp.is_valid) return Interval();
    
    // Compute difference of timestamps
    Interval duration = last_valid_timestamp.sync_time - previous_timestamp.sync_time;
    return duration;
  }
  
}

#include "nmm/nmm_suffix.hpp"

#endif
