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 
29 static void
30 GenerateTraffic (Ptr<Socket> socket, uint32_t size)
31 {
32  std::cout << "at=" << Simulator::Now ().GetSeconds () << "s, tx bytes=" << size << std::endl;
33  socket->Send (Create<Packet> (size));
34  if (size > 0)
35  {
36  Simulator::Schedule (Seconds (0.5), &GenerateTraffic, socket, size - 50);
37  }
38  else
39  {
40  socket->Close ();
41  }
42 }
43 
44 static void
46 {
47  Ptr<Packet> packet;
48  while ((packet = socket->Recv ()))
49  {
50  std::cout << "at=" << Simulator::Now ().GetSeconds () << "s, rx bytes=" << packet->GetSize () << std::endl;
51  }
52 }
53 
54 static void
56 {
58 }
59 
60 void
62 {
63  NodeContainer c;
64  c.Create (1);
65 
66  InternetStackHelper internet;
67  internet.Install (c);
68 
69 
70  TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory");
73  sink->Bind (local);
74 
75  Ptr<Socket> source = Socket::CreateSocket (c.Get (0), tid);
77  source->Connect (remote);
78 
79  GenerateTraffic (source, 500);
81 
82 
83  Simulator::Run ();
84 
86 }
87 
88 int main (int argc, char *argv[])
89 {
91  cmd.Parse (argc, argv);
92 
93  RunSimulation ();
94 
95  return 0;
96 }
Ptr< PacketSink > sink
Definition: wifi-tcp.cc:56
an Inet address class
static Ipv4Address GetAny(void)
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:831
void RunSimulation(void)
Definition: main-simple.cc:61
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:355
static void Run(void)
Run the simulation.
Definition: simulator.cc:226
aggregate IP/TCP/UDP functionality to existing Nodes.
cmd
Definition: second.py:35
static EventId Schedule(Time const &delay, MEM mem_ptr, OBJ obj)
Schedule an event to expire after delay.
Definition: simulator.h:1381
static void GenerateTraffic(Ptr< Socket > socket, uint32_t size)
Definition: main-simple.cc:30
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
void SetRecvCallback(Callback< void, Ptr< Socket > >)
Notify application when new data is available to be read.
Definition: socket.cc:128
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
Parse command-line arguments.
Definition: command-line.h:213
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:190
virtual int Connect(const Address &address)=0
Initiate a connection to a remote host.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
keep track of a set of node pointers.
virtual Ptr< Packet > Recv(uint32_t maxSize, uint32_t flags)=0
Read data from the socket.
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:249
static Ipv4Address GetLoopback(void)
static void PrintTraffic(Ptr< Socket > socket)
Definition: main-simple.cc:55
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1014
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
static void SocketPrinter(Ptr< Socket > socket)
Definition: main-simple.cc:45
virtual int Send(Ptr< Packet > p, uint32_t flags)=0
Send data (or dummy data) to the remote host.
virtual int Close(void)=0
Close a socket.
a unique identifier for an interface.
Definition: type-id.h:58
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition: type-id.cc:824