-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlastchance.cc
222 lines (180 loc) · 7.87 KB
/
lastchance.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include "ns3/command-line.h"
#include "ns3/constant-position-mobility-model.h"
#include "ns3/end-device-lora-phy.h"
#include "ns3/end-device-lorawan-mac.h"
#include "ns3/gateway-lora-phy.h"
#include "ns3/gateway-lorawan-mac.h"
#include "ns3/log.h"
#include "ns3/lora-helper.h"
#include "ns3/mobility-helper.h"
#include "ns3/node-container.h"
#include "ns3/periodic-sender-helper.h"
#include "ns3/position-allocator.h"
#include "ns3/simulator.h"
#include "ns3/core-module.h"
#include "ns3/mobility-module.h"
#include "ns3/ns2-mobility-helper.h"
#include <algorithm>
#include <ctime>
#include <map>
#include <vector>
using namespace ns3;
using namespace lorawan;
NS_LOG_COMPONENT_DEFINE("SimpleLorawanNetworkExample");
std::vector<int> packetsSent(1, 0);
std::vector<int> packetsReceived(1, 0);
std::map<uint64_t, Time> transmissionTimes; // Store transmission times
std::vector<Time> delays; // Store delays
void OnTransmissionCallback(Ptr<const Packet> packet, uint32_t senderNodeId)
{
NS_LOG_FUNCTION(packet << senderNodeId);
LoraTag tag;
packet->PeekPacketTag(tag);
packetsSent[0]++;
// Record the transmission time
uint64_t packetId = packet->GetUid();
transmissionTimes[packetId] = Simulator::Now();
}
void OnPacketReceptionCallback(Ptr<const Packet> packet, uint32_t receiverNodeId)
{
NS_LOG_FUNCTION(packet << receiverNodeId);
LoraTag tag;
packet->PeekPacketTag(tag);
packetsReceived[0]++;
// Calculate the delay
uint64_t packetId = packet->GetUid();
if (transmissionTimes.find(packetId) != transmissionTimes.end())
{
Time transmissionTime = transmissionTimes[packetId];
Time receptionTime = Simulator::Now();
Time delay = receptionTime - transmissionTime;
delays.push_back(delay); // Store the delay
NS_LOG_INFO("Packet " << packetId << " delay: " << delay.GetSeconds() << " seconds");
}
}
CommandLine cmd;
int nDevices = 50; // nDevices değişkenini tanımlıyoruz
std::string OutputFolder = "";
int nGateways =1;
int appPeriodSeconds = 50;
int main(int argc, char* argv[])
{
// Set up logging
/*LogComponentEnable("SimpleLorawanNetworkExample", LOG_LEVEL_ALL);
LogComponentEnable("LoraChannel", LOG_LEVEL_INFO);
LogComponentEnable("LoraPhy", LOG_LEVEL_ALL);
LogComponentEnable("EndDeviceLoraPhy", LOG_LEVEL_ALL);
LogComponentEnable("GatewayLoraPhy", LOG_LEVEL_ALL);
LogComponentEnable("LoraInterferenceHelper", LOG_LEVEL_ALL);
LogComponentEnable("LorawanMac", LOG_LEVEL_ALL);
LogComponentEnable("EndDeviceLorawanMac", LOG_LEVEL_ALL);
LogComponentEnable("ClassAEndDeviceLorawanMac", LOG_LEVEL_ALL);
LogComponentEnable("GatewayLorawanMac", LOG_LEVEL_ALL);
LogComponentEnable("LogicalLoraChannelHelper", LOG_LEVEL_ALL);
LogComponentEnable("LogicalLoraChannel", LOG_LEVEL_ALL);
LogComponentEnable("LoraHelper", LOG_LEVEL_ALL);
LogComponentEnable("LoraPhyHelper", LOG_LEVEL_ALL);
LogComponentEnable("LorawanMacHelper", LOG_LEVEL_ALL);
LogComponentEnable("PeriodicSenderHelper", LOG_LEVEL_ALL);*/
//LogComponentEnable ("Ns2MobilityHelper",LOG_LEVEL_ALL);
// Create the channel
NS_LOG_INFO("Creating the channel...");
Ptr<LogDistancePropagationLossModel> loss = CreateObject<LogDistancePropagationLossModel>();
loss->SetPathLossExponent(3.76);
loss->SetReference(1, 7.7);
Ptr<PropagationDelayModel> delay = CreateObject<ConstantSpeedPropagationDelayModel>();
Ptr<LoraChannel> channel = CreateObject<LoraChannel>(loss, delay);
// Create the helpers
NS_LOG_INFO("Setting up helpers...");
MobilityHelper mobility;
Ptr<ListPositionAllocator> allocator = CreateObject<ListPositionAllocator>();
allocator->Add(Vector(1000,0,0));
mobility.SetPositionAllocator(allocator);
mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
LoraPhyHelper phyHelper = LoraPhyHelper();
phyHelper.SetChannel(channel);
LorawanMacHelper macHelper = LorawanMacHelper();
LoraHelper helper = LoraHelper();
// Create end devices
NS_LOG_INFO("Creating the end devices...");
cmd.AddValue("nDevices", "Number of devices to simulate", nDevices);
cmd.AddValue("nGateways","Number of Gateways",nGateways);
cmd.AddValue("appPeriodSeconds","AppPeriodSeconds ",appPeriodSeconds);
// Diğer komut satırı argümanlarını ekleyin
cmd.AddValue("OutputFolder", "OutputFolder create", OutputFolder);
cmd.Parse(argc, argv);
std::string traceFile = "/home/yucel/ns-allinone-3.41/ns-3.41/scratch/ns2mobility.tcl";
double simulationTimeSeconds = 2171.0;
Ns2MobilityHelper ns2 = Ns2MobilityHelper(traceFile);
NodeContainer endDevices;
endDevices.Create(nDevices);
ns2.Install();
phyHelper.SetDeviceType(LoraPhyHelper::ED);
macHelper.SetDeviceType(LorawanMacHelper::ED_A);
helper.Install(phyHelper, macHelper, endDevices);
// Create gateways
NS_LOG_INFO("Creating the gateway...");
NodeContainer gateways;
gateways.Create(nGateways);
mobility.Install(gateways);
phyHelper.SetDeviceType(LoraPhyHelper::GW);
macHelper.SetDeviceType(LorawanMacHelper::GW);
helper.Install(phyHelper, macHelper, gateways);
// Install applications in end devices
PeriodicSenderHelper appHelper = PeriodicSenderHelper();
appHelper.SetPeriod(Seconds(appPeriodSeconds));
ApplicationContainer appContainer = appHelper.Install(endDevices);
// Set Data Rates
std::vector<int> sfQuantity(1);
sfQuantity = LorawanMacHelper::SetSpreadingFactorsUp(endDevices, gateways, channel);
// Install trace sources for packet transmission and reception
for (auto node = endDevices.Begin(); node != endDevices.End(); ++node)
{
(*node)->GetDevice(0)->GetObject<LoraNetDevice>()->GetPhy()->TraceConnectWithoutContext(
"StartSending",
MakeCallback(OnTransmissionCallback));
}
for (auto node = gateways.Begin(); node != gateways.End(); ++node)
{
(*node)->GetDevice(0)->GetObject<LoraNetDevice>()->GetPhy()->TraceConnectWithoutContext(
"ReceivedPacket",
MakeCallback(OnPacketReceptionCallback));
}
/*Time stateSamplePeriod = Seconds(appPeriodSeconds);
helper.EnablePeriodicDeviceStatusPrinting(endDevices,
gateways,
"nodeData.txt",
stateSamplePeriod);
helper.EnablePeriodicPhyPerformancePrinting(gateways, "phyPerformance.txt", stateSamplePeriod);
helper.EnablePeriodicGlobalPerformancePrinting("globalPerformance.txt", stateSamplePeriod);*/
// Simulation
Simulator::Stop(Seconds(simulationTimeSeconds));
Simulator::Run();
Simulator::Destroy();
// Calculate packet delivery ratio
NS_LOG_INFO("Calculating performance metrics...");
float totalPacketsSent = packetsSent[0];
float totalPacketsReceived = packetsReceived[0];
float totalPacketsLost = totalPacketsSent - totalPacketsReceived;
float percantage = totalPacketsReceived / totalPacketsSent;
std::cout << "Veri Orani: "
<< "Gonderilen: " << totalPacketsSent << ", "
<< "Ulaşan: " << totalPacketsReceived << ", "
<< "Kaybolan: " << totalPacketsLost << ", "
<< "Basari orani: " << percantage << std::endl;
// Calculate average delay
Time totalDelay = Seconds(0);
for (Time delay : delays)
{
totalDelay += delay;
}
double averageDelay = delays.empty() ? 0 : totalDelay.GetSeconds() / delays.size();
std::cout << "Ortalama Gecikme: " << averageDelay << " saniye" << std::endl;
// Print each packet delay
std::cout << "Packet Delays:" << std::endl;
for (size_t i = 0; i < delays.size(); ++i)
{
std::cout << "Packet " << i + 1 << ": " << delays[i].GetSeconds() << " saniye" << std::endl;
}
return 0;
}