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