blob: 5ea290e170c47b5b6ede037d4bc373392f8b8a47 [file] [log] [blame]
Alexander Afanasyev4d325162012-06-01 12:28:50 -07001Examples
2========
3
4Simple scenario
5---------------
6
Alexander Afanasyevb8d14ad2012-08-09 13:19:37 -07007The first example (``ndn-simple.cc``) shows very basics of ndnSIM. In the simulated
Alexander Afanasyev4d325162012-06-01 12:28:50 -07008topology there are 3 nodes, connected with point-to-point links, one
Alexander Afanasyev07b00632012-06-01 23:46:47 -07009NDN consumer, and one NDN producer:
Alexander Afanasyev4d325162012-06-01 12:28:50 -070010
Alexander Afanasyev07b00632012-06-01 23:46:47 -070011.. aafig::
12 :aspect: 60
13 :scale: 120
Alexander Afanasyev4d325162012-06-01 12:28:50 -070014
Alexander Afanasyev07b00632012-06-01 23:46:47 -070015 +----------+ +--------+ +----------+
16 | | 1Mbps | | 1Mbps | |
17 | Consumer |<-------------->| Router |<-------------->| Producer |
18 | | 10ms | | 10ms | |
19 +----------+ +--------+ +----------+
Alexander Afanasyev4d325162012-06-01 12:28:50 -070020
Alexander Afanasyevf6807a52012-08-10 18:11:43 -070021Consumer is simulated using :ndnsim:`ConsumerCbr` reference application and generates Interests towards the producer
Alexander Afanasyev07b00632012-06-01 23:46:47 -070022with frequency of 10 Interests per second (see :doc:`applications`).
23
Alexander Afanasyevf6807a52012-08-10 18:11:43 -070024Producer is simulated using :ndnsim:`Producer` class, which is used to satisfy all incoming Interests with virtual payload data (1024 bytes).
Alexander Afanasyev07b00632012-06-01 23:46:47 -070025
26FIB on every node is populated using default routes (see :doc:`helpers`).
Alexander Afanasyev4d325162012-06-01 12:28:50 -070027
28The following code represents all that is necessary to run such a
29simple 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 Afanasyev07b00632012-06-01 23:46:47 -070037
Alexander Afanasyev4d325162012-06-01 12:28:50 -070038 using namespace ns3;
39
Alexander Afanasyev07b00632012-06-01 23:46:47 -070040 int
Alexander Afanasyev4d325162012-06-01 12:28:50 -070041 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 Afanasyev07b00632012-06-01 23:46:47 -070047
Alexander Afanasyev4d325162012-06-01 12:28:50 -070048 // Creating nodes
49 NodeContainer nodes;
50 nodes.Create (3);
Alexander Afanasyev07b00632012-06-01 23:46:47 -070051
Alexander Afanasyev4d325162012-06-01 12:28:50 -070052 // 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 Afanasyev07b00632012-06-01 23:46:47 -070056
Alexander Afanasyev4d325162012-06-01 12:28:50 -070057 // Install CCNx stack on all nodes
Alexander Afanasyevf6807a52012-08-10 18:11:43 -070058 ndn::StackHelper ccnxHelper;
Alexander Afanasyev4d325162012-06-01 12:28:50 -070059 ccnxHelper.SetDefaultRoutes (true);
60 ccnxHelper.InstallAll ();
61
Alexander Afanasyev07b00632012-06-01 23:46:47 -070062 // Installing applications
Alexander Afanasyev4d325162012-06-01 12:28:50 -070063
64 // Consumer
Alexander Afanasyevf6807a52012-08-10 18:11:43 -070065 ndn::AppHelper consumerHelper ("ns3::ndn::ConsumerCbr");
Alexander Afanasyev4d325162012-06-01 12:28:50 -070066 // 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 Afanasyev07b00632012-06-01 23:46:47 -070070
Alexander Afanasyev4d325162012-06-01 12:28:50 -070071 // Producer
Alexander Afanasyevf6807a52012-08-10 18:11:43 -070072 ndn::AppHelper producerHelper ("ns3::ndn::Producer");
Alexander Afanasyev4d325162012-06-01 12:28:50 -070073 // 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 Afanasyev07b00632012-06-01 23:46:47 -070077
78 Simulator::Stop (Seconds (20.0));
79
80 Simulator::Run ();
81 Simulator::Destroy ();
82
83 return 0;
84 }
85
Alexander Afanasyevb8d14ad2012-08-09 13:19:37 -070086If 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 Afanasyev07b00632012-06-01 23:46:47 -070087simulation using the following command (in optimized mode nothing will be printed out)::
88
Alexander Afanasyevf6807a52012-08-10 18:11:43 -070089 NS_LOG=ndn.Consumer:ndn.Producer ./waf --run=ndn-simple
Alexander Afanasyev07b00632012-06-01 23:46:47 -070090
91
929-node grid example
93-------------------
94
Alexander Afanasyevb8d14ad2012-08-09 13:19:37 -070095This scenario (``ndn-grid.cc``) simulates using a grid topology build with PointToPointGrid NS-3 module
Alexander Afanasyev07b00632012-06-01 23:46:47 -070096
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 Afanasyevf6807a52012-08-10 18:11:43 -0700118FIB is populated using :ndnsim:`GlobalRoutingHelper` (see :doc:`helpers`).
Alexander Afanasyev07b00632012-06-01 23:46:47 -0700119
Alexander Afanasyevf6807a52012-08-10 18:11:43 -0700120Consumer is simulated using :ndnsim:`ConsumerCbr` reference application and generates Interests towards the producer
Alexander Afanasyev07b00632012-06-01 23:46:47 -0700121with frequency of 10 Interests per second (see :doc:`applications`).
122
Alexander Afanasyevf6807a52012-08-10 18:11:43 -0700123Producer is simulated using :ndnsim:`Producer` class, which is used to satisfy all incoming Interests with virtual payload data (1024 bytes).
Alexander Afanasyev07b00632012-06-01 23:46:47 -0700124
125
126The following code represents all that is necessary to run such a
127simple 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 Afanasyevf6807a52012-08-10 18:11:43 -0700153 ndn::StackHelper ccnxHelper;
Alexander Afanasyev07b00632012-06-01 23:46:47 -0700154 ccnxHelper.InstallAll ();
155
156 // Installing global routing interface on all nodes
Alexander Afanasyevf6807a52012-08-10 18:11:43 -0700157 ndn::GlobalRoutingHelper ccnxGlobalRoutingHelper;
Alexander Afanasyev07b00632012-06-01 23:46:47 -0700158 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 Afanasyevf6807a52012-08-10 18:11:43 -0700168 ndn::AppHelper consumerHelper ("ns3::ndn::ConsumerCbr");
Alexander Afanasyev07b00632012-06-01 23:46:47 -0700169 consumerHelper.SetPrefix (prefix);
170 consumerHelper.SetAttribute ("Frequency", StringValue ("10")); // 10 interests a second
171 consumerHelper.Install (consumerNodes);
172
Alexander Afanasyevf6807a52012-08-10 18:11:43 -0700173 ndn::AppHelper producerHelper ("ns3::ndn::Producer");
Alexander Afanasyev07b00632012-06-01 23:46:47 -0700174 producerHelper.SetPrefix (prefix);
175 producerHelper.SetAttribute ("PayloadSize", StringValue("1024"));
176 producerHelper.Install (producer);
177
Alexander Afanasyevf6807a52012-08-10 18:11:43 -0700178 // Add /prefix origins to ndn::GlobalRouter
Alexander Afanasyev07b00632012-06-01 23:46:47 -0700179 ccnxGlobalRoutingHelper.AddOrigins (prefix, producer);
180
181 // Calculate and install FIBs
182 ccnxGlobalRoutingHelper.CalculateRoutes ();
Alexander Afanasyev4d325162012-06-01 12:28:50 -0700183
184 Simulator::Stop (Seconds (20.0));
185
186 Simulator::Run ();
187 Simulator::Destroy ();
188
189 return 0;
190 }
191
Alexander Afanasyev4d325162012-06-01 12:28:50 -0700192
Alexander Afanasyevb8d14ad2012-08-09 13:19:37 -0700193If 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 Afanasyev07b00632012-06-01 23:46:47 -0700194simulation using the following command (in optimized mode nothing will be printed out)::
195
Alexander Afanasyevf6807a52012-08-10 18:11:43 -0700196 NS_LOG=ndn.Consumer:ndn.Producer ./waf --run=ndn-grid
Alexander Afanasyev07b00632012-06-01 23:46:47 -0700197