A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
main-simple.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2007 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 * Author: Mathieu Lacage <mathieu.lacage@cutebugs.net>
18 */
19
20#include "ns3/core-module.h"
21#include "ns3/internet-module.h"
22#include "ns3/network-module.h"
23
24#include <iostream>
25
26using namespace ns3;
27
28/**
29 * Generates traffic.
30 *
31 * The first call sends a packet of the specified size, and then
32 * the function is scheduled to send a packet of (size-50) after 0.5s.
33 * The process is iterated until the packet size is zero.
34 *
35 * \param socket output socket
36 * \param size packet size
37 */
38static void
40{
41 if (size <= 0)
42 {
43 socket->Close();
44 return;
45 }
46
47 std::cout << "at=" << Simulator::Now().GetSeconds() << "s, tx bytes=" << size << std::endl;
48 socket->Send(Create<Packet>(size));
49 Simulator::Schedule(Seconds(0.5), &GenerateTraffic, socket, size - 50);
50}
51
52/**
53 * Prints the packets received by a socket
54 * \param socket input socket
55 */
56static void
58{
59 Ptr<Packet> packet;
60 while ((packet = socket->Recv()))
61 {
62 std::cout << "at=" << Simulator::Now().GetSeconds() << "s, rx bytes=" << packet->GetSize()
63 << std::endl;
64 }
65}
66
67int
68main(int argc, char* argv[])
69{
70 CommandLine cmd(__FILE__);
71 cmd.Parse(argc, argv);
72
74 c.Create(1);
75
77 internet.Install(c);
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
92
94
95 return 0;
96}
Parse command-line arguments.
Definition: command-line.h:232
an Inet address class
aggregate IP/TCP/UDP functionality to existing Nodes.
static Ipv4Address GetLoopback()
static Ipv4Address GetAny()
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.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:571
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static Time Now()
Return the current simulation virtual time.
Definition: simulator.cc:208
static void Run()
Run the simulation.
Definition: simulator.cc:178
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:72
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:403
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:836
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
static void GenerateTraffic(Ptr< Socket > socket, int32_t size)
Generates traffic.
Definition: main-simple.cc:39
static void SocketPrinter(Ptr< Socket > socket)
Prints the packets received by a socket.
Definition: main-simple.cc:57
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:704
ns cmd
Definition: second.py:40
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition: wifi-tcp.cc:55