Alexander Afanasyev | a4e7428 | 2013-07-11 15:23:20 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2013, Regents of the University of California |
| 4 | * Alexander Afanasyev |
| 5 | * |
| 6 | * GNU v3.0 license, See the LICENSE file for more information |
| 7 | * |
| 8 | * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 9 | */ |
| 10 | |
| 11 | #include "ndnSIM-api.h" |
| 12 | #include "ns3/core-module.h" |
| 13 | #include "ns3/ndnSIM-module.h" |
| 14 | #include "ns3/point-to-point-module.h" |
| 15 | |
| 16 | #include <boost/lexical_cast.hpp> |
| 17 | |
| 18 | NS_LOG_COMPONENT_DEFINE ("ndn.ApiTest"); |
| 19 | |
| 20 | namespace ns3 |
| 21 | { |
| 22 | |
| 23 | class ApiTestClient : public Application |
| 24 | { |
| 25 | public: |
| 26 | static TypeId |
| 27 | GetTypeId () |
| 28 | { |
| 29 | static TypeId tid = TypeId ("ns3::ndn::test::ApiTestClient") |
| 30 | .SetParent<Application> () |
| 31 | .AddConstructor<ApiTestClient> () |
| 32 | ; |
| 33 | |
| 34 | return tid; |
| 35 | } |
| 36 | |
| 37 | ApiTestClient () |
| 38 | : datas (0) |
| 39 | , timeouts (0) |
| 40 | { |
| 41 | } |
| 42 | |
| 43 | protected: |
| 44 | void |
| 45 | StartApplication () |
| 46 | { |
| 47 | m_face = Create<ndn::ApiFace> (GetNode ()); |
| 48 | |
| 49 | Simulator::Schedule (Seconds (0.1), &ApiTestClient::SendPacket, this, std::string ("/1")); |
| 50 | Simulator::Schedule (Seconds (5.0), &ApiTestClient::SendPacket, this, std::string ("/2")); |
| 51 | } |
| 52 | |
| 53 | void |
| 54 | StopApplication () |
| 55 | { |
| 56 | m_face->Shutdown (); |
| 57 | m_face = 0; |
| 58 | } |
| 59 | |
| 60 | private: |
| 61 | void |
| 62 | GotData (Ptr<const ndn::Interest>, Ptr<const ndn::ContentObject>) |
| 63 | { |
| 64 | datas++; |
| 65 | } |
| 66 | |
| 67 | void |
| 68 | GotTimeout (Ptr<const ndn::Interest>) |
| 69 | { |
| 70 | timeouts++; |
| 71 | } |
| 72 | |
| 73 | void |
| 74 | SendPacket (const std::string &prefix) |
| 75 | { |
| 76 | Ptr<ndn::Interest> interest = Create<ndn::Interest> (); |
| 77 | interest->SetName (Create<ndn::Name> (prefix)); |
| 78 | interest->SetInterestLifetime (Seconds (0.5)); |
| 79 | |
| 80 | m_face->ExpressInterest (interest, |
| 81 | MakeCallback (&ApiTestClient::GotData, this), |
| 82 | MakeCallback (&ApiTestClient::GotTimeout, this)); |
| 83 | } |
| 84 | |
| 85 | public: |
| 86 | uint32_t datas; |
| 87 | uint32_t timeouts; |
| 88 | |
| 89 | private: |
| 90 | Ptr<ndn::ApiFace> m_face; |
| 91 | }; |
| 92 | |
| 93 | NS_OBJECT_ENSURE_REGISTERED (ApiTestClient); |
| 94 | |
| 95 | void |
| 96 | ApiTest::Check0 (Ptr<Application> app) |
| 97 | { |
| 98 | NS_TEST_ASSERT_MSG_EQ (DynamicCast<ApiTestClient> (app)->datas, 0, ""); |
| 99 | NS_TEST_ASSERT_MSG_EQ (DynamicCast<ApiTestClient> (app)->timeouts, 0, ""); |
| 100 | } |
| 101 | |
| 102 | void |
| 103 | ApiTest::Check1 (Ptr<Application> app) |
| 104 | { |
| 105 | NS_TEST_ASSERT_MSG_EQ (DynamicCast<ApiTestClient> (app)->datas, 1, ""); |
| 106 | NS_TEST_ASSERT_MSG_EQ (DynamicCast<ApiTestClient> (app)->timeouts, 0, ""); |
| 107 | } |
| 108 | |
| 109 | void |
| 110 | ApiTest::Check2 (Ptr<Application> app) |
| 111 | { |
| 112 | NS_TEST_ASSERT_MSG_EQ (DynamicCast<ApiTestClient> (app)->datas, 1, ""); |
| 113 | NS_TEST_ASSERT_MSG_EQ (DynamicCast<ApiTestClient> (app)->timeouts, 1, ""); |
| 114 | } |
| 115 | |
| 116 | |
| 117 | void |
| 118 | ApiTest::DoRun () |
| 119 | { |
| 120 | Config::SetDefault ("ns3::PointToPointNetDevice::DataRate", StringValue ("1Mbps")); |
| 121 | Config::SetDefault ("ns3::PointToPointChannel::Delay", StringValue ("10ms")); |
| 122 | Config::SetDefault ("ns3::DropTailQueue::MaxPackets", StringValue ("20")); |
| 123 | |
| 124 | // Creating nodes |
| 125 | NodeContainer nodes; |
| 126 | nodes.Create (3); |
| 127 | |
| 128 | // Connecting nodes using two links |
| 129 | PointToPointHelper p2p; |
| 130 | p2p.Install (nodes.Get (0), nodes.Get (1)); |
| 131 | p2p.Install (nodes.Get (1), nodes.Get (2)); |
| 132 | |
| 133 | // Install NDN stack on all nodes |
| 134 | ndn::StackHelper ndnHelper; |
| 135 | ndnHelper.SetDefaultRoutes (true); |
| 136 | ndnHelper.InstallAll (); |
| 137 | |
| 138 | // Installing applications |
| 139 | |
| 140 | // Consumer |
| 141 | ndn::AppHelper consumerHelper ("ns3::ndn::test::ApiTestClient"); |
| 142 | ApplicationContainer apps = consumerHelper.Install (nodes.Get (0)); // first node |
| 143 | |
| 144 | // Producer |
| 145 | ndn::AppHelper producerHelper ("ns3::ndn::Producer"); |
| 146 | // Producer will reply to all requests starting with /prefix |
| 147 | producerHelper.SetPrefix ("/"); |
| 148 | producerHelper.SetAttribute ("Postfix", StringValue ("/unique/postfix")); |
| 149 | producerHelper.SetAttribute ("PayloadSize", StringValue("1024")); |
| 150 | producerHelper.Install (nodes.Get (2)).Stop (Seconds (4.0)); // last node |
| 151 | |
| 152 | Simulator::Schedule (Seconds (0.0001), &ApiTest::Check0, this, apps.Get (0)); |
| 153 | Simulator::Schedule (Seconds (0.2000), &ApiTest::Check1, this, apps.Get (0)); |
| 154 | Simulator::Schedule (Seconds (5.6100), &ApiTest::Check2, this, apps.Get (0)); |
| 155 | |
| 156 | Simulator::Stop (Seconds (20.0)); |
| 157 | |
| 158 | Simulator::Run (); |
| 159 | Simulator::Destroy (); |
| 160 | } |
| 161 | |
| 162 | } |