blob: b8cd098b5f85d45b49442956ab533e7212b4ebe4 [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 Afanasyevcf6dc922012-08-10 16:55:27 -070028#include "ns3/object.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 Afanasyevcf6dc922012-08-10 16:55:27 -070031#include "ns3/traced-callback.h"
Alexander Afanasyev0c395372014-12-20 15:54:02 -080032#include "ns3/ndn-name.hpp"
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070033
34namespace ns3 {
35
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070036class Packet;
37class Node;
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070038
39namespace ndn {
40
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -070041class Interest;
Alexander Afanasyev772f51b2013-08-01 18:53:25 -070042class Data;
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -070043
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070044/**
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070045 * \ingroup ndn
46 * \defgroup ndn-face Faces
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070047 */
48/**
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070049 * \ingroup ndn-face
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070050 * \brief Virtual class defining NDN face
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070051 *
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070052 * This class defines basic functionality of NDN face. Face is core
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070053 * component responsible for actual delivery of data packet to and
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070054 * from NDN stack
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070055 *
Alexander Afanasyevc57a8772013-03-16 11:55:20 -070056 * \see ndn::AppFace, ndn::NetDeviceFace
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070057 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080058class Face : public Object {
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070059public:
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -080060 static TypeId
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080061 GetTypeId();
Alexander Afanasyevdd9fa4f2013-05-15 16:35:04 -070062
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070063 /**
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -070064 * \brief NDN protocol handlers
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070065 *
66 * \param face Face from which packet has been received
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080067 * \param packet Original packet
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070068 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080069 typedef Callback<void, Ptr<Face>, Ptr<Interest>> InterestHandler;
70 typedef Callback<void, Ptr<Face>, Ptr<Data>> DataHandler;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070071
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070072 /**
73 * \brief Default constructor
74 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080075 Face(Ptr<Node> node);
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070076 virtual ~Face();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070077
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070078 /**
79 * @brief Get node to which this face is associated
80 */
Alexander Afanasyeve9c9d722012-01-19 16:59:30 -080081 Ptr<Node>
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080082 GetNode() const;
Alexander Afanasyeve9c9d722012-01-19 16:59:30 -080083
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070084 ////////////////////////////////////////////////////////////////////
Alexander Afanasyevdd9fa4f2013-05-15 16:35:04 -070085
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070086 /**
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070087 * \brief Register callback to call when new packet arrives on the face
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070088 *
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070089 * This method should call protocol-dependent registration function
90 */
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080091 virtual void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080092 RegisterProtocolHandlers(const InterestHandler& interestHandler, const DataHandler& dataHandler);
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070093
94 /**
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -070095 * \brief Un-Register callback to call when new packet arrives on the face
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070096 *
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -070097 * This method should call protocol-dependent registration function
Alexander Afanasyevdd9fa4f2013-05-15 16:35:04 -070098 */
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -070099 virtual void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800100 UnRegisterProtocolHandlers();
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800101
102 /**
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700103 * @brief Send out interest through the face
104 * @param interest Interest to send out
105 * @param packet "payload" that is attached to the interest (can carry some packet tags)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800106 *
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700107 * @returns true if interest is considered to be send out (enqueued)
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700108 */
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700109 virtual bool
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800110 SendInterest(Ptr<const Interest> interest);
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700111
112 /**
113 * @brief Send out Dat packet through the face
114 * @param data Data packet to send out
115 * @param packet Data packet payload, can also carry packet tags
116 *
117 * @returns true if Data packet is considered to be send out (enqueued)
118 */
119 virtual bool
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800120 SendData(Ptr<const Data> data);
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700121
122 /**
123 * \brief Receive interest from application or another node and forward it up to the NDN stack
124 *
125 * By default it is called from inside Receive method, but can be used directly, if appropriate
126 */
127 virtual bool
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800128 ReceiveInterest(Ptr<Interest> interest);
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700129
130 /**
131 * \brief Receive Data packet from application or another node and forward it up to the NDN stack
132 *
133 * By default it is called from inside Receive method, but can be used directly, if appropriate
134 */
135 virtual bool
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800136 ReceiveData(Ptr<Data> data);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800137 ////////////////////////////////////////////////////////////////////
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700138
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800139 /**
Alexander Afanasyev042b4a72012-11-09 17:47:48 -0800140 * \brief Assign routing/forwarding metric with face
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800141 *
142 * \param metric configured routing metric (cost) of this face
143 */
Alexander Afanasyevdd9fa4f2013-05-15 16:35:04 -0700144 virtual void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800145 SetMetric(uint16_t metric);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700146
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800147 /**
148 * \brief Get routing/forwarding metric assigned to the face
149 *
150 * \returns configured routing/forwarding metric (cost) of this face
151 */
Alexander Afanasyevdd9fa4f2013-05-15 16:35:04 -0700152 virtual uint16_t
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800153 GetMetric(void) const;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700154
155 /**
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700156 * These are face states and may be distinct from actual lower-layer
157 * device states, such as found in real implementations (where the
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700158 * device may be down but ndn face state is still up).
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700159 */
Alexander Afanasyevdd9fa4f2013-05-15 16:35:04 -0700160
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700161 /**
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800162 * \brief Enable or disable this face
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700163 */
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700164 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800165 SetUp(bool up = true);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700166
167 /**
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700168 * \brief Returns true if this face is enabled, false otherwise.
169 */
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700170 inline bool
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800171 IsUp() const;
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700172
173 /**
Alexander Afanasyevdd9fa4f2013-05-15 16:35:04 -0700174 * @brief Get face flags
175 *
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800176 * Face flags may indicate various properties of the face. For example, if the face is an
177 *application face,
Alexander Afanasyevdd9fa4f2013-05-15 16:35:04 -0700178 * than the returned flags have Face::APPLICATION bit set.
179 *
180 * @see ndn::Face::Flags for the list of currently defined face flags
181 */
182 inline uint32_t
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800183 GetFlags() const;
Alexander Afanasyevdd9fa4f2013-05-15 16:35:04 -0700184
185 /**
186 * @brief List of currently defined face flags
187 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800188 enum Flags {
189 APPLICATION = 1 ///< @brief An application face
190 };
Alexander Afanasyev5ab2bcd2013-07-15 18:21:19 -0700191
Alexander Afanasyevdd9fa4f2013-05-15 16:35:04 -0700192 /**
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700193 * @brief Print information about the face into the stream
194 * @param os stream to write information to
195 */
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700196 virtual std::ostream&
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800197 Print(std::ostream& os) const;
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700198
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700199 /**
Alexander Afanasyevc57a8772013-03-16 11:55:20 -0700200 * \brief Set face Id
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700201 *
202 * Id is purely informative and should not be used for any other purpose
203 *
204 * \param id id to set
205 */
206 inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800207 SetId(uint32_t id);
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700208
209 /**
Alexander Afanasyevc57a8772013-03-16 11:55:20 -0700210 * \brief Get face Id
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700211 *
212 * Id is purely informative and should not be used for any other purpose
213 *
214 * \returns id id to set
215 */
216 inline uint32_t
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800217 GetId() const;
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700218
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700219 /**
220 * \brief Compare two faces. Only two faces on the same node could be compared.
221 *
222 * Internal index is used for comparison.
223 */
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700224 bool
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800225 operator==(const Face& face) const;
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700226
227 /**
228 * \brief Compare two faces. Only two faces on the same node could be compared.
229 *
230 * Internal index is used for comparison.
231 */
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700232 inline bool
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800233 operator!=(const Face& face) const;
Alexander Afanasyevdd9fa4f2013-05-15 16:35:04 -0700234
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700235 /**
236 * \brief Compare two faces. Only two faces on the same node could be compared.
237 *
238 * Internal index is used for comparison.
239 */
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700240 bool
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800241 operator<(const Face& face) const;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700242
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800243protected:
244 /**
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700245 * @brief Send packet down to the stack (towards app or network)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800246 */
Alexander Afanasyev1c0248b2012-07-24 15:59:50 -0700247 virtual bool
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800248 Send(Ptr<Packet> packet);
Alexander Afanasyev5ab2bcd2013-07-15 18:21:19 -0700249
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700250 /**
251 * @brief Send packet up to the stack (towards forwarding strategy)
252 */
253 virtual bool
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800254 Receive(Ptr<const Packet> p);
Alexander Afanasyevdd9fa4f2013-05-15 16:35:04 -0700255
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700256 /**
257 * @brief Set face flags
258 */
Alexander Afanasyevdd9fa4f2013-05-15 16:35:04 -0700259 void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800260 SetFlags(uint32_t flags);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800261
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700262private:
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800263 Face(const Face&); ///< \brief Disabled copy constructor
264 Face&
265 operator=(const Face&); ///< \brief Disabled copy operator
Alexander Afanasyevdd9fa4f2013-05-15 16:35:04 -0700266
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700267protected:
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700268 Ptr<Node> m_node; ///< \brief Smart pointer to Node
Alexander Afanasyevdd9fa4f2013-05-15 16:35:04 -0700269
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700270private:
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700271 InterestHandler m_upstreamInterestHandler;
272 DataHandler m_upstreamDataHandler;
273 bool m_ifup;
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800274 uint32_t m_id; ///< \brief id of the interface in NDN stack (per-node uniqueness)
Alexander Afanasyev52a09bd2013-07-29 18:49:01 -0700275 uint16_t m_metric; ///< \brief metric of the face
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800276 uint32_t m_flags; ///< @brief faces flags (e.g., APPLICATION)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700277};
278
Alexander Afanasyevdd9fa4f2013-05-15 16:35:04 -0700279std::ostream&
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800280operator<<(std::ostream& os, const Face& face);
Alexander Afanasyevdd9fa4f2013-05-15 16:35:04 -0700281
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700282inline bool
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800283Face::IsUp(void) const
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700284{
285 return m_ifup;
286}
287
288inline void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800289Face::SetUp(bool up /* = true*/)
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700290{
291 m_ifup = up;
292}
293
Alexander Afanasyevdd9fa4f2013-05-15 16:35:04 -0700294inline uint32_t
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800295Face::GetFlags() const
Alexander Afanasyevdd9fa4f2013-05-15 16:35:04 -0700296{
297 return m_flags;
298}
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700299
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700300inline bool
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800301operator<(const Ptr<Face>& lhs, const Ptr<Face>& rhs)
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700302{
303 return *lhs < *rhs;
304}
305
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700306void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800307Face::SetId(uint32_t id)
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700308{
309 m_id = id;
310}
311
312uint32_t
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800313Face::GetId() const
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700314{
315 return m_id;
316}
Alexander Afanasyev98256102011-08-14 01:00:02 -0700317
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700318inline bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700319Face::operator!= (const Face &face) const
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700320{
321 return !(*this == face);
322}
323
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700324} // namespace ndn
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700325} // namespace ns3
326
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700327#endif // NDN_FACE_H