OLD | NEW |
(Empty) | |
| 1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 /*· |
| 3 * Copyright (c) 2012 Michal Paszta |
| 4 *· |
| 5 * This program is free software; you can redistribute it and/or modify |
| 6 * it under the terms of the GNU General Public License version 2 as |
| 7 * published by the Free Software Foundation; |
| 8 * |
| 9 * This program is distributed in the hope that it will be useful, |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 * GNU General Public License for more details. |
| 13 * |
| 14 * You should have received a copy of the GNU General Public License |
| 15 * along with this program; if not, write to the Free Software |
| 16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 *· |
| 18 * Author: Michal Paszta· |
| 19 */ |
| 20 #include <ns3/rns-helper.h> |
| 21 #include "rns.h" |
| 22 #include "rns-header.h" |
| 23 #include "ns3/wifi-net-device.h" |
| 24 #include "ns3/yans-wifi-channel.h" |
| 25 #include "ns3/pointer.h" |
| 26 #include "ns3/integer.h" |
| 27 #include "ns3/double.h" |
| 28 |
| 29 namespace ns3 { |
| 30 namespace rns { |
| 31 |
| 32 |
| 33 /* Ipv4 specialization */ |
| 34 |
| 35 template <> TypeId Rns<Ipv4Address>::GetTypeId(void) |
| 36 { |
| 37 static TypeId tid = TypeId ("ns3::rns::RnsIpv4AddressImpl") |
| 38 .SetParent<Object> () |
| 39 .AddConstructor<Rns<Ipv4Address> > () |
| 40 .AddAttribute("Test","Set to true for testing.", |
| 41 BooleanValue(false), |
| 42 MakeBooleanAccessor(&Rns<Ipv4Address>::IsTest), |
| 43 MakeBooleanChecker()) |
| 44 .AddAttribute("ConnectivityMaintenanceMode","Use automatic radio range est
imation and maintain network's connectivity.", |
| 45 BooleanValue(false), |
| 46 MakeBooleanAccessor(&Rns<Ipv4Address>::ConnectivityMaintenan
ceMode), |
| 47 MakeBooleanChecker()) |
| 48 .AddAttribute("EventTriggerredBroadcast","Whether Rns broadcast should be
triggerred by events or by timer", |
| 49 BooleanValue(false), |
| 50 MakeBooleanAccessor(&Rns<Ipv4Address>::EventTrigerredBroadca
st), |
| 51 MakeBooleanChecker()) |
| 52 .AddAttribute("UpdateInterval","Interval between automatic Rns updates; Rn
s message is sent only if EventTriggerBroadcast is set to false.", |
| 53 TimeValue(Seconds(1.0)), |
| 54 MakeTimeAccessor(&Rns<Ipv4Address>::UpdateInterval), |
| 55 MakeTimeChecker()) |
| 56 .AddAttribute("MaxEntryAge","The age at which a node's position, range and
status are regarded out of date.", |
| 57 TimeValue(Seconds(100)), |
| 58 MakeTimeAccessor(&Rns<Ipv4Address>::MaxEntryAge), |
| 59 MakeTimeChecker()) |
| 60 .AddAttribute("UpdateIntervalTimeScatter","Time scatter of Update Interval
to avoid collision [ms].", |
| 61 DoubleValue(100), |
| 62 MakeDoubleAccessor(&Rns<Ipv4Address>::UpdateIntervalTimeScat
ter), |
| 63 MakeDoubleChecker<double>()) |
| 64 .AddAttribute("AllowedStatusChanges","How often is the status allowed to c
hange per update period.", |
| 65 IntegerValue(5), |
| 66 MakeIntegerAccessor(&Rns<Ipv4Address>::AllowedStatusChanges)
, |
| 67 MakeIntegerChecker<uint32_t>()) |
| 68 .AddAttribute ("RangeEstimationAccuracy","How accurate should the range es
timation be [m].", |
| 69 DoubleValue (0.01), |
| 70 MakeDoubleAccessor (&Rns<Ipv4Address>::RangeEstimationAccura
cy), |
| 71 MakeDoubleChecker<double> ()) |
| 72 .AddAttribute ("ProtocolPeriodicUpdate","Whether the protocol will provide
a periodic update on neighbors situation.", |
| 73 BooleanValue(false), |
| 74 MakeBooleanAccessor(&Rns<Ipv4Address>::ProtocolPeriodicUpdat
e), |
| 75 MakeBooleanChecker()) |
| 76 .AddAttribute ("PositionInRnsHeader","Whether position information has to
be passed in RNS header.", |
| 77 BooleanValue(true), |
| 78 MakeBooleanAccessor(&Rns<Ipv4Address>::PositionInRnsHeader), |
| 79 MakeBooleanChecker()) |
| 80 ; |
| 81 return tid; |
| 82 } |
| 83 |
| 84 template <> |
| 85 void Rns<Ipv4Address>::Ipv4Receive (Ptr<Socket> socket) |
| 86 { |
| 87 while (m_socket->GetRxAvailable () > 0) |
| 88 { |
| 89 Address from; |
| 90 Ptr<Packet> p = m_socket->RecvFrom (0xffffffff, 0, from); |
| 91 Ipv4Header ipv4; |
| 92 p->RemoveHeader (ipv4); |
| 93 Ipv4Address sender = ipv4.GetSource(); |
| 94 |
| 95 RnsHeader<Ipv4Address> rnsMsg; |
| 96 if (PositionInRnsHeader) rnsMsg.SetPositionInRnsHeader(true); |
| 97 p->RemoveHeader (rnsMsg); |
| 98 ReceiveRnsMessage(rnsMsg,sender); |
| 99 } |
| 100 } |
| 101 |
| 102 template <> |
| 103 void Rns<Ipv4Address>::Ipv4Send (RnsHeader<Ipv4Address> h) |
| 104 { |
| 105 Ptr<Packet> p = Create<Packet>(); |
| 106 p->AddHeader(h); |
| 107 |
| 108 //Set power level. |
| 109 uint8_t txPowerLevelBuffer = m_yansWifiPhy->GetTxPowerLevel(); |
| 110 m_yansWifiPhy->SetTxPowerLevel(m_broadcastPowerLevel); |
| 111 |
| 112 Ipv4Address destination = Ipv4Address ("255.255.255.255"); |
| 113 m_socket->SendTo(p,0,InetSocketAddress(destination)); |
| 114 |
| 115 //Reset original power level. |
| 116 m_yansWifiPhy->SetTxPowerLevel(txPowerLevelBuffer); |
| 117 } |
| 118 |
| 119 template <> |
| 120 void Rns<Ipv4Address>::Start(Ptr<Node> node) |
| 121 { |
| 122 if (IsTest) |
| 123 { |
| 124 PositionInRnsHeader = false; |
| 125 m_protocolUpdate = MakeNullCallback<void, Neighbor<Ipv4Address> > (); |
| 126 return; |
| 127 } |
| 128 m_node = node; |
| 129 m_redundantRelatedNodes.clear(); |
| 130 |
| 131 m_broadcastTimer.SetFunction(&Rns<Ipv4Address>::UpdateTimerExpire,this); |
| 132 m_broadcastTimer.Schedule(UpdateInterval |
| 133 - Time (MilliSeconds (UniformVariable().GetValue(0.0, UpdateIntervalT
imeScatter)))); |
| 134 |
| 135 m_agingTimer.SetFunction(&Rns<Ipv4Address>::AgingTimerExpire,this); |
| 136 m_agingTimer.Schedule(Seconds (1)); |
| 137 m_neighbors.SetMaxAge(MaxEntryAge); |
| 138 |
| 139 Ptr<NetDevice> netDev = m_node->GetDevice(0); |
| 140 Ptr<WifiNetDevice> wifiNetDev = DynamicCast<WifiNetDevice>(netDev); |
| 141 |
| 142 //Phy detection |
| 143 Ptr<WifiPhy> wifiPhy = wifiNetDev->GetPhy(); |
| 144 Ptr<YansWifiPhy> yansWifiPhy = DynamicCast<YansWifiPhy>(wifiPhy); |
| 145 if (yansWifiPhy != NULL) |
| 146 { |
| 147 m_yansWifiPhy = yansWifiPhy; |
| 148 } |
| 149 |
| 150 //Mobility Detection |
| 151 Ptr<MobilityModel> AggregatedMobilityModel = m_node->GetObject<MobilityModel>(
); |
| 152 NS_ASSERT(AggregatedMobilityModel != NULL); |
| 153 this->SetMobility(AggregatedMobilityModel); |
| 154 |
| 155 //PropagationLossModel detection |
| 156 Ptr<Channel> wifiCh = wifiNetDev->GetChannel(); |
| 157 Ptr<YansWifiChannel> yansWifiCh = DynamicCast<YansWifiChannel>(wifiCh); |
| 158 PointerValue pval; |
| 159 wifiCh->GetAttribute("PropagationLossModel",pval); |
| 160 Ptr<PropagationLossModel> prop = pval.Get<PropagationLossModel> (); |
| 161 if (prop != NULL) |
| 162 { |
| 163 m_propagationModel = prop; |
| 164 } |
| 165 |
| 166 SetRanges(); |
| 167 RangeUpdate(); |
| 168 |
| 169 //Ipv4 stack detection |
| 170 Ptr<Ipv4> ipv4 = node->GetObject<Ipv4> (); |
| 171 // The assertion should be altered or removed when other stacks are supported. |
| 172 NS_ASSERT_MSG(ipv4 != NULL, "No Ipv4 stack detected.\n"); |
| 173 if (ipv4 != NULL) |
| 174 { |
| 175 //Obtain this nodes own address. |
| 176 m_id = ipv4->GetAddress(1,0).GetLocal(); |
| 177 |
| 178 m_socket = Socket::CreateSocket (m_node, |
| 179 TypeId::LookupByName ("ns3::Ipv4RawSocketFactory")); |
| 180 NS_ASSERT_MSG (m_socket != 0,"Socket not created."); |
| 181 m_socket->SetAttribute ("Protocol", UintegerValue (4)); // ip |
| 182 m_socket->SetRecvCallback (MakeCallback (&Rns::Ipv4Receive, this)); |
| 183 InetSocketAddress src = InetSocketAddress (Ipv4Address::GetAny (), 0); |
| 184 int status; |
| 185 status = m_socket->Bind (src); |
| 186 NS_ASSERT (status != -1); |
| 187 m_socket->SetAllowBroadcast(true); |
| 188 |
| 189 return; |
| 190 } |
| 191 } |
| 192 |
| 193 template <> |
| 194 uint32_t NeighborsTable<Ipv4Address>::Ipv4ToNodeNumber(Ipv4Address addr) const |
| 195 { |
| 196 uint32_t n = addr.Get(); |
| 197 n = n & 0x000000FF; |
| 198 return n-1; |
| 199 } |
| 200 |
| 201 template <> |
| 202 uint32_t Neighbor<Ipv4Address>::Ipv4ToNodeNumber(Ipv4Address addr) const |
| 203 { |
| 204 uint32_t n = addr.Get(); |
| 205 n = n & 0x000000FF; |
| 206 return n-1; |
| 207 } |
| 208 |
| 209 template <> |
| 210 uint32_t Rns<Ipv4Address>::Ipv4ToNodeNumber(Ipv4Address addr) const |
| 211 { |
| 212 uint32_t n = addr.Get(); |
| 213 n = n & 0x000000FF; |
| 214 return n-1; |
| 215 } |
| 216 |
| 217 template <> |
| 218 TypeId RnsHeader<Ipv4Address>::GetTypeId (void) |
| 219 { |
| 220 static TypeId tid = TypeId ("ns3::rns::RnsHeaderIpv4Address") |
| 221 .SetParent<Header> () |
| 222 .AddConstructor<RnsHeader>(); |
| 223 return tid; |
| 224 } |
| 225 |
| 226 template <> |
| 227 uint32_t RnsHeader<Ipv4Address>::GetSerializedSize () const |
| 228 { |
| 229 uint32_t size = 1 + sizeof(Ipv4Address) + 4; //status + id + range |
| 230 if (PositionInRnsHeader) |
| 231 { |
| 232 size += 8; // + position |
| 233 } |
| 234 size += sizeof(uint32_t); //entries_number |
| 235 size += m_entriesNumber*sizeof(Ipv4Address); |
| 236 return size; |
| 237 } |
| 238 |
| 239 template <> |
| 240 void RnsHeader<Ipv4Address>::Serialize (Buffer::Iterator start) const |
| 241 { |
| 242 WriteTo (start,m_id); |
| 243 start.WriteU8(m_status); |
| 244 start.WriteHtonU32(m_range); |
| 245 if (PositionInRnsHeader) |
| 246 { |
| 247 start.WriteHtonU32(m_x); |
| 248 start.WriteHtonU32(m_y); |
| 249 } |
| 250 start.WriteHtonU32(m_entriesNumber); |
| 251 for (std::vector<Ipv4Address>::const_iterator i = m_rrns.begin(); |
| 252 i != m_rrns.end(); i++) |
| 253 { |
| 254 WriteTo(start,*i); |
| 255 } |
| 256 } |
| 257 |
| 258 template <> |
| 259 uint32_t RnsHeader<Ipv4Address>::Deserialize (Buffer::Iterator start) |
| 260 { |
| 261 Buffer::Iterator i= start; |
| 262 |
| 263 ReadFrom (i,m_id); |
| 264 m_status = i.ReadU8(); |
| 265 m_range = i.ReadNtohU32(); |
| 266 if (PositionInRnsHeader) |
| 267 { |
| 268 m_x = i.ReadNtohU32(); |
| 269 m_y = i.ReadNtohU32(); |
| 270 } |
| 271 m_entriesNumber = i.ReadNtohU32(); |
| 272 for (uint32_t k = 0; k < m_entriesNumber; k++) |
| 273 { |
| 274 Ipv4Address addr; |
| 275 ReadFrom(i,addr); |
| 276 m_rrns.push_back(addr); |
| 277 } |
| 278 |
| 279 |
| 280 uint32_t dist = i.GetDistanceFrom(start); |
| 281 NS_ASSERT(dist == GetSerializedSize()); |
| 282 return dist; |
| 283 } |
| 284 |
| 285 #ifndef IPV4_REGISTERED |
| 286 #define IPV4_REGISTERED |
| 287 typedef class RnsHeader<Ipv4Address> RnsHeaderIpv4AddressImpl; |
| 288 NS_OBJECT_ENSURE_REGISTERED (RnsHeaderIpv4AddressImpl); |
| 289 typedef class Rns<Ipv4Address> RnsIpv4AddressImpl; |
| 290 NS_OBJECT_ENSURE_REGISTERED(RnsIpv4AddressImpl); |
| 291 #endif |
| 292 |
| 293 |
| 294 |
| 295 /* uint32_t specialization */ |
| 296 |
| 297 template <> TypeId Rns<uint32_t>::GetTypeId(void) |
| 298 { |
| 299 static TypeId tid = TypeId ("ns3::rns::RnsUint32Impl") |
| 300 .SetParent<Object> () |
| 301 .AddConstructor<Rns<uint32_t> > () |
| 302 .AddAttribute("Test","Set to true for testing.", |
| 303 BooleanValue(false), |
| 304 MakeBooleanAccessor(&Rns<uint32_t>::IsTest), |
| 305 MakeBooleanChecker()) |
| 306 .AddAttribute("ConnectivityMaintenanceMode","Use automatic radio range est
imation and maintain network's connectivity.", |
| 307 BooleanValue(false), |
| 308 MakeBooleanAccessor(&Rns<uint32_t>::ConnectivityMaintenanceM
ode), |
| 309 MakeBooleanChecker()) |
| 310 .AddAttribute("EventTriggerredBroadcast","Whether Rns broadcast should be
triggerred by events or by timer", |
| 311 BooleanValue(false), |
| 312 MakeBooleanAccessor(&Rns<uint32_t>::EventTrigerredBroadcast)
, |
| 313 MakeBooleanChecker()) |
| 314 .AddAttribute("UpdateInterval","Interval between automatic Rns updates; Rn
s message is sent only if EventTriggerBroadcast is set to false.", |
| 315 TimeValue(Seconds(1.0)), |
| 316 MakeTimeAccessor(&Rns<uint32_t>::UpdateInterval), |
| 317 MakeTimeChecker()) |
| 318 .AddAttribute("MaxEntryAge","The age at which a node's position, range and
status are regarded out of date.", |
| 319 TimeValue(Seconds(100)), |
| 320 MakeTimeAccessor(&Rns<uint32_t>::MaxEntryAge), |
| 321 MakeTimeChecker()) |
| 322 .AddAttribute("UpdateIntervalTimeScatter","Time scatter of Update Interval
to avoid collision.", |
| 323 DoubleValue(100), |
| 324 MakeDoubleAccessor(&Rns<uint32_t>::UpdateIntervalTimeScatter
), |
| 325 MakeDoubleChecker<double>()) |
| 326 .AddAttribute ("RangeEstimationAccuracy","How accurate should the range es
timation be [m].", |
| 327 DoubleValue (0.01), |
| 328 MakeDoubleAccessor (&Rns<uint32_t>::RangeEstimationAccuracy)
, |
| 329 MakeDoubleChecker<double> ()) |
| 330 .AddAttribute ("ProtocolPeriodicUpdate","Whether the protocol will provide
a periodic update on neighbors situation.", |
| 331 BooleanValue(false), |
| 332 MakeBooleanAccessor(&Rns<uint32_t>::ProtocolPeriodicUpdate), |
| 333 MakeBooleanChecker()) |
| 334 .AddAttribute ("PositionInRnsHeader","Whether position information has to
be passed in RNS header.", |
| 335 BooleanValue(true), |
| 336 MakeBooleanAccessor(&Rns<uint32_t>::PositionInRnsHeader), |
| 337 MakeBooleanChecker()) |
| 338 ; |
| 339 return tid; |
| 340 } |
| 341 |
| 342 template <> |
| 343 void Rns<uint32_t>::Start(Ptr<Node> node) |
| 344 { |
| 345 m_node = node; |
| 346 |
| 347 if (IsTest) |
| 348 { |
| 349 PositionInRnsHeader = false; |
| 350 m_protocolUpdate = MakeNullCallback<void, Neighbor<uint32_t> > (); |
| 351 return; |
| 352 } |
| 353 |
| 354 m_broadcastTimer.SetFunction(&Rns<uint32_t>::UpdateTimerExpire,this); |
| 355 m_broadcastTimer.Schedule(UpdateInterval - |
| 356 Time (MilliSeconds (UniformVariable ().GetInteger (0, 10))/100.0)); |
| 357 |
| 358 m_agingTimer.SetFunction(&Rns<uint32_t>::AgingTimerExpire,this); |
| 359 m_agingTimer.Schedule(Seconds (1)); |
| 360 m_neighbors.SetMaxAge(MaxEntryAge); |
| 361 |
| 362 SetRanges(); |
| 363 } |
| 364 |
| 365 template <> |
| 366 void Rns<uint32_t>::Ipv4Receive (Ptr<Socket> socket) |
| 367 { |
| 368 NS_FATAL_ERROR("Ipv4Receive message used with uint32_t IdType."); |
| 369 } |
| 370 |
| 371 template <> |
| 372 void Rns<uint32_t>::Ipv4Send (RnsHeader<uint32_t> h) |
| 373 { |
| 374 NS_FATAL_ERROR("Ipv4Send message used with uint32_t IdType."); |
| 375 } |
| 376 |
| 377 template <> |
| 378 uint32_t Rns<uint32_t>::Ipv4ToNodeNumber(uint32_t addr) const |
| 379 { |
| 380 return 0; |
| 381 } |
| 382 |
| 383 template <> |
| 384 uint32_t NeighborsTable<uint32_t>::Ipv4ToNodeNumber(uint32_t addr) const |
| 385 { |
| 386 return addr; |
| 387 } |
| 388 |
| 389 template <> |
| 390 uint32_t Neighbor<uint32_t>::Ipv4ToNodeNumber(uint32_t addr) const |
| 391 { |
| 392 return addr; |
| 393 } |
| 394 |
| 395 template <> |
| 396 TypeId RnsHeader<uint32_t>::GetTypeId (void) |
| 397 { |
| 398 static TypeId tid = TypeId ("ns3::rns::RnsHeaderU32") |
| 399 .SetParent<Header> () |
| 400 .AddConstructor<RnsHeader>(); |
| 401 return tid; |
| 402 } |
| 403 |
| 404 template <> |
| 405 uint32_t RnsHeader<uint32_t>::GetSerializedSize () const |
| 406 { |
| 407 uint32_t size = 1 + sizeof(uint32_t) + 4; //status + id + range |
| 408 if (PositionInRnsHeader) |
| 409 { |
| 410 size += 8; // + position |
| 411 } |
| 412 size += sizeof(uint32_t); //entries_number |
| 413 size += m_entriesNumber*sizeof(uint32_t); |
| 414 return size; |
| 415 } |
| 416 |
| 417 template <> |
| 418 void RnsHeader<uint32_t>::Serialize (Buffer::Iterator start) const |
| 419 { |
| 420 start.WriteHtonU32(m_id); |
| 421 start.WriteU8(m_status); |
| 422 start.WriteHtonU32(m_range); |
| 423 if (PositionInRnsHeader) |
| 424 { |
| 425 start.WriteHtonU32(m_x); |
| 426 start.WriteHtonU32(m_y); |
| 427 } |
| 428 start.WriteHtonU32(m_entriesNumber); |
| 429 for (std::vector<uint32_t>::const_iterator i = m_rrns.begin(); |
| 430 i != m_rrns.end(); i++) |
| 431 { |
| 432 start.WriteHtonU32(*i); |
| 433 } |
| 434 } |
| 435 |
| 436 template <> |
| 437 uint32_t RnsHeader<uint32_t>::Deserialize (Buffer::Iterator start) |
| 438 { |
| 439 Buffer::Iterator i= start; |
| 440 |
| 441 m_id = i.ReadNtohU32(); |
| 442 m_status = i.ReadU8(); |
| 443 m_range = i.ReadNtohU32(); |
| 444 if (PositionInRnsHeader) |
| 445 { |
| 446 m_x = i.ReadNtohU32(); |
| 447 m_y = i.ReadNtohU32(); |
| 448 } |
| 449 m_entriesNumber = i.ReadNtohU32(); |
| 450 for (uint32_t k = 0; k < m_entriesNumber; k++) |
| 451 { |
| 452 uint32_t addr; |
| 453 addr = i.ReadNtohU32(); |
| 454 m_rrns.push_back(addr); |
| 455 } |
| 456 |
| 457 uint32_t dist = i.GetDistanceFrom(start); |
| 458 NS_ASSERT(dist == GetSerializedSize()); |
| 459 return dist; |
| 460 } |
| 461 |
| 462 |
| 463 |
| 464 |
| 465 #ifndef UINT32_REGISTERED |
| 466 #define UINT32_REGISTERED |
| 467 typedef class RnsHeader<uint32_t> RnsHeaderUint32Impl; |
| 468 NS_OBJECT_ENSURE_REGISTERED (RnsHeaderUint32Impl); |
| 469 typedef class Rns<uint32_t> RnsUint32Impl; |
| 470 NS_OBJECT_ENSURE_REGISTERED(RnsUint32Impl); |
| 471 #endif |
| 472 |
| 473 } // namespace rns |
| 474 /* Helper constructor specializations */ |
| 475 |
| 476 template <> RnsHelper<Ipv4Address>::RnsHelper () |
| 477 { |
| 478 m_agentFactory.SetTypeId ("ns3::rns::RnsIpv4AddressImpl"); |
| 479 } |
| 480 |
| 481 template <> RnsHelper<uint32_t>::RnsHelper () |
| 482 { |
| 483 m_agentFactory.SetTypeId ("ns3::rns::RnsUint32Impl"); |
| 484 } |
| 485 |
| 486 |
| 487 |
| 488 } // namespace ns3 |
OLD | NEW |