diff --git a/src/simulator/watchdog.cc b/src/simulator/watchdog.cc new file mode 100644 index 000000000..c12743e34 --- /dev/null +++ b/src/simulator/watchdog.cc @@ -0,0 +1,60 @@ +/* -*- 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 + */ +#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 diff --git a/src/simulator/watchdog.h b/src/simulator/watchdog.h new file mode 100644 index 000000000..3bb37a9b4 --- /dev/null +++ b/src/simulator/watchdog.h @@ -0,0 +1,217 @@ +/* -*- 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 + */ +#ifndef WATCHDOG_H +#define WATCHDOG_H + +#include "nstime.h" +#include "event-id.h" + +namespace ns3 { + +class TimerImpl; + +class Watchdog +{ +public: + Watchdog (); + ~Watchdog (); + + void Ping (Time delay); + + /** + * \param fn the function + * + * Store this function in this Timer for later use by Timer::Schedule. + */ + template + void SetFunction (FN fn); + + /** + * \param memPtr the member function pointer + * \param objPtr the pointer to object + * + * Store this function and object in this Timer for later use by Timer::Schedule. + */ + template + void SetFunction (MEM_PTR memPtr, OBJ_PTR objPtr); + + + /** + * \param a1 the first argument + * + * Store this argument in this Timer for later use by Timer::Schedule. + */ + template + void SetArguments (T1 a1); + /** + * \param a1 the first argument + * \param a2 the second argument + * + * Store these arguments in this Timer for later use by Timer::Schedule. + */ + template + void SetArguments (T1 a1, T2 a2); + /** + * \param a1 the first argument + * \param a2 the second argument + * \param a3 the third argument + * + * Store these arguments in this Timer for later use by Timer::Schedule. + */ + template + void SetArguments (T1 a1, T2 a2, T3 a3); + /** + * \param a1 the first argument + * \param a2 the second argument + * \param a3 the third argument + * \param a4 the fourth argument + * + * Store these arguments in this Timer for later use by Timer::Schedule. + */ + template + void SetArguments (T1 a1, T2 a2, T3 a3, T4 a4); + /** + * \param a1 the first argument + * \param a2 the second argument + * \param a3 the third argument + * \param a4 the fourth argument + * \param a5 the fifth argument + * + * Store these arguments in this Timer for later use by Timer::Schedule. + */ + template + void SetArguments (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5); + /** + * \param a1 the first argument + * \param a2 the second argument + * \param a3 the third argument + * \param a4 the fourth argument + * \param a5 the fifth argument + * \param a6 the sixth argument + * + * Store these arguments in this Timer for later use by Timer::Schedule. + */ + template + void SetArguments (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6); + +private: + void Expire (void); + TimerImpl *m_impl; + EventId m_event; + Time m_end; +}; + +} // namespace ns3 + +#include "timer-impl.h" + +namespace ns3 { + + +template +void +Watchdog::SetFunction (FN fn) +{ + delete m_impl; + m_impl = MakeTimerImpl (fn); +} +template +void +Watchdog::SetFunction (MEM_PTR memPtr, OBJ_PTR objPtr) +{ + delete m_impl; + m_impl = MakeTimerImpl (memPtr, objPtr); +} + +template +void +Watchdog::SetArguments (T1 a1) +{ + if (m_impl == 0) + { + NS_FATAL_ERROR ("You cannot set the arguments of a Watchdog before setting its function."); + return; + } + m_impl->SetArgs (a1); +} +template +void +Watchdog::SetArguments (T1 a1, T2 a2) +{ + if (m_impl == 0) + { + NS_FATAL_ERROR ("You cannot set the arguments of a Watchdog before setting its function."); + return; + } + m_impl->SetArgs (a1, a2); +} + +template +void +Watchdog::SetArguments (T1 a1, T2 a2, T3 a3) +{ + if (m_impl == 0) + { + NS_FATAL_ERROR ("You cannot set the arguments of a Watchdog before setting its function."); + return; + } + m_impl->SetArgs (a1, a2, a3); +} + +template +void +Watchdog::SetArguments (T1 a1, T2 a2, T3 a3, T4 a4) +{ + if (m_impl == 0) + { + NS_FATAL_ERROR ("You cannot set the arguments of a Watchdog before setting its function."); + return; + } + m_impl->SetArgs (a1, a2, a3, a4); +} + +template +void +Watchdog::SetArguments (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5) +{ + if (m_impl == 0) + { + NS_FATAL_ERROR ("You cannot set the arguments of a Watchdog before setting its function."); + return; + } + m_impl->SetArgs (a1, a2, a3, a4, a5); +} + +template +void +Watchdog::SetArguments (T1 a1, T2 a2, T3 a3, T4 a4, T5 a5, T6 a6) +{ + if (m_impl == 0) + { + NS_FATAL_ERROR ("You cannot set the arguments of a Watchdog before setting its function."); + return; + } + m_impl->SetArgs (a1, a2, a3, a4, a5, a6); +} + +} // namespace ns3 + + +#endif /* WATCHDOG_H */ diff --git a/src/simulator/wscript b/src/simulator/wscript index d0a0817a7..09da776f3 100644 --- a/src/simulator/wscript +++ b/src/simulator/wscript @@ -62,6 +62,7 @@ def build(bld): 'time-default-value.cc', 'timer.cc', 'event-garbage-collector.cc', + 'watchdog.cc', ] headers = bld.create_obj('ns3header')