diff --git a/src/olsr/examples/olsr-hna.cc b/src/olsr/examples/olsr-hna.cc index 457ecb23d..e19de7c92 100644 --- a/src/olsr/examples/olsr-hna.cc +++ b/src/olsr/examples/olsr-hna.cc @@ -41,12 +41,12 @@ // One way is to use olsr::RoutingProtocol::SetRoutingTableAssociation () // to use which you may run: // -// ./ns3 run "olsr-hna --assocMethod1=1" +// ./ns3 run "olsr-hna --assocMethod=SetRoutingTable" // // The other way is to use olsr::RoutingProtocol::AddHostNetworkAssociation () // to use which you may run: // -// ./ns3 run "olsr-hna --assocMethod2=1" +// ./ns3 run "olsr-hna --assocMethod=AddHostNetwork" // #include "ns3/config-store-module.h" @@ -102,8 +102,8 @@ main(int argc, char* argv[]) uint32_t numPackets = 1; double interval = 1.0; // seconds bool verbose = false; - bool assocMethod1 = false; - bool assocMethod2 = false; + std::string assocMethod("SetRoutingTable"); + bool reverse = false; CommandLine cmd(__FILE__); @@ -113,8 +113,11 @@ main(int argc, char* argv[]) cmd.AddValue("numPackets", "number of packets generated", numPackets); cmd.AddValue("interval", "interval (seconds) between packets", interval); cmd.AddValue("verbose", "turn on all WifiNetDevice log components", verbose); - cmd.AddValue("assocMethod1", "Use SetRoutingTableAssociation () method", assocMethod1); - cmd.AddValue("assocMethod2", "Use AddHostNetworkAssociation () method", assocMethod2); + cmd.AddValue("assocMethod", + "How to add the host to the HNA table (SetRoutingTable, " + "AddHostNetwork)", + assocMethod); + cmd.AddValue("reverse", "Send packets from CSMA to WiFi if set to true", reverse); cmd.Parse(argc, argv); // Convert to time object @@ -210,34 +213,39 @@ main(int argc, char* argv[]) ipv4.Assign(csmaDevices); TypeId tid = TypeId::LookupByName("ns3::UdpSocketFactory"); - Ptr recvSink = Socket::CreateSocket(csmaNodes.Get(0), tid); + Ptr recvSink; + if (!reverse) + { + recvSink = Socket::CreateSocket(csmaNodes.Get(0), tid); + } + else + { + recvSink = Socket::CreateSocket(olsrNodes.Get(0), tid); + } InetSocketAddress local = InetSocketAddress(Ipv4Address::GetAny(), 80); recvSink->Bind(local); recvSink->SetRecvCallback(MakeCallback(&ReceivePacket)); - Ptr source = Socket::CreateSocket(olsrNodes.Get(0), tid); - InetSocketAddress remote = InetSocketAddress(Ipv4Address("172.16.1.1"), 80); - source->Connect(remote); + Ptr source; + if (!reverse) + { + source = Socket::CreateSocket(olsrNodes.Get(0), tid); + InetSocketAddress remote = InetSocketAddress(Ipv4Address("172.16.1.1"), 80); + source->Connect(remote); + } + else + { + source = Socket::CreateSocket(csmaNodes.Get(0), tid); + InetSocketAddress remote = InetSocketAddress(Ipv4Address("10.1.1.1"), 80); + source->Connect(remote); + } // Obtain olsr::RoutingProtocol instance of gateway node // (namely, node B) and add the required association - Ptr stack = olsrNodes.Get(1)->GetObject(); - Ptr rp_Gw = (stack->GetRoutingProtocol()); - Ptr lrp_Gw = DynamicCast(rp_Gw); + Ptr olsrrp_Gw = Ipv4RoutingHelper::GetRouting( + olsrNodes.Get(1)->GetObject()->GetRoutingProtocol()); - Ptr olsrrp_Gw; - - for (uint32_t i = 0; i < lrp_Gw->GetNRoutingProtocols(); i++) - { - int16_t priority; - Ptr temp = lrp_Gw->GetRoutingProtocol(i, priority); - if (DynamicCast(temp)) - { - olsrrp_Gw = DynamicCast(temp); - } - } - - if (assocMethod1) + if (assocMethod == "SetRoutingTable") { // Create a special Ipv4StaticRouting instance for RoutingTableAssociation // Even the Ipv4StaticRouting instance added to list may be used @@ -252,16 +260,26 @@ main(int argc, char* argv[]) uint32_t(1)); olsrrp_Gw->SetRoutingTableAssociation(hnaEntries); } - - if (assocMethod2) + else if (assocMethod == "AddHostNetwork") { // Specify the required associations directly. olsrrp_Gw->AddHostNetworkAssociation(Ipv4Address("172.16.1.0"), Ipv4Mask("255.255.255.0")); } + else + { + std::cout << "invalid HnaMethod option (" << assocMethod << ")" << std::endl; + exit(0); + } + + // Add a default route to the CSMA node (to enable replies) + Ptr staticRoutingProt; + staticRoutingProt = Ipv4RoutingHelper::GetRouting( + csmaNodes.Get(0)->GetObject()->GetRoutingProtocol()); + staticRoutingProt->AddNetworkRouteTo("10.1.1.0", "255.255.255.0", "172.16.1.2", 1); // Tracing - wifiPhy.EnablePcap("olsr-hna", devices); - csma.EnablePcap("olsr-hna", csmaDevices, false); + wifiPhy.EnablePcap("olsr-hna-wifi", devices); + csma.EnablePcap("olsr-hna-csma", csmaDevices, false); AsciiTraceHelper ascii; wifiPhy.EnableAsciiAll(ascii.CreateFileStream("olsr-hna-wifi.tr")); csma.EnableAsciiAll(ascii.CreateFileStream("olsr-hna-csma.tr")); @@ -274,6 +292,10 @@ main(int argc, char* argv[]) numPackets, interPacketInterval); + Ptr routingStream = + Create("olsr-hna.routes", std::ios::out); + Ipv4RoutingHelper::PrintRoutingTableAllAt(Seconds(15), routingStream); + Simulator::Stop(Seconds(20.0)); Simulator::Run(); Simulator::Destroy(); diff --git a/src/olsr/model/olsr-routing-protocol.cc b/src/olsr/model/olsr-routing-protocol.cc index e50015987..0c9e2dec3 100644 --- a/src/olsr/model/olsr-routing-protocol.cc +++ b/src/olsr/model/olsr-routing-protocol.cc @@ -337,12 +337,14 @@ RoutingProtocol::PrintRoutingTable(Ptr stream, Time::Unit u *os << std::setw(16) << "Interface"; *os << "Distance" << std::endl; - for (std::map::const_iterator iter = m_table.begin(); - iter != m_table.end(); - iter++) + for (auto iter = m_table.begin(); iter != m_table.end(); iter++) { - *os << std::setw(16) << iter->first; - *os << std::setw(16) << iter->second.nextAddr; + std::ostringstream dest; + std::ostringstream nextHop; + dest << iter->first; + nextHop << iter->second.nextAddr; + *os << std::setw(16) << dest.str(); + *os << std::setw(16) << nextHop.str(); *os << std::setw(16); if (!Names::FindName(m_ipv4->GetNetDevice(iter->second.interface)).empty()) { @@ -364,7 +366,7 @@ RoutingProtocol::PrintRoutingTable(Ptr stream, Time::Unit u } else { - *os << "HNA Routing Table: empty" << std::endl; + *os << "HNA Routing Table: empty" << std::endl << std::endl; } // Restore the previous ostream state (*os).copyfmt(oldState);