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 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Mathieu Lacage <mathieu.lacage@cutebugs.net>
7 */
8
9#include "ns3/core-module.h"
10#include "ns3/internet-module.h"
11#include "ns3/network-module.h"
12
13#include <iostream>
14
15using namespace ns3;
16
17/**
18 * Generates traffic.
19 *
20 * The first call sends a packet of the specified size, and then
21 * the function is scheduled to send a packet of (size-50) after 0.5s.
22 * The process is iterated until the packet size is zero.
23 *
24 * \param socket output socket
25 * \param size packet size
26 */
27static void
29{
30 if (size <= 0)
31 {
32 socket->Close();
33 return;
34 }
35
36 std::cout << "at=" << Simulator::Now().GetSeconds() << "s, tx bytes=" << size << std::endl;
37 socket->Send(Create<Packet>(size));
38 Simulator::Schedule(Seconds(0.5), &GenerateTraffic, socket, size - 50);
39}
40
41/**
42 * Prints the packets received by a socket
43 * \param socket input socket
44 */
45static void
47{
48 Ptr<Packet> packet;
49 while ((packet = socket->Recv()))
50 {
51 std::cout << "at=" << Simulator::Now().GetSeconds() << "s, rx bytes=" << packet->GetSize()
52 << std::endl;
53 }
54}
55
56int
57main(int argc, char* argv[])
58{
59 CommandLine cmd(__FILE__);
60 cmd.Parse(argc, argv);
61
63 c.Create(1);
64
66 internet.Install(c);
67
68 TypeId tid = TypeId::LookupByName("ns3::UdpSocketFactory");
71 sink->Bind(local);
72
73 Ptr<Socket> source = Socket::CreateSocket(c.Get(0), tid);
75 source->Connect(remote);
76
77 GenerateTraffic(source, 500);
78 sink->SetRecvCallback(MakeCallback(&SocketPrinter));
79
81
83
84 return 0;
85}
Parse command-line arguments.
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.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:560
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static Time Now()
Return the current simulation virtual time.
Definition simulator.cc:197
static void Run()
Run the simulation.
Definition simulator.cc:167
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:61
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition nstime.h:392
a unique identifier for an interface.
Definition type-id.h:48
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition type-id.cc:872
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
static void GenerateTraffic(Ptr< Socket > socket, int32_t size)
Generates traffic.
static void SocketPrinter(Ptr< Socket > socket)
Prints the packets received by a socket.
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:684
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition wifi-tcp.cc:44