2007-02-12 19:47:36 +01:00
|
|
|
#include <iostream>
|
|
|
|
|
|
2007-02-12 19:28:48 +01:00
|
|
|
#include "ns3/internet-node.h"
|
|
|
|
|
#include "ns3/simulator.h"
|
2007-06-04 17:54:56 +02:00
|
|
|
#include "ns3/socket-factory.h"
|
2007-04-30 16:23:10 +02:00
|
|
|
#include "ns3/socket.h"
|
2007-07-30 13:29:36 +02:00
|
|
|
#include "ns3/inet-socket-address.h"
|
2007-02-12 19:47:36 +01:00
|
|
|
#include "ns3/nstime.h"
|
2007-08-01 19:09:11 +02:00
|
|
|
#include "ns3/packet.h"
|
2007-02-12 19:28:48 +01:00
|
|
|
|
|
|
|
|
using namespace ns3;
|
|
|
|
|
|
2007-02-12 19:47:36 +01:00
|
|
|
static void
|
2007-05-10 20:19:26 +02:00
|
|
|
GenerateTraffic (Ptr<Socket> socket, uint32_t size)
|
2007-02-12 19:47:36 +01:00
|
|
|
{
|
2007-02-12 19:52:01 +01:00
|
|
|
std::cout << "at=" << Simulator::Now ().GetSeconds () << "s, tx bytes=" << size << std::endl;
|
2007-08-29 09:35:53 +02:00
|
|
|
socket->Send (Packet (size));
|
2007-02-12 19:52:01 +01:00
|
|
|
if (size > 0)
|
|
|
|
|
{
|
|
|
|
|
Simulator::Schedule (Seconds (0.5), &GenerateTraffic, socket, size - 50);
|
|
|
|
|
}
|
2007-05-03 00:12:39 +02:00
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
socket->Close ();
|
|
|
|
|
}
|
2007-02-12 19:47:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2007-08-01 19:09:11 +02:00
|
|
|
SocketPrinter (Ptr<Socket> socket, const Packet &packet, const Address &from)
|
2007-02-12 19:47:36 +01:00
|
|
|
{
|
2007-08-01 19:09:11 +02:00
|
|
|
std::cout << "at=" << Simulator::Now ().GetSeconds () << "s, rx bytes=" << packet.GetSize () << std::endl;
|
2007-02-12 19:47:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2007-05-10 20:19:26 +02:00
|
|
|
PrintTraffic (Ptr<Socket> socket)
|
2007-02-12 19:47:36 +01:00
|
|
|
{
|
2007-08-01 19:09:11 +02:00
|
|
|
socket->SetRecvCallback (MakeCallback (&SocketPrinter));
|
2007-02-12 19:47:36 +01:00
|
|
|
}
|
|
|
|
|
|
2007-05-10 18:33:52 +02:00
|
|
|
void
|
|
|
|
|
RunSimulation (void)
|
2007-02-12 19:28:48 +01:00
|
|
|
{
|
2007-06-04 16:32:37 +02:00
|
|
|
Ptr<Node> a = Create<InternetNode> ();
|
2007-04-30 16:23:10 +02:00
|
|
|
|
2007-06-06 10:54:58 +02:00
|
|
|
InterfaceId iid = InterfaceId::LookupByName ("Udp");
|
2007-06-04 17:57:01 +02:00
|
|
|
Ptr<SocketFactory> socketFactory = a->QueryInterface<SocketFactory> (iid);
|
2007-05-03 00:12:39 +02:00
|
|
|
|
2007-05-14 12:59:44 +02:00
|
|
|
Ptr<Socket> sink = socketFactory->CreateSocket ();
|
2007-07-30 13:29:36 +02:00
|
|
|
InetSocketAddress local = InetSocketAddress (Ipv4Address::GetAny (), 80);
|
2007-07-30 14:07:39 +02:00
|
|
|
sink->Bind (local);
|
2007-02-12 19:47:36 +01:00
|
|
|
|
2007-05-14 12:59:44 +02:00
|
|
|
Ptr<Socket> source = socketFactory->CreateSocket ();
|
2007-07-30 13:29:36 +02:00
|
|
|
InetSocketAddress remote = InetSocketAddress (Ipv4Address::GetLoopback (), 80);
|
2007-07-30 14:07:39 +02:00
|
|
|
source->Connect (remote);
|
2007-02-12 19:47:36 +01:00
|
|
|
|
|
|
|
|
GenerateTraffic (source, 500);
|
|
|
|
|
PrintTraffic (sink);
|
|
|
|
|
|
2007-02-12 19:28:48 +01:00
|
|
|
|
|
|
|
|
Simulator::Run ();
|
|
|
|
|
|
|
|
|
|
Simulator::Destroy ();
|
2007-05-10 18:33:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int main (int argc, char *argv[])
|
|
|
|
|
{
|
|
|
|
|
RunSimulation ();
|
2007-02-12 19:28:48 +01:00
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|