A Discrete-Event Network Simulator
API
dummy-network.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012 University of Washington, 2012 INRIA
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  */
18 
19 // Network topology
20 //
21 #include "ns3/core-module.h"
22 #include "ns3/fd-net-device-module.h"
23 #include "ns3/internet-apps-module.h"
24 #include "ns3/internet-module.h"
25 #include "ns3/network-module.h"
26 
27 #include <errno.h>
28 #include <sys/socket.h>
29 
30 using namespace ns3;
31 
32 NS_LOG_COMPONENT_DEFINE("DummyNetworkExample");
33 
34 int
35 main(int argc, char* argv[])
36 {
37  CommandLine cmd(__FILE__);
38  cmd.Parse(argc, argv);
39 
41  nodes.Create(2);
42 
44  stack.Install(nodes);
45 
48 
49  int sv[2];
50  if (socketpair(AF_UNIX, SOCK_DGRAM, 0, sv) < 0)
51  {
52  NS_FATAL_ERROR("Error creating pipe=" << strerror(errno));
53  }
54 
55  Ptr<NetDevice> d1 = devices.Get(0);
56  Ptr<FdNetDevice> device1 = d1->GetObject<FdNetDevice>();
57  device1->SetFileDescriptor(sv[0]);
58 
59  Ptr<NetDevice> d2 = devices.Get(1);
60  Ptr<FdNetDevice> device2 = d2->GetObject<FdNetDevice>();
61  device2->SetFileDescriptor(sv[1]);
62 
63  Ipv4AddressHelper addresses;
64  addresses.SetBase("10.0.0.0", "255.255.255.0");
66 
67  Ptr<Ping> app = CreateObject<Ping>();
68  app->SetAttribute("Destination", AddressValue(interfaces.GetAddress(0)));
69  app->SetAttribute("VerboseMode", EnumValue(Ping::VerboseMode::VERBOSE));
71  app->SetStartTime(Seconds(0.0));
72  app->SetStopTime(Seconds(4.0));
73 
74  fd.EnablePcapAll("dummy-network", true);
75 
76  // The next three lines will stop the simulator at time 5 seconds. Usually,
77  // it is sufficient to just call Simulator::Stop (Seconds (5)).
78  // However, in order to produce a clean valgrind output
79  // when running this example in our test suite (see issue #343), we
80  // first stop each device explicitly at time 5, and then wait one
81  // simulator timestep later (1 nanosecond by default) to call
82  // Simulator::Stop ().
83  device1->Stop(Seconds(5));
84  device2->Stop(Seconds(5));
85  Simulator::Stop(Seconds(5) + TimeStep(1));
88 
89  return 0;
90 }
Parse command-line arguments.
Definition: command-line.h:232
Hold variables of type enum.
Definition: enum.h:62
build a set of FdNetDevice objects Normally we eschew multiple inheritance, however,...
virtual NetDeviceContainer Install(Ptr< Node > node) const
This method creates a FdNetDevice and associates it to a node.
a NetDevice to read/write network traffic from/into a file descriptor.
Definition: fd-net-device.h:84
aggregate IP/TCP/UDP functionality to existing Nodes.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
holds a vector of std::pair of Ptr<Ipv4> and interface index.
holds a vector of ns3::NetDevice pointers
keep track of a set of node pointers.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
uint32_t AddApplication(Ptr< Application > application)
Associate an Application to this Node.
Definition: node.cc:169
void EnablePcapAll(std::string prefix, bool promiscuous=false)
Enable pcap output on each device (which is of the appropriate type) in the set of all nodes created ...
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void Run()
Run the simulation.
Definition: simulator.cc:178
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:186
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:179
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
NodeContainer nodes
devices
Definition: first.py:42
stack
Definition: first.py:44
interfaces
Definition: first.py:50
Every class exported by the ns3 library is enclosed in the ns3 namespace.
cmd
Definition: second.py:40