blob: 0e9890532ae25c307a598f491c2453c0ea944c47 [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/*
Ilya Moiseenko956d0542012-01-02 15:26:40 -08003 * Copyright (c) 2011 University of California, Los Angeles
Alexander Afanasyev08d984e2011-08-13 19:20:22 -07004 *
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 * Authors: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070019 */
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070020
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070021#ifndef NDN_FACE_H
22#define NDN_FACE_H
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070023
Alexander Afanasyev98256102011-08-14 01:00:02 -070024#include <ostream>
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080025#include <algorithm>
Alexander Afanasyev98256102011-08-14 01:00:02 -070026
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070027#include "ns3/ptr.h"
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070028#include "ns3/ndn.h"
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080029#include "ns3/nstime.h"
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -070030#include "ns3/type-id.h"
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070031
32namespace ns3 {
33
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070034class Packet;
35class Node;
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070036
37namespace ndn {
38
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070039/**
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070040 * \ingroup ndn
41 * \defgroup ndn-face Faces
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070042 */
43/**
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070044 * \ingroup ndn-face
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070045 * \brief Virtual class defining NDN face
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070046 *
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070047 * This class defines basic functionality of NDN face. Face is core
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070048 * component responsible for actual delivery of data packet to and
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070049 * from NDN stack
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070050 *
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070051 * \see ndn::LocalFace, ndn::NetDeviceFace, ndn::Ipv4Face, ndn::UdpFace
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070052 */
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070053class Face : public Object
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070054{
55public:
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -080056 static TypeId
57 GetTypeId ();
58
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070059 /**
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070060 * \brief Ndn protocol handler
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070061 *
62 * \param face Face from which packet has been received
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080063 * \param packet Original packet
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070064 */
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070065 typedef Callback<void,const Ptr<Face>&,const Ptr<const Packet>& > ProtocolHandler;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070066
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070067 /**
68 * \brief Default constructor
69 */
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070070 Face (Ptr<Node> node);
71 virtual ~Face();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070072
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070073 /**
74 * @brief Get node to which this face is associated
75 */
Alexander Afanasyeve9c9d722012-01-19 16:59:30 -080076 Ptr<Node>
77 GetNode () const;
78
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070079 ////////////////////////////////////////////////////////////////////
80
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070081 /**
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070082 * \brief Register callback to call when new packet arrives on the face
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070083 *
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070084 * This method should call protocol-dependent registration function
85 */
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080086 virtual void
87 RegisterProtocolHandler (ProtocolHandler handler);
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070088
89 /**
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080090 * @brief Check if Interest limit is reached
91 *
92 * Side effect: if limit is not yet reached, the number of outstanding packets will be increased
93 *
94 * @returns true if Interest limit is not yet reached
95 */
96 bool
97 IsBelowLimit ();
98
99 /**
100 * \brief Send packet on a face
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700101 *
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800102 * This method will be called by lower layers to send data to device or application
103 *
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800104 * \param p smart pointer to a packet to send
105 *
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800106 * @return false if either limit is reached
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800107 */
108 bool
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800109 Send (Ptr<Packet> p);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800110
111 /**
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700112 * \brief Receive packet from application or another node and forward it to the Ndn stack
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800113 *
114 * \todo The only reason for this call is to handle tracing, if requested
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700115 */
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800116 bool
117 Receive (const Ptr<const Packet> &p);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800118 ////////////////////////////////////////////////////////////////////
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700119
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800120 /**
121 * \Brief Assign routing/forwarding metric with face
122 *
123 * \param metric configured routing metric (cost) of this face
124 */
125 virtual void SetMetric (uint16_t metric);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700126
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800127 /**
128 * \brief Get routing/forwarding metric assigned to the face
129 *
130 * \returns configured routing/forwarding metric (cost) of this face
131 */
132 virtual uint16_t GetMetric (void) const;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700133
134 /**
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700135 * These are face states and may be distinct from actual lower-layer
136 * device states, such as found in real implementations (where the
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700137 * device may be down but ndn face state is still up).
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700138 */
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700139
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700140 /**
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800141 * \brief Enable or disable this face
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700142 */
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800143 virtual void
144 SetUp (bool up = true);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700145
146 /**
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700147 * \brief Returns true if this face is enabled, false otherwise.
148 */
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800149 virtual bool
150 IsUp () const;
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700151
152 /**
153 * @brief Print information about the face into the stream
154 * @param os stream to write information to
155 */
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700156 virtual std::ostream&
157 Print (std::ostream &os) const;
158
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700159 /**
160 * \brief Set node Id
161 *
162 * Id is purely informative and should not be used for any other purpose
163 *
164 * \param id id to set
165 */
166 inline void
167 SetId (uint32_t id);
168
169 /**
170 * \brief Get node Id
171 *
172 * Id is purely informative and should not be used for any other purpose
173 *
174 * \returns id id to set
175 */
176 inline uint32_t
177 GetId () const;
178
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700179 /**
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800180 * @brief Set maximum value for Interest allowance
181 *
182 * @param bucket maximum value for Interest allowance. If < 0, then limit will be disabled
183 */
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800184 void
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800185 SetBucketMax (double bucket);
186
187 /**
Alexander Afanasyev11453142011-11-25 16:13:33 -0800188 * @brief Set a normalized value (one second) for Interest allowance bucket leak
189 */
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800190 void
Alexander Afanasyev11453142011-11-25 16:13:33 -0800191 SetBucketLeak (double leak);
192
193 /**
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800194 * @brief Leak the Interest allowance bucket by (1/interval) * m_bucketMax amount,
195 * where interval is time between two consecutive calls of LeakBucket
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800196 */
Alexander Afanasyevb5703a92011-11-25 16:46:15 -0800197 void
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800198 LeakBucket ();
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800199
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800200 /**
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700201 * \brief Compare two faces. Only two faces on the same node could be compared.
202 *
203 * Internal index is used for comparison.
204 */
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700205 bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700206 operator== (const Face &face) const;
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700207
208 /**
209 * \brief Compare two faces. Only two faces on the same node could be compared.
210 *
211 * Internal index is used for comparison.
212 */
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700213 inline bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700214 operator!= (const Face &face) const;
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700215
216 /**
217 * \brief Compare two faces. Only two faces on the same node could be compared.
218 *
219 * Internal index is used for comparison.
220 */
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700221 bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700222 operator< (const Face &face) const;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700223
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800224protected:
225 /**
226 * \brief Send packet on a face (actual implementation)
227 *
228 * \param p smart pointer to a packet to send
229 */
Alexander Afanasyev1c0248b2012-07-24 15:59:50 -0700230 virtual bool
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800231 SendImpl (Ptr<Packet> p) = 0;
232
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700233private:
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700234 Face (const Face &); ///< \brief Disabled copy constructor
235 Face& operator= (const Face &); ///< \brief Disabled copy operator
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700236
237protected:
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700238 // uint16_t m_metric; ///< \brief Routing/forwarding metric
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700239 Ptr<Node> m_node; ///< \brief Smart pointer to Node
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800240
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800241 double m_bucket; ///< \brief Value representing current size of the Interest allowance for this face
242 double m_bucketMax; ///< \brief Maximum Interest allowance for this face
243 double m_bucketLeak; ///< \brief Normalized amount that should be leaked every second
244
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700245private:
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700246 ProtocolHandler m_protocolHandler; ///< Callback via which packets are getting send to Ndn stack
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700247 bool m_ifup; ///< \brief flag indicating that the interface is UP
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700248 uint32_t m_id; ///< \brief id of the interface in Ndn stack (per-node uniqueness)
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800249 Time m_lastLeakTime;
250 uint32_t m_metric; ///< \brief metric of the face
Alexander Afanasyev98d85e12012-08-09 10:06:05 -0700251 bool m_randomizeLimitChecking;
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800252
Alexander Afanasyev30c33e32012-06-05 21:28:44 -0700253 // bool m_enableMetricTagging;
254
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700255 TracedCallback<Ptr<const Packet> > m_txTrace;
256 TracedCallback<Ptr<const Packet> > m_rxTrace;
257 TracedCallback<Ptr<const Packet> > m_dropTrace;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700258};
259
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700260std::ostream& operator<< (std::ostream& os, const Face &face);
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700261
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700262inline bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700263operator < (const Ptr<Face> &lhs, const Ptr<Face> &rhs)
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700264{
265 return *lhs < *rhs;
266}
267
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700268void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700269Face::SetId (uint32_t id)
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700270{
271 m_id = id;
272}
273
274uint32_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700275Face::GetId () const
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700276{
277 return m_id;
278}
Alexander Afanasyev98256102011-08-14 01:00:02 -0700279
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700280inline bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700281Face::operator!= (const Face &face) const
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700282{
283 return !(*this == face);
284}
285
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700286} // namespace ndn
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700287} // namespace ns3
288
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700289#endif // NDN_FACE_H