Alexander Afanasyev | c74a602 | 2011-08-15 20:01:35 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2005,2006,2007 INRIA |
| 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 | * |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 18 | * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 19 | * |
| 20 | */ |
| 21 | |
Alexander Afanasyev | 9825610 | 2011-08-14 01:00:02 -0700 | [diff] [blame] | 22 | #include "ccnx-face.h" |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 23 | |
Alexander Afanasyev | 19426ef | 2011-11-23 20:55:28 -0800 | [diff] [blame] | 24 | #include "ns3/packet.h" |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 25 | #include "ns3/log.h" |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 26 | #include "ns3/node.h" |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 27 | #include "ns3/assert.h" |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 28 | |
Alexander Afanasyev | 23d2b54 | 2011-12-07 18:54:46 -0800 | [diff] [blame] | 29 | #include <boost/ref.hpp> |
| 30 | |
Alexander Afanasyev | 9825610 | 2011-08-14 01:00:02 -0700 | [diff] [blame] | 31 | NS_LOG_COMPONENT_DEFINE ("CcnxFace"); |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 32 | |
| 33 | namespace ns3 { |
| 34 | |
Alexander Afanasyev | bdc0d98 | 2011-12-16 01:15:26 -0800 | [diff] [blame^] | 35 | TypeId |
| 36 | CcnxFace::GetTypeId () |
| 37 | { |
| 38 | static TypeId tid = TypeId ("ns3::CcnxFace") |
| 39 | .SetParent<Object> () |
| 40 | .SetGroupName ("Ccnx") |
| 41 | ; |
| 42 | return tid; |
| 43 | } |
| 44 | |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 45 | /** |
Alexander Afanasyev | 9825610 | 2011-08-14 01:00:02 -0700 | [diff] [blame] | 46 | * By default, Ccnx face are created in the "down" state |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 47 | * with no IP addresses. Before becoming useable, the user must |
| 48 | * invoke SetUp on them once an Ccnx address and mask have been set. |
| 49 | */ |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 50 | CcnxFace::CcnxFace (Ptr<Node> node) |
Alexander Afanasyev | 19426ef | 2011-11-23 20:55:28 -0800 | [diff] [blame] | 51 | : m_node (node) |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 52 | , m_bucket (0.0) |
| 53 | , m_bucketMax (-1.0) |
| 54 | , m_bucketLeak (0.0) |
| 55 | , m_protocolHandler (MakeNullCallback<void,const Ptr<CcnxFace>&,const Ptr<const Packet>&> ()) |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 56 | , m_ifup (false) |
| 57 | , m_id ((uint32_t)-1) |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 58 | { |
| 59 | NS_LOG_FUNCTION (this); |
Alexander Afanasyev | 19426ef | 2011-11-23 20:55:28 -0800 | [diff] [blame] | 60 | |
| 61 | NS_ASSERT_MSG (node != 0, "node cannot be NULL. Check the code"); |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Alexander Afanasyev | 9825610 | 2011-08-14 01:00:02 -0700 | [diff] [blame] | 64 | CcnxFace::~CcnxFace () |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 65 | { |
| 66 | NS_LOG_FUNCTION_NOARGS (); |
| 67 | } |
| 68 | |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 69 | CcnxFace::CcnxFace (const CcnxFace &) |
| 70 | { |
| 71 | } |
| 72 | |
| 73 | CcnxFace& CcnxFace::operator= (const CcnxFace &) |
| 74 | { |
| 75 | return *this; |
| 76 | } |
| 77 | |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 78 | void |
| 79 | CcnxFace::RegisterProtocolHandler (ProtocolHandler handler) |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 80 | { |
Alexander Afanasyev | 19426ef | 2011-11-23 20:55:28 -0800 | [diff] [blame] | 81 | NS_LOG_FUNCTION_NOARGS (); |
| 82 | |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 83 | m_protocolHandler = handler; |
| 84 | } |
| 85 | |
| 86 | bool |
Alexander Afanasyev | 19426ef | 2011-11-23 20:55:28 -0800 | [diff] [blame] | 87 | CcnxFace::IsBelowLimit () |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 88 | { |
Alexander Afanasyev | 19426ef | 2011-11-23 20:55:28 -0800 | [diff] [blame] | 89 | NS_LOG_FUNCTION_NOARGS (); |
| 90 | |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 91 | /// \todo Implement tracing, if requested |
Alexander Afanasyev | bdc0d98 | 2011-12-16 01:15:26 -0800 | [diff] [blame^] | 92 | |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 93 | if (!IsUp ()) |
| 94 | return false; |
Alexander Afanasyev | 19426ef | 2011-11-23 20:55:28 -0800 | [diff] [blame] | 95 | |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 96 | if (m_bucketMax > 0) |
| 97 | { |
Alexander Afanasyev | e67a97f | 2011-11-29 14:28:59 -0800 | [diff] [blame] | 98 | //NS_LOG_DEBUG ("Limits enabled: " << m_bucketMax << ", current: " << m_bucket); |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 99 | if (m_bucket+1.0 > m_bucketMax) |
Alexander Afanasyev | c39f0b4 | 2011-11-28 12:51:12 -0800 | [diff] [blame] | 100 | { |
Alexander Afanasyev | e67a97f | 2011-11-29 14:28:59 -0800 | [diff] [blame] | 101 | //NS_LOG_DEBUG ("Returning false"); |
Alexander Afanasyev | c39f0b4 | 2011-11-28 12:51:12 -0800 | [diff] [blame] | 102 | return false; |
| 103 | } |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 104 | |
| 105 | m_bucket += 1.0; |
| 106 | } |
| 107 | |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 108 | return true; |
| 109 | } |
| 110 | |
Alexander Afanasyev | b5703a9 | 2011-11-25 16:46:15 -0800 | [diff] [blame] | 111 | void |
| 112 | CcnxFace::LeakBucket (const Time &interval) |
| 113 | { |
| 114 | const double leak = m_bucketLeak * interval.ToDouble (Time::S); |
Alexander Afanasyev | c39f0b4 | 2011-11-28 12:51:12 -0800 | [diff] [blame] | 115 | m_bucket = std::max (0.0, m_bucket - leak); |
Alexander Afanasyev | b5703a9 | 2011-11-25 16:46:15 -0800 | [diff] [blame] | 116 | |
Alexander Afanasyev | e67a97f | 2011-11-29 14:28:59 -0800 | [diff] [blame] | 117 | // NS_LOG_DEBUG ("max: " << m_bucketMax << ", Current bucket: " << m_bucket << ", leak size: " << leak << ", interval: " << interval << ", " << m_bucketLeak); |
Alexander Afanasyev | b5703a9 | 2011-11-25 16:46:15 -0800 | [diff] [blame] | 118 | } |
| 119 | |
Alexander Afanasyev | c39f0b4 | 2011-11-28 12:51:12 -0800 | [diff] [blame] | 120 | void |
| 121 | CcnxFace::SetBucketMax (double bucket) |
| 122 | { |
| 123 | NS_LOG_FUNCTION (this << bucket); |
| 124 | m_bucketMax = bucket; |
| 125 | } |
| 126 | |
| 127 | void |
| 128 | CcnxFace::SetBucketLeak (double leak) |
| 129 | { |
| 130 | NS_LOG_FUNCTION (this << leak); |
| 131 | m_bucketLeak = leak; |
| 132 | } |
| 133 | |
| 134 | void |
| 135 | CcnxFace::LeakBucketByOnePacket () |
| 136 | { |
| 137 | m_bucket = std::max (0.0, m_bucket-1.0); |
| 138 | } |
| 139 | |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 140 | bool |
Alexander Afanasyev | 19426ef | 2011-11-23 20:55:28 -0800 | [diff] [blame] | 141 | CcnxFace::Send (Ptr<Packet> packet) |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 142 | { |
Alexander Afanasyev | 23d2b54 | 2011-12-07 18:54:46 -0800 | [diff] [blame] | 143 | NS_LOG_FUNCTION (boost::cref (*this) << packet << packet->GetSize ()); |
Alexander Afanasyev | 19426ef | 2011-11-23 20:55:28 -0800 | [diff] [blame] | 144 | |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 145 | /// \todo Implement tracing, if requested |
| 146 | |
| 147 | if (!IsUp ()) |
| 148 | return false; |
| 149 | |
| 150 | SendImpl (packet); |
| 151 | return true; |
| 152 | } |
| 153 | |
| 154 | bool |
Alexander Afanasyev | 19426ef | 2011-11-23 20:55:28 -0800 | [diff] [blame] | 155 | CcnxFace::Receive (const Ptr<const Packet> &packet) |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 156 | { |
Alexander Afanasyev | 23d2b54 | 2011-12-07 18:54:46 -0800 | [diff] [blame] | 157 | NS_LOG_FUNCTION (boost::cref (*this) << packet << packet->GetSize ()); |
Alexander Afanasyev | 19426ef | 2011-11-23 20:55:28 -0800 | [diff] [blame] | 158 | |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 159 | /// \todo Implement tracing, if requested |
| 160 | |
| 161 | if (!IsUp ()) |
| 162 | return false; |
| 163 | |
| 164 | m_protocolHandler (this, packet); |
Alexander Afanasyev | 19426ef | 2011-11-23 20:55:28 -0800 | [diff] [blame] | 165 | |
| 166 | return true; |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 169 | // void |
| 170 | // CcnxFace::SetMetric (uint16_t metric) |
| 171 | // { |
| 172 | // NS_LOG_FUNCTION (metric); |
| 173 | // m_metric = metric; |
| 174 | // } |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 175 | |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 176 | // uint16_t |
| 177 | // CcnxFace::GetMetric (void) const |
| 178 | // { |
| 179 | // NS_LOG_FUNCTION_NOARGS (); |
| 180 | // return m_metric; |
| 181 | // } |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 182 | |
| 183 | /** |
Alexander Afanasyev | 9825610 | 2011-08-14 01:00:02 -0700 | [diff] [blame] | 184 | * These are face states and may be distinct from |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 185 | * NetDevice states, such as found in real implementations |
Alexander Afanasyev | 9825610 | 2011-08-14 01:00:02 -0700 | [diff] [blame] | 186 | * (where the device may be down but face state is still up). |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 187 | */ |
| 188 | bool |
Alexander Afanasyev | 9825610 | 2011-08-14 01:00:02 -0700 | [diff] [blame] | 189 | CcnxFace::IsUp (void) const |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 190 | { |
| 191 | NS_LOG_FUNCTION_NOARGS (); |
| 192 | return m_ifup; |
| 193 | } |
| 194 | |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 195 | void |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 196 | CcnxFace::SetUp (bool up/* = true*/) |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 197 | { |
| 198 | NS_LOG_FUNCTION_NOARGS (); |
Alexander Afanasyev | 09c7deb | 2011-11-23 14:50:10 -0800 | [diff] [blame] | 199 | m_ifup = up; |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 202 | bool |
| 203 | CcnxFace::operator== (const CcnxFace &face) const |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 204 | { |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 205 | NS_ASSERT_MSG (m_node->GetId () == face.m_node->GetId (), |
| 206 | "Faces of different nodes should not be compared to each other"); |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 207 | |
| 208 | return (m_id == face.m_id); |
| 209 | } |
| 210 | |
| 211 | bool |
| 212 | CcnxFace::operator< (const CcnxFace &face) const |
| 213 | { |
Alexander Afanasyev | a46844b | 2011-11-21 19:13:26 -0800 | [diff] [blame] | 214 | NS_ASSERT_MSG (m_node->GetId () == face.m_node->GetId (), |
| 215 | "Faces of different nodes should not be compared to each other"); |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 216 | |
| 217 | return (m_id < face.m_id); |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 220 | std::ostream& |
| 221 | CcnxFace::Print (std::ostream &os) const |
| 222 | { |
| 223 | os << "id=" << GetId (); |
| 224 | return os; |
| 225 | } |
| 226 | |
Alexander Afanasyev | 56f79ea | 2011-08-17 23:54:27 -0700 | [diff] [blame] | 227 | std::ostream& operator<< (std::ostream& os, const CcnxFace &face) |
Alexander Afanasyev | 9825610 | 2011-08-14 01:00:02 -0700 | [diff] [blame] | 228 | { |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 229 | face.Print (os); |
Alexander Afanasyev | 9825610 | 2011-08-14 01:00:02 -0700 | [diff] [blame] | 230 | return os; |
| 231 | } |
| 232 | |
Alexander Afanasyev | 08d984e | 2011-08-13 19:20:22 -0700 | [diff] [blame] | 233 | }; // namespace ns3 |
| 234 | |