A Discrete-Event Network Simulator
API
csma-duplex.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 Luciano Jerez Chaves
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Luciano Jerez Chaves <ljerezchaves@gmail.com>
18  */
19 
20 #include "ns3/applications-module.h"
21 #include "ns3/core-module.h"
22 #include "ns3/csma-module.h"
23 #include "ns3/internet-module.h"
24 #include "ns3/network-module.h"
25 
26 using namespace ns3;
27 
28 int
29 main(int argc, char* argv[])
30 {
31  bool verbose = false;
32  bool trace = false;
33 
35  cmd.AddValue("verbose", "Tell application to log if true", verbose);
36  cmd.AddValue("trace", "Tracing traffic to files", trace);
37  cmd.AddValue("duplex", "ns3::CsmaChannel::FullDuplex");
38  cmd.Parse(argc, argv);
39 
40  if (verbose)
41  {
42  LogComponentEnable("CsmaNetDevice", LOG_LEVEL_ALL);
43  LogComponentEnable("CsmaChannel", LOG_LEVEL_ALL);
45  }
46 
47  // Create the host nodes
48  NodeContainer hosts;
49  hosts.Create(2);
50 
51  // Connecting the hosts
52  CsmaHelper csmaHelper;
53  csmaHelper.SetChannelAttribute("DataRate", DataRateValue(DataRate("100Mbps")));
54  csmaHelper.SetChannelAttribute("Delay", TimeValue(MilliSeconds(1)));
55  NetDeviceContainer hostDevices = csmaHelper.Install(hosts);
56 
57  // Enable pcap traces
58  if (trace)
59  {
60  csmaHelper.EnablePcap("csma-duplex", hostDevices);
61  }
62 
63  // Installing the tcp/ip stack into hosts
64  InternetStackHelper internet;
65  internet.Install(hosts);
66 
67  // Set IPv4 host address
68  Ipv4AddressHelper ipv4switches;
69  Ipv4InterfaceContainer internetIpIfaces;
70  ipv4switches.SetBase("10.1.1.0", "255.255.255.0");
71  internetIpIfaces = ipv4switches.Assign(hostDevices);
72  Ipv4Address h0Addr = internetIpIfaces.GetAddress(0);
73  Ipv4Address h1Addr = internetIpIfaces.GetAddress(1);
74 
75  ApplicationContainer senderApps;
76  ApplicationContainer sinkApps;
77 
78  // TCP traffic from host 0 to 1
79  BulkSendHelper sender0("ns3::TcpSocketFactory", InetSocketAddress(h1Addr, 10000));
80  PacketSinkHelper sink1("ns3::TcpSocketFactory", InetSocketAddress(Ipv4Address::GetAny(), 10000));
81  senderApps.Add(sender0.Install(hosts.Get(0)));
82  sinkApps.Add(sink1.Install(hosts.Get(1)));
83 
84  // TCP traffic from host 1 to 0
85  BulkSendHelper sender1("ns3::TcpSocketFactory", InetSocketAddress(h0Addr, 10001));
86  PacketSinkHelper sink0("ns3::TcpSocketFactory", InetSocketAddress(Ipv4Address::GetAny(), 10001));
87  senderApps.Add(sender1.Install(hosts.Get(1)));
88  sinkApps.Add(sink0.Install(hosts.Get(0)));
89 
90  sinkApps.Start(Seconds(0));
91  senderApps.Start(Seconds(0.01));
92  senderApps.Stop(Seconds(5));
93 
94  // Run the simulation
97 
98  // Transmitted bytes
99  Ptr<PacketSink> pktSink0 = DynamicCast<PacketSink>(sinkApps.Get(1));
100  Ptr<PacketSink> pktSink1 = DynamicCast<PacketSink>(sinkApps.Get(0));
101  std::cout << "Total bytes sent from H0 to H1: " << pktSink1->GetTotalRx() << std::endl;
102  std::cout << "Total bytes sent from H1 to H0: " << pktSink0->GetTotalRx() << std::endl;
103 }
holds a vector of ns3::Application pointers.
void Start(Time start) const
Start all of the Applications in this container at the start time given as a parameter.
Ptr< Application > Get(uint32_t i) const
Get the Ptr<Application> stored in this container at a given index.
void Stop(Time stop) const
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
void Add(ApplicationContainer other)
Append the contents of another ApplicationContainer to the end of this container.
A helper to make it easier to instantiate an ns3::BulkSendApplication on a set of nodes.
Parse command-line arguments.
Definition: command-line.h:232
build a set of CsmaNetDevice objects
Definition: csma-helper.h:48
void SetChannelAttribute(std::string n1, const AttributeValue &v1)
Definition: csma-helper.cc:56
NetDeviceContainer Install(Ptr< Node > node) const
This method creates an ns3::CsmaChannel with the attributes configured by CsmaHelper::SetChannelAttri...
Definition: csma-helper.cc:226
AttributeValue implementation for DataRate.
an Inet address class
aggregate IP/TCP/UDP functionality to existing Nodes.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:43
static Ipv4Address GetAny()
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
holds a vector of ns3::NetDevice pointers
keep track of a set of node pointers.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes.
uint64_t GetTotalRx() const
Definition: packet-sink.cc:96
void EnablePcap(std::string prefix, Ptr< NetDevice > nd, bool promiscuous=false, bool explicitFilename=false)
Enable pcap output the indicated net device.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:140
static void Run()
Run the simulation.
Definition: simulator.cc:176
AttributeValue implementation for Time.
Definition: nstime.h:1423
void(* DataRate)(DataRate oldValue, DataRate newValue)
TracedValue callback signature for DataRate.
Definition: data-rate.h:328
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1336
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1348
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void LogComponentEnable(const std::string &name, LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:305
@ LOG_LEVEL_ALL
Print everything.
Definition: log.h:116
cmd
Definition: second.py:33
bool verbose