A Discrete-Event Network Simulator
Home
Tutorials ▼
English
Portuguese
Docs ▼
Wiki
Manual
Models
Develop ▼
API
Bugs
API
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Properties
Friends
Macros
Groups
Pages
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
47
CommandLine
cmd;
48
cmd.
Parse
(argc, argv);
49
50
NodeContainer
n;
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
83
CsmaHelper
csma;
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
();
182
Simulator::Destroy
();
183
}
examples
naming
object-names.cc
Generated on Fri Aug 30 2013 01:42:44 for ns-3 by
1.8.1.2