A Discrete-Event Network Simulator
API
main-simple.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2007 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  * Author: Mathieu Lacage <mathieu.lacage@cutebugs.net>
19  */
20 
21 #include <iostream>
22 
23 #include "ns3/core-module.h"
24 #include "ns3/network-module.h"
25 #include "ns3/internet-module.h"
26 
27 using namespace ns3;
28 
39 static void
40 GenerateTraffic (Ptr<Socket> socket, int32_t size)
41 {
42  if (size <= 0)
43  {
44  socket->Close ();
45  return;
46  }
47 
48  std::cout << "at=" << Simulator::Now ().GetSeconds () << "s, tx bytes=" << size << std::endl;
49  socket->Send (Create<Packet> (size));
50  Simulator::Schedule (Seconds (0.5), &GenerateTraffic, socket, size - 50);
51 }
52 
57 static void
59 {
60  Ptr<Packet> packet;
61  while ((packet = socket->Recv ()))
62  {
63  std::cout << "at=" << Simulator::Now ().GetSeconds () << "s, rx bytes=" << packet->GetSize () << std::endl;
64  }
65 }
66 
67 int main (int argc, char *argv[])
68 {
69  CommandLine cmd (__FILE__);
70  cmd.Parse (argc, argv);
71 
72  NodeContainer c;
73  c.Create (1);
74 
75  InternetStackHelper internet;
76  internet.Install (c);
77 
78 
79  TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory");
82  sink->Bind (local);
83 
84  Ptr<Socket> source = Socket::CreateSocket (c.Get (0), tid);
86  source->Connect (remote);
87 
88  GenerateTraffic (source, 500);
89  sink->SetRecvCallback (MakeCallback (&SocketPrinter));
90 
91  Simulator::Run ();
92 
94 
95  return 0;
96 }
Parse command-line arguments.
Definition: command-line.h:229
an Inet address class
aggregate IP/TCP/UDP functionality to existing Nodes.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
static Ipv4Address GetLoopback(void)
static Ipv4Address GetAny(void)
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 GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:856
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:136
static EventId Schedule(Time const &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:556
static void Run(void)
Run the simulation.
Definition: simulator.cc:172
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:195
virtual int Send(Ptr< Packet > p, uint32_t flags)=0
Send data (or dummy data) to the remote host.
virtual Ptr< Packet > Recv(uint32_t maxSize, uint32_t flags)=0
Read data from the socket.
virtual int Connect(const Address &address)=0
Initiate a connection to a remote host.
virtual int Close(void)=0
Close a socket.
static Ptr< Socket > CreateSocket(Ptr< Node > node, TypeId tid)
This method wraps the creation of sockets that is performed on a given node by a SocketFactory specif...
Definition: socket.cc:71
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:379
a unique identifier for an interface.
Definition: type-id.h:59
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition: type-id.cc:829
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1244
static void GenerateTraffic(Ptr< Socket > socket, int32_t size)
Generates traffic.
Definition: main-simple.cc:40
static void SocketPrinter(Ptr< Socket > socket)
Prints the packets received by a socket.
Definition: main-simple.cc:58
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Ts... > MakeCallback(R(T::*memPtr)(Ts...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:1648
cmd
Definition: second.py:35
Ptr< PacketSink > sink
Definition: wifi-tcp.cc:56