author | Mathieu Lacage <mathieu.lacage@sophia.inria.fr> |
Tue, 25 Mar 2008 14:16:40 -0700 | |
changeset 2739 | 5234783968ff |
parent 2602 | d9262bff6df2 |
child 2860 | 9105a5cf6535 |
permissions | -rw-r--r-- |
987 | 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 |
// Port of ns-2/tcl/ex/simple.tcl to ns-3 |
|
18 |
// |
|
19 |
// Network topology |
|
20 |
// |
|
21 |
// n0 n1 n2 n3 |
|
22 |
// | | | | |
|
23 |
// ===================== |
|
24 |
// |
|
25 |
// - CBR/UDP flows from n0 to n1, and from n3 to n0 |
|
26 |
// - UDP packet size of 210 bytes, with per-packet interval 0.00375 sec. |
|
27 |
// (i.e., DataRate of 448,000 bps) |
|
28 |
// - DropTail queues |
|
1272 | 29 |
// - Tracing of queues and packet receptions to file "csma-one-subnet.tr" |
987 | 30 |
|
31 |
#include <iostream> |
|
32 |
#include <fstream> |
|
33 |
#include <string> |
|
34 |
#include <cassert> |
|
35 |
||
2739
5234783968ff
convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2602
diff
changeset
|
36 |
#include "ns3/core-module.h" |
5234783968ff
convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2602
diff
changeset
|
37 |
#include "ns3/simulator-module.h" |
5234783968ff
convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2602
diff
changeset
|
38 |
#include "ns3/helper-module.h" |
987 | 39 |
|
40 |
#include "ns3/ascii-trace.h" |
|
41 |
#include "ns3/pcap-trace.h" |
|
42 |
||
43 |
using namespace ns3; |
|
44 |
||
1504
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
45 |
NS_LOG_COMPONENT_DEFINE ("CsmaPacketSocketExample"); |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
46 |
|
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
47 |
int |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
48 |
main (int argc, char *argv[]) |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
49 |
{ |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
50 |
#if 0 |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
51 |
LogComponentEnable ("CsmaPacketSocketExample", LOG_LEVEL_INFO); |
987 | 52 |
|
1504
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
53 |
LogComponentEnable("Object", LOG_LEVEL_ALL); |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
54 |
LogComponentEnable("Queue", LOG_LEVEL_ALL); |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
55 |
LogComponentEnable("DropTailQueue", LOG_LEVEL_ALL); |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
56 |
LogComponentEnable("Channel", LOG_LEVEL_ALL); |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
57 |
LogComponentEnable("CsmaChannel", LOG_LEVEL_ALL); |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
58 |
LogComponentEnable("NetDevice", LOG_LEVEL_ALL); |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
59 |
LogComponentEnable("CsmaNetDevice", LOG_LEVEL_ALL); |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
60 |
LogComponentEnable("Ipv4L3Protocol", LOG_LEVEL_ALL); |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
61 |
LogComponentEnable("PacketSocket", LOG_LEVEL_ALL); |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
62 |
LogComponentEnable("Socket", LOG_LEVEL_ALL); |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
63 |
LogComponentEnable("UdpSocket", LOG_LEVEL_ALL); |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
64 |
LogComponentEnable("UdpL4Protocol", LOG_LEVEL_ALL); |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
65 |
LogComponentEnable("Ipv4L3Protocol", LOG_LEVEL_ALL); |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
66 |
LogComponentEnable("Ipv4StaticRouting", LOG_LEVEL_ALL); |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
67 |
LogComponentEnable("Ipv4Interface", LOG_LEVEL_ALL); |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
68 |
LogComponentEnable("ArpIpv4Interface", LOG_LEVEL_ALL); |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
69 |
LogComponentEnable("Ipv4LoopbackInterface", LOG_LEVEL_ALL); |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
70 |
LogComponentEnable("OnOffApplication", LOG_LEVEL_ALL); |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
71 |
LogComponentEnable("PacketSinkApplication", LOG_LEVEL_ALL); |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
72 |
LogComponentEnable("UdpEchoClientApplication", LOG_LEVEL_ALL); |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
73 |
LogComponentEnable("UdpEchoServerApplication", LOG_LEVEL_ALL); |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
74 |
#endif |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
75 |
|
2575
1aae382e65e2
rewrite CommandLine to not handle DefaultValues anymore.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2533
diff
changeset
|
76 |
CommandLine cmd; |
1aae382e65e2
rewrite CommandLine to not handle DefaultValues anymore.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2533
diff
changeset
|
77 |
cmd.Parse (argc, argv); |
987 | 78 |
|
2739
5234783968ff
convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2602
diff
changeset
|
79 |
// Here, we will explicitly create four nodes. |
1504
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
80 |
NS_LOG_INFO ("Create nodes."); |
2739
5234783968ff
convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2602
diff
changeset
|
81 |
NodeContainer c; |
5234783968ff
convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2602
diff
changeset
|
82 |
c.Create (4); |
5234783968ff
convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2602
diff
changeset
|
83 |
|
5234783968ff
convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2602
diff
changeset
|
84 |
PacketSocketHelper packetSocket; |
5234783968ff
convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2602
diff
changeset
|
85 |
packetSocket.Build (c); |
987 | 86 |
|
1272 | 87 |
// create the shared medium used by all csma devices. |
1504
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
88 |
NS_LOG_INFO ("Create channels."); |
2592
3ebf97150166
get rid of CreateObjectWith
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2577
diff
changeset
|
89 |
Ptr<CsmaChannel> channel = CreateObject<CsmaChannel> ("BitRate", DataRate(5000000), |
3ebf97150166
get rid of CreateObjectWith
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2577
diff
changeset
|
90 |
"Delay", MilliSeconds(2)); |
1193
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
91 |
|
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
92 |
// use a helper function to connect our nodes to the shared channel. |
1504
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
93 |
NS_LOG_INFO ("Build Topology."); |
2739
5234783968ff
convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2602
diff
changeset
|
94 |
CsmaHelper csma; |
5234783968ff
convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2602
diff
changeset
|
95 |
csma.SetDeviceParameter ("EncapsulationMode", String ("Llc")); |
5234783968ff
convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2602
diff
changeset
|
96 |
NetDeviceContainer devs = csma.Build (c, channel); |
987 | 97 |
|
2739
5234783968ff
convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2602
diff
changeset
|
98 |
NS_LOG_INFO ("Create Applications."); |
5234783968ff
convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2602
diff
changeset
|
99 |
// Create the OnOff application to send raw datagrams |
5234783968ff
convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2602
diff
changeset
|
100 |
OnOffHelper onoff; |
5234783968ff
convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2602
diff
changeset
|
101 |
onoff.SetAppAttribute ("OnTime", ConstantVariable (1.0)); |
5234783968ff
convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2602
diff
changeset
|
102 |
onoff.SetAppAttribute ("OffTime", ConstantVariable (0.0)); |
5234783968ff
convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2602
diff
changeset
|
103 |
onoff.SetPacketRemote (devs.Get (0), devs.Get (1)->GetAddress (), 2); |
5234783968ff
convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2602
diff
changeset
|
104 |
ApplicationContainer apps = onoff.Build (c.Get (0)); |
5234783968ff
convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2602
diff
changeset
|
105 |
apps.Start (Seconds (1.0)); |
5234783968ff
convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2602
diff
changeset
|
106 |
apps.Stop (Seconds (10.0)); |
987 | 107 |
|
108 |
||
2739
5234783968ff
convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2602
diff
changeset
|
109 |
onoff.SetPacketRemote (devs.Get (3), devs.Get (0)->GetAddress (), 3); |
5234783968ff
convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2602
diff
changeset
|
110 |
apps = onoff.Build (c.Get (3)); |
5234783968ff
convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2602
diff
changeset
|
111 |
apps.Start (Seconds (1.0)); |
5234783968ff
convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2602
diff
changeset
|
112 |
apps.Stop (Seconds (10.0)); |
987 | 113 |
|
114 |
// Configure tracing of all enqueue, dequeue, and NetDevice receive events |
|
1272 | 115 |
// Trace output will be sent to the csma-packet-socket.tr file |
1504
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
116 |
NS_LOG_INFO ("Configure Tracing."); |
1272 | 117 |
AsciiTrace asciitrace ("csma-packet-socket.tr"); |
991 | 118 |
asciitrace.TraceAllNetDeviceRx (); |
119 |
asciitrace.TraceAllQueues (); |
|
120 |
||
1504
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
121 |
NS_LOG_INFO ("Run Simulation."); |
987 | 122 |
Simulator::Run (); |
123 |
Simulator::Destroy (); |
|
1504
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
124 |
NS_LOG_INFO ("Done."); |
987 | 125 |
} |