blob: 95c0b7ffb6eca8be51fcf530c4845cec7206d0f9 [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 Afanasyevcbe92ae2011-12-16 13:06:18 -080028#include "ns3/uinteger.h"
29#include "ns3/double.h"
Alexander Afanasyev4975f732011-12-20 17:52:19 -080030#include "ns3/simulator.h"
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070031
Alexander Afanasyev23d2b542011-12-07 18:54:46 -080032#include <boost/ref.hpp>
33
Alexander Afanasyev98256102011-08-14 01:00:02 -070034NS_LOG_COMPONENT_DEFINE ("CcnxFace");
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070035
36namespace ns3 {
37
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -080038TypeId
39CcnxFace::GetTypeId ()
40{
41 static TypeId tid = TypeId ("ns3::CcnxFace")
42 .SetParent<Object> ()
43 .SetGroupName ("Ccnx")
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -080044 .AddAttribute ("Id", "Face id (unique integer for the CCNx stack on this node)",
45 TypeId::ATTR_GET, // allow only getting it.
46 UintegerValue (0),
47 MakeUintegerAccessor (&CcnxFace::m_id),
48 MakeUintegerChecker<uint32_t> ())
49
50 .AddAttribute ("BucketMax", "Maximum size of leaky bucket",
51 DoubleValue (-1.0),
52 MakeDoubleAccessor (&CcnxFace::m_bucketMax),
53 MakeDoubleChecker<double> ())
54 .AddAttribute ("BucketLeak", "Normalized bucket leak size",
55 DoubleValue (0.0),
56 MakeDoubleAccessor (&CcnxFace::m_bucketLeak),
57 MakeDoubleChecker<double> ())
58
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -080059 ;
60 return tid;
61}
62
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070063/**
Alexander Afanasyev98256102011-08-14 01:00:02 -070064 * By default, Ccnx face are created in the "down" state
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070065 * with no IP addresses. Before becoming useable, the user must
66 * invoke SetUp on them once an Ccnx address and mask have been set.
67 */
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080068CcnxFace::CcnxFace (Ptr<Node> node)
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080069 : m_node (node)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080070 , m_bucket (0.0)
71 , m_bucketMax (-1.0)
72 , m_bucketLeak (0.0)
73 , m_protocolHandler (MakeNullCallback<void,const Ptr<CcnxFace>&,const Ptr<const Packet>&> ())
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070074 , m_ifup (false)
75 , m_id ((uint32_t)-1)
Alexander Afanasyev4975f732011-12-20 17:52:19 -080076 , m_lastLeakTime (0)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070077{
78 NS_LOG_FUNCTION (this);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080079
80 NS_ASSERT_MSG (node != 0, "node cannot be NULL. Check the code");
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070081}
82
Alexander Afanasyev98256102011-08-14 01:00:02 -070083CcnxFace::~CcnxFace ()
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070084{
85 NS_LOG_FUNCTION_NOARGS ();
86}
87
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070088CcnxFace::CcnxFace (const CcnxFace &)
89{
90}
91
92CcnxFace& CcnxFace::operator= (const CcnxFace &)
93{
94 return *this;
95}
96
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080097void
98CcnxFace::RegisterProtocolHandler (ProtocolHandler handler)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070099{
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800100 NS_LOG_FUNCTION_NOARGS ();
101
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800102 m_protocolHandler = handler;
103}
104
105bool
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800106CcnxFace::IsBelowLimit ()
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800107{
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800108 NS_LOG_FUNCTION_NOARGS ();
109
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800110 /// \todo Implement tracing, if requested
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -0800111
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800112 if (!IsUp ())
113 return false;
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800114
115 LeakBucket ();
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800116
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800117 if (m_bucketMax > 0)
118 {
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800119 NS_LOG_DEBUG ("Limits enabled: " << m_bucketMax << ", current: " << m_bucket);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800120 if (m_bucket+1.0 > m_bucketMax)
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800121 {
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -0800122 //NS_LOG_DEBUG ("Returning false");
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800123 return false;
124 }
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800125
126 m_bucket += 1.0;
127 }
128
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800129 return true;
130}
131
132bool
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800133CcnxFace::Send (Ptr<Packet> packet)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800134{
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800135 NS_LOG_FUNCTION (boost::cref (*this) << packet << packet->GetSize ());
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800136
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800137 /// \todo Implement tracing, if requested
138
139 if (!IsUp ())
140 return false;
141
142 SendImpl (packet);
143 return true;
144}
145
146bool
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800147CcnxFace::Receive (const Ptr<const Packet> &packet)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800148{
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800149 NS_LOG_FUNCTION (boost::cref (*this) << packet << packet->GetSize ());
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800150
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800151 /// \todo Implement tracing, if requested
152
153 if (!IsUp ())
154 return false;
155
156 m_protocolHandler (this, packet);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800157
158 return true;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700159}
160
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -0800161void
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800162CcnxFace::LeakBucket ()
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -0800163{
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800164 if (m_lastLeakTime.IsZero ())
165 {
166 m_lastLeakTime = Simulator::Now ();
167 return;
168 }
169
170 Time interval = Simulator::Now () - m_lastLeakTime;
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -0800171 const double leak = m_bucketLeak * interval.ToDouble (Time::S);
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800172 if (leak >= 1.0)
173 {
174 m_bucket = std::max (0.0, m_bucket - leak);
175 m_lastLeakTime = Simulator::Now ();
176 }
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -0800177
178 // NS_LOG_DEBUG ("max: " << m_bucketMax << ", Current bucket: " << m_bucket << ", leak size: " << leak << ", interval: " << interval << ", " << m_bucketLeak);
179}
180
181void
182CcnxFace::SetBucketMax (double bucket)
183{
184 NS_LOG_FUNCTION (this << bucket);
185 m_bucketMax = bucket;
186}
187
188void
189CcnxFace::SetBucketLeak (double leak)
190{
191 NS_LOG_FUNCTION (this << leak);
192 m_bucketLeak = leak;
193}
194
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700195// void
196// CcnxFace::SetMetric (uint16_t metric)
197// {
198// NS_LOG_FUNCTION (metric);
199// m_metric = metric;
200// }
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700201
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700202// uint16_t
203// CcnxFace::GetMetric (void) const
204// {
205// NS_LOG_FUNCTION_NOARGS ();
206// return m_metric;
207// }
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700208
209/**
Alexander Afanasyev98256102011-08-14 01:00:02 -0700210 * These are face states and may be distinct from
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700211 * NetDevice states, such as found in real implementations
Alexander Afanasyev98256102011-08-14 01:00:02 -0700212 * (where the device may be down but face state is still up).
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700213 */
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800214
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700215bool
Alexander Afanasyev98256102011-08-14 01:00:02 -0700216CcnxFace::IsUp (void) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700217{
218 NS_LOG_FUNCTION_NOARGS ();
219 return m_ifup;
220}
221
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700222void
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800223CcnxFace::SetUp (bool up/* = true*/)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700224{
225 NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800226 m_ifup = up;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700227}
228
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700229bool
230CcnxFace::operator== (const CcnxFace &face) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700231{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800232 NS_ASSERT_MSG (m_node->GetId () == face.m_node->GetId (),
233 "Faces of different nodes should not be compared to each other");
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700234
235 return (m_id == face.m_id);
236}
237
238bool
239CcnxFace::operator< (const CcnxFace &face) const
240{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800241 NS_ASSERT_MSG (m_node->GetId () == face.m_node->GetId (),
242 "Faces of different nodes should not be compared to each other");
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700243
244 return (m_id < face.m_id);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700245}
246
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700247std::ostream&
248CcnxFace::Print (std::ostream &os) const
249{
250 os << "id=" << GetId ();
251 return os;
252}
253
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700254std::ostream& operator<< (std::ostream& os, const CcnxFace &face)
Alexander Afanasyev98256102011-08-14 01:00:02 -0700255{
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700256 face.Print (os);
Alexander Afanasyev98256102011-08-14 01:00:02 -0700257 return os;
258}
259
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700260}; // namespace ns3
261