/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
 * Copyright (c) 2007 INRIA
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation;
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
 */
#include "watchdog.h"

namespace ns3 {

Watchdog::Watchdog ()
  : m_impl (0),
    m_event (),
    m_end (MicroSeconds (0))
{}

Watchdog::~Watchdog ()
{
  delete m_impl;
}

void 
Watchdog::Ping (Time delay)
{
  Time end = Simulator::Now () + delay;
  m_end = std::max (m_end, end);
  if (m_event.IsRunning ())
    {
      return;
    }
  m_event = Simulator::Schedule (m_end - Now (), &Watchdog::Expire, this);
}

void
Watchdog::Expire (void)
{
  if (m_end == Simulator::Now ())
    {
      m_impl->Invoke ();
    }
  else
    {
      m_event = Simulator::Schedule (m_end - Now (), &Watchdog::Expire, this);
    }
}

} // namespace ns3

#include "ns3/test.h"

namespace ns3 {

class WatchdogTestCase : public TestCase
{
public:
  WatchdogTestCase ();
  virtual bool DoRun (void);
  void Expire (Time expected);
  bool m_expired;
  Time m_expiredTime;
  Time m_expiredArgument;
};

WatchdogTestCase::WatchdogTestCase()
  : TestCase ("Check that we can keepalive a watchdog")
{}

void
WatchdogTestCase::Expire (Time expected)
{
  m_expired = true;
  m_expiredTime = Simulator::Now ();
  m_expiredArgument = expected;
}

bool
WatchdogTestCase::DoRun (void)
{
  m_expired = false;
  m_expiredArgument = Seconds (0);
  m_expiredTime = Seconds (0);
  Watchdog watchdog;
  watchdog.SetFunction (&WatchdogTestCase::Expire, this);
  watchdog.SetArguments (MicroSeconds (40));
  watchdog.Ping (MicroSeconds (10));
  Simulator::Schedule (MicroSeconds (5), &Watchdog::Ping, &watchdog, MicroSeconds (20));
  Simulator::Schedule (MicroSeconds (20), &Watchdog::Ping, &watchdog, MicroSeconds (2));
  Simulator::Schedule (MicroSeconds (23), &Watchdog::Ping, &watchdog, MicroSeconds (17));
  Simulator::Run ();
  Simulator::Destroy ();
  NS_TEST_ASSERT_MSG_EQ (m_expired, true, "The timer did not expire ??");
  NS_TEST_ASSERT_MSG_EQ (m_expiredTime, MicroSeconds (40), "The timer did not expire at the expected time ?");
  NS_TEST_ASSERT_MSG_EQ (m_expiredArgument, MicroSeconds (40), "We did not get the right argument");

  return false;
}



static class WatchdogTestSuite : public TestSuite
{
public:
  WatchdogTestSuite()
    : TestSuite ("watchdog", UNIT)
  {
    AddTestCase (new WatchdogTestCase ());
  }
} g_watchdogTestSuite;

} // namespace ns3

