#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/internet-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/applications-module.h"
using namespace ns3;
int main() {
Time::SetResolution (Time::NS);
// Default port
int default_port = 80;
// Hosts
NodeContainer hosts;
hosts.Create(3);
// Switches
NodeContainer switches;
switches.Create(1);
// All nodes
NodeContainer nodes;
nodes.Add(hosts);
nodes.Add(switches);
// Install IP stack on all nodes
InternetStackHelper stack;
stack.Install(nodes);
// Link model
PointToPointHelper linkModel;
linkModel.SetQueue ("ns3::DropTailQueue<Packet>", "MaxSize", QueueSizeValue (QueueSize ("100p"))); // p in 100p stands for packets
linkModel.SetDeviceAttribute ("DataRate", StringValue ("10Mbps"));
linkModel.SetChannelAttribute ("Delay", StringValue ("2ms"));
// Install links
NetDeviceContainer linkHost0ToSwitch = linkModel.Install(nodes.Get(0), switches.Get(0));
NetDeviceContainer linkHost1ToSwitch = linkModel.Install(nodes.Get(1), switches.Get(0));
NetDeviceContainer linkHost2ToSwitch = linkModel.Install(nodes.Get(2), switches.Get(0));
// Assign IP addresses to all hosts
Ipv4AddressHelper ipAssigner;
ipAssigner.SetBase ("10.0.0.0", "255.255.255.0");
Ipv4InterfaceContainer interfaces0toSwitch = ipAssigner.Assign (linkHost0ToSwitch);
ipAssigner.SetBase ("10.0.1.0", "255.255.255.0");
Ipv4InterfaceContainer interfaces1toSwitch = ipAssigner.Assign (linkHost1ToSwitch);
ipAssigner.SetBase ("10.0.2.0", "255.255.255.0");
Ipv4InterfaceContainer interfaces2toSwitch = ipAssigner.Assign (linkHost2ToSwitch);
// Calculate and populate IP routes
Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
// Create sink at node 2
PacketSinkHelper sink("ns3::TcpSocketFactory", InetSocketAddress(Ipv4Address::GetAny(), default_port));
ApplicationContainer sinkApps;
sinkApps = sink.Install(hosts.Get(2));
sinkApps.Start(Seconds(0));
sinkApps.Stop(Seconds(1000.0));
// Create the two connections to the sink
BulkSendHelper source("ns3::TcpSocketFactory", InetSocketAddress(interfaces2toSwitch.GetAddress(0), default_port));
source.SetAttribute("MaxBytes", UintegerValue(1000000));
source.Install(hosts.Get(0)).Start(Seconds(0));
source.Install(hosts.Get(1)).Start(Seconds(0));
// Run the simulator till completion
Simulator::Run ();
Simulator::Destroy ();
return 0;
}
#include "ns3/simulator.h"
// ...
template <typename Item>
bool
DropTailQueue<Item>::Enqueue (Ptr<Item> item)
{
NS_LOG_FUNCTION (this << item);
if ((GetCurrentSize()).GetValue() > 50) {
double current_time = Simulator::Now().ToDouble(Time::Unit::S);
std::cout << "Enqueueing at " << (GetCurrentSize()).GetValue() << " @ " << current_time << std::endl;
}
bool ret = DoEnqueue (Tail (), item);
if (!ret) {
std::cout << "DTQ: Dropping at size " << (GetCurrentSize()).GetValue() << std::endl;
}
return ret;
}
./waf configure; ./waf --build-profile=optimized --disable-examples --disable-python; ./build/scratch/qtest/qtest > out.txt
#include "ns3/simulator.h"
// ... with SendDataPacket add the following:
uint32_t
TcpSocketBase::SendDataPacket (SequenceNumber32 seq, uint32_t maxSize, bool withAck)
{
NS_LOG_FUNCTION (this << seq << maxSize << withAck);
double current_time = Simulator::Now().ToDouble(Time::Unit::S);
std::cout << "Sending data packet (cwnd = " << m_tcb->m_cWnd << ") @ " << current_time << std::endl;
// ... within TcpSocketBase::ProcessWait add the following:
else if (tcpflags == TcpHeader::FIN || tcpflags == (TcpHeader::FIN | TcpHeader::ACK))
{ // Got FIN, respond with ACK and move to next state
if (tcpflags & TcpHeader::ACK)
{ // Process the ACK first
ReceivedAck (packet, tcpHeader);
}
double current_time = Simulator::Now().ToDouble(Time::Unit::S);
std::cout << "Speed: " << (1000000.0 * 8.0) / 1000000.0 / current_time << " Mbit/s" << std::endl;
std::cout << "Ending at t=." << current_time << std::endl;
// ...