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" |
Alexander Afanasyev | 31cb469 | 2012-08-17 13:08:20 -0700 | [diff] [blame] | 24 | #include "ns3/point-to-point-module.h" |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 25 | |
| 26 | #include <boost/lexical_cast.hpp> |
| 27 | |
Alexander Afanasyev | 2b4c947 | 2012-08-09 15:00:38 -0700 | [diff] [blame] | 28 | NS_LOG_COMPONENT_DEFINE ("ndn.PitTest"); |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 29 | |
| 30 | namespace ns3 |
| 31 | { |
| 32 | |
Alexander Afanasyev | ee76255 | 2013-07-10 22:29:22 -0700 | [diff] [blame] | 33 | class PitTestClient : public ndn::App |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 34 | { |
| 35 | protected: |
| 36 | void |
| 37 | StartApplication () |
| 38 | { |
Alexander Afanasyev | cf6dc92 | 2012-08-10 16:55:27 -0700 | [diff] [blame] | 39 | ndn::App::StartApplication (); |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 40 | |
| 41 | // add default route |
Alexander Afanasyev | cfdc14f | 2013-03-15 14:38:44 -0700 | [diff] [blame] | 42 | Ptr<ndn::fib::Entry> fibEntry = GetNode ()->GetObject<ndn::Fib> ()->Add (ndn::Name ("/"), m_face, 0); |
Alexander Afanasyev | cf6dc92 | 2012-08-10 16:55:27 -0700 | [diff] [blame] | 43 | fibEntry->UpdateStatus (m_face, ndn::fib::FaceMetric::NDN_FIB_GREEN); |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 44 | |
Alexander Afanasyev | ee76255 | 2013-07-10 22:29:22 -0700 | [diff] [blame] | 45 | Simulator::Schedule (Seconds (0.1), &PitTestClient::SendPacket, this, std::string("/1"), 1); |
| 46 | Simulator::Schedule (Seconds (0.2), &PitTestClient::SendPacket, this, std::string("/2"), 1); |
| 47 | Simulator::Schedule (Seconds (0.3), &PitTestClient::SendPacket, this, std::string("/3"), 1); |
| 48 | Simulator::Schedule (Seconds (0.4), &PitTestClient::SendPacket, this, std::string("/1"), 2); |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | void |
| 52 | StopApplication () |
| 53 | { |
Alexander Afanasyev | cf6dc92 | 2012-08-10 16:55:27 -0700 | [diff] [blame] | 54 | ndn::App::StopApplication (); |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | private: |
| 58 | void |
| 59 | SendPacket (const std::string &prefix, uint32_t nonce) |
| 60 | { |
Alexander Afanasyev | ee76255 | 2013-07-10 22:29:22 -0700 | [diff] [blame] | 61 | Ptr<ndn::Interest> interest = Create<ndn::Interest> (); |
Alexander Afanasyev | faa01f9 | 2013-07-10 18:34:31 -0700 | [diff] [blame] | 62 | interest->SetName (Create<ndn::Name> (prefix)); |
| 63 | interest->SetNonce (nonce); |
| 64 | interest->SetInterestLifetime (Seconds (0.5)); |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 65 | |
Alexander Afanasyev | faa01f9 | 2013-07-10 18:34:31 -0700 | [diff] [blame] | 66 | m_face->ReceiveInterest (interest); |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 67 | } |
| 68 | }; |
| 69 | |
| 70 | void |
Alexander Afanasyev | cf6dc92 | 2012-08-10 16:55:27 -0700 | [diff] [blame] | 71 | PitTest::Test (Ptr<ndn::Fib> fib) |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 72 | { |
| 73 | NS_TEST_ASSERT_MSG_EQ (fib->GetSize (), 1, "There should be only one entry"); |
| 74 | |
Alexander Afanasyev | cf6dc92 | 2012-08-10 16:55:27 -0700 | [diff] [blame] | 75 | Ptr<const ndn::fib::Entry> fibEntry = fib->Begin (); |
Alexander Afanasyev | cfdc14f | 2013-03-15 14:38:44 -0700 | [diff] [blame] | 76 | NS_TEST_ASSERT_MSG_EQ (fibEntry->GetPrefix (), ndn::Name ("/"), "prefix should be /"); |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | void |
Alexander Afanasyev | cf6dc92 | 2012-08-10 16:55:27 -0700 | [diff] [blame] | 80 | PitTest::Check0 (Ptr<ndn::Pit> pit) |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 81 | { |
Alexander Afanasyev | cf6dc92 | 2012-08-10 16:55:27 -0700 | [diff] [blame] | 82 | // NS_LOG_DEBUG (*GetNode ()->GetObject<ndn::Pit> ()); |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 83 | NS_TEST_ASSERT_MSG_EQ (pit->GetSize (), 0, "There should 0 entries in PIT"); |
| 84 | } |
| 85 | |
| 86 | void |
Alexander Afanasyev | cf6dc92 | 2012-08-10 16:55:27 -0700 | [diff] [blame] | 87 | PitTest::Check1 (Ptr<ndn::Pit> pit) |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 88 | { |
| 89 | NS_TEST_ASSERT_MSG_EQ (pit->GetSize (), 1, "There should 1 entry in PIT"); |
| 90 | } |
| 91 | |
| 92 | void |
Alexander Afanasyev | cf6dc92 | 2012-08-10 16:55:27 -0700 | [diff] [blame] | 93 | PitTest::Check2 (Ptr<ndn::Pit> pit) |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 94 | { |
Alexander Afanasyev | cf6dc92 | 2012-08-10 16:55:27 -0700 | [diff] [blame] | 95 | // NS_LOG_DEBUG (*GetNode ()->GetObject<ndn::Pit> ()); |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 96 | NS_TEST_ASSERT_MSG_EQ (pit->GetSize (), 2, "There should 2 entries in PIT"); |
| 97 | } |
| 98 | |
| 99 | void |
Alexander Afanasyev | cf6dc92 | 2012-08-10 16:55:27 -0700 | [diff] [blame] | 100 | PitTest::Check3 (Ptr<ndn::Pit> pit) |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 101 | { |
Alexander Afanasyev | cf6dc92 | 2012-08-10 16:55:27 -0700 | [diff] [blame] | 102 | // NS_LOG_DEBUG (*GetNode ()->GetObject<ndn::Pit> ()); |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 103 | NS_TEST_ASSERT_MSG_EQ (pit->GetSize (), 3, "There should 3 entries in PIT"); |
| 104 | } |
| 105 | |
| 106 | |
| 107 | void |
| 108 | PitTest::DoRun () |
| 109 | { |
| 110 | Ptr<Node> node = CreateObject<Node> (); |
Alexander Afanasyev | 31cb469 | 2012-08-17 13:08:20 -0700 | [diff] [blame] | 111 | Ptr<Node> nodeSink = CreateObject<Node> (); |
| 112 | PointToPointHelper p2p; |
| 113 | p2p.Install (node, nodeSink); |
| 114 | |
Alexander Afanasyev | cf6dc92 | 2012-08-10 16:55:27 -0700 | [diff] [blame] | 115 | ndn::StackHelper ndn; |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 116 | ndn.Install (node); |
Alexander Afanasyev | 31cb469 | 2012-08-17 13:08:20 -0700 | [diff] [blame] | 117 | ndn.Install (nodeSink); |
| 118 | |
| 119 | ndn::StackHelper::AddRoute (node, "/", 0, 0); |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 120 | |
Alexander Afanasyev | ee76255 | 2013-07-10 22:29:22 -0700 | [diff] [blame] | 121 | Ptr<Application> app1 = CreateObject<PitTestClient> (); |
| 122 | app1->SetStartTime (Seconds (0.0)); |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 123 | node->AddApplication (app1); |
| 124 | |
Alexander Afanasyev | cf6dc92 | 2012-08-10 16:55:27 -0700 | [diff] [blame] | 125 | Simulator::Schedule (Seconds (0.0001), &PitTest::Test, this, node->GetObject<ndn::Fib> ()); |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 126 | |
Alexander Afanasyev | cf6dc92 | 2012-08-10 16:55:27 -0700 | [diff] [blame] | 127 | Simulator::Schedule (Seconds (0.01), &PitTest::Check0, this, node->GetObject<ndn::Pit> ()); |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 128 | |
Alexander Afanasyev | cf6dc92 | 2012-08-10 16:55:27 -0700 | [diff] [blame] | 129 | Simulator::Schedule (Seconds (0.11), &PitTest::Check1, this, node->GetObject<ndn::Pit> ()); |
| 130 | Simulator::Schedule (Seconds (0.21), &PitTest::Check2, this, node->GetObject<ndn::Pit> ()); |
| 131 | Simulator::Schedule (Seconds (0.31), &PitTest::Check3, this, node->GetObject<ndn::Pit> ()); |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 132 | |
Alexander Afanasyev | cf6dc92 | 2012-08-10 16:55:27 -0700 | [diff] [blame] | 133 | Simulator::Schedule (Seconds (0.61), &PitTest::Check3, this, node->GetObject<ndn::Pit> ()); |
| 134 | Simulator::Schedule (Seconds (0.71), &PitTest::Check2, this, node->GetObject<ndn::Pit> ()); |
| 135 | Simulator::Schedule (Seconds (0.81), &PitTest::Check1, this, node->GetObject<ndn::Pit> ()); |
| 136 | |
| 137 | Simulator::Schedule (Seconds (0.91), &PitTest::Check0, this, node->GetObject<ndn::Pit> ()); |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 138 | |
Alexander Afanasyev | ee76255 | 2013-07-10 22:29:22 -0700 | [diff] [blame] | 139 | Simulator::Stop (Seconds (10.0)); |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 140 | Simulator::Run (); |
| 141 | Simulator::Destroy (); |
| 142 | } |
| 143 | |
| 144 | } |