OLD | NEW |
(Empty) | |
| 1 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ |
| 2 /* |
| 3 * Copyright (c) Waterford Institute of Technology, 2013, Julien Mineraud, BioFI
NT. |
| 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: Julien Mineraud <julien.mineraud@gmail.com> |
| 19 * |
| 20 * Acknowledgements: |
| 21 * This work has received support from Science Foundation Ireland via the |
| 22 * "A Biologically inspired framework supporting network management for |
| 23 * the Future Internet" starting investigator award (grant no. 09/SIRG/I1643). |
| 24 */ |
| 25 |
| 26 #include "ipv4-pgbr-static-gradient-calculator.h" |
| 27 |
| 28 namespace ns3 { |
| 29 namespace pgbr { |
| 30 |
| 31 NS_OBJECT_ENSURE_REGISTERED (Ipv4StaticGradientCalculator); |
| 32 |
| 33 TypeId |
| 34 Ipv4StaticGradientCalculator::GetTypeId (void) |
| 35 { |
| 36 static TypeId tid = |
| 37 TypeId ("ns3::pgbr::Ipv4StaticGradientCalculator") |
| 38 .SetParent<Ipv4GradientCalculator> () |
| 39 .AddConstructor<Ipv4StaticGradientCalculator> () |
| 40 ; |
| 41 return tid; |
| 42 } |
| 43 |
| 44 Ipv4StaticGradientCalculator::Ipv4StaticGradientCalculator () |
| 45 { |
| 46 } |
| 47 |
| 48 Ipv4StaticGradientCalculator::~Ipv4StaticGradientCalculator () |
| 49 { |
| 50 } |
| 51 /** |
| 52 * This function returns the gradient value of a neighbour for a pgbr packet. |
| 53 * |
| 54 * @returns return the gradient value if the destination is known, -1 otherwise/ |
| 55 */ |
| 56 double |
| 57 Ipv4StaticGradientCalculator::GetGradient (const Ipv4Neighbour &neighbour, |
| 58 Ptr<Packet> pgbrPacket, const MessageHeader &pgbrHeader, |
| 59 Ptr<const NetDevice> idev) |
| 60 { |
| 61 switch (pgbrHeader.GetMessageType()) |
| 62 { |
| 63 case MessageHeader::GRADIENT_CALCULATOR_MESSAGE: |
| 64 { |
| 65 return -1; |
| 66 } |
| 67 case MessageHeader::DISCOVERY_MESSAGE: |
| 68 { |
| 69 DiscoveryHeader discoveryHeader; |
| 70 if (!pgbrPacket->PeekHeader (discoveryHeader) > 0) |
| 71 { |
| 72 return -1; |
| 73 } |
| 74 DestinationNeighbour dn; |
| 75 dn.destination = discoveryHeader.GetDestination (); |
| 76 dn.neighbour = neighbour; |
| 77 std::map<DestinationNeighbour, double>::iterator it = m_gradients.find (dn
); |
| 78 if (it != m_gradients.end()) |
| 79 { |
| 80 return it->second; |
| 81 } |
| 82 else |
| 83 { |
| 84 return -1; |
| 85 } |
| 86 } |
| 87 default: |
| 88 return -1; |
| 89 } |
| 90 } |
| 91 |
| 92 void |
| 93 Ipv4StaticGradientCalculator::SetGradient (const Ipv4Address &destination, const
Ipv4Neighbour &neighbour, double gradient) |
| 94 { |
| 95 DestinationNeighbour dn; |
| 96 dn.destination = destination; |
| 97 dn.neighbour = neighbour; |
| 98 std::map<DestinationNeighbour, double>::iterator lowerBound = m_gradients.lowe
r_bound (dn); |
| 99 if (lowerBound != m_gradients.end () && !(m_gradients.key_comp()(dn, lowerBoun
d->first))) |
| 100 { |
| 101 lowerBound->second = gradient; |
| 102 } |
| 103 else |
| 104 { |
| 105 //I need to insert new entry |
| 106 m_gradients.insert (lowerBound, std::map<DestinationNeighbour, double>::va
lue_type (dn, gradient)); |
| 107 } |
| 108 } |
| 109 |
| 110 void |
| 111 Ipv4StaticGradientCalculator::UnsetGradient (const Ipv4Address &destination, con
st Ipv4Neighbour &neighbour) |
| 112 { |
| 113 DestinationNeighbour dn; |
| 114 dn.destination = destination; |
| 115 dn.neighbour = neighbour; |
| 116 m_gradients.erase (dn); |
| 117 } |
| 118 |
| 119 bool |
| 120 operator < (const Ipv4StaticGradientCalculator::DestinationNeighbour &d1, |
| 121 const Ipv4StaticGradientCalculator::DestinationNeighbour &d2) |
| 122 { |
| 123 if (d1.destination < d2.destination) |
| 124 { |
| 125 return true; |
| 126 } |
| 127 else if (d1.destination != d2.destination) |
| 128 { |
| 129 return false; |
| 130 } |
| 131 if (d1.neighbour.GetInterface () < d2.neighbour.GetInterface ()) |
| 132 { |
| 133 return true; |
| 134 } |
| 135 else if (d1.neighbour.GetInterface () > d2.neighbour.GetInterface ()) |
| 136 { |
| 137 return false; |
| 138 } |
| 139 return d1.neighbour.GetAddress () < d2.neighbour.GetAddress (); |
| 140 } |
| 141 |
| 142 bool |
| 143 operator == (const Ipv4StaticGradientCalculator::DestinationNeighbour &d1, |
| 144 const Ipv4StaticGradientCalculator::DestinationNeighbour &d2) |
| 145 { |
| 146 return d1.destination == d2.destination && |
| 147 d1.neighbour.GetInterface () == d2.neighbour.GetInterface () && |
| 148 d1.neighbour.GetAddress () == d2.neighbour.GetAddress (); |
| 149 } |
| 150 |
| 151 }} // namespace pgbr, ns3 |
OLD | NEW |