NetAnim路由打印方法

ns3中NetAnim的路由表可以打印出来。打印的代码是

  AnimationInterface anim ("ospfd-anim.xml");
  anim.EnablePacketMetadata(true);
  anim.AddSourceDestination(0,"10.3.1.2");

  anim.EnableIpv4RouteTracking ("ospfd-tracking.xml", Seconds(0.0), Seconds(300.0), nodes, Seconds(0.1)); 


所有节点路由表显示的代码如下,


void 
AnimationInterface::TrackIpv4Route ()
{
  if (Simulator::Now () > m_routingStopTime)
    {
      NS_LOG_INFO ("TrackIpv4Route completed");
      return;
    }
  if (m_routingNc.GetN ()) 所有的节点都要打印
    {
      for (NodeContainer::Iterator i = m_routingNc.Begin (); i != m_routingNc.End (); ++i)
        {
          Ptr <Node> n = *i;
          WriteXmlRouting (n->GetId (), GetIpv4RoutingTable (n));每个节点的路由表
        }
    }
  else
    {
      for (NodeList::Iterator i = NodeList::Begin (); i != NodeList::End (); ++i)
        {
          Ptr <Node> n = *i;
          WriteXmlRouting (n->GetId (), GetIpv4RoutingTable (n));
        }
    }
  TrackIpv4RoutePaths ();
  Simulator::Schedule (m_routingPollInterval, &AnimationInterface::TrackIpv4Route, this);
}

每个节点的路由表信息,

std::string
AnimationInterface::GetIpv4RoutingTable (Ptr <Node> n)
{

  NS_ASSERT (n);
  Ptr <ns3::Ipv4> ipv4 = n->GetObject <ns3::Ipv4> ();
  if (!ipv4)
    {
      NS_LOG_WARN ("Node " << n->GetId () << " Does not have an Ipv4 object");
      return "";
    }
  std::stringstream stream;
  Ptr<OutputStreamWrapper> routingstream = Create<OutputStreamWrapper> (&stream);
  ipv4->GetRoutingProtocol ()->PrintRoutingTable (routingstream);打印出路由表
  return stream.str ();

}

那么路由协议是怎么设定的?

 ipv4->SetRoutingProtocol (ipv4Routing);

通常情况下,其被调用的方式是,

void
InternetStackHelper::Install (Ptr<Node> node) const
{

      // Set routing
      Ptr<Ipv4> ipv4 = node->GetObject<Ipv4> ();
      Ptr<Ipv4RoutingProtocol> ipv4Routing = m_routing->Create (node);
      ipv4->SetRoutingProtocol (ipv4Routing);

}

其中 m_routing来自于

void 
InternetStackHelper::SetRoutingHelper (const Ipv4RoutingHelper &routing)
{
  delete m_routing;
  m_routing = routing.Copy ();
}

串起来的路由表打印的例子代码,

设置

      // Internet stack install
      InternetStackHelper stack;    // IPv4 is required for GlobalRouteMan
      Ipv4DceRoutingHelper ipv4RoutingHelper;
      stack.SetRoutingHelper (ipv4RoutingHelper);
      stack.Install (nodes);

然后就是开头打印的代码了。



你可能感兴趣的:(NetAnim路由打印方法)