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