Alexander Afanasyev | e74cc1c | 2012-11-21 13:10:03 -0800 | [diff] [blame] | 1 | |
| 2 | Forwarding Strategies |
| 3 | ===================== |
| 4 | |
| 5 | ndnSIM provides simple ways to experiment with custom Interest/Data forwarding strategies. |
| 6 | A new forwarding strategy can be implement completely different processing or override just specific actions/events of the :ndnsim:`forwarding strategy interface <ndn::ForwardingStrategy>`. |
| 7 | Please refer to :ndnsim:`API documentation <ndn::ForwardingStrategy>` of the forwarding strategy interface, which lists all default actions/events. |
| 8 | |
Alexander Afanasyev | e97c607 | 2012-11-21 23:51:12 -0800 | [diff] [blame^] | 9 | .. _Writing your own custom strategy: |
| 10 | |
Alexander Afanasyev | e74cc1c | 2012-11-21 13:10:03 -0800 | [diff] [blame] | 11 | Writing your own custom strategy |
| 12 | ++++++++++++++++++++++++++++++++ |
| 13 | |
| 14 | First step in creating your own strategy is to decide which existing strategy you want to extend. You can either use realize :ndnsim:`forwarding strategy interface <ndn::ForwardingStrategy>` (:ndnsim:`ndn::ForwardingStrategy::DoPropagateInterest` call must be implemented) or extended one of the available forwarding strategies (:ndnsim:`fw::BestRoute` or :ndnsim:`fw::Flooding`). |
| 15 | The following example assumes that we are realizing :ndnsim:`forwarding strategy interface <ndn::ForwardingStrategy>`. |
| 16 | |
| 17 | The follwoing are template strategy h/cc files: |
| 18 | |
Alexander Afanasyev | e97c607 | 2012-11-21 23:51:12 -0800 | [diff] [blame^] | 19 | .. literalinclude:: ../../examples/custom-strategies/custom-strategy.h |
Alexander Afanasyev | e74cc1c | 2012-11-21 13:10:03 -0800 | [diff] [blame] | 20 | :language: c++ |
| 21 | :linenos: |
| 22 | :lines: 1-36,51-55,59- |
| 23 | |
Alexander Afanasyev | e97c607 | 2012-11-21 23:51:12 -0800 | [diff] [blame^] | 24 | .. literalinclude:: ../../examples/custom-strategies/custom-strategy.cc |
Alexander Afanasyev | e74cc1c | 2012-11-21 13:10:03 -0800 | [diff] [blame] | 25 | :language: c++ |
| 26 | :linenos: |
| 27 | :lines: 1-40,42-50,75-76,115- |
Alexander Afanasyev | e97c607 | 2012-11-21 23:51:12 -0800 | [diff] [blame^] | 28 | :emphasize-lines: 21,27 |
Alexander Afanasyev | e74cc1c | 2012-11-21 13:10:03 -0800 | [diff] [blame] | 29 | |
| 30 | After having the template, we can fill the necesasry functionality. |
| 31 | |
| 32 | Let us say, that we want Interest be forwarded to first two best-metric faces specified by FIB. |
| 33 | That is, if node has two or more alternative paths to forward the Interests, this Interest will be forwarded to the best two neighbors. |
| 34 | The following implementation of CustomStrategy::DoPropagateInterest accomplishes the task: |
| 35 | |
Alexander Afanasyev | e97c607 | 2012-11-21 23:51:12 -0800 | [diff] [blame^] | 36 | .. literalinclude:: ../../examples/custom-strategies/custom-strategy.cc |
Alexander Afanasyev | e74cc1c | 2012-11-21 13:10:03 -0800 | [diff] [blame] | 37 | :language: c++ |
| 38 | :linenos: |
| 39 | :lines: 45-75 |
| 40 | :emphasize-lines: 7-30 |
| 41 | |
| 42 | After the compilation, you can use ``"ns3::ndn::fw::CustomStrategy"`` as a parameter to :ndnsim:`ndn::StackHelper::SetForwardingStrategy` helper method. |
| 43 | |
| 44 | .. as well as NS_LOG=ndn.fw.CustomStrategy when running in a debug mode |
| 45 | |
| 46 | Extending strategy |
| 47 | ++++++++++++++++++ |
| 48 | |
| 49 | If you need more customization for the forwarding strategy, there are many forwarding strategy events that can be overriden. |
| 50 | For example, if we want to perform special logging of all forwarded, timed out, and satisfied Intersts, we can override the following events (for more events, refer to :ndnsim:`ForwardingStrategy API documentation <ForwardingStrategy>`): |
| 51 | |
| 52 | - :ndnsim:`DidSendOutInterest <ForwardingStrategy::DidSendOutInterest>`, which fired just after forwarding the Interest |
| 53 | |
| 54 | - :ndnsim:`WillEraseTimedOutPendingInterest <ForwardingStrategy::WillEraseTimedOutPendingInterest>`, which fired just before PIT entry is removed by timeout |
| 55 | |
| 56 | - :ndnsim:`WillSatisfyPendingInterest <ForwardingStrategy::WillSatisfyPendingInterest>`, which fired just before Interest will be satisfied. |
| 57 | |
| 58 | The highlighted ares of the following code demonstrates how it can be impelmented: |
| 59 | |
Alexander Afanasyev | e97c607 | 2012-11-21 23:51:12 -0800 | [diff] [blame^] | 60 | .. literalinclude:: ../../examples/custom-strategies/custom-strategy.h |
Alexander Afanasyev | e74cc1c | 2012-11-21 13:10:03 -0800 | [diff] [blame] | 61 | :language: c++ |
| 62 | :linenos: |
| 63 | :emphasize-lines: 37-50,56-58 |
| 64 | |
Alexander Afanasyev | e97c607 | 2012-11-21 23:51:12 -0800 | [diff] [blame^] | 65 | .. literalinclude:: ../../examples/custom-strategies/custom-strategy.cc |
Alexander Afanasyev | e74cc1c | 2012-11-21 13:10:03 -0800 | [diff] [blame] | 66 | :language: c++ |
| 67 | :linenos: |
Alexander Afanasyev | e97c607 | 2012-11-21 23:51:12 -0800 | [diff] [blame^] | 68 | :emphasize-lines: 41,77-114 |
| 69 | |
| 70 | |
| 71 | Example of using custom strategy |
| 72 | ++++++++++++++++++++++++++++++++ |
| 73 | |
| 74 | Please refer to :ref:`this example <11-node 2-bottleneck topology with custom forwarding strategy>`. |
| 75 | |