blob: 3c88e1cda48c57e6e3eb152d33de717d288dbf89 [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/*
Ilya Moiseenko956d0542012-01-02 15:26:40 -08003 * Copyright (c) 2011 University of California, Los Angeles
Alexander Afanasyev08d984e2011-08-13 19:20:22 -07004 *
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
Lucas64b85362012-01-30 16:28:37 -0800112 if (!IsUp ()){
113 NS_LOG_INFO("CcnxFace is not up.");
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800114 return false;
Lucas64b85362012-01-30 16:28:37 -0800115 }
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800116
117 LeakBucket ();
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800118
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800119 if (m_bucketMax > 0)
120 {
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800121 NS_LOG_DEBUG ("Limits enabled: " << m_bucketMax << ", current: " << m_bucket);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800122 if (m_bucket+1.0 > m_bucketMax)
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800123 {
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -0800124 //NS_LOG_DEBUG ("Returning false");
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800125 return false;
126 }
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800127
128 m_bucket += 1.0;
129 }
130
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800131 return true;
132}
133
134bool
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800135CcnxFace::Send (Ptr<Packet> packet)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800136{
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800137 NS_LOG_FUNCTION (boost::cref (*this) << packet << packet->GetSize ());
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800138
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800139 /// \todo Implement tracing, if requested
140
141 if (!IsUp ())
142 return false;
143
144 SendImpl (packet);
145 return true;
146}
147
148bool
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800149CcnxFace::Receive (const Ptr<const Packet> &packet)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800150{
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800151 NS_LOG_FUNCTION (boost::cref (*this) << packet << packet->GetSize ());
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800152
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800153 /// \todo Implement tracing, if requested
154
155 if (!IsUp ())
156 return false;
157
158 m_protocolHandler (this, packet);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800159
160 return true;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700161}
162
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -0800163void
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800164CcnxFace::LeakBucket ()
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -0800165{
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800166 if (m_lastLeakTime.IsZero ())
167 {
168 m_lastLeakTime = Simulator::Now ();
169 return;
170 }
171
172 Time interval = Simulator::Now () - m_lastLeakTime;
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -0800173 const double leak = m_bucketLeak * interval.ToDouble (Time::S);
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800174 if (leak >= 1.0)
175 {
176 m_bucket = std::max (0.0, m_bucket - leak);
177 m_lastLeakTime = Simulator::Now ();
178 }
Alexander Afanasyevcbe92ae2011-12-16 13:06:18 -0800179
180 // NS_LOG_DEBUG ("max: " << m_bucketMax << ", Current bucket: " << m_bucket << ", leak size: " << leak << ", interval: " << interval << ", " << m_bucketLeak);
181}
182
183void
184CcnxFace::SetBucketMax (double bucket)
185{
186 NS_LOG_FUNCTION (this << bucket);
187 m_bucketMax = bucket;
188}
189
190void
191CcnxFace::SetBucketLeak (double leak)
192{
193 NS_LOG_FUNCTION (this << leak);
194 m_bucketLeak = leak;
195}
196
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700197// void
198// CcnxFace::SetMetric (uint16_t metric)
199// {
200// NS_LOG_FUNCTION (metric);
201// m_metric = metric;
202// }
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700203
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700204// uint16_t
205// CcnxFace::GetMetric (void) const
206// {
207// NS_LOG_FUNCTION_NOARGS ();
208// return m_metric;
209// }
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700210
211/**
Alexander Afanasyev98256102011-08-14 01:00:02 -0700212 * These are face states and may be distinct from
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700213 * NetDevice states, such as found in real implementations
Alexander Afanasyev98256102011-08-14 01:00:02 -0700214 * (where the device may be down but face state is still up).
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700215 */
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800216
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700217bool
Alexander Afanasyev98256102011-08-14 01:00:02 -0700218CcnxFace::IsUp (void) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700219{
220 NS_LOG_FUNCTION_NOARGS ();
221 return m_ifup;
222}
223
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700224void
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800225CcnxFace::SetUp (bool up/* = true*/)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700226{
227 NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800228 m_ifup = up;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700229}
230
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700231bool
232CcnxFace::operator== (const CcnxFace &face) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700233{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800234 NS_ASSERT_MSG (m_node->GetId () == face.m_node->GetId (),
235 "Faces of different nodes should not be compared to each other");
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700236
237 return (m_id == face.m_id);
238}
239
240bool
241CcnxFace::operator< (const CcnxFace &face) const
242{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800243 NS_ASSERT_MSG (m_node->GetId () == face.m_node->GetId (),
244 "Faces of different nodes should not be compared to each other");
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700245
246 return (m_id < face.m_id);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700247}
248
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700249std::ostream&
250CcnxFace::Print (std::ostream &os) const
251{
252 os << "id=" << GetId ();
253 return os;
254}
255
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700256std::ostream& operator<< (std::ostream& os, const CcnxFace &face)
Alexander Afanasyev98256102011-08-14 01:00:02 -0700257{
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700258 face.Print (os);
Alexander Afanasyev98256102011-08-14 01:00:02 -0700259 return os;
260}
261
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700262}; // namespace ns3
263