Skip to content

Commit

Permalink
Added strategy parameters and the BroadcastNewNonceStrategy as simple…
Browse files Browse the repository at this point in the history
… example how to use them.
  • Loading branch information
Klaus (Laptop) committed May 25, 2015
1 parent a5587ca commit f26d1ae
Show file tree
Hide file tree
Showing 10 changed files with 330 additions and 121 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Expand Up @@ -4,4 +4,6 @@
*~
.DS_Store
docs/doxygen.warnings.log

test.cc
.*project
helper
19 changes: 9 additions & 10 deletions NFD/daemon/fw/available-strategies.cpp
Expand Up @@ -28,35 +28,34 @@
#include "client-control-strategy.hpp"
#include "ncc-strategy.hpp"
#include "best-route-strategy2.hpp"
#include "broadcast-newnonce-strategy.hpp"

namespace nfd {
namespace fw {

shared_ptr<Strategy>
makeDefaultStrategy(Forwarder& forwarder)
shared_ptr<Strategy> makeDefaultStrategy(Forwarder& forwarder)
{
return make_shared<BestRouteStrategy2>(ref(forwarder));
return make_shared < BestRouteStrategy2 > (ref(forwarder));
}

template<typename S>
inline void
installStrategy(Forwarder& forwarder)
inline void installStrategy(Forwarder& forwarder)
{
StrategyChoice& strategyChoice = forwarder.getStrategyChoice();
if (!strategyChoice.hasStrategy(S::STRATEGY_NAME)) {
strategyChoice.install(make_shared<S>(ref(forwarder)));
strategyChoice.install(make_shared < S > (ref(forwarder)));
}
}

void
installStrategies(Forwarder& forwarder)
void installStrategies(Forwarder& forwarder)
{
installStrategy<BestRouteStrategy>(forwarder);
installStrategy<BroadcastStrategy>(forwarder);
installStrategy<ClientControlStrategy>(forwarder);
installStrategy<NccStrategy>(forwarder);
installStrategy<BestRouteStrategy2>(forwarder);
installStrategy<BroadcastNewNonceStrategy>(forwarder);
}

} // namespace fw
} // namespace nfd
} // namespace fw
} // namespace nfd
84 changes: 84 additions & 0 deletions NFD/daemon/fw/broadcast-newnonce-strategy.cpp
@@ -0,0 +1,84 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
* Copyright (c) 2014, Regents of the University of California,
* Arizona Board of Regents,
* Colorado State University,
* University Pierre & Marie Curie, Sorbonne University,
* Washington University in St. Louis,
* Beijing Institute of Technology,
* The University of Memphis
*
* This file is part of NFD (Named Data Networking Forwarding Daemon).
* See AUTHORS.md for complete list of NFD authors and contributors.
*
* NFD is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Foundation,
* either version 3 of the License, or (at your option) any later version.
*
* NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
*/

#include "broadcast-newnonce-strategy.hpp"
#include "strategy-helper.hpp"
#include <map>
#include "core/logger.hpp"

namespace nfd {
namespace fw {

NFD_LOG_INIT("BroadcastNewNonceStrategy");

const Name BroadcastNewNonceStrategy::STRATEGY_NAME(
"ndn:/localhost/nfd/strategy/broadcast-newnonce/%FD%01");

BroadcastNewNonceStrategy::BroadcastNewNonceStrategy(Forwarder& forwarder, const Name& name) :
Strategy(forwarder, name), ownStrategyChoice(forwarder.getStrategyChoice())
{
NFD_LOG_INFO("BroadcastNewNonceStrategy initialized!");
}

BroadcastNewNonceStrategy::~BroadcastNewNonceStrategy()
{
}

void BroadcastNewNonceStrategy::afterReceiveInterest(const Face& inFace, const Interest& interest,
shared_ptr<fib::Entry> fibEntry, shared_ptr<pit::Entry> pitEntry)
{

bool useNonce = true;
// Getting nonce value from parameters;
std::string parameterString = ownStrategyChoice.findEffectiveParameters(
interest.getName().toUri());
std::map < std::string, std::string > parameterMap = StrategyHelper::getParameterMap(
parameterString);

std::map<std::string, std::string>::const_iterator it = parameterMap.find("nonce");
if (it != parameterMap.end()) {
if (it->second == "false") {
useNonce = false;
}
}

const fib::NextHopList& nexthops = fibEntry->getNextHops();

for (fib::NextHopList::const_iterator it = nexthops.begin(); it != nexthops.end(); ++it) {
shared_ptr < Face > outFace = it->getFace();
if (pitEntry->canForwardTo(*outFace)) {
NFD_LOG_TRACE("New Nonce? " << useNonce);
// If nonce == true, change interest nonce for each new packet
this->sendInterest(pitEntry, outFace, useNonce);
}
}

if (!pitEntry->hasUnexpiredOutRecords()) {
this->rejectPendingInterest(pitEntry);
}
}

} // namespace fw
} // namespace nfd
38 changes: 38 additions & 0 deletions NFD/daemon/fw/broadcast-newnonce-strategy.hpp
@@ -0,0 +1,38 @@
#ifndef NFD_DAEMON_FW_OWNSTRATEGY_HPP
#define NFD_DAEMON_FW_OWNSTRATEGY_HPP

#include "strategy.hpp"

namespace nfd {
namespace fw {

/** \class BroadcastNewNonceStrategy
* \brief Forwards interests to all nexthops using a new nonce.
*
* The parameter "nonce" denotes if a new nonce should be used (nonce=true) or not (nonce=false).
* The default is to use a new nonce.
*/
class BroadcastNewNonceStrategy : public Strategy
{
public:
BroadcastNewNonceStrategy(Forwarder& forwarder, const Name& name = STRATEGY_NAME);

virtual
~BroadcastNewNonceStrategy();

virtual void
afterReceiveInterest(const Face& inFace, const Interest& interest,
shared_ptr<fib::Entry> fibEntry, shared_ptr<pit::Entry> pitEntry)
DECL_OVERRIDE;

public:
static const Name STRATEGY_NAME;

private:
StrategyChoice& ownStrategyChoice;
};

} // namespace fw
} // namespace nfd

#endif
39 changes: 39 additions & 0 deletions NFD/daemon/fw/strategy-helper.cpp
@@ -0,0 +1,39 @@
#include "strategy-helper.hpp"
#include "core/logger.hpp"
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/detail/classification.hpp>
#include <boost/algorithm/string/replace.hpp>
#include <boost/algorithm/string/split.hpp>

namespace nfd {
namespace fw {

NFD_LOG_INIT("StrategyHelper");

std::map<std::string, std::string> StrategyHelper::getParameterMap(std::string parameters)
{
NFD_LOG_INFO("Parsing parameters!");

std::map < std::string, std::string > outputMap;

// Replace ASCII elements
boost::replace_all(parameters, "%2C", ",");
boost::replace_all(parameters, "%3D", "=");

std::vector < std::string > paramVector;
boost::split(paramVector, parameters, boost::is_any_of(","));

for (auto substring : paramVector) {
std::vector < std::string > substringVector;
boost::split(substringVector, substring, boost::is_any_of("="));

std::string key = substringVector.front();
std::string value = substringVector.back();
outputMap[substringVector.front()] = substringVector.back();
}

return outputMap;
}

} // namespace nfd
} // namespace fw
20 changes: 20 additions & 0 deletions NFD/daemon/fw/strategy-helper.hpp
@@ -0,0 +1,20 @@
#ifndef NFD_DAEMON_FW_STRATEGY_HELPER_HPP
#define NFD_DAEMON_FW_STRATEGY_HELPER_HPP

#include <map>
#include <string>

namespace nfd {
namespace fw {

class StrategyHelper
{

public:
static std::map<std::string, std::string> getParameterMap(std::string parameters);
};

} // namespace fw
} // namespace nfd

#endif
9 changes: 4 additions & 5 deletions NFD/daemon/table/strategy-choice-entry.cpp
Expand Up @@ -30,9 +30,8 @@
namespace nfd {
namespace strategy_choice {

Entry::Entry(const Name& prefix)
: m_prefix(prefix)
, m_strategy(nullptr)
Entry::Entry(const Name& prefix) :
m_prefix(prefix), m_strategy(nullptr)
{
}

Expand All @@ -42,5 +41,5 @@ Entry::getStrategyName() const
return m_strategy->getName();
}

} // namespace strategy_choice
} // namespace nfd
} // namespace strategy_choice
} // namespace nfd
26 changes: 21 additions & 5 deletions NFD/daemon/table/strategy-choice-entry.hpp
Expand Up @@ -59,16 +59,22 @@ class Entry : noncopyable
void
setStrategy(fw::Strategy& strategy);

std::string
getParameters() const;

void
setParameters(std::string parameters);

private:
Name m_prefix;
fw::Strategy* m_strategy;
std::string m_parameters;

shared_ptr<name_tree::Entry> m_nameTreeEntry;
friend class nfd::NameTree;
friend class nfd::name_tree::Entry;
};


inline const Name&
Entry::getPrefix() const
{
Expand All @@ -82,13 +88,23 @@ Entry::getStrategy() const
return *m_strategy;
}

inline void
Entry::setStrategy(fw::Strategy& strategy)
inline void Entry::setStrategy(fw::Strategy& strategy)
{
m_strategy = &strategy;
}

} // namespace strategy_choice
} // namespace nfd
inline std::string Entry::getParameters() const
{
// BOOST_ASSERT(m_parameters.size() != 0);
return m_parameters;
}

inline void Entry::setParameters(std::string parameters)
{
m_parameters = parameters;
}

} // namespace strategy_choice
} // namespace nfd

#endif // NFD_DAEMON_TABLE_STRATEGY_CHOICE_ENTRY_HPP

0 comments on commit f26d1ae

Please sign in to comment.