author | Gustavo J. A. M. Carneiro <gjc@inescporto.pt> |
Wed, 03 Sep 2008 18:01:40 +0100 | |
changeset 3592 | 967bf4132c6f |
parent 3568 | 01bebf28addd |
child 4218 | debf1a8a96d3 |
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" |
2890
172baa5960ff
rework app constructors
Craig Dowell <craigdo@ee.washington.edu>
parents:
2887
diff
changeset
|
38 |
#include "ns3/node-module.h" |
2739
5234783968ff
convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2602
diff
changeset
|
39 |
#include "ns3/helper-module.h" |
987 | 40 |
|
41 |
using namespace ns3; |
|
42 |
||
1504
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
43 |
NS_LOG_COMPONENT_DEFINE ("CsmaPacketSocketExample"); |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
44 |
|
3568
01bebf28addd
bug 299: packet socket broken by promiscuous update
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3381
diff
changeset
|
45 |
std::ofstream g_os; |
01bebf28addd
bug 299: packet socket broken by promiscuous update
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3381
diff
changeset
|
46 |
|
01bebf28addd
bug 299: packet socket broken by promiscuous update
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3381
diff
changeset
|
47 |
static void |
01bebf28addd
bug 299: packet socket broken by promiscuous update
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3381
diff
changeset
|
48 |
SinkRx (std::string path, Ptr<const Packet> p, const Address &address) |
01bebf28addd
bug 299: packet socket broken by promiscuous update
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3381
diff
changeset
|
49 |
{ |
01bebf28addd
bug 299: packet socket broken by promiscuous update
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3381
diff
changeset
|
50 |
g_os << p->GetSize () << std::endl; |
01bebf28addd
bug 299: packet socket broken by promiscuous update
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3381
diff
changeset
|
51 |
} |
01bebf28addd
bug 299: packet socket broken by promiscuous update
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3381
diff
changeset
|
52 |
|
1504
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
53 |
int |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
54 |
main (int argc, char *argv[]) |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
55 |
{ |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
56 |
#if 0 |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
57 |
LogComponentEnable ("CsmaPacketSocketExample", LOG_LEVEL_INFO); |
2860
9105a5cf6535
use global seed in examples, new regression tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
2739
diff
changeset
|
58 |
#endif |
987 | 59 |
|
2860
9105a5cf6535
use global seed in examples, new regression tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
2739
diff
changeset
|
60 |
// |
9105a5cf6535
use global seed in examples, new regression tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
2739
diff
changeset
|
61 |
// Make the random number generators generate reproducible results. |
9105a5cf6535
use global seed in examples, new regression tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
2739
diff
changeset
|
62 |
// |
9105a5cf6535
use global seed in examples, new regression tests
Craig Dowell <craigdo@ee.washington.edu>
parents:
2739
diff
changeset
|
63 |
RandomVariable::UseGlobalSeed (1, 1, 2, 3, 5, 8); |
1504
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
64 |
|
2575
1aae382e65e2
rewrite CommandLine to not handle DefaultValues anymore.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2533
diff
changeset
|
65 |
CommandLine cmd; |
1aae382e65e2
rewrite CommandLine to not handle DefaultValues anymore.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2533
diff
changeset
|
66 |
cmd.Parse (argc, argv); |
987 | 67 |
|
3568
01bebf28addd
bug 299: packet socket broken by promiscuous update
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3381
diff
changeset
|
68 |
g_os.open ("csma-packet-socket-sink.tr"); |
01bebf28addd
bug 299: packet socket broken by promiscuous update
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3381
diff
changeset
|
69 |
|
2739
5234783968ff
convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2602
diff
changeset
|
70 |
// Here, we will explicitly create four nodes. |
1504
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
71 |
NS_LOG_INFO ("Create nodes."); |
3568
01bebf28addd
bug 299: packet socket broken by promiscuous update
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3381
diff
changeset
|
72 |
NodeContainer nodes; |
01bebf28addd
bug 299: packet socket broken by promiscuous update
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3381
diff
changeset
|
73 |
nodes.Create (4); |
2739
5234783968ff
convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2602
diff
changeset
|
74 |
|
5234783968ff
convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2602
diff
changeset
|
75 |
PacketSocketHelper packetSocket; |
3568
01bebf28addd
bug 299: packet socket broken by promiscuous update
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3381
diff
changeset
|
76 |
packetSocket.Install (nodes); |
987 | 77 |
|
1272 | 78 |
// 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
|
79 |
NS_LOG_INFO ("Create channels."); |
3257
ba198dad54a2
CSMA Cleanup, XXX in echo apps, clarification of point-to-point data rates in dox
Craig Dowell <craigdo@ee.washington.edu>
parents:
3026
diff
changeset
|
80 |
Ptr<CsmaChannel> channel = CreateObject<CsmaChannel> ( |
ba198dad54a2
CSMA Cleanup, XXX in echo apps, clarification of point-to-point data rates in dox
Craig Dowell <craigdo@ee.washington.edu>
parents:
3026
diff
changeset
|
81 |
"DataRate", DataRateValue (DataRate(5000000)), |
ba198dad54a2
CSMA Cleanup, XXX in echo apps, clarification of point-to-point data rates in dox
Craig Dowell <craigdo@ee.washington.edu>
parents:
3026
diff
changeset
|
82 |
"Delay", TimeValue (MilliSeconds(2))); |
1193
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. |
1504
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
85 |
NS_LOG_INFO ("Build Topology."); |
2739
5234783968ff
convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2602
diff
changeset
|
86 |
CsmaHelper csma; |
3381
3cdd9d60f7c7
bug 232, references to Parameter obsolete
Craig Dowell <craigdo@ee.washington.edu>
parents:
3257
diff
changeset
|
87 |
csma.SetDeviceAttribute ("EncapsulationMode", StringValue ("Llc")); |
3568
01bebf28addd
bug 299: packet socket broken by promiscuous update
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3381
diff
changeset
|
88 |
NetDeviceContainer devs = csma.Install (nodes, channel); |
987 | 89 |
|
2739
5234783968ff
convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2602
diff
changeset
|
90 |
NS_LOG_INFO ("Create Applications."); |
5234783968ff
convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2602
diff
changeset
|
91 |
// Create the OnOff application to send raw datagrams |
2890
172baa5960ff
rework app constructors
Craig Dowell <craigdo@ee.washington.edu>
parents:
2887
diff
changeset
|
92 |
PacketSocketAddress socket; |
172baa5960ff
rework app constructors
Craig Dowell <craigdo@ee.washington.edu>
parents:
2887
diff
changeset
|
93 |
socket.SetSingleDevice(devs.Get (0)->GetIfIndex ()); |
172baa5960ff
rework app constructors
Craig Dowell <craigdo@ee.washington.edu>
parents:
2887
diff
changeset
|
94 |
socket.SetPhysicalAddress (devs.Get (1)->GetAddress ()); |
172baa5960ff
rework app constructors
Craig Dowell <craigdo@ee.washington.edu>
parents:
2887
diff
changeset
|
95 |
socket.SetProtocol (2); |
172baa5960ff
rework app constructors
Craig Dowell <craigdo@ee.washington.edu>
parents:
2887
diff
changeset
|
96 |
OnOffHelper onoff ("ns3::PacketSocketFactory", Address (socket)); |
2965
4b28e9740e3b
get rid of Attribute class. Use AttributeValue subclasses directly.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2890
diff
changeset
|
97 |
onoff.SetAttribute ("OnTime", RandomVariableValue (ConstantVariable (1.0))); |
4b28e9740e3b
get rid of Attribute class. Use AttributeValue subclasses directly.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2890
diff
changeset
|
98 |
onoff.SetAttribute ("OffTime", RandomVariableValue (ConstantVariable (0.0))); |
3568
01bebf28addd
bug 299: packet socket broken by promiscuous update
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3381
diff
changeset
|
99 |
ApplicationContainer apps = onoff.Install (nodes.Get (0)); |
2739
5234783968ff
convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2602
diff
changeset
|
100 |
apps.Start (Seconds (1.0)); |
5234783968ff
convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2602
diff
changeset
|
101 |
apps.Stop (Seconds (10.0)); |
3568
01bebf28addd
bug 299: packet socket broken by promiscuous update
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3381
diff
changeset
|
102 |
|
2890
172baa5960ff
rework app constructors
Craig Dowell <craigdo@ee.washington.edu>
parents:
2887
diff
changeset
|
103 |
socket.SetSingleDevice (devs.Get (3)->GetIfIndex ()); |
172baa5960ff
rework app constructors
Craig Dowell <craigdo@ee.washington.edu>
parents:
2887
diff
changeset
|
104 |
socket.SetPhysicalAddress (devs.Get (0)->GetAddress ()); |
172baa5960ff
rework app constructors
Craig Dowell <craigdo@ee.washington.edu>
parents:
2887
diff
changeset
|
105 |
socket.SetProtocol (3); |
2965
4b28e9740e3b
get rid of Attribute class. Use AttributeValue subclasses directly.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2890
diff
changeset
|
106 |
onoff.SetAttribute ("Remote", AddressValue (socket)); |
4b28e9740e3b
get rid of Attribute class. Use AttributeValue subclasses directly.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2890
diff
changeset
|
107 |
onoff.SetAttribute ("OffTime", RandomVariableValue (ConstantVariable (0.0))); |
3568
01bebf28addd
bug 299: packet socket broken by promiscuous update
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3381
diff
changeset
|
108 |
apps = onoff.Install (nodes.Get (3)); |
2739
5234783968ff
convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2602
diff
changeset
|
109 |
apps.Start (Seconds (1.0)); |
5234783968ff
convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2602
diff
changeset
|
110 |
apps.Stop (Seconds (10.0)); |
3568
01bebf28addd
bug 299: packet socket broken by promiscuous update
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3381
diff
changeset
|
111 |
|
01bebf28addd
bug 299: packet socket broken by promiscuous update
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3381
diff
changeset
|
112 |
PacketSinkHelper sink = PacketSinkHelper ("ns3::PacketSocketFactory", |
01bebf28addd
bug 299: packet socket broken by promiscuous update
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3381
diff
changeset
|
113 |
socket); |
01bebf28addd
bug 299: packet socket broken by promiscuous update
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3381
diff
changeset
|
114 |
apps = sink.Install (nodes.Get (0)); |
01bebf28addd
bug 299: packet socket broken by promiscuous update
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3381
diff
changeset
|
115 |
apps.Start (Seconds (0.0)); |
01bebf28addd
bug 299: packet socket broken by promiscuous update
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3381
diff
changeset
|
116 |
apps.Stop (Seconds (20.0)); |
01bebf28addd
bug 299: packet socket broken by promiscuous update
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3381
diff
changeset
|
117 |
|
01bebf28addd
bug 299: packet socket broken by promiscuous update
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3381
diff
changeset
|
118 |
Config::Connect ("/NodeList/*/ApplicationList/*/$ns3::PacketSink/Rx", |
01bebf28addd
bug 299: packet socket broken by promiscuous update
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3381
diff
changeset
|
119 |
MakeCallback (&SinkRx)); |
987 | 120 |
|
121 |
// Configure tracing of all enqueue, dequeue, and NetDevice receive events |
|
1272 | 122 |
// 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
|
123 |
NS_LOG_INFO ("Configure Tracing."); |
3026
4ef834f6b67d
remove dead code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2965
diff
changeset
|
124 |
std::ofstream os; |
4ef834f6b67d
remove dead code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2965
diff
changeset
|
125 |
os.open ("csma-packet-socket.tr"); |
4ef834f6b67d
remove dead code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2965
diff
changeset
|
126 |
csma.EnableAsciiAll (os); |
991 | 127 |
|
1504
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
128 |
NS_LOG_INFO ("Run Simulation."); |
987 | 129 |
Simulator::Run (); |
130 |
Simulator::Destroy (); |
|
1504
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
131 |
NS_LOG_INFO ("Done."); |
3568
01bebf28addd
bug 299: packet socket broken by promiscuous update
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3381
diff
changeset
|
132 |
|
01bebf28addd
bug 299: packet socket broken by promiscuous update
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3381
diff
changeset
|
133 |
g_os.close (); |
01bebf28addd
bug 299: packet socket broken by promiscuous update
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3381
diff
changeset
|
134 |
|
01bebf28addd
bug 299: packet socket broken by promiscuous update
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3381
diff
changeset
|
135 |
return 0; |
987 | 136 |
} |