blob: dd4176bcddbf8d839186ead0c300b86237edfd92 [file] [log] [blame]
Alexander Afanasyeve74cc1c2012-11-21 13:10:03 -08001
2Forwarding Strategies
3=====================
4
5ndnSIM provides simple ways to experiment with custom Interest/Data forwarding strategies.
6A new forwarding strategy can be implement completely different processing or override just specific actions/events of the :ndnsim:`forwarding strategy interface <ndn::ForwardingStrategy>`.
7Please refer to :ndnsim:`API documentation <ndn::ForwardingStrategy>` of the forwarding strategy interface, which lists all default actions/events.
8
Alexander Afanasyeve97c6072012-11-21 23:51:12 -08009.. _Writing your own custom strategy:
10
Alexander Afanasyeve74cc1c2012-11-21 13:10:03 -080011Writing your own custom strategy
12++++++++++++++++++++++++++++++++
13
14First 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`).
15The following example assumes that we are realizing :ndnsim:`forwarding strategy interface <ndn::ForwardingStrategy>`.
16
17The follwoing are template strategy h/cc files:
18
Alexander Afanasyeve97c6072012-11-21 23:51:12 -080019.. literalinclude:: ../../examples/custom-strategies/custom-strategy.h
Alexander Afanasyeve74cc1c2012-11-21 13:10:03 -080020 :language: c++
21 :linenos:
22 :lines: 1-36,51-55,59-
23
Alexander Afanasyeve97c6072012-11-21 23:51:12 -080024.. literalinclude:: ../../examples/custom-strategies/custom-strategy.cc
Alexander Afanasyeve74cc1c2012-11-21 13:10:03 -080025 :language: c++
26 :linenos:
27 :lines: 1-40,42-50,75-76,115-
Alexander Afanasyeve97c6072012-11-21 23:51:12 -080028 :emphasize-lines: 21,27
Alexander Afanasyeve74cc1c2012-11-21 13:10:03 -080029
30After having the template, we can fill the necesasry functionality.
31
32Let us say, that we want Interest be forwarded to first two best-metric faces specified by FIB.
33That is, if node has two or more alternative paths to forward the Interests, this Interest will be forwarded to the best two neighbors.
34The following implementation of CustomStrategy::DoPropagateInterest accomplishes the task:
35
Alexander Afanasyeve97c6072012-11-21 23:51:12 -080036.. literalinclude:: ../../examples/custom-strategies/custom-strategy.cc
Alexander Afanasyeve74cc1c2012-11-21 13:10:03 -080037 :language: c++
38 :linenos:
39 :lines: 45-75
40 :emphasize-lines: 7-30
41
42After 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
46Extending strategy
47++++++++++++++++++
48
49If you need more customization for the forwarding strategy, there are many forwarding strategy events that can be overriden.
50For 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
58The highlighted ares of the following code demonstrates how it can be impelmented:
59
Alexander Afanasyeve97c6072012-11-21 23:51:12 -080060.. literalinclude:: ../../examples/custom-strategies/custom-strategy.h
Alexander Afanasyeve74cc1c2012-11-21 13:10:03 -080061 :language: c++
62 :linenos:
63 :emphasize-lines: 37-50,56-58
64
Alexander Afanasyeve97c6072012-11-21 23:51:12 -080065.. literalinclude:: ../../examples/custom-strategies/custom-strategy.cc
Alexander Afanasyeve74cc1c2012-11-21 13:10:03 -080066 :language: c++
67 :linenos:
Alexander Afanasyeve97c6072012-11-21 23:51:12 -080068 :emphasize-lines: 41,77-114
69
70
71Example of using custom strategy
72++++++++++++++++++++++++++++++++
73
74Please refer to :ref:`this example <11-node 2-bottleneck topology with custom forwarding strategy>`.
75