Commit 4220e2e5 authored by caminha's avatar caminha
Browse files

streams distance

parent 9915e153
......@@ -62,8 +62,6 @@ namespace ndn
currNode->node = physicalNodes.Get(randPhysicalIdx);
chosenNodes.insert(randPhysicalIdx);
topology->activatedNodes.Add(currNode->node);
// How much to branch out in this node
const uint rngValue = branchCountRNG->GetInteger(),
......@@ -80,7 +78,9 @@ namespace ndn
currNode->children.push_back(child);
treeNodeQueue.push(child);
}
if (branchCount == 0) topology->leaves.push_back(currNode);
else topology->activatedNodes.Add(currNode->node);
}
......
......@@ -163,7 +163,10 @@ main(int argc, char *argv[])
streamArranger.setupDirectSubscriptions(physicalTopology);
Time TraceConfigTime = Seconds(0.3);
Simulator::Schedule(TraceConfigTime, MakeEvent(&ndn::GlobalRoutingHelper::CalculateRoutes));
Simulator::Schedule(TraceConfigTime, MakeEvent([& ]() {
std::map<uint32_t, std::map<uint32_t, uint32_t>> map_of_distances = ndn::GlobalRoutingHelper::CalculateRoutes();
TracesArranger::writeStreamDistancesFile(map_of_distances, appScenario, physicalTopology->consumers, logicalTopology);
}));
TracesArranger::setup(physicalTopology, TraceConfigTime, tSimulationTime);
......
......@@ -7,6 +7,8 @@
#include "traces-arranger.h"
#include <ctype.h>
namespace ns3
{
namespace ndn
......@@ -117,5 +119,97 @@ namespace ndn
Simulator::Schedule(PrintStreamHitTime, &TracesArranger::writeConsumerMetricsFiles, physicalTopology);
}
void
TracesArranger::writeStreamDistancesFile (const std::map<uint32_t, std::map<uint32_t, uint32_t>>& maps_of_distances, const string& appScenario,
const NodeContainer& consumers, const shared_ptr<LogicalTopology>& logTopology)
{
ofstream streamsDistancesFile;
streamsDistancesFile.open("stream-distances.trace", ios::trunc);
BOOST_ASSERT_MSG(streamsDistancesFile.is_open(), "Failed to open output file!!!");
streamsDistancesFile << "Node\tNodeStreamId\tHopCount" << std::endl;
SubscriptionConsumer::InterestPrefixes prefixes = SubscriptionConsumer::getRandomInterestPrefixes();
for (auto consumer = consumers.Begin(); consumer != consumers.End(); consumer++)
{
string consumer_name = Names::FindName(*consumer);
uint32_t cid = (*consumer)->GetId();
if (appScenario == "RandomTrees")
{
Ptr<SingleSubscriptionConsumer> app = DynamicCast<SingleSubscriptionConsumer>((*consumer)->GetApplication(0));
StringValue interestPrefix;
app->GetAttribute("Prefix", interestPrefix);
// NS_LOG_UNCOND("interestPrefix: " << interestPrefix.Get());
uint32_t firstId = getNodeIdFromName(interestPrefix.Get());
shared_ptr<TreeNode> firstNode = findNodeInLogicalTrees(firstId, logTopology);
printLogicalTreesDistancesRecursively(firstNode, *consumer, maps_of_distances, streamsDistancesFile);
}
else
{
Ptr<SubscriptionConsumer> app = DynamicCast<SubscriptionConsumer>((*consumer)->GetApplication(0));
size_t subCount = app->getSubscriptionCount();
for (uint i=0; i<subCount; i++)
{
Name producerName = prefixes[i];
uint32_t producerId = getNodeIdFromName(producerName);
uint32_t current_distance = maps_of_distances.at(cid).at(producerId);
streamsDistancesFile << consumer_name << '\t' << i << '\t' << current_distance << std::endl;
}
}
}
streamsDistancesFile.close();
}
uint32_t
TracesArranger::getNodeIdFromName(const Name& name)
{
string namestr = name.toUri();
auto it_begin = std::find_if(namestr.begin(), namestr.end(), [](int c) { return isdigit(c); });
auto it_end = std::find_if(namestr.rbegin(), namestr.rend(), [](int c) { return isdigit(c); });
uint initial_pos = distance(namestr.begin(), it_begin);
uint id_size = distance(it_end, namestr.rend());
string str_id = namestr.substr(initial_pos, id_size);
return stoi(str_id);
}
shared_ptr<TreeNode>
TracesArranger::findNodeInLogicalTrees(uint32_t nodeid, shared_ptr<LogicalTopology> logTopology)
{
std::queue<shared_ptr<TreeNode>> node_queue;
for (auto treenode : logTopology->initial_nodes)
node_queue.push(treenode);
while(!node_queue.empty()) {
auto curr_node = node_queue.front();
node_queue.pop();
if (curr_node->node->GetId() == nodeid) {
return curr_node;
} else {
for (auto child : curr_node->children)
{ node_queue.push(child); }
}
}
return nullptr;
}
void
TracesArranger::printLogicalTreesDistancesRecursively(shared_ptr<TreeNode> curr_node, const Ptr<Node>& consumer,
const std::map<uint32_t, std::map<uint32_t, uint32_t>>& maps_of_distances,
ofstream& streamsDistancesFile, uint currDistance)
{
// NS_LOG_UNCOND("printLogicalTreesDistancesRecursively (node=" << curr_node->node->GetId() << ", distance="<<currDistance<<")");
uint32_t cid = consumer->GetId();
uint32_t newDistance = currDistance + maps_of_distances.at(cid).at(curr_node->node->GetId());
if (curr_node->children.empty())
streamsDistancesFile << Names::FindName(consumer) << '\t' <<
curr_node->node->GetId() << '\t' <<
newDistance + 1 << std::endl; // plus one for the extra hop to the producer
else
for (auto child : curr_node->children)
printLogicalTreesDistancesRecursively(child, consumer, maps_of_distances,
streamsDistancesFile, newDistance);
}
} /* namespace ndn */
} /* namespace ns3 */
......@@ -17,6 +17,7 @@
#include "ns3/ndnSIM/apps/ndn-subscription-consumer.hpp"
#include "physical-topology-creator.h"
#include <map>
namespace ns3
{
......@@ -30,9 +31,13 @@ namespace ndn
virtual
~TracesArranger () {};
static void setup(shared_ptr<PhysicalTopology> physical_topology,
Time TraceConfigTime, Time tSimulationTime);
static void
setup(shared_ptr<PhysicalTopology> physical_topology,
Time TraceConfigTime, Time tSimulationTime);
static void
writeStreamDistancesFile (const std::map<uint32_t, std::map<uint32_t, uint32_t>>& maps_of_distances, const string& appScenario,
const NodeContainer& consumers, const shared_ptr<LogicalTopology>& logTopology);
private:
static void
printProgress(Time totalTime, Time step);
......@@ -40,6 +45,14 @@ namespace ndn
writeStreamHitTrace (shared_ptr<PhysicalTopology> physicalTopology);
static void
writeConsumerMetricsFiles (shared_ptr<PhysicalTopology> physicalTopology);
static uint32_t
getNodeIdFromName(const Name& name);
static shared_ptr<TreeNode>
findNodeInLogicalTrees(uint32_t nodeid, shared_ptr<LogicalTopology> logTopology);
static void
printLogicalTreesDistancesRecursively(shared_ptr<TreeNode> curr_node, const Ptr<Node>& node,
const std::map<uint32_t, std::map<uint32_t, uint32_t>>& maps_of_distances,
ofstream& streamsDistancesFile, uint currDistance = 0);
};
} /* namespace ndn */
......
......@@ -144,7 +144,15 @@ main(int argc, char *argv[])
streamArranger.setupDirectSubscriptions(physicalTopology);
Time TraceConfigTime = Seconds(0.3);
Simulator::Schedule(TraceConfigTime, MakeEvent(&ndn::GlobalRoutingHelper::CalculateRoutes));
Simulator::Schedule(TraceConfigTime, MakeEvent([&]() {
auto maps_of_distances = ndn::GlobalRoutingHelper::CalculateRoutes();
for (auto it1 : maps_of_distances) {
for (auto it2 : it1.second) {
NS_LOG_UNCOND("The distance between " << it1.first << " and " << it2.first << " is " << it2.second);
}
}
}
));
TracesArranger::setup(physicalTopology, TraceConfigTime, tSimulationTime);
......
......@@ -189,12 +189,10 @@ void
SingleSubscriptionConsumer::StopApplication()
{
NS_LOG_FUNCTION_NOARGS();
// const uint acceptableLoss = 3; //"Losses" that may happen due to stream setup time
//FIXME THERE IS A BUG ON SOME SUBSCRIPTIONS NOT BEING ANSWERED AT ALL
if (hasReceivedFirstdata)
{
const Time streamDuration = Simulator::Now() - firstDataTime;
NS_LOG_UNCOND("Operation Time: " << streamDuration.GetMinutes());
const uint validSubCount = subscriptionCount - toBeReceived.size();
......@@ -204,8 +202,6 @@ SingleSubscriptionConsumer::StopApplication()
const unsigned long expectedReceivedDatas = validSubCount * std::ceil(streamDuration / m_subscriptionInterval) + 1;// - acceptableLoss;
interestSatisfactionRatio = 100.0 * receivedDatas / expectedReceivedDatas;
NS_LOG_UNCOND("interestSatisfactionRatio: "<< interestSatisfactionRatio << " | diff: " << std::abs(((long)receivedDatas) - ((long)expectedReceivedDatas))); // @suppress("Ambiguous problem")
// interestSatisfactionRatio = std::min(interestSatisfactionRatio.Get(), 100.0);
avgDataDelay /= receivedDatas;
}
App::StopApplication();
......
......@@ -180,6 +180,12 @@ SubscriptionConsumer::setRandomInterestPrefixes(InterestPrefixes prefAndCounts)
randomInterestPrefixes = prefAndCounts;
}
SubscriptionConsumer::InterestPrefixes
SubscriptionConsumer::getRandomInterestPrefixes()
{
return randomInterestPrefixes;
}
void
SubscriptionConsumer::addPrefix(Name prefix)
{
......@@ -304,6 +310,12 @@ SubscriptionConsumer::setSubscriptionCount(size_t count)
subscriptionCount = count;
}
size_t
SubscriptionConsumer::getSubscriptionCount() const
{
return subscriptionCount;
}
Time
SubscriptionConsumer::GetSubInterval() const
{
......
......@@ -62,6 +62,9 @@ public:
static void
setRandomInterestPrefixes (InterestPrefixes);
static InterestPrefixes
getRandomInterestPrefixes();
void
addPrefix(Name prefix);
......@@ -89,6 +92,9 @@ public:
void
setSubscriptionCount(size_t count);
size_t
getSubscriptionCount() const;
Time
GetSubInterval() const;
......
......@@ -216,7 +216,7 @@ GlobalRoutingHelper::AddOriginsForAll()
}
}
void
std::map<uint32_t, std::map<uint32_t, uint32_t>>
GlobalRoutingHelper::CalculateRoutes()
{
/**
......@@ -234,6 +234,9 @@ GlobalRoutingHelper::CalculateRoutes()
// Other algorithms should be faster, but they need additional EdgeListGraph concept provided by
// the graph, which
// is not obviously how implement in an efficient manner
std::map<uint32_t, std::map<uint32_t, uint32_t>> map_of_distances;
for (NodeList::Iterator node = NodeList::Begin(); node != NodeList::End(); node++) {
Ptr<GlobalRouter> source = (*node)->GetObject<GlobalRouter>();
if (source == 0) {
......@@ -263,6 +266,8 @@ GlobalRoutingHelper::CalculateRoutes()
continue;
else {
NS_LOG_DEBUG(" Destination Node " << dist.first->GetObject<Node> ()->GetId ());
map_of_distances[(*node)->GetId()][dist.first->GetObject<Node> ()->GetId()] = std::get<1>(dist.second);
if (std::get<0>(dist.second) == 0) {
NS_LOG_DEBUG(" is unreachable");
}
......@@ -276,23 +281,24 @@ GlobalRoutingHelper::CalculateRoutes()
BOOST_ASSERT_MSG(fibEntry.hasNextHops(), "Fib entry not found on destination");
if (fibEntry.hasPrivacyPolicy())
{
NS_LOG_DEBUG(" Adding route w/ policy");
//NAS_FIXME have to check if this destinations is ok with the policy
//IDEA Change and make public the checks for targets and peerlist
FibHelper::AddRoute(*node, *prefix, std::get<0>(dist.second),
std::get<1>(dist.second), fibEntry.getPrivacyPolicy());
NS_LOG_DEBUG(" Adding route w/ policy");
//NAS_FIXME have to check if this destinations is ok with the policy
//IDEA Change and make public the checks for targets and peerlist
FibHelper::AddRoute(*node, *prefix, std::get<0>(dist.second),
std::get<1>(dist.second), fibEntry.getPrivacyPolicy());
}
else
{
NS_LOG_DEBUG(" Adding route w/o policy");
FibHelper::AddRoute(*node, *prefix, std::get<0>(dist.second),
std::get<1>(dist.second));
NS_LOG_DEBUG(" Adding route w/o policy");
FibHelper::AddRoute(*node, *prefix, std::get<0>(dist.second),
std::get<1>(dist.second));
}
}
}
}
}
}
return map_of_distances;
}
void
......
......@@ -24,6 +24,8 @@
#include "ns3/ptr.h"
#include <map>
namespace ns3 {
class Node;
......@@ -97,7 +99,7 @@ public:
/**
* @brief Calculate for every node shortest path trees and install routes to all prefix origins
*/
static void
static std::map<uint32_t, std::map<uint32_t, uint32_t>>
CalculateRoutes();
/**
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment