-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsmamesh.cc
152 lines (122 loc) · 4.91 KB
/
csmamesh.cc
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include "ns3/core-module.h"
#include "ns3/internet-module.h"
#include "ns3/network-module.h"
#include "ns3/applications-module.h"
#include "ns3/wifi-module.h"
#include "ns3/mesh-module.h"
#include "ns3/mobility-module.h"
#include "ns3/mesh-helper.h"
#include "ns3/netanim-module.h"
#include "ns3/csma-module.h"
#include "ns3/olsr-helper.h"
#include <iostream>
#include <sstream>
#include <fstream>
/*
* n1: EchoClient
* n2: Middle device,like a router
* n3: EchoServer
*
*Network topology:
n1 n2 n3
| | |
=========== *
.1 .2 .2 .1
10.0.1.0 10.0.2.0
csma mesh
*/
using namespace ns3;
using namespace std;
NS_LOG_COMPONENT_DEFINE ("CsmaMeshTest");
int main(int argc, char* argv[])
{
ns3::PacketMetadata::Enable();
LogComponentEnable("CsmaMeshTest", LOG_LEVEL_INFO);
LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);
GlobalValue::Bind("ChecksumEnabled", BooleanValue(true));
NodeContainer csmaNodes;
csmaNodes.Create(2);
Ptr<Node> meshNode = CreateObject<Node>();
CsmaHelper csma;
csma.SetChannelAttribute("DataRate", StringValue("100Mbps"));
csma.SetChannelAttribute("Delay", TimeValue(NanoSeconds(6560)));
NetDeviceContainer csmaDevices;
csmaDevices = csma.Install(csmaNodes);
OlsrHelper olsr;
Ipv4StaticRoutingHelper staticRoutingHelper;
Ipv4ListRoutingHelper listRouting;
listRouting.Add(staticRoutingHelper, 0);
listRouting.Add(olsr, 10);
InternetStackHelper stack;
stack.SetRoutingHelper(listRouting);
stack.Install(csmaNodes);
stack.Install(meshNode);
YansWifiChannelHelper channel = YansWifiChannelHelper::Default();
YansWifiPhyHelper phy = YansWifiPhyHelper::Default();
phy.SetChannel(channel.Create());
MeshHelper mesh = MeshHelper::Default();
mesh.SetStandard(WIFI_PHY_STANDARD_80211n_2_4GHZ);
mesh.SetRemoteStationManager("ns3::ConstantRateWifiManager",
"DataMode", StringValue("HtMcs0"),
"ControlMode", StringValue("HtMcs0"));
mesh.SetStackInstaller("ns3::Dot11sStack");
mesh.SetSpreadInterfaceChannels(MeshHelper::SPREAD_CHANNELS);
mesh.SetMacType("RandomStart", TimeValue(Seconds(0.1)));
mesh.SetNumberOfInterfaces(1);
NetDeviceContainer meshDevices;
meshDevices.Add( mesh.Install(phy, meshNode));
meshDevices.Add(mesh.Install(phy, csmaNodes.Get(1)));
MobilityHelper mobility;
Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
positionAlloc->Add (Vector (0.0, 25.0, 0.0));
positionAlloc->Add (Vector (50.0, 50.0, 0.0));
positionAlloc->Add (Vector (100.0, 50.0, 0.0));
mobility.SetPositionAllocator(positionAlloc);
mobility.Install(csmaNodes);
mobility.Install(meshNode);
Ipv4AddressHelper addr;
addr.SetBase("10.0.1.0","255.255.255.0");
Ipv4InterfaceContainer csmaIntf;
csmaIntf = addr.Assign(csmaDevices);
addr.SetBase("10.0.2.0","255.255.255.0");
Ipv4InterfaceContainer meshIntf;
meshIntf = addr.Assign(meshDevices);
Ptr<Ipv4> ptr_ipv4N2 = csmaNodes.Get(1)->GetObject<Ipv4>();
Ptr<Ipv4StaticRouting> ptr_staticRouting = staticRoutingHelper.GetStaticRouting(ptr_ipv4N2);
//Remove the default route
// for(uint16_t i = 0; i<= ptr_staticRouting->GetNRoutes(); i++){
// ptr_staticRouting->RemoveRoute(i);
// }
//Add route to node2, interface zero is the lookback
ptr_staticRouting->AddHostRouteTo(Ipv4Address("10.0.2.1"), 2);
ptr_staticRouting->AddHostRouteTo(Ipv4Address("10.0.1.1"), 1);
for(uint16_t i=0; i < ptr_staticRouting->GetNRoutes(); i++){
Ipv4RoutingTableEntry routeTable = ptr_staticRouting->GetRoute(i);
std::cout<<"N2 dest: "<<routeTable.GetDest()<<", Gateway: "<<routeTable.GetGateway()<<std::endl;
std::cout<<"N2 dest network: "<<routeTable.GetDestNetwork()<<", Interface: "<<routeTable.GetInterface()<<std::endl<<std::endl;
}
UdpEchoServerHelper echoServer(9);
ApplicationContainer serverApps = echoServer.Install(meshNode);
serverApps.Start(Seconds(1.0));
serverApps.Stop(Seconds(10.0));
UdpEchoClientHelper echoClient(meshIntf.GetAddress(0), 9);
echoClient.SetAttribute("MaxPackets", UintegerValue(2));
echoClient.SetAttribute("Interval", TimeValue(Seconds(1.0)));
echoClient.SetAttribute("PacketSize", UintegerValue(1024));
ApplicationContainer clientApps = echoClient.Install(csmaNodes.Get(0));
clientApps.Start(Seconds(2.0));
clientApps.Stop(Seconds(10.0));
csma.EnablePcapAll("pcap/csma-mesh");
phy.EnablePcapAll("pcap/csma-mesh");
AsciiTraceHelper ascii;
csma.EnableAsciiAll(ascii.CreateFileStream("tr/csma-mesh-0.tr"));
phy.EnableAscii(ascii.CreateFileStream("tr/csma-mesh-1.tr"), 1, 1);
phy.EnableAscii(ascii.CreateFileStream("tr/csma-mesh-2.tr"), 2, 0);
AnimationInterface anim("xml/csma-mesh.xml");
Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
Simulator::Stop(Seconds(10));
Simulator::Run();
Simulator::Destroy();
return 0;
}