blob: 40ac57bc3fc34a43c12d7caeb0f6d47344873246 [file] [log] [blame]
Alexander Afanasyevc74a6022011-08-15 20:01:35 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Alexander Afanasyev08d984e2011-08-13 19:20:22 -07002/*
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 Afanasyev56f79ea2011-08-17 23:54:27 -070018 * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070019 *
20 */
21
Alexander Afanasyev98256102011-08-14 01:00:02 -070022#include "ccnx-face.h"
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070023
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080024#include "ns3/packet.h"
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070025#include "ns3/log.h"
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070026#include "ns3/node.h"
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070027#include "ns3/assert.h"
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070028
Alexander Afanasyev23d2b542011-12-07 18:54:46 -080029#include <boost/ref.hpp>
30
Alexander Afanasyev98256102011-08-14 01:00:02 -070031NS_LOG_COMPONENT_DEFINE ("CcnxFace");
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070032
33namespace ns3 {
34
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070035/**
Alexander Afanasyev98256102011-08-14 01:00:02 -070036 * By default, Ccnx face are created in the "down" state
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070037 * 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 Afanasyev09c7deb2011-11-23 14:50:10 -080040CcnxFace::CcnxFace (Ptr<Node> node)
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080041 : m_node (node)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080042 , 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 Afanasyev56f79ea2011-08-17 23:54:27 -070046 , m_ifup (false)
47 , m_id ((uint32_t)-1)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070048{
49 NS_LOG_FUNCTION (this);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080050
51 NS_ASSERT_MSG (node != 0, "node cannot be NULL. Check the code");
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070052}
53
Alexander Afanasyev98256102011-08-14 01:00:02 -070054CcnxFace::~CcnxFace ()
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070055{
56 NS_LOG_FUNCTION_NOARGS ();
57}
58
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070059CcnxFace::CcnxFace (const CcnxFace &)
60{
61}
62
63CcnxFace& CcnxFace::operator= (const CcnxFace &)
64{
65 return *this;
66}
67
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080068void
69CcnxFace::RegisterProtocolHandler (ProtocolHandler handler)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070070{
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080071 NS_LOG_FUNCTION_NOARGS ();
72
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080073 m_protocolHandler = handler;
74}
75
76bool
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080077CcnxFace::IsBelowLimit ()
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080078{
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080079 NS_LOG_FUNCTION_NOARGS ();
80
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080081 /// \todo Implement tracing, if requested
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080082 if (!IsUp ())
83 return false;
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080084
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080085 if (m_bucketMax > 0)
86 {
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -080087 //NS_LOG_DEBUG ("Limits enabled: " << m_bucketMax << ", current: " << m_bucket);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080088 if (m_bucket+1.0 > m_bucketMax)
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -080089 {
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -080090 //NS_LOG_DEBUG ("Returning false");
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -080091 return false;
92 }
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080093
94 m_bucket += 1.0;
95 }
96
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080097 return true;
98}
99
Alexander Afanasyevb5703a92011-11-25 16:46:15 -0800100void
101CcnxFace::LeakBucket (const Time &interval)
102{
103 const double leak = m_bucketLeak * interval.ToDouble (Time::S);
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800104 m_bucket = std::max (0.0, m_bucket - leak);
Alexander Afanasyevb5703a92011-11-25 16:46:15 -0800105
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -0800106 // NS_LOG_DEBUG ("max: " << m_bucketMax << ", Current bucket: " << m_bucket << ", leak size: " << leak << ", interval: " << interval << ", " << m_bucketLeak);
Alexander Afanasyevb5703a92011-11-25 16:46:15 -0800107}
108
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800109void
110CcnxFace::SetBucketMax (double bucket)
111{
112 NS_LOG_FUNCTION (this << bucket);
113 m_bucketMax = bucket;
114}
115
116void
117CcnxFace::SetBucketLeak (double leak)
118{
119 NS_LOG_FUNCTION (this << leak);
120 m_bucketLeak = leak;
121}
122
123void
124CcnxFace::LeakBucketByOnePacket ()
125{
126 m_bucket = std::max (0.0, m_bucket-1.0);
127}
128
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800129bool
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800130CcnxFace::Send (Ptr<Packet> packet)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800131{
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800132 NS_LOG_FUNCTION (boost::cref (*this) << packet << packet->GetSize ());
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800133
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800134 /// \todo Implement tracing, if requested
135
136 if (!IsUp ())
137 return false;
138
139 SendImpl (packet);
140 return true;
141}
142
143bool
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800144CcnxFace::Receive (const Ptr<const Packet> &packet)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800145{
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800146 NS_LOG_FUNCTION (boost::cref (*this) << packet << packet->GetSize ());
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800147
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800148 /// \todo Implement tracing, if requested
149
150 if (!IsUp ())
151 return false;
152
153 m_protocolHandler (this, packet);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800154
155 return true;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700156}
157
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700158// void
159// CcnxFace::SetMetric (uint16_t metric)
160// {
161// NS_LOG_FUNCTION (metric);
162// m_metric = metric;
163// }
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700164
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700165// uint16_t
166// CcnxFace::GetMetric (void) const
167// {
168// NS_LOG_FUNCTION_NOARGS ();
169// return m_metric;
170// }
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700171
172/**
Alexander Afanasyev98256102011-08-14 01:00:02 -0700173 * These are face states and may be distinct from
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700174 * NetDevice states, such as found in real implementations
Alexander Afanasyev98256102011-08-14 01:00:02 -0700175 * (where the device may be down but face state is still up).
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700176 */
177bool
Alexander Afanasyev98256102011-08-14 01:00:02 -0700178CcnxFace::IsUp (void) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700179{
180 NS_LOG_FUNCTION_NOARGS ();
181 return m_ifup;
182}
183
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700184void
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800185CcnxFace::SetUp (bool up/* = true*/)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700186{
187 NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800188 m_ifup = up;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700189}
190
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700191bool
192CcnxFace::operator== (const CcnxFace &face) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700193{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800194 NS_ASSERT_MSG (m_node->GetId () == face.m_node->GetId (),
195 "Faces of different nodes should not be compared to each other");
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700196
197 return (m_id == face.m_id);
198}
199
200bool
201CcnxFace::operator< (const CcnxFace &face) const
202{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800203 NS_ASSERT_MSG (m_node->GetId () == face.m_node->GetId (),
204 "Faces of different nodes should not be compared to each other");
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700205
206 return (m_id < face.m_id);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700207}
208
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700209std::ostream&
210CcnxFace::Print (std::ostream &os) const
211{
212 os << "id=" << GetId ();
213 return os;
214}
215
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700216std::ostream& operator<< (std::ostream& os, const CcnxFace &face)
Alexander Afanasyev98256102011-08-14 01:00:02 -0700217{
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700218 face.Print (os);
Alexander Afanasyev98256102011-08-14 01:00:02 -0700219 return os;
220}
221
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700222}; // namespace ns3
223