#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/internet-module.h"
#include "ns3/node-container.h"
#include "ns3/mobility-helper.h"
#include "ns3/mobility-model.h"
#include "ns3/basic-energy-source-helper.h"
#include "ns3/energy-source-container.h"
#include "ns3/uan-helper.h"
#include "ns3/uan-channel.h"
#include "ns3/acoustic-modem-energy-model-helper.h"
#include "ns3/sixlowpan-helper.h"
#include "ns3/sixlowpan-net-device.h"
// #include "uan-rc-example.h"
#include "ns3/callback.h"
#include "ns3/stats-module.h"
#include "ns3/applications-module.h"
#include "ns3/waypoint-mobility-model.h"
#include"ns3/constant-position-mobility-model.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
#include <iterator>
#include "uan-cw-example.h"
#include "ns3/ipv4-global-routing-helper.h"
using namespace ns3;
using namespace std;
NS_LOG_COMPONENT_DEFINE("UanMyExample");
class UanExperiment
{
public:
UanExperiment();
void ReceivePacket(Ptr<Socket> socket);
//set uan nodes position and mobility model
void SetupMobility();
//set uan nodes communication channels
void SetupCommunications();
//set the uan nodes applications
void SetupApplications();
void Prepare();
// NodeContainer m_nodes;//m_nodes(0)——land;m_nodes(1)——auv;
NodeContainer land_node;
NodeContainer auv_node;
};
void ConnectionSucceededCallback(Ptr<Socket> sock)
{
std::cout << "Socket connection succeeded!" << std::endl;
// 连接成功后的其他操作
}
void ConnectionFailedCallback(Ptr<Socket> sock)
{
std::cout << "Socket connection failed!" << std::endl;
// 连接失败后的其他操作
}
UanExperiment::UanExperiment()
{
std::cout<<"uanexperiment"<<std::endl;
NS_LOG_INFO("进入UanExperiment");
}
struct WaypointStruct
{
double time;
double x;
double y;
};
std::vector<WaypointStruct> ReadCSV(const std::string& filePath)
{
std::vector<WaypointStruct> waypointdatas;
std::ifstream file(filePath);
if (!file.is_open())
{
std::cout << "Error opening file: " << filePath << std::endl;
return waypointdatas;
}
std::string line;
while (std::getline(file, line))
{
std::stringstream ss(line);
std::string token;
WaypointStruct waypointdata;
std::getline(ss, token, ',');
waypointdata.time = std::stod(token);
std::getline(ss, token, ',');
waypointdata.x = std::stod(token);
std::getline(ss, token, ',');
waypointdata.y = std::stod(token);
waypointdatas.push_back(waypointdata);
}
file.close();
return waypointdatas;
}
UanTxMode
uanmodetest(){
std::cout<<"uanmodeuanmodeuanmode"<<std::endl;
uint32_t m_totalRate=4096;
uint32_t m_numRates=1023;
uint32_t kass=1;
uint32_t fc=12000;
bool upperblock=false;
uint32_t phyrate = m_totalRate;
uint32_t rate = m_totalRate/(m_numRates+1)* (kass);//发送速率
uint32_t bw = kass * m_totalRate / (m_numRates+1);//带宽
uint32_t fcmode;
std::cout<<"uanmodeuanmodeuanmode---------------------"<<std::endl;
if(upperblock)
fcmode = (m_totalRate - bw)/2 + fc;
else
fcmode = (uint32_t)((-((double) m_totalRate ) + (double) bw)/2.0 + (double) fc);
UanTxMode mode;
mode = UanTxModeFactory::CreateMode (UanTxMode::OTHER,
rate,
phyrate,
fcmode,
bw,
2,
"name");
return mode;
}
void
UanExperiment::ReceivePacket (Ptr<Socket> socket)
{
std::cout<<"++++++++++++++++++++++进入Reiceiveacket+++++++++++++++++"<<std::endl;
static int ReceivePacket_callCount=0;
ReceivePacket_callCount++;
std::cout<<"ReceivePacket_callCount="<<ReceivePacket_callCount<<std::endl;
Ptr<Packet> packet;
Address from;
while ((packet = socket->RecvFrom(from))) {
uint8_t buffer[packet->GetSize()];
packet->CopyData(buffer, packet->GetSize());
// 打印接收到的数据内容
std::string data(reinterpret_cast<char*>(buffer), packet->GetSize());
std::cout << "Received data: " << data << std::endl;
}
std::cout<<"++++++++++++++++++++++推出Reiceiveacket+++++++++++++++++"<<std::endl;
}
void
ReceivePacket2 (Ptr<Socket> socket)
{
static int ReceivePacket_callCount=0;
ReceivePacket_callCount++;
std::cout<<"ReceivePacket_callCount="<<ReceivePacket_callCount<<std::endl;
std::cout<<"++++++++++++++++++++++进入Reiceiveacket+++++++++++++++++"<<std::endl;
Ptr<Packet> packet;
Address from;
while ((packet = socket->RecvFrom(from))) {
uint8_t buffer[packet->GetSize()];
packet->CopyData(buffer, packet->GetSize());
// 打印接收到的数据内容
std::string data(reinterpret_cast<char*>(buffer), packet->GetSize());
std::cout << "Received data: " << data << std::endl;
}
std::cout<<"++++++++++++++++++++++推出Reiceiveacket+++++++++++++++++"<<std::endl;
}
void
UanExperiment::SetupCommunications ()
{
NS_LOG_INFO("进入SetupMobility");
MobilityHelper mobility;
Ptr<ListPositionAllocator> pos = CreateObject<ListPositionAllocator> ();
Vector position0=Vector(0,0,0);
Vector position1=Vector(0,0,0);
pos->Add(position0); // 第一个节点land起始位置(0.0, 0.0, 0.0)
pos->Add(position1); // 第二个节点auv起始位置 (100.0, 100.0, 0.0)
mobility.SetPositionAllocator(pos);
mobility.SetMobilityModel("ns3::WaypointMobilityModel");
// mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
mobility.Install(auv_node);
mobility.Install(land_node);
Ptr<ConstantPositionMobilityModel> constantMobility =land_node.Get(0)->GetObject<ConstantPositionMobilityModel> ();
Ptr<WaypointMobilityModel> waypointMobility = auv_node.Get(0)->GetObject<WaypointMobilityModel>();
std::vector<WaypointStruct> waypointdatas = ReadCSV("/home/muta/ns-allinone-3.31/ns-3.31/trajectory.csv");//读取轨迹文件
for(const auto& wp : waypointdatas)
{
// std::cout<<"进入for循环了"<<std::endl;
Waypoint tmp_way(Seconds(wp.time),Vector(wp.x,wp.y,0.0));
waypointMobility->AddWaypoint(tmp_way);
std::cout<<"Time: "<<wp.time
<<",X: "<<wp.x
<<",Y: "<<wp.y<<std::endl;
// std::cout << "hahahahahahahahahahah" << std::endl;
position1= Vector(wp.x,wp.y,0.0);
std::cout <<"position0="<<position0 << std::endl;
std::cout <<"position1="<<position1 << std::endl;
double distance=CalculateDistance(position0,position1);
std::cout <<" distance= " << distance << std::endl;
// GetPathLossDb(distance,uanmodetest());
std::cout << "----------- " << std::endl;
// double dist = constantMobility->GetDistanceFrom (waypointMobility);
// std::cout << dist << "----------- " << std::endl;
}
NS_LOG_DEBUG ("Position of auv: "
<< auv_node.Get(0)->GetObject<MobilityModel> ()->GetPosition ());
NS_LOG_INFO("进入SetupCommunications");
NS_LOG_INFO("UAN");
std::cout<<"--------------------------- 创建UanHelper uan----------------------"<<std::endl;
UanHelper uan;
std::string perModel = "ns3::UanPhyPerGenDefault";//默认物理层的误码率(Packet Error Rate)PRE模型
std::string sinrModel = "ns3::UanPhyCalcSinrDefault";
ObjectFactory obf;
obf.SetTypeId (perModel);
Ptr<UanPhyPer> per = obf.Create<UanPhyPer> ();
obf.SetTypeId (sinrModel);
Ptr<UanPhyCalcSinr> sinr = obf.Create<UanPhyCalcSinr> ();
NS_LOG_INFO("mac");
std::cout<<"---------------------------SetMac1----------------------"<<std::endl;
uan.SetMac ("ns3::UanMacCw", "CW", UintegerValue (10), "SlotTime", TimeValue (Seconds (0.2)));
UanModesList myModes;
UanTxMode mode=uanmodetest();
std::cout<<"---------------------------SetPhy1----------------------"<<std::endl;
// uanmodetest();
myModes.AppendMode (mode);
uan.SetPhy ("ns3::UanPhyGen",
"PerModel", PointerValue (per),
"SinrModel", PointerValue (sinr),
"SupportedModes", UanModesListValue (myModes));
NS_LOG_INFO("PROP");
Ptr<UanPropModelThorp> prop = CreateObject<UanPropModelThorp> ();
NS_LOG_INFO("3");
prop->GetPathLossDb2(100.0 , mode);
NS_LOG_INFO("1");
// Ptr<UanChannel> channel = CreateObjectWithAttributes<UanChannel> ("PropagationModel", PointerValue (prop),"PropagationModel",StringValue ("ns3::UanNoiseModelDefault"));
std::cout<<"---------------------------UanChannel----------------------"<<std::endl;
Ptr<UanChannel> channel = CreateObjectWithAttributes<UanChannel> ("PropagationModel", PointerValue (prop));
NS_LOG_INFO("2");
// NetDeviceContainer AuvDevice= uan.Install (auv_node, channel);
// NetDeviceContainer LandDevice= uan.Install (land_node, channel);
std::cout<<"--------------------------- NetDeviceContainer sinkDev----------------------"<<std::endl;
NetDeviceContainer AuvDev= uan.Install (auv_node, channel);
NetDeviceContainer LandDev= uan.Install (land_node, channel);
// NetDeviceContainer AllDevice= uan.Install (land_node.Get(0), channel);
// NetDeviceContainer AllDevice= uan.Install (auv_node.Get(0), channel);
std::cout<<"--------------------------- PacketSocketHelper pktskth----------------------"<<std::endl;
PacketSocketHelper pktskth;
pktskth.Install (auv_node);
pktskth.Install (land_node);
std::cout<<"--------------------------- PacketSocketAddress socket pktskth----------------------"<<std::endl;
PacketSocketAddress socket;
socket.SetSingleDevice (AuvDev.Get (0)->GetIfIndex ());
socket.SetPhysicalAddress (AuvDev.Get (0)->GetAddress ());
socket.SetProtocol (0);
std::cout<<"--------------------------- OnOffHelper app----------------------"<<std::endl;
OnOffHelper app ("ns3::PacketSocketFactory", Address (socket));
// app.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=10]"));
// app.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=10]"));
app.SetAttribute ("DataRate", DataRateValue (80));
app.SetAttribute ("PacketSize", UintegerValue (32));
// // 设置数据速率
// app.SetAttribute("DataRate", DataRateValue(DataRate("1000bps")));
// 设置数据包大小
// uint32_t packetSize = 1000;
// app.SetAttribute("PacketSize", UintegerValue(packetSize));
std::cout<<"--------------------------- ApplicationContainer apps----------------------"<<std::endl;
ApplicationContainer apps = app.Install (land_node.Get(0));
std::string predefinedData = "Hello!!";
uint8_t buffer[7] ;
uint32_t len = predefinedData.length();
for(uint32_t i=0;i<len;i++)
{
buffer[i]=predefinedData[i];//char 与 uint_8逐个赋值
}
buffer[len]='\0';
Ptr<Packet> packet2 = Create<Packet>(buffer,sizeof(buffer));//把buffer写入到包内
std::cout<<"packet2->GetSize()= "<<packet2->GetSize()<<std::endl;
//添!!!
// uint8_t buffer111[packet2->GetSize()];
std::string data2(reinterpret_cast<char*>(buffer), packet2->GetSize());
std::cout << "!!!!!!!!!!!!!!!!!!!!!!!Received data: " << data2 << std::endl;
// 获取OnOffApplication实例
Ptr<OnOffApplication> app22 = DynamicCast<OnOffApplication>(apps.Get(0));
//检查返回指针是否为空
if(!app22)
{
std::cout<<"dynamicCast failure"<<std::endl;
}
else{
std::cout<<"dynamicCast successful"<<std::endl;
}
app22->StartApplication2();
app22->SendPacket2(packet2);
std::cout<<"--------------------------- auvSocket----------------------"<<std::endl;
Ptr<Node> AuvNode= auv_node.Get (0);
TypeId psfid = TypeId::LookupByName ("ns3::PacketSocketFactory");
Ptr<Socket> auvSocket = Socket::CreateSocket (AuvNode, psfid);
auvSocket->Bind (socket);
std::cout<<"--------------------------- MakeCallback RecvString---------------------"<<std::endl;
auvSocket->SetRecvCallback (MakeCallback (&UanExperiment::ReceivePacket, this));
// auvSocket->SetRecvCallback (MakeCallback (&ReceivePacket2,this));
Simulator::Stop (Seconds(1000));
Simulator::Run ();
Simulator::Destroy ();
}
void
UanExperiment::SetupApplications ()
{
}
void
UanExperiment::Prepare ()
{
std::cout<<"prepare"<<std::endl;
NS_LOG_INFO("进入Prepare");
LogComponentEnable ("UanMyExample", LOG_LEVEL_ALL);
auv_node.Create(2);
land_node.Create(2);
// ReadTrajectory();
// SetupMobility();
SetupCommunications();
SetupApplications();
}
int
main (int argc, char *argv[])
{
std::cout<<"kaishi"<<std::endl;
CommandLine cmd (__FILE__);
cmd.Parse (argc, argv);
std::cout<<"kaishi2"<<std::endl;
UanExperiment experiment;
std::cout<<"kaishi3"<<std::endl;
experiment.Prepare ();
NS_LOG_INFO("end");
// Simulator::Stop (Days (50));
// Simulator::Run ();
// Simulator::Destroy ();
return 0;
}
11111111111
最新推荐文章于 2024-10-08 18:38:07 发布