In this section, we take a look at the third example, which applies a peer channel and an Ethernet channel that implements the CSMA.
The network topology is as follows:
Default Network Topology
//
10.1.1.0
N0--------------N1 n2 N3 N4
Point-to-Point | | | |
// ================
LAN 10.1.2.0
Next, analyze the source code implementation of second.cc:
------------------------------------------------------------------------------------------------------
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/csma-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"
using namespace Ns3;
Declares a log widget called Secondscriptexample, by referencing the operation of the name Secondscriptexample,
You can implement the output of opening or closing the console log.
Ns_log_component_define ("Secondscriptexample");
int main (int argc, char *argv[])
{
Defines a variable that determines whether to open two udpapplication logging components;
BOOL verbose = true;
uint32_t Ncsma = 3;
CommandLine cmd;
Cmd. AddValue ("Ncsma", "Number of \" Extra\ "CSMA nodes/devices", Ncsma);
Command-line arguments set whether to turn on logging
Cmd. AddValue ("Verbose", "tell-echo applications to log if True", verbose);
Cmd. Parse (ARGC,ARGV);
if (verbose)
{
Logcomponentenable ("Udpechoclientapplication", log_level_info);
Logcomponentenable ("Udpechoserverapplication", log_level_info);
}
Ncsma = Ncsma = = 0? 1:ncsma;
/******************** Network topology Section ************************/
Create 2 node links using the peer link
Nodecontainer P2pnodes;
P2pnodes.create (2);
Create another Nodecontainer class object for the bus (CSMA) network
Nodecontainer Csmanodes;
Add the second node of the previously-peer Nodecontianer to the Csma Nodecontainer,
To obtain CSMA device; This node will have two device
Csmanodes.add (P2pnodes.get (1));
Create another 3 node on the bus network
Csmanodes.create (NCSMA);
Set transfer rate and channel delay
Pointtopointhelper Pointtopoint;
Pointtopoint.setdeviceattribute ("Datarate", StringValue ("5Mbps"));
Pointtopoint.setchannelattribute ("Delay", StringValue ("2ms"));
To install peer-to network interface devices to peer-to node
Netdevicecontainer p2pdevices;
P2pdevices = Pointtopoint.install (p2pnodes);
Create and connect CSMA devices and channels
Csmahelper Csma;
Csma. Setchannelattribute ("Datarate", StringValue ("100Mbps"));
Csma. Setchannelattribute ("Delay", TimeValue (nanoseconds (6560)));
Netdevicecontainer csmadevices;
Csmadevices = Csma. Install (Csmanodes);
Installing network Protocols
Internetstackhelper Stack;
The first node in the peer link
Stack. Install (p2pnodes.get (0));
The second node in the peer link
Stack. Install (Csmanodes);
IP address class object for two network segments
Ipv4addresshelper address;
Arrange the address of the peer segment
Address. SetBase ("10.1.1.0", "255.255.255.0");
Ipv4interfacecontainer p2pinterfaces;
P2pinterfaces = Address. Assign (p2pdevices);
Arranging CSMA Network segment address
Address. SetBase ("10.1.2.0", "255.255.255.0");
Ipv4interfacecontainer csmainterfaces;
Csmainterfaces = Address. Assign (csmadevices);
/******************** network topology partial end *********************/
/********************** Application Section *********************/
Udpechoserverhelper Echoserver (9);
Install the Server service on the last node of the CSMA network segment
Applicationcontainer Serverapps = Echoserver.install (Csmanodes.get (Ncsma));
Serverapps.start (Seconds (1.0));
Serverapps.stop (Seconds (10.0));
Udpechoclienthelper echoclient (csmainterfaces.getaddress (Ncsma), 9);
Echoclient.setattribute ("Maxpackets", Uintegervalue (1));
Echoclient.setattribute ("Interval", TimeValue (Seconds (1.0)));
Echoclient.setattribute ("PacketSize", Uintegervalue (1024));
Install the client service on the first node of the peer-to segment
Applicationcontainer ClientApps = Echoclient.install (p2pnodes.get (0));
Clientapps.start (Seconds (2.0));
Clientapps.stop (Seconds (10.0));
/********************** Application Partial End *********************/
/**************** calls Global routing helper to help establish network routing *******************/
The global Routing Manager establishes a routing table for each node based on the link advertisements generated by the node
Ipv4globalroutinghelper::P opulateroutingtables ();
/**************** turn on pcap tracking *******************/
Turn on Pcap for P2phelper class objects
Pointtopoint.enablepcapall ("second");
Turn on Pcap for Csmahelper class objects
Sniff,true turn on promiscuous mode using the second node of the CSMA segment
Csma. Enablepcap ("Second", Csmadevices.get (1), true);
Simulator::run ();
Simulator::D Estroy ();
return 0;
}
-------------------------------------------------------------------------------------------------------
Compile and run with the following results:
Here are a few things to keep in mind:
1. The Csmachannel channel simulates a medium used in a subnet that can implement a carrier-listening multi-access communication network. This channel has a similar function to Ethernet.
2. The Netdevice class provides various methods for managing connections to other nodes and channel objects, and allows developers to customize them in an object-oriented way. In this example, Csmanetdevice and Pointtopointnetdevice are used. The Csmanetdevice is designed to work in the CSMA channel, while Pointtopointnetdevice works in the Pointtopoint channel. In the future, I will meet Wifinetnevice, which is working in the WiFi channel.
3, using the global routing manager, according to the link generated by the node to establish a routing table for each node
4. Open Network Sniffer
NS3 Example Analysis (3)--second.cc