LEFT | RIGHT |
(no file at all) | |
| 1 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ |
| 2 /* |
| 3 * Copyright (c) 2010 Network Security Lab, University of Washington, Seattle. |
| 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 * Authors: Sidharth Nabar <snabar@uw.edu>, He Wu <mdzz@u.washington.edu> |
| 19 */ |
| 20 |
| 21 #include "jamming-mitigation-helper.h" |
| 22 #include "ns3/energy-source-container.h" |
| 23 #include "ns3/log.h" |
| 24 #include "ns3/config.h" |
| 25 #include "ns3/names.h" |
| 26 |
| 27 NS_LOG_COMPONENT_DEFINE ("JammingMitigationHelper"); |
| 28 |
| 29 namespace ns3 { |
| 30 |
| 31 JammingMitigationHelper::JammingMitigationHelper () |
| 32 { |
| 33 m_jammingMitigation.SetTypeId ("ns3::MitigateByChannelHop"); |
| 34 } |
| 35 |
| 36 JammingMitigationHelper::~JammingMitigationHelper () |
| 37 { |
| 38 } |
| 39 |
| 40 void |
| 41 JammingMitigationHelper::SetJammingMitigationType (std::string type) |
| 42 { |
| 43 NS_LOG_FUNCTION (this); |
| 44 m_jammingMitigation.SetTypeId (type); |
| 45 } |
| 46 |
| 47 std::string |
| 48 JammingMitigationHelper::GetJammingMitigationType (void) const |
| 49 { |
| 50 NS_LOG_FUNCTION (this); |
| 51 return m_jammingMitigation.GetTypeId ().GetName (); |
| 52 } |
| 53 |
| 54 void |
| 55 JammingMitigationHelper::Set (std::string name, const AttributeValue &v) |
| 56 { |
| 57 NS_LOG_FUNCTION (this); |
| 58 m_jammingMitigation.Set (name, v); |
| 59 } |
| 60 |
| 61 JammingMitigationContainer |
| 62 JammingMitigationHelper::Install (Ptr<Node> node) const |
| 63 { |
| 64 NS_LOG_FUNCTION (this << node); |
| 65 return Install (NodeContainer (node)); |
| 66 } |
| 67 |
| 68 JammingMitigationContainer |
| 69 JammingMitigationHelper::Install (NodeContainer c) const |
| 70 { |
| 71 NS_LOG_DEBUG (this); |
| 72 JammingMitigationContainer container; |
| 73 for (NodeContainer::Iterator i = c.Begin (); i != c.End (); ++i) |
| 74 { |
| 75 Ptr<JammingMitigation> mitigator = DoInstall (*i); |
| 76 container.Add (mitigator); |
| 77 } |
| 78 return container; |
| 79 } |
| 80 |
| 81 JammingMitigationContainer |
| 82 JammingMitigationHelper::Install (std::string nodeName) const |
| 83 { |
| 84 NS_LOG_DEBUG (this); |
| 85 Ptr<Node> node = Names::Find<Node> (nodeName); |
| 86 return Install (node); |
| 87 } |
| 88 |
| 89 void |
| 90 JammingMitigationHelper::EnableLogComponents (void) |
| 91 { |
| 92 LogComponentEnable ("JammingMitigation", LOG_LEVEL_ALL); |
| 93 LogComponentEnable ("MitigateByChannelHop", LOG_LEVEL_ALL); |
| 94 } |
| 95 |
| 96 /* |
| 97 * Private function starts here. |
| 98 */ |
| 99 |
| 100 Ptr<JammingMitigation> |
| 101 JammingMitigationHelper::DoInstall (Ptr<Node> node) const |
| 102 { |
| 103 NS_LOG_FUNCTION (this << node); |
| 104 NS_LOG_DEBUG ("JammingMitigationHelper:Installing mitigation object onto node
#" << |
| 105 node->GetId ()); |
| 106 |
| 107 // create jammer object |
| 108 Ptr<JammingMitigation> mitigation = m_jammingMitigation.Create<JammingMitigati
on> (); |
| 109 if (mitigation == NULL) |
| 110 { |
| 111 NS_FATAL_ERROR ("Can not create requested mitigation object:" << |
| 112 m_jammingMitigation.GetTypeId ().GetName ()); |
| 113 return NULL; |
| 114 } |
| 115 |
| 116 // set jamming mitigation ID |
| 117 mitigation->SetId (node->GetId ()); |
| 118 |
| 119 // check if utility already exists |
| 120 Ptr<WirelessModuleUtility> util = node->GetObject<WirelessModuleUtility> (); |
| 121 if (util == 0) |
| 122 { |
| 123 NS_LOG_DEBUG ("JammingMitigationHelper:WirelessModuleUtility doesn't exist
, creating!"); |
| 124 // utility doesn't exist, install one. |
| 125 WirelessModuleUtilityHelper utilHelper; |
| 126 utilHelper.Install (node); |
| 127 // get pointer to utility |
| 128 util = node->GetObject<WirelessModuleUtility> (); |
| 129 } |
| 130 else |
| 131 { |
| 132 NS_LOG_DEBUG ("JammingMitigationHelper:WirelessModuleUtility already insta
lled!"); |
| 133 } |
| 134 // set pointer to utility |
| 135 mitigation->SetUtility (util); |
| 136 // set callbacks |
| 137 util->SetStartRxCallback (MakeCallback (&ns3::JammingMitigation::StartRxHandle
r, mitigation)); |
| 138 util->SetEndRxCallback (MakeCallback (&ns3::JammingMitigation::EndRxHandler, m
itigation)); |
| 139 util->SetEndTxCallback (MakeCallback (&ns3::JammingMitigation::EndTxHandler, m
itigation)); |
| 140 |
| 141 // check & set energy model |
| 142 Ptr<EnergySourceContainer> sourceContainer = node->GetObject<EnergySourceConta
iner> (); |
| 143 if (sourceContainer == NULL) |
| 144 { |
| 145 NS_FATAL_ERROR ("JammingMitigationHelper:Energy source doesn't exist!"); |
| 146 } |
| 147 else |
| 148 { |
| 149 /* |
| 150 * XXX Force single energy source when using jammers. |
| 151 */ |
| 152 NS_ASSERT (sourceContainer->GetN () == 1); |
| 153 mitigation->SetEnergySource (sourceContainer->Get (0)); |
| 154 } |
| 155 |
| 156 // aggregate jammer to node |
| 157 node->AggregateObject (mitigation); |
| 158 |
| 159 return mitigation; |
| 160 } |
| 161 |
| 162 } // namespace ns3 |
LEFT | RIGHT |