author | Mathieu Lacage <mathieu.lacage@sophia.inria.fr> |
Wed, 01 Aug 2007 10:29:40 +0200 | |
changeset 1193 | ea2185e4e097 |
parent 1190 | examples/csma-cd-one-subnet.cc@ee1f9e701753 |
child 1195 | 53f1175dbe94 |
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 |
|
29 |
// - Tracing of queues and packet receptions to file "csma-cd-one-subnet.tr" |
|
30 |
||
31 |
#include <iostream> |
|
32 |
#include <fstream> |
|
33 |
#include <string> |
|
34 |
#include <cassert> |
|
35 |
||
36 |
#include "ns3/command-line.h" |
|
37 |
#include "ns3/default-value.h" |
|
38 |
#include "ns3/ptr.h" |
|
39 |
#include "ns3/random-variable.h" |
|
40 |
#include "ns3/debug.h" |
|
41 |
||
42 |
#include "ns3/simulator.h" |
|
43 |
#include "ns3/nstime.h" |
|
44 |
#include "ns3/data-rate.h" |
|
45 |
||
46 |
#include "ns3/ascii-trace.h" |
|
47 |
#include "ns3/pcap-trace.h" |
|
48 |
#include "ns3/internet-node.h" |
|
49 |
#include "ns3/csma-cd-channel.h" |
|
50 |
#include "ns3/csma-cd-net-device.h" |
|
1167 | 51 |
#include "ns3/eui48-address.h" |
1193
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
52 |
#include "ns3/packet-socket-address.h" |
987 | 53 |
#include "ns3/socket.h" |
54 |
#include "ns3/onoff-application.h" |
|
1193
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
55 |
#include "ns3/queue.h" |
987 | 56 |
|
57 |
using namespace ns3; |
|
58 |
||
1193
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
59 |
static Ptr<CsmaCdNetDevice> |
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
60 |
CreateCsmaCdDevice (Ptr<Node> node, Ptr<CsmaCdChannel> channel) |
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
61 |
{ |
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
62 |
Ptr<CsmaCdNetDevice> device = Create<CsmaCdNetDevice> (node); |
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
63 |
device->Attach (channel); |
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
64 |
Ptr<Queue> queue = Queue::CreateDefault (); |
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
65 |
device->AddQueue (queue); |
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
66 |
return device; |
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
67 |
} |
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
68 |
|
987 | 69 |
|
70 |
int main (int argc, char *argv[]) |
|
71 |
{ |
|
72 |
CommandLine::Parse (argc, argv); |
|
73 |
||
74 |
// Here, we will explicitly create four nodes. In more sophisticated |
|
75 |
// topologies, we could configure a node factory. |
|
1193
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
76 |
Ptr<Node> n0 = Create<Node> (); |
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
77 |
Ptr<Node> n1 = Create<Node> (); |
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
78 |
Ptr<Node> n2 = Create<Node> (); |
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
79 |
Ptr<Node> n3 = Create<Node> (); |
987 | 80 |
|
1193
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
81 |
// create the shared medium used by all csma/cd devices. |
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
82 |
Ptr<CsmaCdChannel> channel = Create<CsmaCdChannel> (DataRate(5000000), MilliSeconds(2)); |
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
83 |
|
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
84 |
// use a helper function to connect our nodes to the shared channel. |
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
85 |
Ptr<NetDevice> n0If = CreateCsmaCdDevice (n0, channel); |
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
86 |
Ptr<NetDevice> n1If = CreateCsmaCdDevice (n1, channel); |
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
87 |
Ptr<NetDevice> n2If = CreateCsmaCdDevice (n2, channel); |
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
88 |
Ptr<NetDevice> n3If = CreateCsmaCdDevice (n3, channel); |
987 | 89 |
|
1193
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
90 |
|
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
91 |
// create the address which identifies n1 from n0 |
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
92 |
PacketSocketAddress n0ToN1; |
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
93 |
n0ToN1.SetDevice (n0If->GetIfIndex ()); // set outgoing interface for outgoing packets |
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
94 |
n0ToN1.SetPhysicalAddress (n1If->GetAddress ()); // set destination address for outgoing packets |
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
95 |
n0ToN1.SetProtocol (2); // set arbitrary protocol for outgoing packets |
987 | 96 |
|
1193
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
97 |
// create the address which identifies n0 from n3 |
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
98 |
PacketSocketAddress n3ToN0; |
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
99 |
n3ToN0.SetDevice (n3If->GetIfIndex ()); |
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
100 |
n3ToN0.SetPhysicalAddress (n0If->GetAddress ()); |
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
101 |
n3ToN0.SetProtocol (3); |
987 | 102 |
|
1193
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
103 |
// Create the OnOff application to send raw datagrams of size |
987 | 104 |
// 210 bytes at a rate of 448 Kb/s |
105 |
// from n0 to n1 |
|
106 |
Ptr<OnOffApplication> ooff = Create<OnOffApplication> ( |
|
107 |
n0, |
|
1193
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
108 |
n0ToN1.ConvertTo (), |
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
109 |
"Packet", |
987 | 110 |
ConstantVariable(1), |
111 |
ConstantVariable(0)); |
|
112 |
// Start the application |
|
113 |
ooff->Start(Seconds(1.0)); |
|
114 |
ooff->Stop (Seconds(10.0)); |
|
115 |
||
116 |
// Create a similar flow from n3 to n0, starting at time 1.1 seconds |
|
117 |
ooff = Create<OnOffApplication> ( |
|
118 |
n3, |
|
1193
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
119 |
n3ToN0.ConvertTo (), |
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
120 |
"Packet", |
987 | 121 |
ConstantVariable(1), |
122 |
ConstantVariable(0)); |
|
123 |
// Start the application |
|
124 |
ooff->Start(Seconds(1.1)); |
|
125 |
ooff->Stop (Seconds(10.0)); |
|
126 |
||
127 |
// Configure tracing of all enqueue, dequeue, and NetDevice receive events |
|
1193
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
128 |
// Trace output will be sent to the csma-cd-packet-socket.tr file |
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
129 |
AsciiTrace asciitrace ("csma-cd-packet-socket.tr"); |
991 | 130 |
asciitrace.TraceAllNetDeviceRx (); |
131 |
asciitrace.TraceAllQueues (); |
|
132 |
||
987 | 133 |
Simulator::Run (); |
134 |
||
135 |
Simulator::Destroy (); |
|
136 |
} |