/* 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_TIMEREVENTS_HPP_
#define _NMM_TIMEREVENTS_HPP_

#include "nmm/nmm_prefix.hpp"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
//#include <sys/ioctl.h>
#include <fcntl.h>
//#include <unistd.h>
#include <signal.h>
#include <queue>

#include <vector>

#include "nmm/utils/thread/ThreadedObject.hpp"
#include "nmm/multimedia/GenericProducer.hpp"
#include "nmm/comm/messaging/Event.hpp"

#include "visibility.hpp"
namespace NMM {

  class DLLEXPORT TimedEvent {
    public:
      TimedEvent() {
        time  = 0;
        event = NULL;
      }

      TimedEvent( time_t t, Event* e ) {
        set( t, e );
      }

      void set( time_t t, Event* e ) {
        time  = t;
        event = e;
      }

      time_t getTime() {
        return( time );
      }

      Event* getEvent() {
        return( event );
      }

      bool operator< (const TimedEvent &right) const {
        return ( time < right.time );
      }

      bool operator> (const TimedEvent &right) const {
        return ( time > right.time );
      }

      bool operator== (const TimedEvent &right) const {
        return ( time == right.time );
      }

      void print() {
        printf("Time:  %i\n", (int) time );
        printf("Event: %p\n\n", event );
      }

     private:
       time_t        time;
       Event*        event;
  };

  typedef priority_queue<TimedEvent, vector<TimedEvent>, greater<TimedEvent> > pqueue;

  class DLLEXPORT TimerProducer
    : public GenericProducer,
      private ThreadedObject
  {
  public:
    
    TimerProducer();
    ~TimerProducer();
    
    /**
     * Starts the event-handling in a new thread
     */
    void start();
    
    /**
     * Stops the event-handling (exit thread)
     */
    void stop();
    
    /**
     * schedule event at given time
     * @param sec second
     * @param min minute
     * @param h   hour
     * @param day day
     * @param mon month
     * @param year year
     * @param e event to be sent
     */
    void registerTimer( int sec, int min, int h, int day, int mon, int year, Event* e );
    
    /**
     * schedule event at given time
     * @param t absolute time when event should be sent
     * @param e event to be sent
     */
    void registerTimer( time_t t, Event* e );
    
  private:
    ThreadMutex               mutex;
    ThreadCondition*          cond;
    
    time_t 		      new_time;
    
    /* event with time information when it should be scheduled */
    TimedEvent		      te;
    
    /* priority queue where events are queued */
    pqueue		      queue;
    
    /* indicates, that the Thread is running */
    bool isRunning;

    /**
     * set absolute time when timerThread should be awake
     * @param t absolute time
     * if t is zero, timerThread waits until a new timer is set
     */
    void setTimer( time_t t );
    
    /**
     * The timer handling function
     */
    void run();

  };

}

#include "nmm/nmm_suffix.hpp"

#endif
