A Discrete-Event Network Simulator
API
dummy-network.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2012 University of Washington, 2012 INRIA
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  */
19 
20 // Network topology
21 //
22 #include <sys/socket.h>
23 #include <errno.h>
24 
25 #include "ns3/core-module.h"
26 #include "ns3/network-module.h"
27 #include "ns3/internet-module.h"
28 #include "ns3/fd-net-device-module.h"
29 #include "ns3/internet-apps-module.h"
30 
31 using namespace ns3;
32 
33 NS_LOG_COMPONENT_DEFINE ("DummyNetworkExample");
34 
35 int
36 main (int argc, char *argv[])
37 {
38  CommandLine cmd (__FILE__);
39  cmd.Parse (argc, argv);
40 
42  nodes.Create (2);
43 
45  stack.Install (nodes);
46 
49 
50  int sv[2];
51  if (socketpair (AF_UNIX, SOCK_DGRAM, 0, sv) < 0)
52  {
53  NS_FATAL_ERROR ("Error creating pipe=" << strerror (errno));
54  }
55 
56  Ptr<NetDevice> d1 = devices.Get (0);
57  Ptr<FdNetDevice> device1 = d1->GetObject<FdNetDevice> ();
58  device1->SetFileDescriptor (sv[0]);
59 
60  Ptr<NetDevice> d2 = devices.Get (1);
61  Ptr<FdNetDevice> device2 = d2->GetObject<FdNetDevice> ();
62  device2->SetFileDescriptor (sv[1]);
63 
64  Ipv4AddressHelper addresses;
65  addresses.SetBase ("10.0.0.0", "255.255.255.0");
67 
68  Ptr<V4Ping> app = CreateObject<V4Ping> ();
69  app->SetAttribute ("Remote", Ipv4AddressValue (interfaces.GetAddress (0)));
70  app->SetAttribute ("Verbose", BooleanValue (true));
71  nodes.Get (1)->AddApplication (app);
72  app->SetStartTime (Seconds (0.0));
73  app->SetStopTime (Seconds (4.0));
74 
75  fd.EnablePcapAll ("dummy-network", true);
76 
77  // The next three lines will stop the simulator at time 5 seconds. Usually,
78  // it is sufficient to just call Simulator::Stop (Seconds (5)).
79  // However, in order to produce a clean valgrind output
80  // when running this example in our test suite (see issue #343), we
81  // first stop each device explicitly at time 5, and then wait one
82  // simulator timestep later (1 nanosecond by default) to call
83  // Simulator::Stop ().
84  device1->Stop (Seconds (5));
85  device2->Stop (Seconds (5));
86  Simulator::Stop (Seconds (5) + TimeStep (1));
87  Simulator::Run ();
89 }
ns3::NetDeviceContainer
holds a vector of ns3::NetDevice pointers
Definition: net-device-container.h:42
ns3::FdNetDeviceHelper::Install
virtual NetDeviceContainer Install(Ptr< Node > node) const
This method creates a FdNetDevice and associates it to a node.
Definition: fd-net-device-helper.cc:176
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
ns3::CommandLine
Parse command-line arguments.
Definition: command-line.h:228
ns3::BooleanValue
AttributeValue implementation for Boolean.
Definition: boolean.h:37
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::Ipv4AddressHelper
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
Definition: ipv4-address-helper.h:48
ns3::FdNetDevice::SetFileDescriptor
void SetFileDescriptor(int fd)
Set the associated file descriptor.
Definition: fd-net-device.cc:658
ns3::PcapHelperForDevice::EnablePcapAll
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 ...
Definition: trace-helper.cc:439
ns3::Object::GetObject
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:470
ns3::ObjectBase::SetAttribute
void SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:185
first.devices
devices
Definition: first.py:39
first.nodes
nodes
Definition: first.py:32
ns3::Application::SetStopTime
void SetStopTime(Time stop)
Specify application stop time.
Definition: application.cc:75
ns3::Ipv4AddressHelper::SetBase
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
Definition: ipv4-address-helper.cc:64
ns3::Ptr< NetDevice >
NS_FATAL_ERROR
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:165
ns3::Application::SetStartTime
void SetStartTime(Time start)
Specify application start time.
Definition: application.cc:69
first.stack
stack
Definition: first.py:41
ns3::Simulator::Stop
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:180
ns3::Ipv4InterfaceContainer
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Definition: ipv4-interface-container.h:55
second.cmd
cmd
Definition: second.py:35
ns3::Simulator::Run
static void Run(void)
Run the simulation.
Definition: simulator.cc:172
first.interfaces
interfaces
Definition: first.py:48
ns3::FdNetDevice
a NetDevice to read/write network traffic from/into a file descriptor.
Definition: fd-net-device.h:85
ns3::FdNetDeviceHelper
build a set of FdNetDevice objects Normally we eschew multiple inheritance, however,...
Definition: fd-net-device-helper.h:47
ns3::Seconds
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1289
ns3::Ipv4AddressHelper::Assign
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
Definition: ipv4-address-helper.cc:135
ns3::Simulator::Destroy
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:136
ns3::NodeContainer
keep track of a set of node pointers.
Definition: node-container.h:39
ns3::InternetStackHelper
aggregate IP/TCP/UDP functionality to existing Nodes.
Definition: internet-stack-helper.h:88
ns3::FdNetDevice::Stop
void Stop(Time tStop)
Set a stop time for the device.
Definition: fd-net-device.cc:233
ns3::Ipv4AddressValue
AttributeValue implementation for Ipv4Address.
Definition: ipv4-address.h:341