blob: 18c27512c8b60f443a6d5d22903428604ef359be [file] [log] [blame]
Alexander Afanasyevf1e013f2012-07-11 17:59:40 -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 "ndnSIM-pit.h"
22#include "ns3/core-module.h"
23#include "ns3/ndnSIM-module.h"
Alexander Afanasyev31cb4692012-08-17 13:08:20 -070024#include "ns3/point-to-point-module.h"
Alexander Afanasyevf1e013f2012-07-11 17:59:40 -070025
26#include <boost/lexical_cast.hpp>
27
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070028NS_LOG_COMPONENT_DEFINE ("ndn.PitTest");
Alexander Afanasyevf1e013f2012-07-11 17:59:40 -070029
30namespace ns3
31{
32
Alexander Afanasyevcf6dc922012-08-10 16:55:27 -070033class Client : public ndn::App
Alexander Afanasyevf1e013f2012-07-11 17:59:40 -070034{
35protected:
36 void
37 StartApplication ()
38 {
Alexander Afanasyevcf6dc922012-08-10 16:55:27 -070039 ndn::App::StartApplication ();
Alexander Afanasyevf1e013f2012-07-11 17:59:40 -070040
41 // add default route
Alexander Afanasyevcf6dc922012-08-10 16:55:27 -070042 Ptr<ndn::fib::Entry> fibEntry = GetNode ()->GetObject<ndn::Fib> ()->Add (ndn::NameComponents ("/"), m_face, 0);
43 fibEntry->UpdateStatus (m_face, ndn::fib::FaceMetric::NDN_FIB_GREEN);
Alexander Afanasyevf1e013f2012-07-11 17:59:40 -070044
45 Simulator::Schedule (Seconds (0.1), &Client::SendPacket, this, std::string("/1"), 1);
46 Simulator::Schedule (Seconds (0.2), &Client::SendPacket, this, std::string("/2"), 1);
47 Simulator::Schedule (Seconds (0.3), &Client::SendPacket, this, std::string("/3"), 1);
48 Simulator::Schedule (Seconds (0.4), &Client::SendPacket, this, std::string("/1"), 2);
49 }
50
51 void
52 StopApplication ()
53 {
Alexander Afanasyevcf6dc922012-08-10 16:55:27 -070054 ndn::App::StopApplication ();
Alexander Afanasyevf1e013f2012-07-11 17:59:40 -070055 }
56
57private:
58 void
59 SendPacket (const std::string &prefix, uint32_t nonce)
60 {
61 Ptr<Packet> pkt = Create<Packet> (0);
Alexander Afanasyevcf6dc922012-08-10 16:55:27 -070062 ndn::InterestHeader i;
63 i.SetName (Create<ndn::NameComponents> (prefix));
Alexander Afanasyevf1e013f2012-07-11 17:59:40 -070064 i.SetNonce (nonce);
65 i.SetInterestLifetime (Seconds (0.5));
66
67 pkt->AddHeader (i);
68 m_protocolHandler (pkt);
69 }
70};
71
72void
Alexander Afanasyevcf6dc922012-08-10 16:55:27 -070073PitTest::Test (Ptr<ndn::Fib> fib)
Alexander Afanasyevf1e013f2012-07-11 17:59:40 -070074{
75 NS_TEST_ASSERT_MSG_EQ (fib->GetSize (), 1, "There should be only one entry");
76
Alexander Afanasyevcf6dc922012-08-10 16:55:27 -070077 Ptr<const ndn::fib::Entry> fibEntry = fib->Begin ();
78 NS_TEST_ASSERT_MSG_EQ (fibEntry->GetPrefix (), ndn::NameComponents ("/"), "prefix should be /");
Alexander Afanasyevf1e013f2012-07-11 17:59:40 -070079}
80
81void
Alexander Afanasyevcf6dc922012-08-10 16:55:27 -070082PitTest::Check0 (Ptr<ndn::Pit> pit)
Alexander Afanasyevf1e013f2012-07-11 17:59:40 -070083{
Alexander Afanasyevcf6dc922012-08-10 16:55:27 -070084 // NS_LOG_DEBUG (*GetNode ()->GetObject<ndn::Pit> ());
Alexander Afanasyevf1e013f2012-07-11 17:59:40 -070085 NS_TEST_ASSERT_MSG_EQ (pit->GetSize (), 0, "There should 0 entries in PIT");
86}
87
88void
Alexander Afanasyevcf6dc922012-08-10 16:55:27 -070089PitTest::Check1 (Ptr<ndn::Pit> pit)
Alexander Afanasyevf1e013f2012-07-11 17:59:40 -070090{
91 NS_TEST_ASSERT_MSG_EQ (pit->GetSize (), 1, "There should 1 entry in PIT");
92}
93
94void
Alexander Afanasyevcf6dc922012-08-10 16:55:27 -070095PitTest::Check2 (Ptr<ndn::Pit> pit)
Alexander Afanasyevf1e013f2012-07-11 17:59:40 -070096{
Alexander Afanasyevcf6dc922012-08-10 16:55:27 -070097 // NS_LOG_DEBUG (*GetNode ()->GetObject<ndn::Pit> ());
Alexander Afanasyevf1e013f2012-07-11 17:59:40 -070098 NS_TEST_ASSERT_MSG_EQ (pit->GetSize (), 2, "There should 2 entries in PIT");
99}
100
101void
Alexander Afanasyevcf6dc922012-08-10 16:55:27 -0700102PitTest::Check3 (Ptr<ndn::Pit> pit)
Alexander Afanasyevf1e013f2012-07-11 17:59:40 -0700103{
Alexander Afanasyevcf6dc922012-08-10 16:55:27 -0700104 // NS_LOG_DEBUG (*GetNode ()->GetObject<ndn::Pit> ());
Alexander Afanasyevf1e013f2012-07-11 17:59:40 -0700105 NS_TEST_ASSERT_MSG_EQ (pit->GetSize (), 3, "There should 3 entries in PIT");
106}
107
108
109void
110PitTest::DoRun ()
111{
112 Ptr<Node> node = CreateObject<Node> ();
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700113 Ptr<Node> nodeSink = CreateObject<Node> ();
114 PointToPointHelper p2p;
115 p2p.Install (node, nodeSink);
116
Alexander Afanasyevcf6dc922012-08-10 16:55:27 -0700117 ndn::StackHelper ndn;
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700118 ndn.Install (node);
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700119 ndn.Install (nodeSink);
120
121 ndn::StackHelper::AddRoute (node, "/", 0, 0);
Alexander Afanasyevf1e013f2012-07-11 17:59:40 -0700122
123 Ptr<Client> app1 = CreateObject<Client> ();
124 node->AddApplication (app1);
125
Alexander Afanasyevcf6dc922012-08-10 16:55:27 -0700126 Simulator::Schedule (Seconds (0.0001), &PitTest::Test, this, node->GetObject<ndn::Fib> ());
Alexander Afanasyevf1e013f2012-07-11 17:59:40 -0700127
Alexander Afanasyevcf6dc922012-08-10 16:55:27 -0700128 Simulator::Schedule (Seconds (0.01), &PitTest::Check0, this, node->GetObject<ndn::Pit> ());
Alexander Afanasyevf1e013f2012-07-11 17:59:40 -0700129
Alexander Afanasyevcf6dc922012-08-10 16:55:27 -0700130 Simulator::Schedule (Seconds (0.11), &PitTest::Check1, this, node->GetObject<ndn::Pit> ());
131 Simulator::Schedule (Seconds (0.21), &PitTest::Check2, this, node->GetObject<ndn::Pit> ());
132 Simulator::Schedule (Seconds (0.31), &PitTest::Check3, this, node->GetObject<ndn::Pit> ());
Alexander Afanasyevf1e013f2012-07-11 17:59:40 -0700133
Alexander Afanasyevcf6dc922012-08-10 16:55:27 -0700134 Simulator::Schedule (Seconds (0.61), &PitTest::Check3, this, node->GetObject<ndn::Pit> ());
135 Simulator::Schedule (Seconds (0.71), &PitTest::Check2, this, node->GetObject<ndn::Pit> ());
136 Simulator::Schedule (Seconds (0.81), &PitTest::Check1, this, node->GetObject<ndn::Pit> ());
137
138 Simulator::Schedule (Seconds (0.91), &PitTest::Check0, this, node->GetObject<ndn::Pit> ());
Alexander Afanasyevf1e013f2012-07-11 17:59:40 -0700139
140 Simulator::Stop (Seconds (1.0));
141 Simulator::Run ();
142 Simulator::Destroy ();
143}
144
145}