A Discrete-Event Network Simulator
API
object-names.cc
Go to the documentation of this file.
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 // Network topology
18 //
19 // n0 n1 n2 n3
20 // | | | |
21 // =================
22 // LAN
23 //
24 
25 #include "ns3/core-module.h"
26 #include "ns3/csma-module.h"
27 #include "ns3/applications-module.h"
28 #include "ns3/internet-module.h"
29 
30 using namespace ns3;
31 
32 NS_LOG_COMPONENT_DEFINE ("ObjectNamesExample");
33 
34 void
35 RxEvent (std::string context, Ptr<const Packet> packet)
36 {
37  NS_LOG_INFO (context << " packet " << packet);
38 }
39 
40 int
41 main (int argc, char *argv[])
42 {
43 #if 1
44  LogComponentEnable ("ObjectNamesExample", LOG_LEVEL_INFO);
45 #endif
46 
48  cmd.Parse (argc, argv);
49 
51  n.Create (4);
52 
53  //
54  // We're going to use the zeroth node in the container as the client, and
55  // the first node as the server. Add some "human readable" names for these
56  // nodes. The names below will go into the name system as "/Names/clientZero"
57  // and "/Names/server", but note that the Add function assumes that if you
58  // omit the leading "/Names/" the remaining string is assumed to be rooted
59  // in the "/Names" namespace. The following calls,
60  //
61  // Names::Add ("clientZero", n.Get (0));
62  // Names::Add ("/Names/clientZero", n.Get (0));
63  //
64  // will produce identical results.
65  //
66  Names::Add ("clientZero", n.Get (0));
67  Names::Add ("/Names/server", n.Get (1));
68 
69  //
70  // It is possible to rename a node that has been previously named. This is
71  // useful in automatic name generation. You can automatically generate node
72  // names such as, "node-0", "node-1", etc., and then go back and change
73  // the name of some distinguished node to another value -- "access-point"
74  // for example. We illustrate this by just changing the client's name.
75  // As is typical of the object name service, you can either provide or elide
76  // the "/Names" prefix as you choose.
77  //
78  Names::Rename ("clientZero", "client");
79 
80  InternetStackHelper internet;
81  internet.Install (n);
82 
84  csma.SetChannelAttribute ("DataRate", DataRateValue (DataRate (5000000)));
85  csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2)));
86  csma.SetDeviceAttribute ("Mtu", UintegerValue (1400));
87  NetDeviceContainer d = csma.Install (n);
88 
89  //
90  // Add some human readable names for the devices we'll be interested in.
91  // We add the names to the name space "under" the nodes we created above.
92  // This has the effect of making "/Names/client/eth0" and "/Names/server/eth0".
93  // In this case, we again omit the "/Names/" prefix on one call to illustrate
94  // the shortcut.
95  //
96  Names::Add ("/Names/client/eth0", d.Get (0));
97  Names::Add ("server/eth0", d.Get (1));
98 
99  //
100  // You can use the object names that you've assigned in calls to the Config
101  // system to set Object Attributes. For example, you can set the Mtu
102  // Attribute of a Csma devices using the object naming service. Note that
103  // in this case, the "/Names" prefix is always required since the _Config_
104  // system always expects to see a fully qualified path name.
105  //
106  Config::Set ("/Names/client/eth0/Mtu", UintegerValue (1234));
107 
108  //
109  // You can mix and match names and Attributes in calls to the Config system.
110  // For example, if "eth0" is a named object, you can get to its parent through
111  // a different namespace. For example, you could use the NodeList namespace
112  // to get to the server node, and then continue seamlessly adding named objects
113  // in the path. This is not nearly as readable as the previous version, but it
114  // illustrates how you can mix and match object names and Attribute names.
115  // Note that the config path now begins with a path in the "/NodeList"
116  // namespace.
117  //
118  Config::Set ("/NodeList/1/eth0/Mtu", UintegerValue (1234));
119 
120  Ipv4AddressHelper ipv4;
121  ipv4.SetBase ("10.1.1.0", "255.255.255.0");
122  Ipv4InterfaceContainer i = ipv4.Assign (d);
123 
124  uint16_t port = 9;
125  UdpEchoServerHelper server (port);
126  //
127  // Install the UdpEchoServer application on the server node using its name
128  // directly.
129  //
130  ApplicationContainer apps = server.Install ("/Names/server");
131  apps.Start (Seconds (1.0));
132  apps.Stop (Seconds (10.0));
133 
134  uint32_t packetSize = 1024;
135  uint32_t maxPacketCount = 1;
136  Time interPacketInterval = Seconds (1.);
137  UdpEchoClientHelper client (i.GetAddress (1), port);
138  client.SetAttribute ("MaxPackets", UintegerValue (maxPacketCount));
139  client.SetAttribute ("Interval", TimeValue (interPacketInterval));
140  client.SetAttribute ("PacketSize", UintegerValue (packetSize));
141  //
142  // Install the UdpEchoClient application on the server node using its name
143  // directly.
144  //
145  apps = client.Install ("/Names/client");
146  apps.Start (Seconds (2.0));
147  apps.Stop (Seconds (10.0));
148 
149  //
150  // Use the Config system to connect a trace source using the object name
151  // service to specify the path. Note that in this case, the "/Names"
152  // prefix is always required since the _Config_ system always expects to
153  // see a fully qualified path name
154  //
155  Config::Connect ("/Names/client/eth0/MacRx", MakeCallback (&RxEvent));
156 
157  //
158  // Set up some pcap tracing on the CSMA devices. The names of the trace
159  // files will automatically correspond to the object names if present.
160  // In this case, you will find trace files called:
161  //
162  // object-names-client-eth0.pcap
163  // object-names-server-eth0.pcap
164  //
165  // since those nodes and devices have had names associated with them. You
166  // will also see:
167  //
168  // object-names-2-1.pcap
169  // object-names-3-1.pcap
170  //
171  // since nodes two and three have no associated names.
172  //
173  csma.EnablePcapAll ("object-names");
174 
175  //
176  // We can also create a trace file with a name we completely control by
177  // overriding a couple of default parameters.
178  //
179  csma.EnablePcap ("client-device.pcap", d.Get (0), false, true);
180 
181  Simulator::Run ();
183 }
holds a vector of ns3::Application pointers.
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
holds a vector of std::pair of Ptr<Ipv4> and interface index.
void Set(std::string path, const AttributeValue &value)
Definition: config.cc:777
Create an application which sends a UDP packet and waits for an echo of this packet.
static void Run(void)
Run the simulation.
Definition: simulator.cc:226
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
Time MilliSeconds(uint64_t value)
Construct a Time in the indicated unit.
Definition: nstime.h:1022
aggregate IP/TCP/UDP functionality to existing Nodes.
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:278
cmd
Definition: second.py:35
uint16_t port
Definition: dsdv-manet.cc:45
static void Add(std::string name, Ptr< Object > object)
Add the association between the string "name" and the Ptr<Object> obj.
Definition: names.cc:770
Class for representing data rates.
Definition: data-rate.h:88
Create a server application which waits for input UDP packets and sends them back to the original sen...
void LogComponentEnable(char const *name, enum LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:369
AttributeValue implementation for Time.
Definition: nstime.h:1076
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
Hold an unsigned integer type.
Definition: uinteger.h:44
LOG_INFO and above.
Definition: log.h:104
holds a vector of ns3::NetDevice pointers
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter...
csma
Definition: second.py:63
Parse command-line arguments.
Definition: command-line.h:213
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:843
void RxEvent(std::string context, Ptr< const Packet > packet)
Definition: object-names.cc:35
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:190
Every class exported by the ns3 library is enclosed in the ns3 namespace.
keep track of a set of node pointers.
build a set of CsmaNetDevice objects
Definition: csma-helper.h:46
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
static void Rename(std::string oldpath, std::string newname)
Rename a previously associated name.
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter...
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
AttributeValue implementation for DataRate.
Definition: data-rate.h:242
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1014
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
static const uint32_t packetSize
void SetAttribute(std::string name, const AttributeValue &value)
Record an attribute to be set in each Application after it is is created.
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.