-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwsn-application.cc
316 lines (283 loc) · 10.2 KB
/
wsn-application.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#include "ns3/log.h"
#include "ns3/address.h"
#include "ns3/inet-socket-address.h"
#include "ns3/inet6-socket-address.h"
#include "ns3/packet-socket-address.h"
#include "ns3/node.h"
#include "ns3/nstime.h"
#include "ns3/data-rate.h"
#include "ns3/random-variable-stream.h"
#include "ns3/socket.h"
#include "ns3/simulator.h"
#include "ns3/socket-factory.h"
#include "ns3/packet.h"
#include "ns3/uinteger.h"
#include "ns3/integer.h"
#include "ns3/double.h"
#include "ns3/trace-source-accessor.h"
#include "wsn-application.h"
#include "ns3/udp-socket-factory.h"
#include "ns3/string.h"
#include "ns3/pointer.h"
#include "LeachPacket.h"
#include <cmath>
namespace ns3 {
NS_LOG_COMPONENT_DEFINE ("WsnApplication");
NS_OBJECT_ENSURE_REGISTERED (WsnApplication);
TypeId
WsnApplication::GetTypeId (void)
{
static TypeId tid = TypeId ("ns3::WsnApplication")
.SetParent<Application> ()
.SetGroupName("Applications")
.AddConstructor<WsnApplication> ()
.AddAttribute ("DataRate", "The data rate in on state.",
DataRateValue (DataRate ("500kb/s")),
MakeDataRateAccessor (&WsnApplication::m_cbrRate),
MakeDataRateChecker ())
.AddAttribute ("PacketSize", "The size of packets sent in on state",
UintegerValue (512),
MakeUintegerAccessor (&WsnApplication::m_pktSize),
MakeUintegerChecker<uint32_t> (1))
.AddAttribute ("PacketDeadlineLen", "The deadline range of packets",
IntegerValue (3),
MakeIntegerAccessor (&WsnApplication::m_pktDeadlineLen),
MakeIntegerChecker<int64_t> (1))
.AddAttribute ("PacketDeadlineMin", "The minimum deadline of packets",
IntegerValue (5),
MakeIntegerAccessor (&WsnApplication::m_pktDeadlineMin),
MakeIntegerChecker<int64_t> (1))
.AddAttribute ("Remote", "The address of the destination",
AddressValue (),
MakeAddressAccessor (&WsnApplication::m_peer),
MakeAddressChecker ())
.AddAttribute ("PktGenRate", "Packet generation rate",
DoubleValue (1.0),
MakeDoubleAccessor (&WsnApplication::m_pktGenRate),
MakeDoubleChecker <double>())
.AddAttribute ("PktGenPattern", "Packet generation distribution model",
IntegerValue (0),
MakeIntegerAccessor (&WsnApplication::m_pktGenPattern),
MakeIntegerChecker <int>())
.AddAttribute ("MaxBytes",
"The total number of bytes to send. Once these bytes are sent, "
"no packet is sent again, even in on state. The value zero means "
"that there is no limit.",
UintegerValue (0),
MakeUintegerAccessor (&WsnApplication::m_maxBytes),
MakeUintegerChecker<uint64_t> ())
.AddAttribute ("Protocol", "The type of protocol to use.",
TypeIdValue (UdpSocketFactory::GetTypeId ()),
MakeTypeIdAccessor (&WsnApplication::m_tid),
MakeTypeIdChecker ())
.AddTraceSource ("Tx", "A new packet is created and is sent",
MakeTraceSourceAccessor (&WsnApplication::m_txTrace),
"ns3::Packet::TracedCallback")
.AddTraceSource ("PktCount", "Total packets count",
MakeTraceSourceAccessor (&WsnApplication::m_pktCount),
"ns3::TracedValueCallback::Uint32")
;
return tid;
}
WsnApplication::WsnApplication ()
: m_socket (0),
m_connected (false),
m_residualBits (0),
m_lastStartTime (Seconds (0)),
m_totBytes (0),
m_pktCount (0)
{
NS_LOG_FUNCTION (this);
}
WsnApplication::~WsnApplication()
{
NS_LOG_FUNCTION (this);
//NS_LOG_UNCOND(m_pktCount);
}
void
WsnApplication::SetMaxBytes (uint64_t maxBytes)
{
NS_LOG_FUNCTION (this << maxBytes);
m_maxBytes = maxBytes;
}
Ptr<Socket>
WsnApplication::GetSocket (void) const
{
NS_LOG_FUNCTION (this);
return m_socket;
}
void
WsnApplication::DoDispose (void)
{
NS_LOG_FUNCTION (this);
m_socket = 0;
// chain up
Application::DoDispose ();
}
// Application Methods
void WsnApplication::StartApplication () // Called at time specified by Start
{
NS_LOG_FUNCTION (this);
// Create the socket if not already
if (!m_socket)
{
m_socket = Socket::CreateSocket (GetNode (), m_tid);
if (Inet6SocketAddress::IsMatchingType (m_peer))
{
m_socket->Bind6 ();
}
else if (InetSocketAddress::IsMatchingType (m_peer) ||
PacketSocketAddress::IsMatchingType (m_peer))
{
m_socket->Bind ();
}
m_socket->Connect (m_peer);
m_socket->SetAllowBroadcast (true);
m_socket->ShutdownRecv ();
m_socket->SetConnectCallback (
MakeCallback (&WsnApplication::ConnectionSucceeded, this),
MakeCallback (&WsnApplication::ConnectionFailed, this));
}
m_cbrRateFailSafe = m_cbrRate;
// Insure no pending event
CancelEvents ();
// If we are not yet connected, there is nothing to do here
// The ConnectionComplete upcall will start timers at that time
//if (!m_connected) return;
//ScheduleStartEvent ();
StartSending();
}
void WsnApplication::StopApplication () // Called at time specified by Stop
{
NS_LOG_FUNCTION (this);
CancelEvents ();
if(m_socket != 0)
{
m_socket->Close ();
}
else
{
NS_LOG_WARN ("WsnApplication found null socket to close in StopApplication");
}
}
void WsnApplication::CancelEvents ()
{
NS_LOG_FUNCTION (this);
if (m_sendEvent.IsRunning () && m_cbrRateFailSafe == m_cbrRate )
{
// Cancel the pending send packet event
// Calculate residual bits since last packet sent
Time delta (Simulator::Now () - m_lastStartTime);
int64x64_t bits = delta.To (Time::S) * m_cbrRate.GetBitRate ();
m_residualBits += bits.GetHigh ();
}
m_cbrRateFailSafe = m_cbrRate;
Simulator::Cancel (m_sendEvent);
Simulator::Cancel (m_startStopEvent);
}
// Event handlers
void WsnApplication::StartSending ()
{
NS_LOG_FUNCTION (this);
m_lastStartTime = Simulator::Now ();
ScheduleNextTx (); // Schedule the send packet event
}
// Private helpers
void WsnApplication::ScheduleNextTx ()
{
NS_LOG_FUNCTION (this);
if (m_maxBytes == 0 || m_totBytes < m_maxBytes)
{
uint32_t bits = m_pktSize * 8 - m_residualBits;
NS_LOG_LOGIC ("bits = " << bits);
Time nextTime (Seconds (bits /
static_cast<double>(m_cbrRate.GetBitRate ()))); // Time till next packet
switch(m_pktGenPattern)
{
case 0:
// suppose periodic model
nextTime += Seconds(1.0/m_pktGenRate);
break;
case 1:
// suppose Poisson model
{
Ptr<UniformRandomVariable> m_uniformRandomVariable = CreateObject<UniformRandomVariable> ();
double p = m_uniformRandomVariable->GetValue (0,1);
double poisson, expo = exp(-m_pktGenRate);
int k;
poisson = expo;
for(k=1; poisson < p; k++)
{
double temp = pow(m_pktGenRate, k);
for(int i=1; i<=k; i++) temp /= i;
poisson += temp*expo;
}
k--;
if(k>0) nextTime += Seconds(1.0/k);
else
{
Simulator::Schedule (Seconds(1.0), &WsnApplication::ScheduleNextTx, this);
return;
}
break;
}
default:
break;
}
NS_LOG_LOGIC ("nextTime = " << nextTime);
m_sendEvent = Simulator::Schedule (nextTime, &WsnApplication::SendPacket, this);
}
else
{
// All done, cancel any pending events
StopApplication ();
}
}
void WsnApplication::SendPacket ()
{
NS_LOG_FUNCTION (this);
NS_ASSERT (m_sendEvent.IsExpired ());
//leach::LeachHeader hdr(BooleanValue(false), Vector(0.0,0.0,0.0), Vector(0.0,0.0,0.0), Ipv4Address("255.255.255.255"), Time(0));
leach::LeachHeader hdr;
Ptr<Packet> packet = Create<Packet> (m_pktSize - sizeof(hdr));
Ptr<UniformRandomVariable> m_uniformRandomVariable = CreateObject<UniformRandomVariable> ();
int64_t temp = (m_uniformRandomVariable->GetInteger(0, m_pktDeadlineLen) + m_pktDeadlineMin) + Now ().ToInteger(Time::NS);
m_pktCount++;
hdr.SetDeadline(Time(temp));
NS_LOG_INFO(temp << ", " << hdr.GetDeadline());
packet->AddHeader(hdr);
m_txTrace (packet);
m_socket->Send (packet);
m_totBytes += m_pktSize;
if (InetSocketAddress::IsMatchingType (m_peer))
{
NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds ()
<< "s on-off application sent "
<< packet->GetSize () << " bytes to "
<< InetSocketAddress::ConvertFrom(m_peer).GetIpv4 ()
<< " port " << InetSocketAddress::ConvertFrom (m_peer).GetPort ()
<< " total Tx " << m_totBytes << " bytes");
}
else if (Inet6SocketAddress::IsMatchingType (m_peer))
{
NS_LOG_INFO ("At time " << Simulator::Now ().GetSeconds ()
<< "s on-off application sent "
<< packet->GetSize () << " bytes to "
<< Inet6SocketAddress::ConvertFrom(m_peer).GetIpv6 ()
<< " port " << Inet6SocketAddress::ConvertFrom (m_peer).GetPort ()
<< " total Tx " << m_totBytes << " bytes");
}
m_lastStartTime = Simulator::Now ();
m_residualBits = 0;
ScheduleNextTx ();
}
void WsnApplication::ConnectionSucceeded (Ptr<Socket> socket)
{
NS_LOG_FUNCTION (this << socket);
m_connected = true;
}
void WsnApplication::ConnectionFailed (Ptr<Socket> socket)
{
NS_LOG_FUNCTION (this << socket);
}
} // Namespace ns3