VANET仿真-ns3(5)

VANET 仿真

ns3示例代码fifth.cc注释整理,我们希望在这部分代码观察TCP的拥塞窗口情况,因此需要调高数据流并且将拥塞窗口这个属性挂到发送方的套接字socket上。现在我们通过一个可以开关的应用来生成数据流,但是有一系列问题有待解决。首先,套接字socket是在应用开始之后才被创建的,因而我们不能在配置阶段处理套接字。其次,套接字socket是非公共属性,因而我们无法访问它。

所以,我们可以通过应用程式来实现我们想要的东西。首先创建一个套接字并连接一个追踪,然后将该套接字传递到我们应用程式的构造器中,并把它安装到源代码上即可。


#include <fstream>
#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;

NS_LOG_COMPONENT_DEFINE ("FifthScriptExample");

// ===========================================================================
//
// node 0 node 1
// +----------------+ +----------------+
// | ns-3 TCP | | ns-3 TCP |
// +----------------+ +----------------+
// | 10.1.1.1 | | 10.1.1.2 |
// +----------------+ +----------------+
// | point-to-point | | point-to-point |
// +----------------+ +----------------+
// | |
// +---------------------+
// 5 Mbps, 2 ms
//
//
// We want to look at changes in the ns-3 TCP congestion window. We need
// to crank up a flow and hook the CongestionWindow attribute on the socket
// of the sender. Normally one would use an on-off application to generate a
// flow, but this has a couple of problems. First, the socket of the on-off
// application is not created until Application Start time, so we wouldn't be
// able to hook the socket (now) at configuration time. Second, even if we
// could arrange a call after start time, the socket is not public so we
// couldn't get at it.
//
// So, we can cook up a simple version of the on-off application that does what
// we want. On the plus side we don't need all of the complexity of the on-off
// application. On the minus side, we don't have a helper, so we have to get
// a little more involved in the details, but this is trivial.
//
// So first, we create a socket and do the trace connect on it; then we pass
// this socket into the constructor of our simple application which we then
// install in the source node.
// ===========================================================================
//

//继承自Application类,然后重写了StartApplication和StopApplication,这两个方法在调用MyApp时自动开始和停止发送数据。
class MyApp : public Application
{
public:

MyApp ();
virtual ~MyApp();

void Setup (Ptr<Socket> socket, Address address, uint32_t packetSize, uint32_t nPackets, DataRate dataRate);

private:
virtual void StartApplication (void);
virtual void StopApplication (void);

void ScheduleTx (void);
void SendPacket (void);

Ptr<Socket> m_socket;
Address m_peer;
uint32_t m_packetSize;
uint32_t m_nPackets;
DataRate m_dataRate;
EventId m_sendEvent;
bool m_running;
uint32_t m_packetsSent;
};

MyApp::MyApp ()
: m_socket (0),
m_peer (),
m_packetSize (0),
m_nPackets (0),
m_dataRate (0),
m_sendEvent (),
m_running (false),
m_packetsSent (0)
{
}

MyApp::~MyApp()
{
m_socket = 0;
}

//将类里面的属性赋予初值
void
MyApp::Setup (Ptr<Socket> socket, Address address, uint32_t packetSize, uint32_t nPackets, DataRate dataRate)
{
m_socket = socket;
m_peer = address;
m_packetSize = packetSize;
m_nPackets = nPackets;
m_dataRate = dataRate;
}


void
MyApp::StartApplication (void)
{
m_running = true;
m_packetsSent = 0;
//bind函数,为socket()函数创建的套接字关联一个相应地址,发送到这个地址的数据可以通过该套接字读取与使用。
m_socket->Bind ();
//connect函数,用TCP建立了地址联系
m_socket->Connect (m_peer);
//开始建立一个模拟事件
SendPacket ();
}

//当模拟启动的时候,事件也就被创建了
void
MyApp::StopApplication (void)
{
m_running = false;

//如果事件正在被执行,则.IsRunning返回true
if (m_sendEvent.IsRunning ())
{
//cancel则取消该事务队列
Simulator::Cancel (m_sendEvent);
}

if (m_socket)
{
m_socket->Close ();
}
}

void
MyApp::SendPacket (void)
{
Ptr<Packet> packet = Create<Packet> (m_packetSize);
m_socket->Send (packet);

if (++m_packetsSent < m_nPackets)
{
//schedule another transmit, 知道达到系统的上界
ScheduleTx ();
}
}

void
MyApp::ScheduleTx (void)
{
if (m_running)
{
Time tNext (Seconds (m_packetSize * 8 / static_cast<double> (m_dataRate.GetBitRate ())));
//再一次发送数据包
m_sendEvent = Simulator::Schedule (tNext, &MyApp::SendPacket, this);
}
}

static void
CwndChange (uint32_t oldCwnd, uint32_t newCwnd)
{
//每当发生变化时通过日志写出当前仿真的时间以及新的拥塞窗口大小
NS_LOG_UNCOND (Simulator::Now ().GetSeconds () << "\t" << newCwnd);
}

static void
RxDrop (Ptr<const Packet> p)
{
//检查数据包在物理层哪里drop了
NS_LOG_UNCOND ("RxDrop at " << Simulator::Now ().GetSeconds ());
}

int
main (int argc, char \*argv[])
{
CommandLine cmd;
cmd.Parse (argc, argv);

NodeContainer nodes;
nodes.Create (2);

PointToPointHelper pointToPoint;
pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));

NetDeviceContainer devices;
devices = pointToPoint.Install (nodes);

//跟踪channel信道上的错误信息,通过实例化一个RateErrorModel的对象,设置一个ErrorRate的属性,将实例作为错误模式
Ptr<RateErrorModel> em = CreateObject<RateErrorModel> ();
em->SetAttribute ("ErrorRate", DoubleValue (0.00001));
devices.Get (1)->SetAttribute ("ReceiveErrorModel", PointerValue (em));

InternetStackHelper stack;
stack.Install (nodes);

Ipv4AddressHelper address;
address.SetBase ("10.1.1.0", "255.255.255.252");
Ipv4InterfaceContainer interfaces = address.Assign (devices);

//TCP协议当中需要设置目标节点
uint16_t sinkPort = 8080;
Address sinkAddress (InetSocketAddress (interfaces.GetAddress (1), sinkPort));
//1. ns3::TcpSocketFactory:抽象地创建对象,这里不用创建对象本身,而是通过对象工厂(object factory)的方式进行创建
//2. 告知需要bind的地址与端口
PacketSinkHelper packetSinkHelper ("ns3::TcpSocketFactory", InetSocketAddress (Ipv4Address::GetAny (), sinkPort));
ApplicationContainer sinkApps = packetSinkHelper.Install (nodes.Get (1));
sinkApps.Start (Seconds (0.));
sinkApps.Stop (Seconds (20.));

//通过调用一个静态成员函数,提供一个节点和明确的TypeId,创建一个socket,这里有一个CwndChange的回调非常关键
Ptr<Socket> ns3TcpSocket = Socket::CreateSocket (nodes.Get (0), TcpSocketFactory::GetTypeId ());
ns3TcpSocket->TraceConnectWithoutContext ("CongestionWindow", MakeCallback (&CwndChange));

Ptr<MyApp> app = CreateObject<MyApp> ();
app->Setup (ns3TcpSocket, sinkAddress, 1040, 1000, DataRate ("1Mbps"));
nodes.Get (0)->AddApplication (app);
app->SetStartTime (Seconds (1.));
app->SetStopTime (Seconds (20.));

devices.Get (1)->TraceConnectWithoutContext ("PhyRxDrop", MakeCallback (&RxDrop));

Simulator::Stop (Seconds (20));
Simulator::Run ();
Simulator::Destroy ();

return 0;
}