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