blob: 3c092e95ae740aae0dfaf903c72523ddf5fc06cf [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 Afanasyev98256102011-08-14 01:00:02 -070029NS_LOG_COMPONENT_DEFINE ("CcnxFace");
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070030
31namespace ns3 {
32
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070033/**
Alexander Afanasyev98256102011-08-14 01:00:02 -070034 * By default, Ccnx face are created in the "down" state
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070035 * with no IP addresses. Before becoming useable, the user must
36 * invoke SetUp on them once an Ccnx address and mask have been set.
37 */
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080038CcnxFace::CcnxFace (Ptr<Node> node)
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080039 : m_node (node)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080040 , m_bucket (0.0)
41 , m_bucketMax (-1.0)
42 , m_bucketLeak (0.0)
43 , m_protocolHandler (MakeNullCallback<void,const Ptr<CcnxFace>&,const Ptr<const Packet>&> ())
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070044 , m_ifup (false)
45 , m_id ((uint32_t)-1)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070046{
47 NS_LOG_FUNCTION (this);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080048
49 NS_ASSERT_MSG (node != 0, "node cannot be NULL. Check the code");
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070050}
51
Alexander Afanasyev98256102011-08-14 01:00:02 -070052CcnxFace::~CcnxFace ()
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070053{
54 NS_LOG_FUNCTION_NOARGS ();
55}
56
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070057CcnxFace::CcnxFace (const CcnxFace &)
58{
59}
60
61CcnxFace& CcnxFace::operator= (const CcnxFace &)
62{
63 return *this;
64}
65
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080066void
67CcnxFace::RegisterProtocolHandler (ProtocolHandler handler)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070068{
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080069 NS_LOG_FUNCTION_NOARGS ();
70
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080071 m_protocolHandler = handler;
72}
73
74bool
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080075CcnxFace::IsBelowLimit ()
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080076{
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080077 NS_LOG_FUNCTION_NOARGS ();
78
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080079 /// \todo Implement tracing, if requested
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080080 if (!IsUp ())
81 return false;
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080082
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080083 if (m_bucketMax > 0)
84 {
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -080085 //NS_LOG_ERROR ("Limits enabled: " << m_bucketMax << ", current: " << m_bucket);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080086 if (m_bucket+1.0 > m_bucketMax)
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -080087 {
88 //NS_LOG_ERROR ("Returning false");
89 return false;
90 }
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080091
92 m_bucket += 1.0;
93 }
94
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080095 return true;
96}
97
Alexander Afanasyevb5703a92011-11-25 16:46:15 -080098void
99CcnxFace::LeakBucket (const Time &interval)
100{
101 const double leak = m_bucketLeak * interval.ToDouble (Time::S);
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800102 m_bucket = std::max (0.0, m_bucket - leak);
Alexander Afanasyevb5703a92011-11-25 16:46:15 -0800103
104 NS_LOG_ERROR ("max: " << m_bucketMax << ", Current bucket: " << m_bucket << ", leak size: " << leak << ", interval: " << interval << ", " << m_bucketLeak);
105}
106
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800107void
108CcnxFace::SetBucketMax (double bucket)
109{
110 NS_LOG_FUNCTION (this << bucket);
111 m_bucketMax = bucket;
112}
113
114void
115CcnxFace::SetBucketLeak (double leak)
116{
117 NS_LOG_FUNCTION (this << leak);
118 m_bucketLeak = leak;
119}
120
121void
122CcnxFace::LeakBucketByOnePacket ()
123{
124 m_bucket = std::max (0.0, m_bucket-1.0);
125}
126
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800127bool
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800128CcnxFace::Send (Ptr<Packet> packet)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800129{
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800130 NS_LOG_FUNCTION_NOARGS ();
131
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800132 /// \todo Implement tracing, if requested
133
134 if (!IsUp ())
135 return false;
136
137 SendImpl (packet);
138 return true;
139}
140
141bool
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800142CcnxFace::Receive (const Ptr<const Packet> &packet)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800143{
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800144 NS_LOG_FUNCTION_NOARGS ();
145
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800146 /// \todo Implement tracing, if requested
147
148 if (!IsUp ())
149 return false;
150
151 m_protocolHandler (this, packet);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800152
153 return true;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700154}
155
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700156// void
157// CcnxFace::SetMetric (uint16_t metric)
158// {
159// NS_LOG_FUNCTION (metric);
160// m_metric = metric;
161// }
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700162
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700163// uint16_t
164// CcnxFace::GetMetric (void) const
165// {
166// NS_LOG_FUNCTION_NOARGS ();
167// return m_metric;
168// }
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700169
170/**
Alexander Afanasyev98256102011-08-14 01:00:02 -0700171 * These are face states and may be distinct from
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700172 * NetDevice states, such as found in real implementations
Alexander Afanasyev98256102011-08-14 01:00:02 -0700173 * (where the device may be down but face state is still up).
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700174 */
175bool
Alexander Afanasyev98256102011-08-14 01:00:02 -0700176CcnxFace::IsUp (void) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700177{
178 NS_LOG_FUNCTION_NOARGS ();
179 return m_ifup;
180}
181
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700182void
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800183CcnxFace::SetUp (bool up/* = true*/)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700184{
185 NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800186 m_ifup = up;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700187}
188
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700189bool
190CcnxFace::operator== (const CcnxFace &face) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700191{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800192 NS_ASSERT_MSG (m_node->GetId () == face.m_node->GetId (),
193 "Faces of different nodes should not be compared to each other");
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700194
195 return (m_id == face.m_id);
196}
197
198bool
199CcnxFace::operator< (const CcnxFace &face) const
200{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800201 NS_ASSERT_MSG (m_node->GetId () == face.m_node->GetId (),
202 "Faces of different nodes should not be compared to each other");
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700203
204 return (m_id < face.m_id);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700205}
206
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700207std::ostream&
208CcnxFace::Print (std::ostream &os) const
209{
210 os << "id=" << GetId ();
211 return os;
212}
213
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700214std::ostream& operator<< (std::ostream& os, const CcnxFace &face)
Alexander Afanasyev98256102011-08-14 01:00:02 -0700215{
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700216 face.Print (os);
Alexander Afanasyev98256102011-08-14 01:00:02 -0700217 return os;
218}
219
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700220}; // namespace ns3
221