author | Tom Henderson <tomh@tomh.org> |
Fri, 20 Aug 2010 12:17:19 -0700 | |
changeset 6589 | 9c325569fb01 |
parent 6047 | 09841960107d |
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 |
// |
|
18 |
// Network topology |
|
19 |
// |
|
20 |
// n0 n1 n2 n3 |
|
21 |
// | | | | |
|
22 |
// ===================== |
|
23 |
// |
|
5988
80584c404aba
Clean up some stale comments in examples
Tom Henderson <tomh@tomh.org>
parents:
5369
diff
changeset
|
24 |
// - Packet socket flow from n0 to n3 |
80584c404aba
Clean up some stale comments in examples
Tom Henderson <tomh@tomh.org>
parents:
5369
diff
changeset
|
25 |
// - Default 512 byte packets generated by traffic generator |
80584c404aba
Clean up some stale comments in examples
Tom Henderson <tomh@tomh.org>
parents:
5369
diff
changeset
|
26 |
// Trace output will be sent to the csma-packet-socket.tr file |
987 | 27 |
|
28 |
#include <iostream> |
|
29 |
#include <fstream> |
|
30 |
#include <string> |
|
31 |
#include <cassert> |
|
32 |
||
2739
5234783968ff
convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2602
diff
changeset
|
33 |
#include "ns3/core-module.h" |
5234783968ff
convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2602
diff
changeset
|
34 |
#include "ns3/simulator-module.h" |
2890
172baa5960ff
rework app constructors
Craig Dowell <craigdo@ee.washington.edu>
parents:
2887
diff
changeset
|
35 |
#include "ns3/node-module.h" |
2739
5234783968ff
convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2602
diff
changeset
|
36 |
#include "ns3/helper-module.h" |
987 | 37 |
|
38 |
using namespace ns3; |
|
39 |
||
1504
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
40 |
NS_LOG_COMPONENT_DEFINE ("CsmaPacketSocketExample"); |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
41 |
|
3568
01bebf28addd
bug 299: packet socket broken by promiscuous update
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3381
diff
changeset
|
42 |
std::ofstream g_os; |
01bebf28addd
bug 299: packet socket broken by promiscuous update
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3381
diff
changeset
|
43 |
|
01bebf28addd
bug 299: packet socket broken by promiscuous update
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3381
diff
changeset
|
44 |
static void |
01bebf28addd
bug 299: packet socket broken by promiscuous update
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3381
diff
changeset
|
45 |
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
|
46 |
{ |
01bebf28addd
bug 299: packet socket broken by promiscuous update
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3381
diff
changeset
|
47 |
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
|
48 |
} |
01bebf28addd
bug 299: packet socket broken by promiscuous update
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3381
diff
changeset
|
49 |
|
1504
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
50 |
int |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
51 |
main (int argc, char *argv[]) |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
52 |
{ |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
53 |
#if 0 |
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
54 |
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
|
55 |
#endif |
987 | 56 |
|
2575
1aae382e65e2
rewrite CommandLine to not handle DefaultValues anymore.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2533
diff
changeset
|
57 |
CommandLine cmd; |
1aae382e65e2
rewrite CommandLine to not handle DefaultValues anymore.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2533
diff
changeset
|
58 |
cmd.Parse (argc, argv); |
987 | 59 |
|
4435
54cb46af8003
pass regression tests on mingw
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
4225
diff
changeset
|
60 |
g_os.open ("csma-packet-socket-sink.tr",std::ios_base::binary | std::ios_base::out); |
3568
01bebf28addd
bug 299: packet socket broken by promiscuous update
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3381
diff
changeset
|
61 |
|
2739
5234783968ff
convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2602
diff
changeset
|
62 |
// Here, we will explicitly create four nodes. |
1504
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
63 |
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
|
64 |
NodeContainer nodes; |
01bebf28addd
bug 299: packet socket broken by promiscuous update
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3381
diff
changeset
|
65 |
nodes.Create (4); |
2739
5234783968ff
convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2602
diff
changeset
|
66 |
|
5234783968ff
convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2602
diff
changeset
|
67 |
PacketSocketHelper packetSocket; |
3568
01bebf28addd
bug 299: packet socket broken by promiscuous update
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3381
diff
changeset
|
68 |
packetSocket.Install (nodes); |
987 | 69 |
|
1272 | 70 |
// 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
|
71 |
NS_LOG_INFO ("Create channels."); |
4554
b1940a738981
make CreateObject<> behave like Create<>: use positional constructor arguments rather than attribute lists.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
4435
diff
changeset
|
72 |
Ptr<CsmaChannel> channel = CreateObjectWithAttributes<CsmaChannel> ( |
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
|
73 |
"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
|
74 |
"Delay", TimeValue (MilliSeconds(2))); |
1193
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
75 |
|
ea2185e4e097
example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
1190
diff
changeset
|
76 |
// 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
|
77 |
NS_LOG_INFO ("Build Topology."); |
2739
5234783968ff
convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2602
diff
changeset
|
78 |
CsmaHelper csma; |
3381
3cdd9d60f7c7
bug 232, references to Parameter obsolete
Craig Dowell <craigdo@ee.washington.edu>
parents:
3257
diff
changeset
|
79 |
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
|
80 |
NetDeviceContainer devs = csma.Install (nodes, channel); |
987 | 81 |
|
2739
5234783968ff
convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2602
diff
changeset
|
82 |
NS_LOG_INFO ("Create Applications."); |
5234783968ff
convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2602
diff
changeset
|
83 |
// Create the OnOff application to send raw datagrams |
2890
172baa5960ff
rework app constructors
Craig Dowell <craigdo@ee.washington.edu>
parents:
2887
diff
changeset
|
84 |
PacketSocketAddress socket; |
172baa5960ff
rework app constructors
Craig Dowell <craigdo@ee.washington.edu>
parents:
2887
diff
changeset
|
85 |
socket.SetSingleDevice(devs.Get (0)->GetIfIndex ()); |
172baa5960ff
rework app constructors
Craig Dowell <craigdo@ee.washington.edu>
parents:
2887
diff
changeset
|
86 |
socket.SetPhysicalAddress (devs.Get (1)->GetAddress ()); |
172baa5960ff
rework app constructors
Craig Dowell <craigdo@ee.washington.edu>
parents:
2887
diff
changeset
|
87 |
socket.SetProtocol (2); |
172baa5960ff
rework app constructors
Craig Dowell <craigdo@ee.washington.edu>
parents:
2887
diff
changeset
|
88 |
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
|
89 |
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
|
90 |
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
|
91 |
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
|
92 |
apps.Start (Seconds (1.0)); |
5234783968ff
convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2602
diff
changeset
|
93 |
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
|
94 |
|
2890
172baa5960ff
rework app constructors
Craig Dowell <craigdo@ee.washington.edu>
parents:
2887
diff
changeset
|
95 |
socket.SetSingleDevice (devs.Get (3)->GetIfIndex ()); |
172baa5960ff
rework app constructors
Craig Dowell <craigdo@ee.washington.edu>
parents:
2887
diff
changeset
|
96 |
socket.SetPhysicalAddress (devs.Get (0)->GetAddress ()); |
172baa5960ff
rework app constructors
Craig Dowell <craigdo@ee.washington.edu>
parents:
2887
diff
changeset
|
97 |
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
|
98 |
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
|
99 |
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
|
100 |
apps = onoff.Install (nodes.Get (3)); |
2739
5234783968ff
convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2602
diff
changeset
|
101 |
apps.Start (Seconds (1.0)); |
5234783968ff
convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
2602
diff
changeset
|
102 |
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
|
103 |
|
01bebf28addd
bug 299: packet socket broken by promiscuous update
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3381
diff
changeset
|
104 |
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
|
105 |
socket); |
01bebf28addd
bug 299: packet socket broken by promiscuous update
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3381
diff
changeset
|
106 |
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
|
107 |
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
|
108 |
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
|
109 |
|
01bebf28addd
bug 299: packet socket broken by promiscuous update
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3381
diff
changeset
|
110 |
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
|
111 |
MakeCallback (&SinkRx)); |
987 | 112 |
|
113 |
// Configure tracing of all enqueue, dequeue, and NetDevice receive events |
|
1272 | 114 |
// 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
|
115 |
NS_LOG_INFO ("Configure Tracing."); |
6014 | 116 |
|
117 |
AsciiTraceHelper ascii; |
|
118 |
csma.EnableAsciiAll (ascii.CreateFileStream ("csma-packet-socket.tr")); |
|
991 | 119 |
|
1504
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
120 |
NS_LOG_INFO ("Run Simulation."); |
987 | 121 |
Simulator::Run (); |
122 |
Simulator::Destroy (); |
|
1504
36ecc970ba96
checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents:
1494
diff
changeset
|
123 |
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
|
124 |
|
01bebf28addd
bug 299: packet socket broken by promiscuous update
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3381
diff
changeset
|
125 |
g_os.close (); |
01bebf28addd
bug 299: packet socket broken by promiscuous update
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3381
diff
changeset
|
126 |
|
01bebf28addd
bug 299: packet socket broken by promiscuous update
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents:
3381
diff
changeset
|
127 |
return 0; |
987 | 128 |
} |