-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprintRoutingTbl.cc
More file actions
118 lines (96 loc) · 4.47 KB
/
printRoutingTbl.cc
File metadata and controls
118 lines (96 loc) · 4.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/**
* Learn to print routing table.
**/
// For General Imports
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/internet-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/csma-module.h"
// For Routing...
#include "ns3/ipv4-static-routing-helper.h"
#include "ns3/ipv4-routing-table-entry.h"
// For client and server communication...
#include "ns3/applications-module.h"
// For Netanim
#include "ns3/netanim-module.h"
using namespace ns3;
void printRoutingTable(Ptr<Node> node, Ptr<OutputStreamWrapper> os) {
Ipv4StaticRoutingHelper staticRoutingHelper;
Ptr<Ipv4StaticRouting> staticRouting = staticRoutingHelper.GetStaticRouting(node->GetObject<Ipv4>());
staticRouting->PrintRoutingTable (os);
}
int main() {
// Enable logging for server and client...
Time::SetResolution(Time::NS);
LogComponentEnable("UdpEchoClientApplication", LOG_LEVEL_INFO);
LogComponentEnable("UdpEchoServerApplication", LOG_LEVEL_INFO);
// Create 3 nodes...
NodeContainer nodes,nodesGrpAB,nodesGrpBC;
nodes.Create(3);
nodesGrpAB.Add(nodes.Get(0));
nodesGrpAB.Add(nodes.Get(1));
nodesGrpBC.Add(nodes.Get(1));
nodesGrpBC.Add(nodes.Get(2));
// Set up Point-to-Point links...
PointToPointHelper p2p;
p2p.SetDeviceAttribute("DataRate", StringValue("5Mbps"));
p2p.SetChannelAttribute("Delay", StringValue("2ms"));
// Create devices...
NetDeviceContainer devicesGrpAB,devicesGrpBC;
devicesGrpAB = p2p.Install(nodesGrpAB);
devicesGrpBC = p2p.Install(nodesGrpBC);
// Install Internet stack
InternetStackHelper internet;
internet.Install(nodes);
// Assign IP addresses
Ipv4AddressHelper address;
address.SetBase("192.168.1.0", "255.255.255.0");
Ipv4InterfaceContainer interfacesAB = address.Assign(devicesGrpAB);
address.SetBase("192.168.2.0", "255.255.255.0");
Ipv4InterfaceContainer interfacesBC = address.Assign(devicesGrpBC);
// Get the Ipv4StaticRouting instance for each node
Ipv4StaticRoutingHelper staticRoutingHelper;
Ptr<Ipv4StaticRouting> staticRoutingGrpABNode0 = staticRoutingHelper.GetStaticRouting(nodesGrpAB.Get(0)->GetObject<Ipv4>());
Ptr<Ipv4StaticRouting> staticRoutingGrpBCNode1 = staticRoutingHelper.GetStaticRouting(nodesGrpBC.Get(1)->GetObject<Ipv4>());
// Configure static routes
staticRoutingGrpABNode0->AddNetworkRouteTo(Ipv4Address("192.168.2.0"), Ipv4Mask("255.255.255.0"), interfacesAB.GetAddress(1), 1);
staticRoutingGrpBCNode1->AddNetworkRouteTo(Ipv4Address("192.168.1.0"), Ipv4Mask("255.255.255.0"), interfacesBC.GetAddress(0), 1);
// Schedule printing of the routing tables at 1 second into the simulation
Ptr<OutputStreamWrapper> routingTblPrintStream = Create<OutputStreamWrapper> (&std::cout);
staticRoutingGrpABNode0->PrintRoutingTable (routingTblPrintStream);
// print routing table at specific time...
std::cout << "Printing Routing Table With Scheduling" << std::endl;
Simulator::Schedule (Seconds(1.0), &printRoutingTable, nodes.Get(0), routingTblPrintStream);
Simulator::Schedule (Seconds(1.0), &printRoutingTable, nodes.Get(1), routingTblPrintStream);
Simulator::Schedule (Seconds(1.0), &printRoutingTable, nodes.Get(2), routingTblPrintStream);
// Without client server communication, this has no effect...
/*
// Enable packet capture
p2p.EnablePcapAll("basic-static-routing");
*/
// We don't necessary need the client server communication and netanim's code...
/*
// Client and Server Communication...
UdpEchoServerHelper echoServer(9);
ApplicationContainer serverApps = echoServer.Install(nodesGrpBC.Get(1));
serverApps.Start(Seconds(1.0));
serverApps.Stop(Seconds(10.0));
UdpEchoClientHelper echoClient(interfacesBC.GetAddress(1), 9);
echoClient.SetAttribute("MaxPackets", UintegerValue(4));
echoClient.SetAttribute("Interval", TimeValue(Seconds(1.0)));
echoClient.SetAttribute("PacketSize", UintegerValue(1024));
ApplicationContainer clientApps = echoClient.Install(nodesGrpAB.Get(0));
clientApps.Start(Seconds(2.0));
clientApps.Stop(Seconds(10.0));
// for NetAnim...
AnimationInterface anim("basic-static-routing.xml");
anim.SetConstantPosition(nodesGrpAB.Get(0), 10, 10);
anim.SetConstantPosition(nodesGrpAB.Get(1), 0, 20);
anim.SetConstantPosition(nodesGrpBC.Get(1), 20, 20);
*/
// Run simulation
Simulator::Run();
Simulator::Destroy();
return 0;
}