Alexander Afanasyev | 6315ef7 | 2012-06-01 20:56:31 -0700 | [diff] [blame] | 1 | ndnSIM applications |
| 2 | =================== |
| 3 | |
| 4 | ndnSIM includes a few reference applications that can be used as a base for NDN simulations. |
| 5 | |
| 6 | Reference applications |
| 7 | ++++++++++++++++++++++ |
| 8 | |
Alexander Afanasyev | 5b054aa | 2012-08-13 18:56:17 -0700 | [diff] [blame] | 9 | ConsumerCbr |
Alexander Afanasyev | 6315ef7 | 2012-06-01 20:56:31 -0700 | [diff] [blame] | 10 | ^^^^^^^^^^^^^^^ |
| 11 | |
Alexander Afanasyev | 5b054aa | 2012-08-13 18:56:17 -0700 | [diff] [blame] | 12 | :ndnsim:`ConsumerCbr` an application that generates Interest traffic with predefined pattern (constant frequency, constant average rate with inter-Interest gap distributed uniformly at random, exponentially at random, etc.). |
Alexander Afanasyev | 6315ef7 | 2012-06-01 20:56:31 -0700 | [diff] [blame] | 13 | |
| 14 | .. code-block:: c++ |
| 15 | |
| 16 | // Create application using the app helper |
Alexander Afanasyev | 5b054aa | 2012-08-13 18:56:17 -0700 | [diff] [blame] | 17 | ndn::AppHelper helper ("ns3::ndn::ConsumerCbr"); |
Alexander Afanasyev | 6315ef7 | 2012-06-01 20:56:31 -0700 | [diff] [blame] | 18 | |
| 19 | This applications has the following attributes: |
| 20 | |
| 21 | * Frequency |
| 22 | |
| 23 | .. note:: |
| 24 | default: ``1.0`` (1 per second) |
| 25 | |
| 26 | Either exact (for contant version) or expected (for randomized version) frequency with which Interests are generated |
| 27 | |
| 28 | .. code-block:: c++ |
| 29 | |
| 30 | // Set attribute using the app helper |
| 31 | helper.SetAttribute ("Frequency", DoubleValue (1.0)); |
| 32 | |
| 33 | * Randomize |
| 34 | |
| 35 | .. note:: |
| 36 | default: ``"none"`` |
| 37 | |
| 38 | Specify whether to do randomization for inter-Interest gap or not. The following variants are currently supported: |
| 39 | |
| 40 | - ``"none"``: no randomization |
| 41 | |
| 42 | - ``"uniform"``: uniform distribution in range (0, 1/Frequency) |
| 43 | |
| 44 | - ``"exponential"``: exponential distribution with mean 1/Frequency |
| 45 | |
| 46 | .. code-block:: c++ |
| 47 | |
| 48 | // Set attribute using the app helper |
| 49 | helper.SetAttribute ("Randomize", StringValue ("uniform")); |
| 50 | |
Alexander Afanasyev | 5b054aa | 2012-08-13 18:56:17 -0700 | [diff] [blame] | 51 | ConsumerBatches |
Alexander Afanasyev | 6315ef7 | 2012-06-01 20:56:31 -0700 | [diff] [blame] | 52 | ^^^^^^^^^^^^^^^^^^^ |
| 53 | |
Alexander Afanasyev | 5b054aa | 2012-08-13 18:56:17 -0700 | [diff] [blame] | 54 | :ndnsim:`ConsumerBatches` is an on-off-style application gen- erating a specified number of Interests at specified points of simulation. |
Alexander Afanasyev | 6315ef7 | 2012-06-01 20:56:31 -0700 | [diff] [blame] | 55 | |
| 56 | .. code-block:: c++ |
| 57 | |
| 58 | // Create application using the app helper |
Alexander Afanasyev | 5b054aa | 2012-08-13 18:56:17 -0700 | [diff] [blame] | 59 | ndn::AppHelper consumerHelper ("ns3::ndn::ConsumerBatches"); |
Alexander Afanasyev | 6315ef7 | 2012-06-01 20:56:31 -0700 | [diff] [blame] | 60 | |
| 61 | This applications has the following attributes: |
| 62 | |
| 63 | * Batches |
| 64 | |
| 65 | .. note:: |
| 66 | default: Empty |
| 67 | |
| 68 | Specify exact pattern of Interest packets, specifying when and how many Interest packets should be sent. |
| 69 | The following example defines that 1 Interest should be requested at time 1s, 5 Interests at time 5s, and 2 Interests at time 10s.: |
| 70 | |
| 71 | .. code-block:: c++ |
| 72 | |
| 73 | // Set attribute using the app helper |
| 74 | helper.SetAttribute ("Batches", StringValue ("1s 1 2s 5 10s 2")); |
| 75 | |
| 76 | |
Alexander Afanasyev | 5b054aa | 2012-08-13 18:56:17 -0700 | [diff] [blame] | 77 | ConsumerWindow |
Alexander Afanasyev | 6315ef7 | 2012-06-01 20:56:31 -0700 | [diff] [blame] | 78 | ^^^^^^^^^^^^^^^^^^ |
| 79 | |
Alexander Afanasyev | 5b054aa | 2012-08-13 18:56:17 -0700 | [diff] [blame] | 80 | :ndnsim:`ConsumerWindow` is an application generating a variable rate Interest traffic. It relies on an optional NACK-Interest feature and implements a simple sliding-window-based Interest generation mechanism. |
Alexander Afanasyev | 6315ef7 | 2012-06-01 20:56:31 -0700 | [diff] [blame] | 81 | |
| 82 | .. code-block:: c++ |
| 83 | |
| 84 | // Create application using the app helper |
Alexander Afanasyev | 5b054aa | 2012-08-13 18:56:17 -0700 | [diff] [blame] | 85 | ndn::AppHelper consumerHelper ("ns3::ndn::ConsumerWindow"); |
Alexander Afanasyev | 6315ef7 | 2012-06-01 20:56:31 -0700 | [diff] [blame] | 86 | |
| 87 | |
| 88 | This applications has the following attributes: |
| 89 | |
| 90 | * Window |
| 91 | |
| 92 | .. note:: |
| 93 | default: ``1`` |
| 94 | |
| 95 | Initial number of Interests that will be send out without waiting for the data (number of outstanding Interests) |
| 96 | |
| 97 | * PayloadSize |
| 98 | |
| 99 | .. note:: |
| 100 | default: ``1040`` |
| 101 | |
| 102 | Expected size of the Data payload (necessary only when Size is specified) |
| 103 | |
| 104 | * Size |
| 105 | |
| 106 | .. note:: |
| 107 | default: ``-1`` |
| 108 | |
| 109 | Amount of data to be requested (will stop issuing Interests after ``Size`` data is received) |
| 110 | |
Alexander Afanasyev | d4e97b3 | 2012-06-04 15:09:50 -0700 | [diff] [blame] | 111 | If ``Size`` is set to -1, Interests will be requested till the end of the simulation. |
Alexander Afanasyev | 6315ef7 | 2012-06-01 20:56:31 -0700 | [diff] [blame] | 112 | |
Alexander Afanasyev | 5b054aa | 2012-08-13 18:56:17 -0700 | [diff] [blame] | 113 | Producer |
Alexander Afanasyev | 6315ef7 | 2012-06-01 20:56:31 -0700 | [diff] [blame] | 114 | ^^^^^^^^^^^^ |
| 115 | |
Alexander Afanasyev | 5b054aa | 2012-08-13 18:56:17 -0700 | [diff] [blame] | 116 | :ndnsim:`Producer` a simple Interest-sink application, which replying every incoming Interest with Data packet with a specified size and name same as in Interest. |
Alexander Afanasyev | 6315ef7 | 2012-06-01 20:56:31 -0700 | [diff] [blame] | 117 | |
| 118 | .. code-block:: c++ |
| 119 | |
| 120 | // Create application using the app helper |
Alexander Afanasyev | 5b054aa | 2012-08-13 18:56:17 -0700 | [diff] [blame] | 121 | ndn::AppHelper consumerHelper ("ns3::ndn::Producer"); |
Alexander Afanasyev | 6315ef7 | 2012-06-01 20:56:31 -0700 | [diff] [blame] | 122 | |
| 123 | |
| 124 | Custom applications |
| 125 | +++++++++++++++++++ |
| 126 | |
Alexander Afanasyev | 5b054aa | 2012-08-13 18:56:17 -0700 | [diff] [blame] | 127 | Applications interact with the core of the system using :ndnsim:`AppFace` realization of Face abstraction. |
| 128 | To simplify implementation of specific NDN application, ndnSIM provides a base :ndnsim:`App` class that takes care of creating :ndnsim:`AppFace` and registering it inside the NDN protocol stack, as well as provides default processing for incoming Interest and Data packets. |
Alexander Afanasyev | 6315ef7 | 2012-06-01 20:56:31 -0700 | [diff] [blame] | 129 | |
Alexander Afanasyev | 5b054aa | 2012-08-13 18:56:17 -0700 | [diff] [blame] | 130 | .. Base App class |
Alexander Afanasyev | d4e97b3 | 2012-06-04 15:09:50 -0700 | [diff] [blame] | 131 | .. ^^^^^^^^^^^^^^^^^^ |
Alexander Afanasyev | 6315ef7 | 2012-06-01 20:56:31 -0700 | [diff] [blame] | 132 | |
| 133 | |
| 134 | |
Alexander Afanasyev | 6dbacda | 2012-10-23 17:20:18 -0700 | [diff] [blame] | 135 | Customer example |
| 136 | ^^^^^^^^^^^^^^^^ |
Alexander Afanasyev | d4e97b3 | 2012-06-04 15:09:50 -0700 | [diff] [blame] | 137 | |
Alexander Afanasyev | 6dbacda | 2012-10-23 17:20:18 -0700 | [diff] [blame] | 138 | The following code shows how a simple ndnSIM application can be created. |
| 139 | For details refer to API documentation of ndnSIM and NS-3. |
Alexander Afanasyev | d4e97b3 | 2012-06-04 15:09:50 -0700 | [diff] [blame] | 140 | |
| 141 | .. code-block:: c++ |
| 142 | |
Alexander Afanasyev | 5b054aa | 2012-08-13 18:56:17 -0700 | [diff] [blame] | 143 | class CustomApp : public ndn::App |
Alexander Afanasyev | d4e97b3 | 2012-06-04 15:09:50 -0700 | [diff] [blame] | 144 | { |
| 145 | public: |
Alexander Afanasyev | 5b054aa | 2012-08-13 18:56:17 -0700 | [diff] [blame] | 146 | // overridden from ndn::App |
Alexander Afanasyev | d4e97b3 | 2012-06-04 15:09:50 -0700 | [diff] [blame] | 147 | |
| 148 | // Processing upon start of the application |
| 149 | virtual void |
| 150 | StartApplication () |
| 151 | { |
Alexander Afanasyev | 5b054aa | 2012-08-13 18:56:17 -0700 | [diff] [blame] | 152 | // initialize ndn::App |
| 153 | ndn::App::StartApplication (); |
Alexander Afanasyev | d4e97b3 | 2012-06-04 15:09:50 -0700 | [diff] [blame] | 154 | |
| 155 | // Create a name components object for name ``/prefix/sub`` |
Alexander Afanasyev | 5b054aa | 2012-08-13 18:56:17 -0700 | [diff] [blame] | 156 | Ptr<ndn::NameComponents> prefix = Create<ndn::NameComponents> (); // now prefix contains ``/`` |
Alexander Afanasyev | d4e97b3 | 2012-06-04 15:09:50 -0700 | [diff] [blame] | 157 | prefix->Add ("prefix"); // now prefix contains ``/prefix`` |
| 158 | prefix->Add ("sub"); // now prefix contains ``/prefix/sub`` |
| 159 | |
| 160 | ///////////////////////////////////////////////////////////////////////////// |
| 161 | // Creating FIB entry that ensures that we will receive incoming Interests // |
| 162 | ///////////////////////////////////////////////////////////////////////////// |
| 163 | |
| 164 | // Get FIB object |
Alexander Afanasyev | 5b054aa | 2012-08-13 18:56:17 -0700 | [diff] [blame] | 165 | Ptr<ndn::Fib> fib = GetNode ()->GetObject<ndn::Fib> (); |
Alexander Afanasyev | d4e97b3 | 2012-06-04 15:09:50 -0700 | [diff] [blame] | 166 | |
| 167 | // Add entry to FIB |
Alexander Afanasyev | 5b054aa | 2012-08-13 18:56:17 -0700 | [diff] [blame] | 168 | // Note that ``m_face`` is cretaed by ndn::App |
| 169 | ndn::fib::EntryContainer::type::iterator fibEntry = fib->Add (*prefix, m_face, 0); |
Alexander Afanasyev | d4e97b3 | 2012-06-04 15:09:50 -0700 | [diff] [blame] | 170 | |
| 171 | ///////////////////////////////////// |
| 172 | // Sending one Interest packet out // |
| 173 | ///////////////////////////////////// |
| 174 | |
Alexander Afanasyev | 5b054aa | 2012-08-13 18:56:17 -0700 | [diff] [blame] | 175 | // Create and configure ndn::InterestHeader |
| 176 | ndn::InterestHeader interestHeader; |
Alexander Afanasyev | d4e97b3 | 2012-06-04 15:09:50 -0700 | [diff] [blame] | 177 | UniformVariable rand (0,std::numeric_limits<uint32_t>::max ()); |
| 178 | interestHeader.SetNonce (rand.GetValue ()); |
| 179 | interestHeader.SetName (prefix); |
| 180 | interestHeader.SetInterestLifetime (Seconds (1.0)); |
| 181 | |
Alexander Afanasyev | 5b054aa | 2012-08-13 18:56:17 -0700 | [diff] [blame] | 182 | // Create packet and add ndn::InterestHeader |
Alexander Afanasyev | d4e97b3 | 2012-06-04 15:09:50 -0700 | [diff] [blame] | 183 | Ptr<Packet> packet = Create<Packet> (); |
| 184 | packet->AddHeader (interestHeader); |
| 185 | |
| 186 | // Forward packet to lower (network) layer |
| 187 | m_protocolHandler (packet); |
| 188 | |
| 189 | // Call trace (for logging purposes) |
| 190 | m_transmittedInterests (&interestHeader, this, m_face); |
| 191 | } |
| 192 | |
| 193 | // Processing when application is stopped |
| 194 | virtual void |
| 195 | StopApplication () |
| 196 | { |
Alexander Afanasyev | 5b054aa | 2012-08-13 18:56:17 -0700 | [diff] [blame] | 197 | // cleanup ndn::App |
| 198 | ndn::App::StopApplication (); |
Alexander Afanasyev | d4e97b3 | 2012-06-04 15:09:50 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | // Callback that will be called when Interest arrives |
| 202 | virtual void |
Alexander Afanasyev | 5b054aa | 2012-08-13 18:56:17 -0700 | [diff] [blame] | 203 | OnInterest (const Ptr<const ndn::InterestHeader> &interest, Ptr<Packet> packet) |
Alexander Afanasyev | d4e97b3 | 2012-06-04 15:09:50 -0700 | [diff] [blame] | 204 | { |
Alexander Afanasyev | 5b054aa | 2012-08-13 18:56:17 -0700 | [diff] [blame] | 205 | // Create and configure ndn::ContentObjectHeader and ndn::ContentObjectTail |
Alexander Afanasyev | d4e97b3 | 2012-06-04 15:09:50 -0700 | [diff] [blame] | 206 | // (header is added in front of the packet, tail is added at the end of the packet) |
| 207 | |
Alexander Afanasyev | 5b054aa | 2012-08-13 18:56:17 -0700 | [diff] [blame] | 208 | ndn::ContentObjectHeader data; |
| 209 | data.SetName (Create<ndn::NameComponents> (interest->GetName ())); // data will have the same name as Interests |
Alexander Afanasyev | d4e97b3 | 2012-06-04 15:09:50 -0700 | [diff] [blame] | 210 | |
Alexander Afanasyev | 5b054aa | 2012-08-13 18:56:17 -0700 | [diff] [blame] | 211 | ndn::ContentObjectTail trailer; // doesn't require any configuration |
Alexander Afanasyev | d4e97b3 | 2012-06-04 15:09:50 -0700 | [diff] [blame] | 212 | |
| 213 | // Create packet and add header and trailer |
| 214 | Ptr<Packet> packet = Create<Packet> (1024); |
| 215 | packet->AddHeader (data); |
| 216 | packet->AddTrailer (trailer); |
| 217 | |
| 218 | // Forward packet to lower (network) layer |
| 219 | m_protocolHandler (packet); |
| 220 | |
| 221 | // Call trace (for logging purposes) |
| 222 | m_transmittedInterests (&interestHeader, this, m_face); |
| 223 | m_transmittedContentObjects (&data, packet, this, m_face); |
| 224 | } |
| 225 | |
| 226 | // Callback that will be called when Data arrives |
| 227 | virtual void |
Alexander Afanasyev | 5b054aa | 2012-08-13 18:56:17 -0700 | [diff] [blame] | 228 | OnContentObject (const Ptr<const ndn::ContentObjectHeader> &contentObject, |
Alexander Afanasyev | d4e97b3 | 2012-06-04 15:09:50 -0700 | [diff] [blame] | 229 | Ptr<Packet> payload) |
| 230 | { |
| 231 | std::cout << "DATA received for name " << contentObject->GetName () << std::endl; |
| 232 | } |
| 233 | }; |
Alexander Afanasyev | 6dbacda | 2012-10-23 17:20:18 -0700 | [diff] [blame] | 234 | |
| 235 | Producer example (Interest hijacker) |
| 236 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 237 | |
| 238 | The following code demonstrates how to implement a basic producer application that "hijacks" all incoming Interests. |
| 239 | |
| 240 | .. code-block:: c++ |
| 241 | |
| 242 | #include "ns3/core-module.h" |
| 243 | #include "ns3/network-module.h" |
| 244 | #include "ns3/ndnSIM-module.h" |
| 245 | |
| 246 | using namespace ns3; |
| 247 | |
| 248 | class Hijacker : public ndn::App |
| 249 | { |
| 250 | public: |
| 251 | static TypeId |
| 252 | GetTypeId (); |
| 253 | |
| 254 | Hijacker () |
| 255 | { |
| 256 | } |
| 257 | |
| 258 | // inherited from NdnApp |
| 259 | void OnInterest (const Ptr<const ndn::InterestHeader> &interest, Ptr<Packet> packet) |
| 260 | { |
| 261 | ndn::App::OnInterest (interest, packet); // forward call to perform app-level tracing |
| 262 | // do nothing else (hijack interest) |
| 263 | } |
| 264 | |
| 265 | protected: |
| 266 | // inherited from Application base class. |
| 267 | virtual void |
| 268 | StartApplication () |
| 269 | { |
| 270 | App::StartApplication (); |
| 271 | |
| 272 | // equivalent to setting interest filter for "/" prefix |
| 273 | Ptr<ndn::Fib> fib = GetNode ()->GetObject<ndn::Fib> (); |
| 274 | Ptr<ndn::fib::Entry> fibEntry = fib->Add ("/", m_face, 0); |
| 275 | fibEntry->UpdateStatus (m_face, ndn::fib::FaceMetric::NDN_FIB_GREEN); |
| 276 | } |
| 277 | |
| 278 | virtual void |
| 279 | StopApplication () |
| 280 | { |
| 281 | App::StopApplication (); |
| 282 | } |
| 283 | }; |
| 284 | |
| 285 | // Necessary if you are planning to use ndn::AppHelper |
| 286 | NS_OBJECT_ENSURE_REGISTERED (Hijacker); |
| 287 | |
| 288 | TypeId |
| 289 | Hijacker::GetTypeId () |
| 290 | { |
| 291 | static TypeId tid = TypeId ("ndn::Hijacker") |
| 292 | .SetParent<ndn::App> () |
| 293 | .AddConstructor<Hijacker> () |
| 294 | ; |
| 295 | |
| 296 | return tid; |
| 297 | } |
| 298 | |
| 299 | |
| 300 | After defining this class, you can use it with :ndnsim:`ndn::AppHelper`. For example: |
| 301 | |
| 302 | .. code-block:: c++ |
| 303 | |
| 304 | ... |
| 305 | ndn::AppHelper producerHelper ("ndn::Hijacker"); |
| 306 | producerHelper.Install (producerNode); |
| 307 | ... |
| 308 | |