blob: 51b3ba482abab1c8a34c69694a2ca8db0fbe0361 [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 {
85 if (m_bucket+1.0 > m_bucketMax)
86 return false;
87
88 m_bucket += 1.0;
89 }
90
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080091 return true;
92}
93
Alexander Afanasyevb5703a92011-11-25 16:46:15 -080094void
95CcnxFace::LeakBucket (const Time &interval)
96{
97 const double leak = m_bucketLeak * interval.ToDouble (Time::S);
98 m_bucket -= std::max (0.0, m_bucket - leak);
99
100 NS_LOG_ERROR ("max: " << m_bucketMax << ", Current bucket: " << m_bucket << ", leak size: " << leak << ", interval: " << interval << ", " << m_bucketLeak);
101}
102
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800103bool
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800104CcnxFace::Send (Ptr<Packet> packet)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800105{
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800106 NS_LOG_FUNCTION_NOARGS ();
107
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800108 /// \todo Implement tracing, if requested
109
110 if (!IsUp ())
111 return false;
112
113 SendImpl (packet);
114 return true;
115}
116
117bool
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800118CcnxFace::Receive (const Ptr<const Packet> &packet)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800119{
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800120 NS_LOG_FUNCTION_NOARGS ();
121
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800122 /// \todo Implement tracing, if requested
123
124 if (!IsUp ())
125 return false;
126
127 m_protocolHandler (this, packet);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800128
129 return true;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700130}
131
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700132// void
133// CcnxFace::SetMetric (uint16_t metric)
134// {
135// NS_LOG_FUNCTION (metric);
136// m_metric = metric;
137// }
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700138
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700139// uint16_t
140// CcnxFace::GetMetric (void) const
141// {
142// NS_LOG_FUNCTION_NOARGS ();
143// return m_metric;
144// }
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700145
146/**
Alexander Afanasyev98256102011-08-14 01:00:02 -0700147 * These are face states and may be distinct from
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700148 * NetDevice states, such as found in real implementations
Alexander Afanasyev98256102011-08-14 01:00:02 -0700149 * (where the device may be down but face state is still up).
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700150 */
151bool
Alexander Afanasyev98256102011-08-14 01:00:02 -0700152CcnxFace::IsUp (void) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700153{
154 NS_LOG_FUNCTION_NOARGS ();
155 return m_ifup;
156}
157
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700158void
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800159CcnxFace::SetUp (bool up/* = true*/)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700160{
161 NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800162 m_ifup = up;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700163}
164
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700165bool
166CcnxFace::operator== (const CcnxFace &face) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700167{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800168 NS_ASSERT_MSG (m_node->GetId () == face.m_node->GetId (),
169 "Faces of different nodes should not be compared to each other");
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700170
171 return (m_id == face.m_id);
172}
173
174bool
175CcnxFace::operator< (const CcnxFace &face) const
176{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800177 NS_ASSERT_MSG (m_node->GetId () == face.m_node->GetId (),
178 "Faces of different nodes should not be compared to each other");
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700179
180 return (m_id < face.m_id);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700181}
182
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700183std::ostream&
184CcnxFace::Print (std::ostream &os) const
185{
186 os << "id=" << GetId ();
187 return os;
188}
189
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700190std::ostream& operator<< (std::ostream& os, const CcnxFace &face)
Alexander Afanasyev98256102011-08-14 01:00:02 -0700191{
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700192 face.Print (os);
Alexander Afanasyev98256102011-08-14 01:00:02 -0700193 return os;
194}
195
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700196}; // namespace ns3
197