blob: e6aa03f40a058ac96c50c359f836f6385e97e350 [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
94bool
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080095CcnxFace::Send (Ptr<Packet> packet)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080096{
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080097 NS_LOG_FUNCTION_NOARGS ();
98
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080099 /// \todo Implement tracing, if requested
100
101 if (!IsUp ())
102 return false;
103
104 SendImpl (packet);
105 return true;
106}
107
108bool
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800109CcnxFace::Receive (const Ptr<const Packet> &packet)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800110{
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800111 NS_LOG_FUNCTION_NOARGS ();
112
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800113 /// \todo Implement tracing, if requested
114
115 if (!IsUp ())
116 return false;
117
118 m_protocolHandler (this, packet);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800119
120 return true;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700121}
122
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700123// void
124// CcnxFace::SetMetric (uint16_t metric)
125// {
126// NS_LOG_FUNCTION (metric);
127// m_metric = metric;
128// }
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700129
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700130// uint16_t
131// CcnxFace::GetMetric (void) const
132// {
133// NS_LOG_FUNCTION_NOARGS ();
134// return m_metric;
135// }
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700136
137/**
Alexander Afanasyev98256102011-08-14 01:00:02 -0700138 * These are face states and may be distinct from
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700139 * NetDevice states, such as found in real implementations
Alexander Afanasyev98256102011-08-14 01:00:02 -0700140 * (where the device may be down but face state is still up).
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700141 */
142bool
Alexander Afanasyev98256102011-08-14 01:00:02 -0700143CcnxFace::IsUp (void) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700144{
145 NS_LOG_FUNCTION_NOARGS ();
146 return m_ifup;
147}
148
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700149void
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800150CcnxFace::SetUp (bool up/* = true*/)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700151{
152 NS_LOG_FUNCTION_NOARGS ();
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800153 m_ifup = up;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700154}
155
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700156bool
157CcnxFace::operator== (const CcnxFace &face) const
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700158{
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800159 NS_ASSERT_MSG (m_node->GetId () == face.m_node->GetId (),
160 "Faces of different nodes should not be compared to each other");
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700161
162 return (m_id == face.m_id);
163}
164
165bool
166CcnxFace::operator< (const CcnxFace &face) const
167{
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);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700172}
173
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700174std::ostream&
175CcnxFace::Print (std::ostream &os) const
176{
177 os << "id=" << GetId ();
178 return os;
179}
180
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700181std::ostream& operator<< (std::ostream& os, const CcnxFace &face)
Alexander Afanasyev98256102011-08-14 01:00:02 -0700182{
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700183 face.Print (os);
Alexander Afanasyev98256102011-08-14 01:00:02 -0700184 return os;
185}
186
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700187}; // namespace ns3
188