Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 1 | /* -*- 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" |
| 24 | |
| 25 | #include <boost/lexical_cast.hpp> |
| 26 | |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame^] | 27 | NS_LOG_COMPONENT_DEFINE ("NdnPitTest"); |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 28 | |
| 29 | namespace ns3 |
| 30 | { |
| 31 | |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame^] | 32 | class Client : public NdnApp |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 33 | { |
| 34 | protected: |
| 35 | void |
| 36 | StartApplication () |
| 37 | { |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame^] | 38 | NdnApp::StartApplication (); |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 39 | |
| 40 | // add default route |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame^] | 41 | Ptr<NdnFibEntry> fibEntry = GetNode ()->GetObject<NdnFib> ()->Add (NdnNameComponents ("/"), m_face, 0); |
| 42 | fibEntry->UpdateStatus (m_face, NdnFibFaceMetric::NDN_FIB_GREEN); |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 43 | |
| 44 | Simulator::Schedule (Seconds (0.1), &Client::SendPacket, this, std::string("/1"), 1); |
| 45 | Simulator::Schedule (Seconds (0.2), &Client::SendPacket, this, std::string("/2"), 1); |
| 46 | Simulator::Schedule (Seconds (0.3), &Client::SendPacket, this, std::string("/3"), 1); |
| 47 | Simulator::Schedule (Seconds (0.4), &Client::SendPacket, this, std::string("/1"), 2); |
| 48 | } |
| 49 | |
| 50 | void |
| 51 | StopApplication () |
| 52 | { |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame^] | 53 | NdnApp::StopApplication (); |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | private: |
| 57 | void |
| 58 | SendPacket (const std::string &prefix, uint32_t nonce) |
| 59 | { |
| 60 | Ptr<Packet> pkt = Create<Packet> (0); |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame^] | 61 | NdnInterestHeader i; |
| 62 | i.SetName (Create<NdnNameComponents> (prefix)); |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 63 | i.SetNonce (nonce); |
| 64 | i.SetInterestLifetime (Seconds (0.5)); |
| 65 | |
| 66 | pkt->AddHeader (i); |
| 67 | m_protocolHandler (pkt); |
| 68 | } |
| 69 | }; |
| 70 | |
| 71 | void |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame^] | 72 | PitTest::Test (Ptr<NdnFib> fib) |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 73 | { |
| 74 | NS_TEST_ASSERT_MSG_EQ (fib->GetSize (), 1, "There should be only one entry"); |
| 75 | |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame^] | 76 | Ptr<const NdnFibEntry> fibEntry = fib->Begin (); |
| 77 | NS_TEST_ASSERT_MSG_EQ (fibEntry->GetPrefix (), NdnNameComponents ("/"), "prefix should be /"); |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | void |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame^] | 81 | PitTest::Check0 (Ptr<NdnPit> pit) |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 82 | { |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame^] | 83 | // NS_LOG_DEBUG (*GetNode ()->GetObject<NdnPit> ()); |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 84 | NS_TEST_ASSERT_MSG_EQ (pit->GetSize (), 0, "There should 0 entries in PIT"); |
| 85 | } |
| 86 | |
| 87 | void |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame^] | 88 | PitTest::Check1 (Ptr<NdnPit> pit) |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 89 | { |
| 90 | NS_TEST_ASSERT_MSG_EQ (pit->GetSize (), 1, "There should 1 entry in PIT"); |
| 91 | } |
| 92 | |
| 93 | void |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame^] | 94 | PitTest::Check2 (Ptr<NdnPit> pit) |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 95 | { |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame^] | 96 | // NS_LOG_DEBUG (*GetNode ()->GetObject<NdnPit> ()); |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 97 | NS_TEST_ASSERT_MSG_EQ (pit->GetSize (), 2, "There should 2 entries in PIT"); |
| 98 | } |
| 99 | |
| 100 | void |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame^] | 101 | PitTest::Check3 (Ptr<NdnPit> pit) |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 102 | { |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame^] | 103 | // NS_LOG_DEBUG (*GetNode ()->GetObject<NdnPit> ()); |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 104 | NS_TEST_ASSERT_MSG_EQ (pit->GetSize (), 3, "There should 3 entries in PIT"); |
| 105 | } |
| 106 | |
| 107 | |
| 108 | void |
| 109 | PitTest::DoRun () |
| 110 | { |
| 111 | Ptr<Node> node = CreateObject<Node> (); |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame^] | 112 | NdnStackHelper ndn; |
| 113 | ndn.Install (node); |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 114 | |
| 115 | Ptr<Client> app1 = CreateObject<Client> (); |
| 116 | node->AddApplication (app1); |
| 117 | |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame^] | 118 | Simulator::Schedule (Seconds (0.0001), &PitTest::Test, this, node->GetObject<NdnFib> ()); |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 119 | |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame^] | 120 | Simulator::Schedule (Seconds (0.01), &PitTest::Check0, this, node->GetObject<NdnPit> ()); |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 121 | |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame^] | 122 | Simulator::Schedule (Seconds (0.11), &PitTest::Check1, this, node->GetObject<NdnPit> ()); |
| 123 | Simulator::Schedule (Seconds (0.21), &PitTest::Check2, this, node->GetObject<NdnPit> ()); |
| 124 | Simulator::Schedule (Seconds (0.31), &PitTest::Check3, this, node->GetObject<NdnPit> ()); |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 125 | |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame^] | 126 | Simulator::Schedule (Seconds (0.61), &PitTest::Check3, this, node->GetObject<NdnPit> ()); |
| 127 | Simulator::Schedule (Seconds (0.71), &PitTest::Check2, this, node->GetObject<NdnPit> ()); |
| 128 | Simulator::Schedule (Seconds (0.81), &PitTest::Check1, this, node->GetObject<NdnPit> ()); |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 129 | |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame^] | 130 | Simulator::Schedule (Seconds (0.91), &PitTest::Check0, this, node->GetObject<NdnPit> ()); |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 131 | |
| 132 | Simulator::Stop (Seconds (1.0)); |
| 133 | Simulator::Run (); |
| 134 | Simulator::Destroy (); |
| 135 | } |
| 136 | |
| 137 | } |