blob: ec35d8bcd48b551f118a97275b0105f357611148 [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 Afanasyevbdc0d982011-12-16 01:15:26 -080035TypeId
36CcnxFace::GetTypeId ()
37{
38 static TypeId tid = TypeId ("ns3::CcnxFace")
39 .SetParent<Object> ()
40 .SetGroupName ("Ccnx")
41 ;
42 return tid;
43}
44
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070045/**
Alexander Afanasyev98256102011-08-14 01:00:02 -070046 * By default, Ccnx face are created in the "down" state
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070047 * with no IP addresses. Before becoming useable, the user must
48 * invoke SetUp on them once an Ccnx address and mask have been set.
49 */
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080050CcnxFace::CcnxFace (Ptr<Node> node)
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080051 : m_node (node)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080052 , m_bucket (0.0)
53 , m_bucketMax (-1.0)
54 , m_bucketLeak (0.0)
55 , m_protocolHandler (MakeNullCallback<void,const Ptr<CcnxFace>&,const Ptr<const Packet>&> ())
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070056 , m_ifup (false)
57 , m_id ((uint32_t)-1)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070058{
59 NS_LOG_FUNCTION (this);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080060
61 NS_ASSERT_MSG (node != 0, "node cannot be NULL. Check the code");
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070062}
63
Alexander Afanasyev98256102011-08-14 01:00:02 -070064CcnxFace::~CcnxFace ()
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070065{
66 NS_LOG_FUNCTION_NOARGS ();
67}
68
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070069CcnxFace::CcnxFace (const CcnxFace &)
70{
71}
72
73CcnxFace& CcnxFace::operator= (const CcnxFace &)
74{
75 return *this;
76}
77
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080078void
79CcnxFace::RegisterProtocolHandler (ProtocolHandler handler)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070080{
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080081 NS_LOG_FUNCTION_NOARGS ();
82
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080083 m_protocolHandler = handler;
84}
85
86bool
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080087CcnxFace::IsBelowLimit ()
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080088{
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080089 NS_LOG_FUNCTION_NOARGS ();
90
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080091 /// \todo Implement tracing, if requested
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -080092
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080093 if (!IsUp ())
94 return false;
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080095
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080096 if (m_bucketMax > 0)
97 {
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -080098 //NS_LOG_DEBUG ("Limits enabled: " << m_bucketMax << ", current: " << m_bucket);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080099 if (m_bucket+1.0 > m_bucketMax)
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800100 {
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -0800101 //NS_LOG_DEBUG ("Returning false");
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800102 return false;
103 }
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800104
105 m_bucket += 1.0;
106 }
107
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800108 return true;
109}
110
Alexander Afanasyevb5703a92011-11-25 16:46:15 -0800111void
112CcnxFace::LeakBucket (const Time &interval)
113{
114 const double leak = m_bucketLeak * interval.ToDouble (Time::S);
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800115 m_bucket = std::max (0.0, m_bucket - leak);
Alexander Afanasyevb5703a92011-11-25 16:46:15 -0800116
Alexander Afanasyeve67a97f2011-11-29 14:28:59 -0800117 // NS_LOG_DEBUG ("max: " << m_bucketMax << ", Current bucket: " << m_bucket << ", leak size: " << leak << ", interval: " << interval << ", " << m_bucketLeak);
Alexander Afanasyevb5703a92011-11-25 16:46:15 -0800118}
119
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800120void
121CcnxFace::SetBucketMax (double bucket)
122{
123 NS_LOG_FUNCTION (this << bucket);
124 m_bucketMax = bucket;
125}
126
127void
128CcnxFace::SetBucketLeak (double leak)
129{
130 NS_LOG_FUNCTION (this << leak);
131 m_bucketLeak = leak;
132}
133
134void
135CcnxFace::LeakBucketByOnePacket ()
136{
137 m_bucket = std::max (0.0, m_bucket-1.0);
138}
139
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800140bool
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800141CcnxFace::Send (Ptr<Packet> packet)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800142{
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800143 NS_LOG_FUNCTION (boost::cref (*this) << packet << packet->GetSize ());
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800144
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800145 /// \todo Implement tracing, if requested
146
147 if (!IsUp ())
148 return false;
149
150 SendImpl (packet);
151 return true;
152}
153
154bool
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800155CcnxFace::Receive (const Ptr<const Packet> &packet)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800156{
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800157 NS_LOG_FUNCTION (boost::cref (*this) << packet << packet->GetSize ());
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800158
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800159 /// \todo Implement tracing, if requested
160
161 if (!IsUp ())
162 return false;
163
164 m_protocolHandler (this, packet);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800165
166 return true;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700167}
168
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700169// void
170// CcnxFace::SetMetric (uint16_t metric)
171// {
172// NS_LOG_FUNCTION (metric);
173// m_metric = metric;
174// }
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700175
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700176// uint16_t
177// CcnxFace::GetMetric (void) const
178// {
179// NS_LOG_FUNCTION_NOARGS ();
180// return m_metric;
181// }
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700182
183/**
Alexander Afanasyev98256102011-08-14 01:00:02 -0700184 * These are face states and may be distinct from
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700185 * NetDevice states, such as found in real implementations
Alexander Afanasyev98256102011-08-14 01:00:02 -0700186 * (where the device may be down but face state is still up).
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700187 */
188bool
Alexander Afanasyev98256102011-08-14 01:00:02 -0700189CcnxFace::IsUp (void) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700190{
191 NS_LOG_FUNCTION_NOARGS ();
192 return m_ifup;
193}
194
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700195void
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800196CcnxFace::SetUp (bool up/* = true*/)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700197{
198 NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800199 m_ifup = up;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700200}
201
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700202bool
203CcnxFace::operator== (const CcnxFace &face) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700204{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800205 NS_ASSERT_MSG (m_node->GetId () == face.m_node->GetId (),
206 "Faces of different nodes should not be compared to each other");
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700207
208 return (m_id == face.m_id);
209}
210
211bool
212CcnxFace::operator< (const CcnxFace &face) const
213{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800214 NS_ASSERT_MSG (m_node->GetId () == face.m_node->GetId (),
215 "Faces of different nodes should not be compared to each other");
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700216
217 return (m_id < face.m_id);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700218}
219
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700220std::ostream&
221CcnxFace::Print (std::ostream &os) const
222{
223 os << "id=" << GetId ();
224 return os;
225}
226
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700227std::ostream& operator<< (std::ostream& os, const CcnxFace &face)
Alexander Afanasyev98256102011-08-14 01:00:02 -0700228{
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700229 face.Print (os);
Alexander Afanasyev98256102011-08-14 01:00:02 -0700230 return os;
231}
232
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700233}; // namespace ns3
234