Alexander Afanasyev | 4d32516 | 2012-06-01 12:28:50 -0700 | [diff] [blame] | 1 | Examples |
| 2 | ======== |
| 3 | |
| 4 | Simple scenario |
| 5 | --------------- |
| 6 | |
Alexander Afanasyev | b8d14ad | 2012-08-09 13:19:37 -0700 | [diff] [blame] | 7 | The first example (``ndn-simple.cc``) shows very basics of ndnSIM. In the simulated |
Alexander Afanasyev | 4d32516 | 2012-06-01 12:28:50 -0700 | [diff] [blame] | 8 | topology there are 3 nodes, connected with point-to-point links, one |
Alexander Afanasyev | 07b0063 | 2012-06-01 23:46:47 -0700 | [diff] [blame] | 9 | NDN consumer, and one NDN producer: |
Alexander Afanasyev | 4d32516 | 2012-06-01 12:28:50 -0700 | [diff] [blame] | 10 | |
Alexander Afanasyev | 07b0063 | 2012-06-01 23:46:47 -0700 | [diff] [blame] | 11 | .. aafig:: |
| 12 | :aspect: 60 |
| 13 | :scale: 120 |
Alexander Afanasyev | 4d32516 | 2012-06-01 12:28:50 -0700 | [diff] [blame] | 14 | |
Alexander Afanasyev | 07b0063 | 2012-06-01 23:46:47 -0700 | [diff] [blame] | 15 | +----------+ +--------+ +----------+ |
| 16 | | | 1Mbps | | 1Mbps | | |
| 17 | | Consumer |<-------------->| Router |<-------------->| Producer | |
| 18 | | | 10ms | | 10ms | | |
| 19 | +----------+ +--------+ +----------+ |
Alexander Afanasyev | 4d32516 | 2012-06-01 12:28:50 -0700 | [diff] [blame] | 20 | |
Alexander Afanasyev | f6807a5 | 2012-08-10 18:11:43 -0700 | [diff] [blame] | 21 | Consumer is simulated using :ndnsim:`ConsumerCbr` reference application and generates Interests towards the producer |
Alexander Afanasyev | 07b0063 | 2012-06-01 23:46:47 -0700 | [diff] [blame] | 22 | with frequency of 10 Interests per second (see :doc:`applications`). |
| 23 | |
Alexander Afanasyev | f6807a5 | 2012-08-10 18:11:43 -0700 | [diff] [blame] | 24 | Producer is simulated using :ndnsim:`Producer` class, which is used to satisfy all incoming Interests with virtual payload data (1024 bytes). |
Alexander Afanasyev | 07b0063 | 2012-06-01 23:46:47 -0700 | [diff] [blame] | 25 | |
| 26 | FIB on every node is populated using default routes (see :doc:`helpers`). |
Alexander Afanasyev | 4d32516 | 2012-06-01 12:28:50 -0700 | [diff] [blame] | 27 | |
| 28 | The following code represents all that is necessary to run such a |
| 29 | simple scenario |
| 30 | |
| 31 | .. code-block:: c++ |
| 32 | |
| 33 | #include "ns3/core-module.h" |
| 34 | #include "ns3/network-module.h" |
| 35 | #include "ns3/point-to-point-module.h" |
| 36 | #include "ns3/ndnSIM-module.h" |
Alexander Afanasyev | 07b0063 | 2012-06-01 23:46:47 -0700 | [diff] [blame] | 37 | |
Alexander Afanasyev | 4d32516 | 2012-06-01 12:28:50 -0700 | [diff] [blame] | 38 | using namespace ns3; |
| 39 | |
Alexander Afanasyev | 07b0063 | 2012-06-01 23:46:47 -0700 | [diff] [blame] | 40 | int |
Alexander Afanasyev | 4d32516 | 2012-06-01 12:28:50 -0700 | [diff] [blame] | 41 | main (int argc, char *argv[]) |
| 42 | { |
| 43 | // setting default parameters for PointToPoint links and channels |
| 44 | Config::SetDefault ("ns3::PointToPointNetDevice::DataRate", StringValue ("1Mbps")); |
| 45 | Config::SetDefault ("ns3::PointToPointChannel::Delay", StringValue ("10ms")); |
| 46 | Config::SetDefault ("ns3::DropTailQueue::MaxPackets", StringValue ("20")); |
Alexander Afanasyev | 07b0063 | 2012-06-01 23:46:47 -0700 | [diff] [blame] | 47 | |
Alexander Afanasyev | 4d32516 | 2012-06-01 12:28:50 -0700 | [diff] [blame] | 48 | // Creating nodes |
| 49 | NodeContainer nodes; |
| 50 | nodes.Create (3); |
Alexander Afanasyev | 07b0063 | 2012-06-01 23:46:47 -0700 | [diff] [blame] | 51 | |
Alexander Afanasyev | 4d32516 | 2012-06-01 12:28:50 -0700 | [diff] [blame] | 52 | // Connecting nodes using two links |
| 53 | PointToPointHelper p2p; |
| 54 | p2p.Install (nodes.Get (0), nodes.Get (1)); |
| 55 | p2p.Install (nodes.Get (1), nodes.Get (2)); |
Alexander Afanasyev | 07b0063 | 2012-06-01 23:46:47 -0700 | [diff] [blame] | 56 | |
Alexander Afanasyev | 4d32516 | 2012-06-01 12:28:50 -0700 | [diff] [blame] | 57 | // Install CCNx stack on all nodes |
Alexander Afanasyev | f6807a5 | 2012-08-10 18:11:43 -0700 | [diff] [blame] | 58 | ndn::StackHelper ccnxHelper; |
Alexander Afanasyev | 4d32516 | 2012-06-01 12:28:50 -0700 | [diff] [blame] | 59 | ccnxHelper.SetDefaultRoutes (true); |
| 60 | ccnxHelper.InstallAll (); |
| 61 | |
Alexander Afanasyev | 07b0063 | 2012-06-01 23:46:47 -0700 | [diff] [blame] | 62 | // Installing applications |
Alexander Afanasyev | 4d32516 | 2012-06-01 12:28:50 -0700 | [diff] [blame] | 63 | |
| 64 | // Consumer |
Alexander Afanasyev | f6807a5 | 2012-08-10 18:11:43 -0700 | [diff] [blame] | 65 | ndn::AppHelper consumerHelper ("ns3::ndn::ConsumerCbr"); |
Alexander Afanasyev | 4d32516 | 2012-06-01 12:28:50 -0700 | [diff] [blame] | 66 | // Consumer will request /prefix/0, /prefix/1, ... |
| 67 | consumerHelper.SetPrefix ("/prefix"); |
| 68 | consumerHelper.SetAttribute ("Frequency", StringValue ("10")); // 10 interests a second |
| 69 | consumerHelper.Install (nodes.Get (0)); // first node |
Alexander Afanasyev | 07b0063 | 2012-06-01 23:46:47 -0700 | [diff] [blame] | 70 | |
Alexander Afanasyev | 4d32516 | 2012-06-01 12:28:50 -0700 | [diff] [blame] | 71 | // Producer |
Alexander Afanasyev | f6807a5 | 2012-08-10 18:11:43 -0700 | [diff] [blame] | 72 | ndn::AppHelper producerHelper ("ns3::ndn::Producer"); |
Alexander Afanasyev | 4d32516 | 2012-06-01 12:28:50 -0700 | [diff] [blame] | 73 | // Producer will reply to all requests starting with /prefix |
| 74 | producerHelper.SetPrefix ("/prefix"); |
| 75 | producerHelper.SetAttribute ("PayloadSize", StringValue("1024")); |
| 76 | producerHelper.Install (nodes.Get (2)); // last node |
Alexander Afanasyev | 07b0063 | 2012-06-01 23:46:47 -0700 | [diff] [blame] | 77 | |
| 78 | Simulator::Stop (Seconds (20.0)); |
| 79 | |
| 80 | Simulator::Run (); |
| 81 | Simulator::Destroy (); |
| 82 | |
| 83 | return 0; |
| 84 | } |
| 85 | |
Alexander Afanasyev | b8d14ad | 2012-08-09 13:19:37 -0700 | [diff] [blame] | 86 | If this code is placed into ``scratch/ndn-simple.cc`` and NS-3 is compiled in debug mode, you can run and see progress of the |
Alexander Afanasyev | 07b0063 | 2012-06-01 23:46:47 -0700 | [diff] [blame] | 87 | simulation using the following command (in optimized mode nothing will be printed out):: |
| 88 | |
Alexander Afanasyev | f6807a5 | 2012-08-10 18:11:43 -0700 | [diff] [blame] | 89 | NS_LOG=ndn.Consumer:ndn.Producer ./waf --run=ndn-simple |
Alexander Afanasyev | 07b0063 | 2012-06-01 23:46:47 -0700 | [diff] [blame] | 90 | |
| 91 | |
| 92 | 9-node grid example |
| 93 | ------------------- |
| 94 | |
Alexander Afanasyev | b8d14ad | 2012-08-09 13:19:37 -0700 | [diff] [blame] | 95 | This scenario (``ndn-grid.cc``) simulates using a grid topology build with PointToPointGrid NS-3 module |
Alexander Afanasyev | 07b0063 | 2012-06-01 23:46:47 -0700 | [diff] [blame] | 96 | |
| 97 | .. aafig:: |
| 98 | :aspect: 60 |
| 99 | :scale: 120 |
| 100 | |
| 101 | /--------\ /-\ /-\ |
| 102 | |Consumer|<---->| |<------->| | |
| 103 | \--------/ \-/ \-/ |
| 104 | ^ ^ ^ |
| 105 | | | | 1Mbps/10ms delay |
| 106 | v v v |
| 107 | /-\ /-\ /-\ |
| 108 | | |<-------->| |<------->| | |
| 109 | \-/ \-/ \-/ |
| 110 | ^ ^ ^ |
| 111 | | | | |
| 112 | v v v |
| 113 | /-\ /-\ /--------\ |
| 114 | | |<-------->| |<---->|Producer| |
| 115 | \-/ \-/ \--------/ |
| 116 | |
| 117 | |
Alexander Afanasyev | f6807a5 | 2012-08-10 18:11:43 -0700 | [diff] [blame] | 118 | FIB is populated using :ndnsim:`GlobalRoutingHelper` (see :doc:`helpers`). |
Alexander Afanasyev | 07b0063 | 2012-06-01 23:46:47 -0700 | [diff] [blame] | 119 | |
Alexander Afanasyev | f6807a5 | 2012-08-10 18:11:43 -0700 | [diff] [blame] | 120 | Consumer is simulated using :ndnsim:`ConsumerCbr` reference application and generates Interests towards the producer |
Alexander Afanasyev | 07b0063 | 2012-06-01 23:46:47 -0700 | [diff] [blame] | 121 | with frequency of 10 Interests per second (see :doc:`applications`). |
| 122 | |
Alexander Afanasyev | f6807a5 | 2012-08-10 18:11:43 -0700 | [diff] [blame] | 123 | Producer is simulated using :ndnsim:`Producer` class, which is used to satisfy all incoming Interests with virtual payload data (1024 bytes). |
Alexander Afanasyev | 07b0063 | 2012-06-01 23:46:47 -0700 | [diff] [blame] | 124 | |
| 125 | |
| 126 | The following code represents all that is necessary to run such a |
| 127 | simple scenario |
| 128 | |
| 129 | .. code-block:: c++ |
| 130 | |
| 131 | #include "ns3/core-module.h" |
| 132 | #include "ns3/network-module.h" |
| 133 | #include "ns3/point-to-point-module.h" |
| 134 | #include "ns3/point-to-point-grid.h" |
| 135 | #include "ns3/ndnSIM-module.h" |
| 136 | |
| 137 | using namespace ns3; |
| 138 | |
| 139 | int |
| 140 | main (int argc, char *argv[]) |
| 141 | { |
| 142 | // Setting default parameters for PointToPoint links and channels |
| 143 | Config::SetDefault ("ns3::PointToPointNetDevice::DataRate", StringValue ("1Mbps")); |
| 144 | Config::SetDefault ("ns3::PointToPointChannel::Delay", StringValue ("10ms")); |
| 145 | Config::SetDefault ("ns3::DropTailQueue::MaxPackets", StringValue ("20")); |
| 146 | |
| 147 | // Creating 3x3 topology |
| 148 | PointToPointHelper p2p; |
| 149 | PointToPointGridHelper grid (3, 3, p2p); |
| 150 | grid.BoundingBox(100,100,200,200); |
| 151 | |
| 152 | // Install CCNx stack on all nodes |
Alexander Afanasyev | f6807a5 | 2012-08-10 18:11:43 -0700 | [diff] [blame] | 153 | ndn::StackHelper ccnxHelper; |
Alexander Afanasyev | 07b0063 | 2012-06-01 23:46:47 -0700 | [diff] [blame] | 154 | ccnxHelper.InstallAll (); |
| 155 | |
| 156 | // Installing global routing interface on all nodes |
Alexander Afanasyev | f6807a5 | 2012-08-10 18:11:43 -0700 | [diff] [blame] | 157 | ndn::GlobalRoutingHelper ccnxGlobalRoutingHelper; |
Alexander Afanasyev | 07b0063 | 2012-06-01 23:46:47 -0700 | [diff] [blame] | 158 | ccnxGlobalRoutingHelper.InstallAll (); |
| 159 | |
| 160 | // Getting containers for the consumer/producer |
| 161 | Ptr<Node> producer = grid.GetNode (nGrid-1, nGrid-1); |
| 162 | NodeContainer consumerNodes; |
| 163 | consumerNodes.Add (grid.GetNode (0,0)); |
| 164 | |
| 165 | // Install CCNx applications |
| 166 | std::string prefix = "/prefix"; |
| 167 | |
Alexander Afanasyev | f6807a5 | 2012-08-10 18:11:43 -0700 | [diff] [blame] | 168 | ndn::AppHelper consumerHelper ("ns3::ndn::ConsumerCbr"); |
Alexander Afanasyev | 07b0063 | 2012-06-01 23:46:47 -0700 | [diff] [blame] | 169 | consumerHelper.SetPrefix (prefix); |
| 170 | consumerHelper.SetAttribute ("Frequency", StringValue ("10")); // 10 interests a second |
| 171 | consumerHelper.Install (consumerNodes); |
| 172 | |
Alexander Afanasyev | f6807a5 | 2012-08-10 18:11:43 -0700 | [diff] [blame] | 173 | ndn::AppHelper producerHelper ("ns3::ndn::Producer"); |
Alexander Afanasyev | 07b0063 | 2012-06-01 23:46:47 -0700 | [diff] [blame] | 174 | producerHelper.SetPrefix (prefix); |
| 175 | producerHelper.SetAttribute ("PayloadSize", StringValue("1024")); |
| 176 | producerHelper.Install (producer); |
| 177 | |
Alexander Afanasyev | f6807a5 | 2012-08-10 18:11:43 -0700 | [diff] [blame] | 178 | // Add /prefix origins to ndn::GlobalRouter |
Alexander Afanasyev | 07b0063 | 2012-06-01 23:46:47 -0700 | [diff] [blame] | 179 | ccnxGlobalRoutingHelper.AddOrigins (prefix, producer); |
| 180 | |
| 181 | // Calculate and install FIBs |
| 182 | ccnxGlobalRoutingHelper.CalculateRoutes (); |
Alexander Afanasyev | 4d32516 | 2012-06-01 12:28:50 -0700 | [diff] [blame] | 183 | |
| 184 | Simulator::Stop (Seconds (20.0)); |
| 185 | |
| 186 | Simulator::Run (); |
| 187 | Simulator::Destroy (); |
| 188 | |
| 189 | return 0; |
| 190 | } |
| 191 | |
Alexander Afanasyev | 4d32516 | 2012-06-01 12:28:50 -0700 | [diff] [blame] | 192 | |
Alexander Afanasyev | b8d14ad | 2012-08-09 13:19:37 -0700 | [diff] [blame] | 193 | If this code is placed into ``scratch/ndn-grid.cc`` and NS-3 is compiled in debug mode, you can run and see progress of the |
Alexander Afanasyev | 07b0063 | 2012-06-01 23:46:47 -0700 | [diff] [blame] | 194 | simulation using the following command (in optimized mode nothing will be printed out):: |
| 195 | |
Alexander Afanasyev | f6807a5 | 2012-08-10 18:11:43 -0700 | [diff] [blame] | 196 | NS_LOG=ndn.Consumer:ndn.Producer ./waf --run=ndn-grid |
Alexander Afanasyev | 07b0063 | 2012-06-01 23:46:47 -0700 | [diff] [blame] | 197 | |