blob: 21e3531be56dde8175f9cf1f573c2444d5005b9c [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
9Writing your own custom strategy
10++++++++++++++++++++++++++++++++
11
12First 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`).
13The following example assumes that we are realizing :ndnsim:`forwarding strategy interface <ndn::ForwardingStrategy>`.
14
15The follwoing are template strategy h/cc files:
16
17.. literalinclude:: _static/code-samples/custom-strategy.h
18 :language: c++
19 :linenos:
20 :lines: 1-36,51-55,59-
21
22.. literalinclude:: _static/code-samples/custom-strategy.cc
23 :language: c++
24 :linenos:
25 :lines: 1-40,42-50,75-76,115-
26
27After having the template, we can fill the necesasry functionality.
28
29Let us say, that we want Interest be forwarded to first two best-metric faces specified by FIB.
30That is, if node has two or more alternative paths to forward the Interests, this Interest will be forwarded to the best two neighbors.
31The following implementation of CustomStrategy::DoPropagateInterest accomplishes the task:
32
33.. literalinclude:: _static/code-samples/custom-strategy.cc
34 :language: c++
35 :linenos:
36 :lines: 45-75
37 :emphasize-lines: 7-30
38
39After the compilation, you can use ``"ns3::ndn::fw::CustomStrategy"`` as a parameter to :ndnsim:`ndn::StackHelper::SetForwardingStrategy` helper method.
40
41 .. as well as NS_LOG=ndn.fw.CustomStrategy when running in a debug mode
42
43Extending strategy
44++++++++++++++++++
45
46If you need more customization for the forwarding strategy, there are many forwarding strategy events that can be overriden.
47For 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>`):
48
49- :ndnsim:`DidSendOutInterest <ForwardingStrategy::DidSendOutInterest>`, which fired just after forwarding the Interest
50
51- :ndnsim:`WillEraseTimedOutPendingInterest <ForwardingStrategy::WillEraseTimedOutPendingInterest>`, which fired just before PIT entry is removed by timeout
52
53- :ndnsim:`WillSatisfyPendingInterest <ForwardingStrategy::WillSatisfyPendingInterest>`, which fired just before Interest will be satisfied.
54
55The highlighted ares of the following code demonstrates how it can be impelmented:
56
57.. literalinclude:: _static/code-samples/custom-strategy.h
58 :language: c++
59 :linenos:
60 :emphasize-lines: 37-50,56-58
61
62.. literalinclude:: _static/code-samples/custom-strategy.cc
63 :language: c++
64 :linenos:
65 :emphasize-lines: 41,51-74,77-114