//Editor-Info: -*- C++ -*-
//
//Subject: TOVE project
//
//File: timers.cpp
//
//State: $State: Exp $
//
//Version: $Revision: 1.15 $
//
//Date: $Date: 1998/10/19 08:46:02 $
//
//Organisation:
//      Helsinki University of Technology
//      Laboratory of Telecommunications Software and Multimedia
// 
//Authors:
//	Vesa-Matti Puro
//      Jari Pusa
//
//Description:
//
//
//Copyright:    
//      Copyright 1999 Helsinki University of Technology
//      ALL RIGHTS RESERVED BETWEEN JANUARY 1996 AND JUNE 1999.
//
//      
//Licence:
//     
//
//History:
//
//

#include "timers.h"

//
// Function: Class pfTimers constructors
//
// Description:
//      Initializes empty map
//

pfTimers::pfTimers(const pfTimers &other_)
    : _map(other_._map),
      _host(other_._host)
{
    return;
}

pfTimers::pfTimers(pfProtocol *host_)
    : _map(),
      _host(host_)
{
    return;
}

//
// Function: Class pfTimers destructor
//
// Description:
//      Deletes all timers in the map.
//

pfTimers::~pfTimers(void)
{
    return;
}

//
// Function: defineTimer
//
// Description:
//      Creates a new pfTimer instance and places it into a map
//      setting some important parameters.
//

void pfTimers::defineTimer(const string &name_,
                           pfTimerMessenger *timerMessenger_,
                           pfUlong timeout_in_msec_)
{
    mapIterType iter = _map.find(name_);

    if (iter != _map.end())
    {
        throw pfNameAlreadyDefinedException(name_, PF_EX_INFO);
    }

    if (timerMessenger_ == 0)
    {
        throw pfNullPointerException(PF_EX_INFO);
    }

    pfTimer timer;
    timer.setTimeout(timeout_in_msec_);
    timer.setMessenger(timerMessenger_);
    timer.setHost(_host);
    _map[name_] = timer; 
    return;
}

//
// Function: startTimer
//
// Description:
//      Starts timer.
//      throw (pfNameUndefinedException)
//

void pfTimers::startTimer(const string &name_)
{
    getTimer(name_).start();
    return;
}

//
// Function: stopTimer
//
// Description:
//      Stops timer.
//      throw (pfNameUndefinedException)
//

void pfTimers::stopTimer(const string &name_)
{
    getTimer(name_).stop();
    return;
}

//
// Function: setTimeout
//
// Description:
//      Sets a new timeout for the named timer. 
//      throw (pfNameUndefinedException)
//

void pfTimers :: setTimeout(const string &name_, pfUlong timeout_in_msec_)
{
    getTimer(name_).setTimeout(timeout_in_msec_);
    return;
}

//
// Function: isTimerActive
//
// Description:
//      Returns if the timer is active.
//      throw (pfNameUndefinedException)
//
// Note:
//      This method is not const, because getTimer can't
//      be const.
//

bool pfTimers::isTimerActive(const string &name_)
{
    bool result;
    result = getTimer(name_).isActive();
    return result;
}

//
// Function: getTimer
//
// Description:
//      Gets a pointer to the timer 'name_'. Throws a
//      pfNameUndefinedException if fails.
//
// Note:
//      This method is not const, because iterator 'iter' is
//      not const due to method 'find', which can't return
//      const iterator.
//

pfTimer &pfTimers::getTimer(const string &name_)
{
    mapIterType iter = _map.find(name_);
    if (iter == _map.end())
    {
        throw pfNameUndefinedException(name_, PF_EX_INFO);
    }
    return (*iter).second;
}

