blob: 1bb55ab732ba220ee57733f1cdd7d330a072fab4 [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 Afanasyev08d984e2011-08-13 19:20:22 -070024#include "ns3/log.h"
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070025#include "ns3/node.h"
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070026#include "ns3/assert.h"
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070027
Alexander Afanasyev98256102011-08-14 01:00:02 -070028NS_LOG_COMPONENT_DEFINE ("CcnxFace");
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070029
30namespace ns3 {
31
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070032/**
Alexander Afanasyev98256102011-08-14 01:00:02 -070033 * By default, Ccnx face are created in the "down" state
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070034 * with no IP addresses. Before becoming useable, the user must
35 * invoke SetUp on them once an Ccnx address and mask have been set.
36 */
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080037CcnxFace::CcnxFace (Ptr<Node> node)
38 : m_node (Ptr<Node> node)
39 , m_bucket (0.0)
40 , m_bucketMax (-1.0)
41 , m_bucketLeak (0.0)
42 , m_protocolHandler (MakeNullCallback<void,const Ptr<CcnxFace>&,const Ptr<const Packet>&> ())
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070043 , m_ifup (false)
44 , m_id ((uint32_t)-1)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070045{
46 NS_LOG_FUNCTION (this);
47}
48
Alexander Afanasyev98256102011-08-14 01:00:02 -070049CcnxFace::~CcnxFace ()
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070050{
51 NS_LOG_FUNCTION_NOARGS ();
52}
53
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070054CcnxFace::CcnxFace (const CcnxFace &)
55{
56}
57
58CcnxFace& CcnxFace::operator= (const CcnxFace &)
59{
60 return *this;
61}
62
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080063void
64CcnxFace::RegisterProtocolHandler (ProtocolHandler handler)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070065{
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080066 m_protocolHandler = handler;
67}
68
69bool
70CcnxFace::SendWithLimit (Ptr<Packet> packet)
71{
72 /// \todo Implement tracing, if requested
73
74 if (!IsUp ())
75 return false;
76
77 if (m_bucketMax > 0)
78 {
79 if (m_bucket+1.0 > m_bucketMax)
80 return false;
81
82 m_bucket += 1.0;
83 }
84
85 SendImpl (packet);
86 return true;
87}
88
89bool
90CcnxFace::SendWithoutLimits (Ptr<Packet> packet)
91{
92 /// \todo Implement tracing, if requested
93
94 if (!IsUp ())
95 return false;
96
97 SendImpl (packet);
98 return true;
99}
100
101bool
102CcnxFace::Receive (Ptr<const Packet> packet)
103{
104 /// \todo Implement tracing, if requested
105
106 if (!IsUp ())
107 return false;
108
109 m_protocolHandler (this, packet);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700110}
111
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700112// void
113// CcnxFace::SetMetric (uint16_t metric)
114// {
115// NS_LOG_FUNCTION (metric);
116// m_metric = metric;
117// }
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700118
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700119// uint16_t
120// CcnxFace::GetMetric (void) const
121// {
122// NS_LOG_FUNCTION_NOARGS ();
123// return m_metric;
124// }
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700125
126/**
Alexander Afanasyev98256102011-08-14 01:00:02 -0700127 * These are face states and may be distinct from
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700128 * NetDevice states, such as found in real implementations
Alexander Afanasyev98256102011-08-14 01:00:02 -0700129 * (where the device may be down but face state is still up).
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700130 */
131bool
Alexander Afanasyev98256102011-08-14 01:00:02 -0700132CcnxFace::IsUp (void) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700133{
134 NS_LOG_FUNCTION_NOARGS ();
135 return m_ifup;
136}
137
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700138void
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800139CcnxFace::SetUp (bool up/* = true*/)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700140{
141 NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800142 m_ifup = up;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700143}
144
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700145bool
146CcnxFace::operator== (const CcnxFace &face) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700147{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800148 NS_ASSERT_MSG (m_node->GetId () == face.m_node->GetId (),
149 "Faces of different nodes should not be compared to each other");
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700150
151 return (m_id == face.m_id);
152}
153
154bool
155CcnxFace::operator< (const CcnxFace &face) const
156{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800157 NS_ASSERT_MSG (m_node->GetId () == face.m_node->GetId (),
158 "Faces of different nodes should not be compared to each other");
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700159
160 return (m_id < face.m_id);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700161}
162
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700163std::ostream&
164CcnxFace::Print (std::ostream &os) const
165{
166 os << "id=" << GetId ();
167 return os;
168}
169
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700170std::ostream& operator<< (std::ostream& os, const CcnxFace &face)
Alexander Afanasyev98256102011-08-14 01:00:02 -0700171{
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700172 face.Print (os);
Alexander Afanasyev98256102011-08-14 01:00:02 -0700173 return os;
174}
175
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700176}; // namespace ns3
177