blob: 8eda994ff52d474cc4c9856f735ead6e0e458971 [file] [log] [blame]
Alexander Afanasyeve6dc0ac2013-02-21 14:00:48 -08001/* -*- 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-fib-entry.h"
22#include "ns3/core-module.h"
23#include "ns3/ndnSIM-module.h"
24#include "ns3/point-to-point-module.h"
25#include "ns3/node-list.h"
26
27#include <boost/lexical_cast.hpp>
28#include <boost/foreach.hpp>
29#include <boost/shared_ptr.hpp>
30#include <boost/make_shared.hpp>
31
32#include "ns3/ndn-fib-entry.h"
33
34NS_LOG_COMPONENT_DEFINE ("ndn.FibEntryTest");
35
36namespace ns3
37{
38
39class Client : public ndn::App
40{
41protected:
42 void
43 StartApplication ()
44 {
45 ndn::App::StartApplication ();
46
47 // add default route
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070048 Ptr<ndn::fib::Entry> fibEntry = GetNode ()->GetObject<ndn::Fib> ()->Add (ndn::Name ("/"), m_face, 0);
Alexander Afanasyeve6dc0ac2013-02-21 14:00:48 -080049 fibEntry->UpdateStatus (m_face, ndn::fib::FaceMetric::NDN_FIB_GREEN);
50
51 Simulator::Schedule (Seconds (0.5), &Client::SendPacket, this, std::string("/1"), 1);
52 Simulator::Schedule (Seconds (4.0), &Client::SendPacket, this, std::string("/2"), 1);
53 }
54
55 void
56 StopApplication ()
57 {
58 ndn::App::StopApplication ();
59 }
60
61private:
62 void
63 SendPacket (const std::string &prefix, uint32_t nonce)
64 {
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -070065 Ptr<ndn::Interest> interest;
66 interest->SetName (Create<ndn::Name> (prefix));
67 interest->SetNonce (nonce);
68 interest->SetInterestLifetime (Seconds (0.5));
Alexander Afanasyeve6dc0ac2013-02-21 14:00:48 -080069
Alexander Afanasyevfaa01f92013-07-10 18:34:31 -070070 m_face->ReceiveInterest (interest);
Alexander Afanasyeve6dc0ac2013-02-21 14:00:48 -080071 }
72};
73
74struct StatusRecorder
75{
76 StatusRecorder (Ptr<Node> node, Ptr<ndn::fib::Entry> fibEntry, Ptr<ndn::Face> face)
77 : m_node (node)
78 , m_fibEntry (fibEntry)
79 , m_face (face)
80 {
81 count = 0;
82 }
83
84 void
85 StatusChange (ndn::fib::FaceMetric::Status oldStatus, ndn::fib::FaceMetric::Status newStatus)
86 {
87 count ++;
88 // std::cout << Simulator::Now ().ToDouble (Time::S) << "s\tnode " << m_node->GetId () << " has changed fibEntry " << m_fibEntry->GetPrefix () << " face " << *m_face << " to " << newStatus << " from " << oldStatus << std::endl;
89 }
90
91 int count;
92
93private:
94 Ptr<Node> m_node;
95 Ptr<ndn::fib::Entry> m_fibEntry;
96 Ptr<ndn::Face> m_face;
97};
98
99void
100FibEntryTest::DoRun ()
101{
102 Ptr<Node> node = CreateObject<Node> ();
103 Ptr<Node> nodeSink = CreateObject<Node> ();
104 PointToPointHelper p2p;
105 p2p.Install (node, nodeSink);
106
107 ndn::StackHelper ndn;
108 ndn.SetForwardingStrategy ("ns3::ndn::fw::BestRoute");
109 ndn.Install (node);
110 ndn.Install (nodeSink);
111
112 ndn::StackHelper::AddRoute (node, "/", 0, 0);
113
114 Ptr<Client> app1 = CreateObject<Client> ();
115 node->AddApplication (app1);
116
117 ndn::AppHelper sinkHelper ("ns3::ndn::Producer");
118 sinkHelper.SetPrefix ("/");
119 sinkHelper.Install (nodeSink)
120 .Stop (Seconds (2.0));
121
122 std::list< boost::shared_ptr<StatusRecorder> > recorders;
123
124 for (NodeList::Iterator anode = NodeList::Begin ();
125 anode != NodeList::End ();
126 anode ++)
127 {
128 Ptr<ndn::Fib> fib = (*anode)->GetObject<ndn::Fib> ();
129
130 for (Ptr<ndn::fib::Entry> entry = fib->Begin ();
131 entry != fib->End ();
132 entry = fib->Next (entry))
133 {
134 BOOST_FOREACH (const ndn::fib::FaceMetric & faceMetric, entry->m_faces)
135 {
136 boost::shared_ptr<StatusRecorder> recorder = boost::make_shared<StatusRecorder> (*anode, entry, faceMetric.GetFace ());
137 recorders.push_back (recorder);
138
139 const_cast<ndn::fib::FaceMetric &> (faceMetric).GetStatusTrace ().ConnectWithoutContext (MakeCallback (&StatusRecorder::StatusChange, recorder.get ()));
140 }
141 }
142 }
143
144 Simulator::Stop (Seconds (10.0));
145 Simulator::Run ();
146 Simulator::Destroy ();
147
148 NS_TEST_ASSERT_MSG_EQ (recorders.size (), 1, "only one recorder should be: only one real FIB record should have existed");
149 NS_TEST_ASSERT_MSG_EQ (recorders.front ()->count, 2, "two events should have been reported");
150}
151
152}