代码段如下:
void printNeibors(Ptr<Node> node) {
// Ptr<Ipv4> ipv4_1 = node->GetObject<Ipv4>();
// Ptr<Ipv4RoutingProtocol> ipv4RoutingProtocol = ipv4_1->GetRoutingProtocol();
// Ptr<olsr::RoutingProtocol> routingProtocol = DynamicCast<
// olsr::RoutingProtocol>(ipv4RoutingProtocol);
// const NeighborSet &neighbors =
// routingProtocol->GetNeighbors();
// std::cout << "neighborMainAddr: "
// << routingProtocol->GetNeighbors().begin()->neighborMainAddr
// << "\n";
Ptr<Ipv4> ipv4 = node->GetObject<Ipv4>();
Ptr<Ipv4RoutingProtocol> rp_Gw = (ipv4->GetRoutingProtocol());
Ptr<Ipv4ListRouting> lrp_Gw = DynamicCast<Ipv4ListRouting>(rp_Gw);
Ptr<olsr::RoutingProtocol> olsrrp_Gw;
//参考ns-3 3.35 src/olsr/examples/olsr-hna.cc
for (uint32_t i = 0; i < lrp_Gw->GetNRoutingProtocols(); i++) {
int16_t priority;
Ptr<Ipv4RoutingProtocol> temp = lrp_Gw->GetRoutingProtocol(i, priority);
if (DynamicCast<olsr::RoutingProtocol>(temp)) {
olsrrp_Gw = DynamicCast<olsr::RoutingProtocol>(temp);
std::cout << "neighborMainAddr: "
<< olsrrp_Gw->GetNeighbors().size() << "\n";
} else {
std::cout << DynamicCast<olsr::RoutingProtocol>(temp) << "\n";
}
}
}
调用该段代码的代码段如下:
NodeContainer c;
c.Create(5);
/*
*中间还有其他一些配置,这里略过了。完整代码建议结合ns-3 3.35 源码包中的两个例子自行编写:
* src/olsr/examplses/simple-point-to-point-olsr.cc
* src/olsr/examplses/olsr-hna.cc
*
/
Simulator::Schedule(Seconds(2.0), &printNeibors, c.Get(3));
|