blob: 555aca65c9b522efae96d02d942aa6429127e6ab [file] [log] [blame]
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -07001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2011,2012 University of California, Los Angeles
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
19 */
20
21#include "fw-per-fib-limits.h"
22#include "ns3/core-module.h"
23#include "ns3/ndnSIM-module.h"
24#include "ns3/point-to-point-module.h"
25
26#include <boost/lexical_cast.hpp>
27
28NS_LOG_COMPONENT_DEFINE ("ndn.test.fw.PerFibLimits");
29
30namespace ns3 {
31namespace ndn {
32
33void Decay (Ptr<fib::Entry> entry)
34{
35 entry->GetLimits ().DecayCurrentLimit ();
36}
37
38
39template<class T>
40void
41PrintTracedValue (std::string context, T oldValue, T newValue)
42{
43 NS_LOG_DEBUG (context << ": " <<
44 oldValue << " => " << newValue);
45}
46
47void
48Test1 (Ptr<fib::Entry> entry)
49{
50 entry->GetLimits ().IsBelowLimit ();
51 entry->GetLimits ().DecreaseLimit ();
52}
53
54void
55Test2 (Ptr<fib::Entry> entry)
56{
57 entry->GetLimits ().RemoveOutstanding ();
58 for (uint32_t i = 0; i < 40; i++)
59 entry->GetLimits ().IncreaseLimit ();
60}
61
62void
63FwPerFibLimits::CheckCurMaxLimit (Ptr<fib::Entry> entry, double amount)
64{
65 NS_TEST_ASSERT_MSG_EQ_TOL ((double)entry->GetLimits ().m_curMaxLimit, amount, 0.1, "");
66}
67
68void
69FwPerFibLimits::CheckOutstanding (Ptr<fib::Entry> entry, uint32_t amount)
70{
71 NS_TEST_ASSERT_MSG_EQ ((double)entry->GetLimits ().m_outstanding, amount, "");
72}
73
74// void
75// FwPerFibLimits::Check2 (Ptr<fib::Entry> entry)
76// {
77// NS_TEST_ASSERT_MSG_EQ ((double)entry->GetLimits ().m_outstanding, 0, "");
78// }
79
80void
81FwPerFibLimits::DoRun ()
82{
83 Simulator::Destroy ();
84
85 NodeContainer nodes;
86 nodes.Create (2);
87
88 PointToPointHelper p2pHelper;
89 p2pHelper.Install (nodes);
90
91 StackHelper ndn;
92 ndn.SetForwardingStrategy ("ns3::ndn::fw::PerFibLimits");
93 ndn.Install (nodes);
94
95 Ptr<Fib> fib = nodes.Get (0)->GetObject<Fib> ();
96 ndn.AddRoute (nodes.Get (0), "/bla", 0, 10);
97
98 Ptr<fib::Entry> entry = fib->Begin ();
99
100 bool ok = entry->GetLimits ().TraceConnect ("CurMaxLimit", "fibEntry.curMax", MakeCallback (PrintTracedValue<double>));
101 NS_TEST_ASSERT_MSG_EQ (ok, true, "");
102
103 ok = entry->GetLimits ().TraceConnect ("Outstanding", "fibEntry.out", MakeCallback (PrintTracedValue<uint32_t>));
104 NS_TEST_ASSERT_MSG_EQ (ok, true, "");
105
Alexander Afanasyev6a3bb132012-08-15 09:47:35 -0700106 ok = nodes.Get (0)->GetObject<L3Protocol> ()->GetFace (0)->GetLimits ().TraceConnect ("CurMaxLimit", "face.curMax", MakeCallback (PrintTracedValue<double>));
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700107 NS_TEST_ASSERT_MSG_EQ (ok, true, "");
Alexander Afanasyev6a3bb132012-08-15 09:47:35 -0700108 nodes.Get (0)->GetObject<L3Protocol> ()->GetFace (0)->GetLimits ().TraceDisconnect ("CurMaxLimit", "face.curMax", MakeCallback (PrintTracedValue<double>));
Alexander Afanasyev3476edf2012-08-14 11:26:00 -0700109
Alexander Afanasyev6a3bb132012-08-15 09:47:35 -0700110 ok = nodes.Get (0)->GetObject<L3Protocol> ()->GetFace (0)->GetLimits ().TraceConnect ("Outstanding", "face.out", MakeCallback (PrintTracedValue<uint32_t>));
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700111 NS_TEST_ASSERT_MSG_EQ (ok, true, "");
Alexander Afanasyev6a3bb132012-08-15 09:47:35 -0700112 nodes.Get (0)->GetObject<L3Protocol> ()->GetFace (0)->GetLimits ().TraceDisconnect ("Outstanding", "face.out", MakeCallback (PrintTracedValue<uint32_t>));
Alexander Afanasyev3476edf2012-08-14 11:26:00 -0700113
114 Config::Connect ("/NodeList/0/$ns3::ndn::L3Protocol/FaceList/*/Limits/CurMaxLimit", MakeCallback (PrintTracedValue<double>));
115 Config::Connect ("/NodeList/0/$ns3::ndn::L3Protocol/FaceList/*/Limits/Outstanding", MakeCallback (PrintTracedValue<uint32_t>));
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700116
Alexander Afanasyev6a3bb132012-08-15 09:47:35 -0700117 nodes.Get (0)->GetObject<L3Protocol> ()->GetFace (0)->GetLimits ().SetMaxLimit (100);
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700118
119 entry->GetLimits ().SetMaxLimit (100);
120 NS_TEST_ASSERT_MSG_EQ_TOL ((double)entry->GetLimits ().m_curMaxLimit, 100, 0.1, "");
121
122 entry->GetLimits ().DecreaseLimit ();
123 NS_TEST_ASSERT_MSG_EQ_TOL ((double)entry->GetLimits ().m_curMaxLimit, 50, 0.1, "");
124
125 entry = fib->Begin ();
126
127 NS_LOG_DEBUG (entry);
128 Simulator::Schedule (Seconds (0.1), Decay, entry);
129 Simulator::Schedule (Seconds (25.0), Decay, entry);
130 Simulator::Schedule (Seconds (28.0), Decay, entry);
131 Simulator::Schedule (Seconds (40.0), Decay, entry);
132 Simulator::Schedule (Seconds (60.0), Decay, entry);
133 Simulator::Schedule (Seconds (100.0), Decay, entry);
134
135 Simulator::Schedule (Seconds (100.1), &FwPerFibLimits::CheckCurMaxLimit, this, entry, 81.5);
136
137 Simulator::Schedule (Seconds (100.5), &FwPerFibLimits::CheckOutstanding, this, entry, 0);
138 Simulator::Schedule (Seconds (101.0), Test1, entry);
139 Simulator::Schedule (Seconds (101.5), &FwPerFibLimits::CheckOutstanding, this, entry, 1);
140 Simulator::Schedule (Seconds (101.5), &FwPerFibLimits::CheckCurMaxLimit, this, entry, 40.75);
141
142 Simulator::Schedule (Seconds (102.0), Test2, entry);
143 Simulator::Schedule (Seconds (102.5), &FwPerFibLimits::CheckOutstanding, this, entry, 0);
144 Simulator::Schedule (Seconds (102.5), &FwPerFibLimits::CheckCurMaxLimit, this, entry, 41.75);
145
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700146 AppHelper consumer ("ns3::ndn::ConsumerBatches");
147 consumer.SetPrefix ("/bla");
148 consumer.SetAttribute ("Batches", StringValue ("105 1"));
149 consumer.SetAttribute ("LifeTime", StringValue ("1s"));
Alexander Afanasyev3476edf2012-08-14 11:26:00 -0700150 consumer.Install (nodes.Get (0))
151 .Stop (Seconds (110.0));
152
153 Simulator::Schedule (Seconds (105.1), &FwPerFibLimits::CheckOutstanding, this, entry, 1);
154 Simulator::Schedule (Seconds (106.7), &FwPerFibLimits::CheckOutstanding, this, entry, 0);
155
156 Simulator::Schedule (Seconds (109.0), &FwPerFibLimits::CheckCurMaxLimit, this, entry, 14.27);
157 Simulator::Schedule (Seconds (119.9), &FwPerFibLimits::CheckCurMaxLimit, this, entry, 16.32);
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700158
Alexander Afanasyev3476edf2012-08-14 11:26:00 -0700159 Simulator::Stop (Seconds (120.0));
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700160 Simulator::Run ();
161
162 Simulator::Destroy ();
163}
164
165} // namespace ndn
166} // namespace ns3