author | Tom Henderson <tomh@tomh.org> |
Fri, 20 Aug 2010 12:17:19 -0700 | |
changeset 6589 | 9c325569fb01 |
parent 5987 | 026548ddc1d0 |
permissions | -rw-r--r-- |
3821
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
1 |
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
2 |
/* |
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
3 |
* This program is free software; you can redistribute it and/or modify |
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
4 |
* it under the terms of the GNU General Public License version 2 as |
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
5 |
* published by the Free Software Foundation; |
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
6 |
* |
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
7 |
* This program is distributed in the hope that it will be useful, |
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
8 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
9 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
10 |
* GNU General Public License for more details. |
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
11 |
* |
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
12 |
* You should have received a copy of the GNU General Public License |
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
13 |
* along with this program; if not, write to the Free Software |
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
14 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
15 |
*/ |
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
16 |
|
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
17 |
// |
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
18 |
// Network topology |
5987
026548ddc1d0
fix comments in example; change some defaults
Tom Henderson <tomh@tomh.org>
parents:
5369
diff
changeset
|
19 |
// (sender) (receiver) |
3821
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
20 |
// n0 n1 n2 n3 |
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
21 |
// | | | | |
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
22 |
// ===================== |
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
23 |
// |
5987
026548ddc1d0
fix comments in example; change some defaults
Tom Henderson <tomh@tomh.org>
parents:
5369
diff
changeset
|
24 |
// Node n0 sends data to node n3 over a raw IP socket. The protocol |
026548ddc1d0
fix comments in example; change some defaults
Tom Henderson <tomh@tomh.org>
parents:
5369
diff
changeset
|
25 |
// number used is 2. |
026548ddc1d0
fix comments in example; change some defaults
Tom Henderson <tomh@tomh.org>
parents:
5369
diff
changeset
|
26 |
// The default traffic rate is to send 1250 bytes/second for 10 kb/s |
026548ddc1d0
fix comments in example; change some defaults
Tom Henderson <tomh@tomh.org>
parents:
5369
diff
changeset
|
27 |
// The data rate can be toggled by command line argument |
3821
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
28 |
|
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
29 |
#include <iostream> |
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
30 |
#include <fstream> |
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
31 |
#include <string> |
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
32 |
#include <cassert> |
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
33 |
|
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
34 |
#include "ns3/core-module.h" |
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
35 |
#include "ns3/simulator-module.h" |
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
36 |
#include "ns3/node-module.h" |
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
37 |
#include "ns3/helper-module.h" |
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
38 |
|
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
39 |
using namespace ns3; |
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
40 |
|
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
41 |
NS_LOG_COMPONENT_DEFINE ("CsmaPacketSocketExample"); |
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
42 |
|
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
43 |
static void SinkRx (Ptr<const Packet> p, const Address &ad) |
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
44 |
{ |
5987
026548ddc1d0
fix comments in example; change some defaults
Tom Henderson <tomh@tomh.org>
parents:
5369
diff
changeset
|
45 |
// Enable the below line to see the packet contents printed out at the |
026548ddc1d0
fix comments in example; change some defaults
Tom Henderson <tomh@tomh.org>
parents:
5369
diff
changeset
|
46 |
// receive sink |
026548ddc1d0
fix comments in example; change some defaults
Tom Henderson <tomh@tomh.org>
parents:
5369
diff
changeset
|
47 |
//std::cout << Simulator::Now().GetSeconds () << " " << *p << std::endl; |
3821
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
48 |
} |
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
49 |
|
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
50 |
int |
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
51 |
main (int argc, char *argv[]) |
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
52 |
{ |
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
53 |
#if 0 |
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
54 |
LogComponentEnable ("CsmaPacketSocketExample", LOG_LEVEL_INFO); |
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
55 |
#endif |
5987
026548ddc1d0
fix comments in example; change some defaults
Tom Henderson <tomh@tomh.org>
parents:
5369
diff
changeset
|
56 |
uint32_t dataRate = 10; |
3821
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
57 |
CommandLine cmd; |
5987
026548ddc1d0
fix comments in example; change some defaults
Tom Henderson <tomh@tomh.org>
parents:
5369
diff
changeset
|
58 |
cmd.AddValue ("dataRate", "application dataRate (Kb/s)", dataRate); |
3821
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
59 |
cmd.Parse (argc, argv); |
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
60 |
|
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
61 |
// Here, we will explicitly create four nodes. |
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
62 |
NS_LOG_INFO ("Create nodes."); |
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
63 |
NodeContainer c; |
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
64 |
c.Create (4); |
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
65 |
|
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
66 |
// connect all our nodes to a shared channel. |
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
67 |
NS_LOG_INFO ("Build Topology."); |
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
68 |
CsmaHelper csma; |
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
69 |
csma.SetChannelAttribute ("DataRate", DataRateValue (DataRate (5000000))); |
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
70 |
csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2))); |
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
71 |
csma.SetDeviceAttribute ("EncapsulationMode", StringValue ("Llc")); |
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
72 |
NetDeviceContainer devs = csma.Install (c); |
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
73 |
|
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
74 |
// add an ip stack to all nodes. |
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
75 |
NS_LOG_INFO ("Add ip stack."); |
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
76 |
InternetStackHelper ipStack; |
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
77 |
ipStack.Install (c); |
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
78 |
|
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
79 |
// assign ip addresses |
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
80 |
NS_LOG_INFO ("Assign ip addresses."); |
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
81 |
Ipv4AddressHelper ip; |
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
82 |
ip.SetBase ("192.168.1.0", "255.255.255.0"); |
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
83 |
Ipv4InterfaceContainer addresses = ip.Assign (devs); |
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
84 |
|
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
85 |
NS_LOG_INFO ("Create Source"); |
5987
026548ddc1d0
fix comments in example; change some defaults
Tom Henderson <tomh@tomh.org>
parents:
5369
diff
changeset
|
86 |
// IP protocol configuration |
3821
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
87 |
Config::SetDefault ("ns3::Ipv4RawSocketImpl::Protocol", StringValue ("2")); |
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
88 |
InetSocketAddress dst = InetSocketAddress (addresses.GetAddress (3)); |
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
89 |
OnOffHelper onoff = OnOffHelper ("ns3::Ipv4RawSocketFactory", dst); |
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
90 |
onoff.SetAttribute ("OnTime", RandomVariableValue (ConstantVariable (1.0))); |
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
91 |
onoff.SetAttribute ("OffTime", RandomVariableValue (ConstantVariable (0.0))); |
5987
026548ddc1d0
fix comments in example; change some defaults
Tom Henderson <tomh@tomh.org>
parents:
5369
diff
changeset
|
92 |
onoff.SetAttribute ("DataRate", DataRateValue (DataRate (dataRate*1000))); |
026548ddc1d0
fix comments in example; change some defaults
Tom Henderson <tomh@tomh.org>
parents:
5369
diff
changeset
|
93 |
onoff.SetAttribute ("PacketSize", UintegerValue (1250)); |
3821
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
94 |
|
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
95 |
ApplicationContainer apps = onoff.Install (c.Get (0)); |
5987
026548ddc1d0
fix comments in example; change some defaults
Tom Henderson <tomh@tomh.org>
parents:
5369
diff
changeset
|
96 |
apps.Start (Seconds (0.5)); |
026548ddc1d0
fix comments in example; change some defaults
Tom Henderson <tomh@tomh.org>
parents:
5369
diff
changeset
|
97 |
apps.Stop (Seconds (11)); |
3821
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
98 |
|
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
99 |
NS_LOG_INFO ("Create Sink."); |
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
100 |
PacketSinkHelper sink = PacketSinkHelper ("ns3::Ipv4RawSocketFactory", dst); |
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
101 |
apps = sink.Install (c.Get (3)); |
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
102 |
apps.Start (Seconds (0.0)); |
5987
026548ddc1d0
fix comments in example; change some defaults
Tom Henderson <tomh@tomh.org>
parents:
5369
diff
changeset
|
103 |
apps.Stop (Seconds (12.0)); |
3821
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
104 |
|
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
105 |
NS_LOG_INFO ("Configure Tracing."); |
4264
9d2e96c4e6e4
Piles of changes for tracing rework
Craig Dowell <craigdo@ee.washington.edu>
parents:
4225
diff
changeset
|
106 |
// first, pcap tracing in non-promiscuous mode |
9d2e96c4e6e4
Piles of changes for tracing rework
Craig Dowell <craigdo@ee.washington.edu>
parents:
4225
diff
changeset
|
107 |
csma.EnablePcapAll ("csma-raw-ip-socket", false); |
3821
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
108 |
// then, print what the packet sink receives. |
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
109 |
Config::ConnectWithoutContext ("/NodeList/3/ApplicationList/0/$ns3::PacketSink/Rx", |
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
110 |
MakeCallback (&SinkRx)); |
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
111 |
|
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
112 |
Packet::EnablePrinting (); |
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
113 |
|
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
114 |
|
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
115 |
NS_LOG_INFO ("Run Simulation."); |
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
116 |
Simulator::Run (); |
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
117 |
Simulator::Destroy (); |
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
118 |
NS_LOG_INFO ("Done."); |
9ea825f066a2
Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff
changeset
|
119 |
} |