Adapt the OLSR agent code to use the new Timer class.

This commit is contained in:
Gustavo J. A. M. Carneiro
2007-10-08 11:37:03 +01:00
parent 4df07ae61f
commit 743a4eaf02
7 changed files with 74 additions and 482 deletions

View File

@@ -1,152 +0,0 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2007 INESC Porto
* All rights reserved.
*
* 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: Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
*/
#include "event-collector.h"
#define CLEANUP_CHUNK_MIN_SIZE 8
#define CLEANUP_CHUNK_MAX_SIZE 128
namespace ns3 {
EventCollector::EventCollector () :
m_nextCleanupSize (CLEANUP_CHUNK_MIN_SIZE)
{}
void
EventCollector::Track (EventId event)
{
m_events.push_back (event);
if (m_events.size () >= m_nextCleanupSize)
Cleanup ();
}
inline bool
EventExpiredPredicate (const EventId &event)
{
return event.IsExpired ();
}
void
EventCollector::Grow ()
{
m_nextCleanupSize += (m_nextCleanupSize < CLEANUP_CHUNK_MAX_SIZE?
m_nextCleanupSize : CLEANUP_CHUNK_MAX_SIZE);
}
void
EventCollector::Shrink ()
{
while (m_nextCleanupSize > m_events.size ())
m_nextCleanupSize >>= 1;
Grow ();
}
// Called when a new event was added and the cleanup limit was exceeded in consequence.
void
EventCollector::Cleanup ()
{
m_events.remove_if (EventExpiredPredicate);
// If after cleanup we are still over the limit, increase the limit.
if (m_events.size () >= m_nextCleanupSize)
Grow ();
else
Shrink ();
}
EventCollector::~EventCollector ()
{
for (std::list<EventId>::iterator event = m_events.begin ();
event != m_events.end (); event++)
{
Simulator::Cancel (*event);
}
}
}; // namespace ns3
#ifdef RUN_SELF_TESTS
#include "ns3/test.h"
namespace ns3 {
class EventCollectorTests : public Test
{
int m_counter;
EventCollector *m_events;
void EventCollectorCallback ();
public:
EventCollectorTests ();
virtual ~EventCollectorTests ();
virtual bool RunTests (void);
};
EventCollectorTests::EventCollectorTests ()
: Test ("EventCollector"), m_counter (0), m_events (0)
{}
EventCollectorTests::~EventCollectorTests ()
{}
void
EventCollectorTests::EventCollectorCallback ()
{
m_counter++;
if (m_counter == 50)
{
// this should cause the remaining (50) events to be cancelled
delete m_events;
m_events = 0;
}
}
bool EventCollectorTests::RunTests (void)
{
bool result = true;
m_events = new EventCollector ();
for (int n = 0; n < 100; n++)
{
m_events->Track (Simulator::Schedule
(Simulator::Now (),
&EventCollectorTests::EventCollectorCallback,
this));
}
Simulator::Run ();
NS_TEST_ASSERT_EQUAL (m_events, 0);
NS_TEST_ASSERT_EQUAL (m_counter, 50);
return result;
}
static EventCollectorTests g_eventCollectorTests;
};
#endif /* RUN_SELF_TESTS */

View File

@@ -1,62 +0,0 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2007 INESC Porto
* All rights reserved.
*
* 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: Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
*/
#ifndef EVENT_COLLECTOR_H
#define EVENT_COLLECTOR_H
#include <list>
#include "ns3/event-id.h"
#include "ns3/simulator.h"
namespace ns3 {
/**
* \brief An object that tracks scheduled events and automatically
* cancels them when it is destroyed. It is useful in situations
* where multiple instances of the same type of event can
* simultaneously be scheduled, and when the events should be limited
* to the lifetime of a container object.
*/
class EventCollector
{
public:
EventCollector ();
/**
* \brief Tracks a new event
*/
void Track (EventId event);
~EventCollector ();
private:
std::list<EventId>::size_type m_nextCleanupSize;
std::list<EventId> m_events;
void Cleanup ();
void Grow ();
void Shrink ();
};
}; // namespace ns3
#endif /* EVENT_COLLECTOR_H */

View File

@@ -151,8 +151,18 @@ NS_DEBUG_COMPONENT_DEFINE ("OlsrAgent");
OlsrAgentImpl::OlsrAgentImpl (Ptr<Node> node)
:
m_useL2Notifications (false)
m_events (Timer::GARBAGE_COLLECT),
m_useL2Notifications (false),
m_helloTimer (Timer::CHECK_ON_SCHEDULE, Timer::CANCEL_ON_DESTROY),
m_tcTimer (Timer::CHECK_ON_SCHEDULE, Timer::CANCEL_ON_DESTROY),
m_midTimer (Timer::CHECK_ON_SCHEDULE, Timer::CANCEL_ON_DESTROY)
{
m_helloTimer.SetFunction (&OlsrAgentImpl::HelloTimerExpire, this);
m_tcTimer.SetFunction (&OlsrAgentImpl::TcTimerExpire, this);
m_midTimer.SetFunction (&OlsrAgentImpl::MidTimerExpire, this);
m_queuedMessagesTimer.SetFunction (&OlsrAgentImpl::SendQueuedMessages, this);
SetInterfaceId (OlsrAgentImpl::iid);
// Aggregate with the Node, so that OLSR dies when the node is destroyed.
@@ -881,9 +891,9 @@ OlsrAgentImpl::ProcessTc (const OlsrMessageHeader &msg,
AddTopologyTuple (topologyTuple);
// Schedules topology tuple deletion
Simulator::Schedule (DELAY (topologyTuple.expirationTime),
&OlsrAgentImpl::TopologyTupleTimerExpire,
this, topologyTuple);
m_events.SetFunction (&OlsrAgentImpl::TopologyTupleTimerExpire, this);
m_events.SetArguments (topologyTuple);
m_events.Schedule (DELAY (topologyTuple.expirationTime));
}
}
}
@@ -1035,7 +1045,8 @@ OlsrAgentImpl::QueueMessage (const OlsrMessageHeader &message, Time delay)
m_queuedMessages.push_back (message);
if (not m_queuedMessagesTimer.IsRunning ())
{
m_queuedMessagesTimer = Simulator::Schedule (delay, &OlsrAgentImpl::SendQueuedMessages, this);
m_queuedMessagesTimer.SetDelay (delay);
m_queuedMessagesTimer.Schedule ();
}
}
@@ -1339,9 +1350,9 @@ OlsrAgentImpl::LinkSensing (const OlsrMessageHeader &msg,
// Schedules link tuple deletion
if (created && link_tuple != NULL)
{
m_events.Track (Simulator::Schedule
(DELAY (std::min (link_tuple->time, link_tuple->symTime)),
&OlsrAgentImpl::LinkTupleTimerExpire, this, *link_tuple));
m_events.SetFunction (&OlsrAgentImpl::LinkTupleTimerExpire, this);
m_events.SetArguments (*link_tuple);
m_events.Schedule (DELAY (std::min (link_tuple->time, link_tuple->symTime)));
}
}
@@ -1409,10 +1420,9 @@ OlsrAgentImpl::PopulateTwoHopNeighborSet (const OlsrMessageHeader &msg,
now + msg.GetVTime ();
// Schedules nb2hop tuple
// deletion
m_events.Track (Simulator::Schedule
(DELAY (new_nb2hop_tuple.expirationTime),
&OlsrAgentImpl::Nb2hopTupleTimerExpire, this,
new_nb2hop_tuple));
m_events.SetFunction (&OlsrAgentImpl::Nb2hopTupleTimerExpire, this);
m_events.SetArguments (new_nb2hop_tuple);
m_events.Schedule (DELAY (new_nb2hop_tuple.expirationTime));
}
else
{
@@ -1478,10 +1488,9 @@ OlsrAgentImpl::PopulateMprSelectorSet (const OlsrMessageHeader &msg,
AddMprSelectorTuple (mprsel_tuple);
// Schedules mpr selector tuple deletion
m_events.Track (Simulator::Schedule
(DELAY (mprsel_tuple.expirationTime),
&OlsrAgentImpl::MprSelTupleTimerExpire,
this, mprsel_tuple));
m_events.SetFunction (&OlsrAgentImpl::MprSelTupleTimerExpire, this);
m_events.SetArguments (mprsel_tuple);
m_events.Schedule (DELAY (mprsel_tuple.expirationTime));
}
else
{
@@ -1874,7 +1883,7 @@ void
OlsrAgentImpl::HelloTimerExpire ()
{
SendHello ();
m_helloTimer = Simulator::Schedule (m_helloInterval, &OlsrAgentImpl::HelloTimerExpire, this);
m_helloTimer.Schedule (m_helloInterval);
}
///
@@ -1885,8 +1894,10 @@ void
OlsrAgentImpl::TcTimerExpire ()
{
if (m_state.GetMprSelectors ().size () > 0)
SendTc ();
m_tcTimer = Simulator::Schedule (m_tcInterval, &OlsrAgentImpl::TcTimerExpire, this);
{
SendTc ();
}
m_tcTimer.Schedule (m_tcInterval);
}
///
@@ -1898,15 +1909,15 @@ void
OlsrAgentImpl::MidTimerExpire ()
{
SendMid ();
m_midTimer = Simulator::Schedule (m_midInterval, &OlsrAgentImpl::MidTimerExpire, this);
m_midTimer.Schedule (m_midInterval);
}
///
/// \brief Removes tuple_ if expired. Else timer is rescheduled to expire at tuple_->time().
/// \brief Removes tuple if expired. Else timer is rescheduled to expire at tuple.expirationTime.
///
/// The task of actually removing the tuple is left to the OLSR agent.
///
/// \param e The event which has expired.
/// \param tuple The tuple which has expired.
///
void
OlsrAgentImpl::DupTupleTimerExpire (DuplicateTuple tuple)
@@ -1916,9 +1927,11 @@ OlsrAgentImpl::DupTupleTimerExpire (DuplicateTuple tuple)
RemoveDuplicateTuple (tuple);
}
else
m_events.Track (Simulator::Schedule
(DELAY (tuple.expirationTime),
&OlsrAgentImpl::DupTupleTimerExpire, this, tuple));
{
m_events.SetFunction (&OlsrAgentImpl::DupTupleTimerExpire, this);
m_events.SetArguments (tuple);
m_events.Schedule (DELAY (tuple.expirationTime));
}
}
///
@@ -1948,15 +1961,16 @@ OlsrAgentImpl::LinkTupleTimerExpire (LinkTuple tuple)
else
NeighborLoss (tuple);
m_events.Track (Simulator::Schedule
(DELAY(tuple.time),
&OlsrAgentImpl::LinkTupleTimerExpire, this, tuple));
m_events.SetFunction (&OlsrAgentImpl::LinkTupleTimerExpire, this);
m_events.SetArguments (tuple);
m_events.Schedule (DELAY (tuple.time));
}
else
m_events.Track (Simulator::Schedule
(DELAY (std::min (tuple.time, tuple.symTime)),
&OlsrAgentImpl::LinkTupleTimerExpire, this, tuple));
{
m_events.SetFunction (&OlsrAgentImpl::LinkTupleTimerExpire, this);
m_events.SetArguments (tuple);
m_events.Schedule (DELAY (std::min (tuple.time, tuple.symTime)));
}
}
///
@@ -1974,9 +1988,11 @@ OlsrAgentImpl::Nb2hopTupleTimerExpire (TwoHopNeighborTuple tuple)
RemoveTwoHopNeighborTuple (tuple);
}
else
m_events.Track (Simulator::Schedule
(DELAY (tuple.expirationTime),
&OlsrAgentImpl::Nb2hopTupleTimerExpire, this, tuple));
{
m_events.SetFunction (&OlsrAgentImpl::Nb2hopTupleTimerExpire, this);
m_events.SetArguments (tuple);
m_events.Schedule (DELAY (tuple.expirationTime));
}
}
///
@@ -1994,9 +2010,11 @@ OlsrAgentImpl::MprSelTupleTimerExpire (MprSelectorTuple tuple)
RemoveMprSelectorTuple (tuple);
}
else
m_events.Track (Simulator::Schedule
(DELAY (tuple.expirationTime),
&OlsrAgentImpl::MprSelTupleTimerExpire, this, tuple));
{
m_events.SetFunction (&OlsrAgentImpl::MprSelTupleTimerExpire, this);
m_events.SetArguments (tuple);
m_events.Schedule (DELAY (tuple.expirationTime));
}
}
///
@@ -2014,9 +2032,11 @@ OlsrAgentImpl::TopologyTupleTimerExpire (TopologyTuple tuple)
RemoveTopologyTuple (tuple);
}
else
m_events.Track (Simulator::Schedule
(DELAY (tuple.expirationTime),
&OlsrAgentImpl::TopologyTupleTimerExpire, this, tuple));
{
m_events.SetFunction (&OlsrAgentImpl::TopologyTupleTimerExpire, this);
m_events.SetArguments (tuple);
m_events.Schedule (DELAY (tuple.expirationTime));
}
}
///
@@ -2032,9 +2052,11 @@ OlsrAgentImpl::IfaceAssocTupleTimerExpire (IfaceAssocTuple tuple)
RemoveIfaceAssocTuple (tuple);
}
else
m_events.Track (Simulator::Schedule
(DELAY (tuple.time),
&OlsrAgentImpl::IfaceAssocTupleTimerExpire, this, tuple));
{
m_events.SetFunction (&OlsrAgentImpl::IfaceAssocTupleTimerExpire, this);
m_events.SetArguments (tuple);
m_events.Schedule (DELAY (tuple.time));
}
}

View File

@@ -38,8 +38,9 @@
#include "ns3/packet.h"
#include "ns3/node.h"
#include "ns3/socket.h"
#include "single-event.h"
#include "event-collector.h"
//#include "single-event.h"
//#include "event-collector.h"
#include "ns3/timer.h"
namespace ns3 {
@@ -58,7 +59,7 @@ public:
virtual void SetMainInterface (uint32_t interface);
private:
EventCollector m_events;
Timer m_events;
/// Address of the routing agent.
Ipv4Address m_routingAgentAddr;
@@ -106,13 +107,13 @@ protected:
Ipv4Address GetMainAddress (Ipv4Address iface_addr);
// Timer handlers
SingleEvent m_helloTimer;
Timer m_helloTimer;
void HelloTimerExpire ();
SingleEvent m_tcTimer;
Timer m_tcTimer;
void TcTimerExpire ();
SingleEvent m_midTimer;
Timer m_midTimer;
void MidTimerExpire ();
void DupTupleTimerExpire (DuplicateTuple tuple);
@@ -125,7 +126,7 @@ protected:
/// A list of pending messages which are buffered awaiting for being sent.
std::vector<OlsrMessageHeader> m_queuedMessages;
SingleEvent m_queuedMessagesTimer; // timer for throttling outgoing messages
Timer m_queuedMessagesTimer; // timer for throttling outgoing messages
void ForwardDefault (OlsrMessageHeader olsrMessage,
DuplicateTuple *duplicated,

View File

@@ -1,135 +0,0 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2007 INESC Porto
* All rights reserved.
*
* 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: Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
*/
#include "single-event.h"
namespace ns3 {
SingleEvent::SingleEvent ()
{
}
SingleEvent::~SingleEvent ()
{
Simulator::Cancel (m_event);
}
void
SingleEvent::operator = (const EventId &event)
{
Simulator::Cancel (m_event);
m_event = event;
}
void
SingleEvent::Cancel (void)
{
m_event.Cancel ();
}
bool
SingleEvent::IsExpired (void) const
{
return m_event.IsExpired ();
}
bool
SingleEvent::IsRunning (void) const
{
return m_event.IsRunning ();
}
}; // namespace ns3
#ifdef RUN_SELF_TESTS
#include "ns3/test.h"
namespace ns3 {
class SingleEventTests : public Test
{
int m_counter;
SingleEvent *m_events;
void SingleEventCallback ();
public:
SingleEventTests ();
virtual ~SingleEventTests ();
virtual bool RunTests (void);
};
SingleEventTests::SingleEventTests ()
: Test ("SingleEvent"), m_counter (0), m_events (0)
{}
SingleEventTests::~SingleEventTests ()
{}
void
SingleEventTests::SingleEventCallback ()
{
m_counter++;
}
bool SingleEventTests::RunTests (void)
{
bool result = true;
{
SingleEvent event;
event = Simulator::Schedule (Simulator::Now (), &SingleEventTests::SingleEventCallback, this);
Simulator::Run ();
NS_TEST_ASSERT_EQUAL (m_counter, 1);
}
{
SingleEvent event;
event = Simulator::Schedule (Simulator::Now (), &SingleEventTests::SingleEventCallback, this);
} // event is destroyed => the eventid cancelled
Simulator::Run ();
NS_TEST_ASSERT_EQUAL (m_counter, 1);
{
SingleEvent event;
event = Simulator::Schedule (Simulator::Now (), &SingleEventTests::SingleEventCallback, this);
// the second event cancels the first one
event = Simulator::Schedule (Simulator::Now (), &SingleEventTests::SingleEventCallback, this);
Simulator::Run ();
NS_TEST_ASSERT_EQUAL (m_counter, 2);
}
return result;
}
static SingleEventTests g_singleEventTests;
};
#endif /* RUN_SELF_TESTS */

View File

@@ -1,76 +0,0 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* Copyright (c) 2007 INESC Porto
* All rights reserved.
*
* 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: Gustavo J. A. M. Carneiro <gjc@inescporto.pt>
*/
#ifndef SINGLE_EVENT_H
#define SINGLE_EVENT_H
#include <list>
#include "ns3/event-id.h"
#include "ns3/simulator.h"
namespace ns3 {
/**
* \brief An object that tracks a single event
*
* A SingleEvent acts as a smart proxy for an EventId. It
* automatically cancels the underlying EventId when it is replaced
* for a new EventId, or when the SingleEvent is destroyed. It is
* useful in situations where only one type of an event can possibly
* be scheduled at any single time (e.g. a single timer instance), and
* when the event should be limited to the lifetime of a container
* object.
*/
class SingleEvent
{
public:
SingleEvent ();
~SingleEvent ();
/**
* \brief Tracks a newly scheduled event.
*
* Tracks a newly scheduled event. Any previous event that may have
* been scheduled is first cancelled.
*/
void operator = (const EventId &event);
/**
* This method is syntactic sugar for the ns3::Simulator::cancel
* method.
*/
void Cancel (void);
/**
* This method is syntactic sugar for the ns3::Simulator::isExpired
* method.
* \returns true if the event has expired, false otherwise.
*/
bool IsExpired (void) const;
bool IsRunning (void) const;
private:
EventId m_event;
};
}; // namespace ns3
#endif /* SINGLE_EVENT_H */

View File

@@ -18,9 +18,3 @@ def build(bld):
'olsr.h',
]
## globally useful classes, temporarily private...
module.source.extend([
'event-collector.cc',
'single-event.cc',
])