前因:ns3网络仿真
实验进行到很关键的一步,我尝试进行了代码的编写(还没有添加Traceback的函数),如下:
#include "ns3/core-module.h"
#include "ns3/internet-module.h"
#include "ns3/network-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/applications-module.h"
#include "ns3/netanim-module.h"
#include "ns3/csma-module.h"
#include <string>
#include <iostream>
using namespace std;
using namespace ns3;
NS_LOG_COMPONENT_DEFINE( "MyFirstNS3Project" );
int main(int argc, char *argv[])
{
NodeContainer nodes;
nodes.Create(2);
PointToPointHelper p2pHelper;
p2pHelper.SetDeviceAttribute( "DataRate", StringValue("5Mbps") );
p2pHelper.SetChannelAttribute( "Delay", StringValue("2ms") );
NetDeviceContainer devices;
devices = p2pHelper.Install( nodes );
//Rate Error Model
Ptr<RateErrorModel> em = CreateObject<RateErrorModel>();
em->SetAttribute( "ErrorRate", DoubleValue(0.00001) );
devices.Get(1)->SetAttribute( "RecieveErrorModel", PointerValue(em) );
//Agreement Stack
InternetStackHelper stack;
stack.Install( nodes );
//Set IP Address
Ipv4AddressHelper addressHelper;
addressHelper.SetBase( "10.1.1.0", "255.255.255.252" );
Ipv4InterfaceContainer interfaces = addressHelper.Assign( devices ); //interfaces
uint16_t sinkPort = 8080;
Address sinkAddress( InetSocketAddress( interfaces.GetAddress(1), sinkPort ) );
PacketSinkHelper packetSinkHelper( "TcpSocketFactory", InetSocketAddress( Ipv4Address::GetAny(), sinkPort ) );
ApplicationContainer sinkApps = packetSinkHelper.Install( nodes.Get(1) ); //Set send-message-application in nodes.Get(1); port : 8080
sinkApps.Start( Seconds(0.0) );
sinkApps.Stop( Seconds(10.0) );
uint32_t a = 1040, b = 1000;
Ptr<MyApp> app = CreateObject <MyApp> ();
Ptr<Socket> p = TcpSocket;
DataRate d("1Mbps");
app->Setup(p, sinkAddress, a, b, d);
nodes.Get(0)->AddApplication(app); //Set receive-message-application in nodes.Get(0);
app->SetStartTime( Seconds(1.0) );
app->SetStopTime( Seconds(10.0) );
AnimationInterface anim("MyFirstNS3Project.xml");
Simulator::Run();
Simulator::Destroy();
return 0;
}
// 2016/7/13 wasdns
接下来我将它在scratch目录下改为.cc模式,正打算产生xml文件时,./waf编译错误,提示没有MyApp这个类。
好吧,那我就暂时当编译器瞎了。在原有的fifth.cc中找到了MyApp的原型,并且将它拷贝了过来。参见我的上一篇博客:MyApp
OK,类的代码添加上去了,这下总行了吧。结果在一个智能指针的地方报错,中文大意是在“,”前有一个语法错误,编译器不通过。
经过漫长时间的排查,锁定在了原文的:app->Setup (ns3::TcpSocket, sinkAddress, 1040, 1000, DataRate ("1Mbps"));
之中的TcpRocket
关于Setup函数:
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;
}
关于TcpRocket:官方文档
1.首先它是由 Rocket 派生而来的。2.其次由于内含纯虚函数,无法进行实例化。3.TcpRocket是一个类的名字。
文档中传递TcpRocket类名字的方法肯定是不可取的,于是查阅了相关资料和咨询了老师以后,决定使用Socket::CreateSocket()这个函数,它返回的类型是Ptr。于是做出了如下改正:
//uint32_t a = 1040, b = 1000;
Ptr<MyApp> app = CreateObject <MyApp> ();
Ptr<Socket> ns3TcpSocket = Socket::CreateSocket(nodes.Get(0), TcpSocketFactory::GetTypeId());
app->Setup(ns3TcpSocket, sinkAddress);
在上面我也对MyApp的函数Setup进行了重构:
void Setup (Ptr<Socket>socket, Address address, uint32_t packetSize = 1040, uint32_t nPackets = 1000);
void
MyApp::Setup (Ptr<Socket>socket, Address address, uint32_t packetSize, uint32_t nPackets)
{
DataRate dataRate("1Mbps");
m_socket = socket;
m_peer = address;
m_packetSize = packetSize;
m_nPackets = nPackets;
m_dataRate = dataRate;
}