1529
|
1 |
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
|
|
2 |
/*
|
|
3 |
* This program is free software; you can redistribute it and/or modify
|
|
4 |
* it under the terms of the GNU General Public License version 2 as
|
|
5 |
* published by the Free Software Foundation;
|
|
6 |
*
|
|
7 |
* This program is distributed in the hope that it will be useful,
|
|
8 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
9 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
10 |
* GNU General Public License for more details.
|
|
11 |
*
|
|
12 |
* You should have received a copy of the GNU General Public License
|
|
13 |
* along with this program; if not, write to the Free Software
|
|
14 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
15 |
*/
|
|
16 |
|
|
17 |
#include "ns3/log.h"
|
|
18 |
#include "ns3/ptr.h"
|
|
19 |
#include "ns3/internet-node.h"
|
|
20 |
#include "ns3/point-to-point-channel.h"
|
|
21 |
#include "ns3/csma-channel.h"
|
|
22 |
#include "ns3/mac48-address.h"
|
|
23 |
#include "ns3/point-to-point-net-device.h"
|
|
24 |
#include "ns3/csma-net-device.h"
|
|
25 |
#include "ns3/point-to-point-topology.h"
|
|
26 |
#include "ns3/csma-topology.h"
|
|
27 |
#include "ns3/csma-ipv4-topology.h"
|
|
28 |
#include "ns3/udp-echo-client.h"
|
|
29 |
#include "ns3/udp-echo-server.h"
|
|
30 |
#include "ns3/simulator.h"
|
|
31 |
#include "ns3/nstime.h"
|
|
32 |
#include "ns3/ascii-trace.h"
|
|
33 |
#include "ns3/pcap-trace.h"
|
|
34 |
#include "ns3/global-route-manager.h"
|
|
35 |
|
|
36 |
NS_LOG_COMPONENT_DEFINE ("UdpEchoSimulation");
|
|
37 |
|
|
38 |
using namespace ns3;
|
|
39 |
|
|
40 |
// Network topology
|
|
41 |
//
|
|
42 |
// point to point
|
|
43 |
// +--------------+
|
|
44 |
// | |
|
|
45 |
// n0 n1 n2 n3 n4 n5 n6 n7
|
|
46 |
// | | | | | | | |
|
|
47 |
// ================ ================
|
|
48 |
// lan0 lan1
|
|
49 |
//
|
|
50 |
int
|
|
51 |
main (int argc, char *argv[])
|
|
52 |
{
|
|
53 |
LogComponentEnable ("UdpEchoSimulation", LOG_LEVEL_INFO);
|
|
54 |
|
|
55 |
NS_LOG_INFO ("UDP Echo Simulation");
|
|
56 |
|
|
57 |
Ptr<Node> n0 = Create<InternetNode> ();
|
|
58 |
Ptr<Node> n1 = Create<InternetNode> ();
|
|
59 |
Ptr<Node> n2 = Create<InternetNode> ();
|
|
60 |
Ptr<Node> n3 = Create<InternetNode> ();
|
|
61 |
|
|
62 |
Ptr<Node> n4 = Create<InternetNode> ();
|
|
63 |
Ptr<Node> n5 = Create<InternetNode> ();
|
|
64 |
Ptr<Node> n6 = Create<InternetNode> ();
|
|
65 |
Ptr<Node> n7 = Create<InternetNode> ();
|
|
66 |
|
|
67 |
Ptr<CsmaChannel> lan1 =
|
|
68 |
CsmaTopology::CreateCsmaChannel (DataRate (5000000), MilliSeconds (2));
|
|
69 |
|
|
70 |
uint32_t nd0 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n0, lan1,
|
|
71 |
"08:00:2e:00:00:00");
|
|
72 |
|
|
73 |
uint32_t nd1 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n1, lan1,
|
|
74 |
"08:00:2e:00:00:01");
|
|
75 |
|
|
76 |
uint32_t nd2 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n2, lan1,
|
|
77 |
"08:00:2e:00:00:02");
|
|
78 |
|
|
79 |
uint32_t nd3 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n3, lan1,
|
|
80 |
"08:00:2e:00:00:03");
|
|
81 |
|
|
82 |
CsmaIpv4Topology::AddIpv4Address (n0, nd0, "10.1.1.1", "255.255.255.0");
|
|
83 |
CsmaIpv4Topology::AddIpv4Address (n1, nd1, "10.1.1.2", "255.255.255.0");
|
|
84 |
CsmaIpv4Topology::AddIpv4Address (n2, nd2, "10.1.1.3", "255.255.255.0");
|
|
85 |
CsmaIpv4Topology::AddIpv4Address (n3, nd3, "10.1.1.4", "255.255.255.0");
|
|
86 |
|
|
87 |
Ptr<CsmaChannel> lan2 =
|
|
88 |
CsmaTopology::CreateCsmaChannel (DataRate (5000000), MilliSeconds (2));
|
|
89 |
|
|
90 |
uint32_t nd4 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n4, lan2,
|
|
91 |
"08:00:2e:00:00:04");
|
|
92 |
|
|
93 |
uint32_t nd5 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n5, lan2,
|
|
94 |
"08:00:2e:00:00:05");
|
|
95 |
|
|
96 |
uint32_t nd6 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n6, lan2,
|
|
97 |
"08:00:2e:00:00:06");
|
|
98 |
|
|
99 |
uint32_t nd7 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n7, lan2,
|
|
100 |
"08:00:2e:00:00:07");
|
|
101 |
|
|
102 |
CsmaIpv4Topology::AddIpv4Address (n4, nd4, "10.1.2.1", "255.255.255.0");
|
|
103 |
CsmaIpv4Topology::AddIpv4Address (n5, nd5, "10.1.2.2", "255.255.255.0");
|
|
104 |
CsmaIpv4Topology::AddIpv4Address (n6, nd6, "10.1.2.3", "255.255.255.0");
|
|
105 |
CsmaIpv4Topology::AddIpv4Address (n7, nd7, "10.1.2.4", "255.255.255.0");
|
|
106 |
|
|
107 |
Ptr<PointToPointChannel> link = PointToPointTopology::AddPointToPointLink (
|
|
108 |
n3, n4, DataRate (500000), MilliSeconds (20));
|
|
109 |
|
|
110 |
PointToPointTopology::AddIpv4Addresses (link, n3, "10.1.3.1",
|
|
111 |
n4, "10.1.3.2");
|
|
112 |
|
|
113 |
uint16_t port = 7;
|
|
114 |
|
|
115 |
Ptr<UdpEchoClient> client = Create<UdpEchoClient> (n0, "10.1.2.4", port,
|
|
116 |
1, Seconds(1.), 1024);
|
|
117 |
|
|
118 |
Ptr<UdpEchoServer> server = Create<UdpEchoServer> (n7, port);
|
|
119 |
|
|
120 |
server->Start(Seconds(1.));
|
|
121 |
client->Start(Seconds(2.));
|
|
122 |
|
|
123 |
server->Stop (Seconds(10.));
|
|
124 |
client->Stop (Seconds(10.));
|
|
125 |
|
|
126 |
AsciiTrace asciitrace ("tutorial-4.tr");
|
|
127 |
asciitrace.TraceAllQueues ();
|
|
128 |
asciitrace.TraceAllNetDeviceRx ();
|
|
129 |
|
|
130 |
PcapTrace pcaptrace ("tutorial-4.pcap");
|
|
131 |
pcaptrace.TraceAllIp ();
|
|
132 |
|
|
133 |
GlobalRouteManager::PopulateRoutingTables ();
|
|
134 |
|
|
135 |
Simulator::Run ();
|
|
136 |
Simulator::Destroy ();
|
|
137 |
}
|