-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsma.cc
108 lines (88 loc) · 3.35 KB
/
csma.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
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation;
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
Network topology:
Client 10.1.1.0 Server
A B C
| | |
=========================
.1 .2 .3
*/
#include "ns3/core-module.h"
#include "ns3/csma-module.h"
#include "ns3/network-module.h"
#include "ns3/internet-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/applications-module.h"
#include "ns3/ipv4-global-routing-helper.h"
#include "ns3/net-device-container.h"
#include "ns3/netanim-module.h"
using namespace ns3;
using namespace std;
NS_LOG_COMPONENT_DEFINE ("CsmaScriptExample");
int
main (int argc, char *argv[])
{
Time::SetResolution (Time::NS);
LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);
GlobalValue::Bind ("ChecksumEnabled", BooleanValue(true));
NodeContainer csmaNodes;
csmaNodes.Create (3);
CsmaHelper csma;
csma.SetChannelAttribute("DataRate", StringValue("100Mbps"));
csma.SetChannelAttribute("Delay", TimeValue(NanoSeconds(6560)));
NetDeviceContainer csmaDevices;
csmaDevices = csma.Install(csmaNodes);
InternetStackHelper stack;
stack.Install(csmaNodes);
Ipv4AddressHelper addr;
addr.SetBase("10.1.1.0", "255.255.255.0");
Ipv4InterfaceContainer csmaInterfaces;
csmaInterfaces = addr.Assign(csmaDevices);
///////////////////////////
Ipv4Address addrArray[3];
Ptr<Ipv4> ip_ptr;
Ptr<Node> node_ptr;
for(int i=0; i<3; i++){
node_ptr = csmaNodes.Get(i);
ip_ptr = node_ptr->GetObject<Ipv4>();
addrArray[i] = ip_ptr->GetAddress(1,0).GetLocal();
cout<<"csma nodes["<<i<<"] address:"<<ip_ptr->GetAddress(1,0).GetLocal()<<endl;
}
///////////////////////////
UdpEchoServerHelper echoServer(9);
ApplicationContainer serverApps = echoServer.Install(csmaNodes.Get(2));
serverApps.Start(Seconds(1.0));
serverApps.Stop(Seconds(10.0));
UdpEchoClientHelper echoClient(csmaInterfaces.GetAddress(2), 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");
AsciiTraceHelper ascii;
csma.EnableAsciiAll(ascii.CreateFileStream("tr/csma.tr"));
AnimationInterface anim("xml/csma-anim.xml");
anim.SetConstantPosition(csmaNodes.Get(0), 1.0, 1.0);
anim.SetConstantPosition(csmaNodes.Get(1), 5.0, 1.0);
anim.SetConstantPosition(csmaNodes.Get(2), 9.0, 1.0);
Simulator::Run();
Simulator::Destroy();
return 0;
}