blob: 86003ffb433ad8da20e3df7bf925cc48eedc25a5 [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 Afanasyeva89bc102013-07-16 10:17:31 -070032#include "ns3/ndn-name.h"
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;
42class ContentObject;
43
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 Afanasyevcf6dc922012-08-10 16:55:27 -070058class Face :
59 public Object
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070060{
61public:
Alexander Afanasyev5ab2bcd2013-07-15 18:21:19 -070062 enum
63 {
64 WIRE_FORMAT_NDNSIM = 0,
65 WIRE_FORMAT_CCNB = 1
66 };
67
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -080068 static TypeId
69 GetTypeId ();
Alexander Afanasyevdd9fa4f2013-05-15 16:35:04 -070070
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070071 /**
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -070072 * \brief NDN protocol handlers
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070073 *
74 * \param face Face from which packet has been received
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080075 * \param packet Original packet
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070076 */
Alexander Afanasyevb989b122013-07-10 17:15:46 -070077 typedef Callback<void, Ptr<Face>, Ptr<Interest> > InterestHandler;
78 typedef Callback<void, Ptr<Face>, Ptr<ContentObject> > DataHandler;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070079
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070080 /**
81 * \brief Default constructor
82 */
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070083 Face (Ptr<Node> node);
84 virtual ~Face();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070085
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070086 /**
87 * @brief Get node to which this face is associated
88 */
Alexander Afanasyeve9c9d722012-01-19 16:59:30 -080089 Ptr<Node>
90 GetNode () const;
91
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070092 ////////////////////////////////////////////////////////////////////
Alexander Afanasyevdd9fa4f2013-05-15 16:35:04 -070093
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070094 /**
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070095 * \brief Register callback to call when new packet arrives on the face
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070096 *
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070097 * This method should call protocol-dependent registration function
98 */
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080099 virtual void
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700100 RegisterProtocolHandlers (const InterestHandler &interestHandler, const DataHandler &dataHandler);
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700101
102 /**
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700103 * \brief Un-Register callback to call when new packet arrives on the face
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700104 *
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700105 * This method should call protocol-dependent registration function
Alexander Afanasyevdd9fa4f2013-05-15 16:35:04 -0700106 */
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700107 virtual void
108 UnRegisterProtocolHandlers ();
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800109
110 /**
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700111 * @brief Send out interest through the face
112 * @param interest Interest to send out
113 * @param packet "payload" that is attached to the interest (can carry some packet tags)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800114 *
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700115 * @returns true if interest is considered to be send out (enqueued)
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700116 */
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700117 virtual bool
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700118 SendInterest (Ptr<const Interest> interest);
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700119
120 /**
121 * @brief Send out Dat packet through the face
122 * @param data Data packet to send out
123 * @param packet Data packet payload, can also carry packet tags
124 *
125 * @returns true if Data packet is considered to be send out (enqueued)
126 */
127 virtual bool
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700128 SendData (Ptr<const ContentObject> data);
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700129
130 /**
131 * \brief Receive interest 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 Afanasyevb989b122013-07-10 17:15:46 -0700136 ReceiveInterest (Ptr<Interest> interest);
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700137
138 /**
139 * \brief Receive Data packet from application or another node and forward it up to the NDN stack
140 *
141 * By default it is called from inside Receive method, but can be used directly, if appropriate
142 */
143 virtual bool
Alexander Afanasyevb989b122013-07-10 17:15:46 -0700144 ReceiveData (Ptr<ContentObject> data);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800145 ////////////////////////////////////////////////////////////////////
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700146
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800147 /**
Alexander Afanasyev042b4a72012-11-09 17:47:48 -0800148 * \brief Assign routing/forwarding metric with face
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800149 *
150 * \param metric configured routing metric (cost) of this face
151 */
Alexander Afanasyevdd9fa4f2013-05-15 16:35:04 -0700152 virtual void
153 SetMetric (uint16_t metric);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700154
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800155 /**
156 * \brief Get routing/forwarding metric assigned to the face
157 *
158 * \returns configured routing/forwarding metric (cost) of this face
159 */
Alexander Afanasyevdd9fa4f2013-05-15 16:35:04 -0700160 virtual uint16_t
161 GetMetric (void) const;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700162
163 /**
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700164 * These are face states and may be distinct from actual lower-layer
165 * device states, such as found in real implementations (where the
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700166 * device may be down but ndn face state is still up).
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700167 */
Alexander Afanasyevdd9fa4f2013-05-15 16:35:04 -0700168
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700169 /**
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800170 * \brief Enable or disable this face
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700171 */
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700172 inline void
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800173 SetUp (bool up = true);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700174
175 /**
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700176 * \brief Returns true if this face is enabled, false otherwise.
177 */
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700178 inline bool
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800179 IsUp () const;
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700180
181 /**
Alexander Afanasyevdd9fa4f2013-05-15 16:35:04 -0700182 * @brief Get face flags
183 *
184 * Face flags may indicate various properties of the face. For example, if the face is an application face,
185 * than the returned flags have Face::APPLICATION bit set.
186 *
187 * @see ndn::Face::Flags for the list of currently defined face flags
188 */
189 inline uint32_t
190 GetFlags () const;
191
192 /**
193 * @brief List of currently defined face flags
194 */
195 enum Flags
196 {
197 APPLICATION = 1 ///< @brief An application face
198 };
Alexander Afanasyev5ab2bcd2013-07-15 18:21:19 -0700199
Alexander Afanasyevdd9fa4f2013-05-15 16:35:04 -0700200 /**
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700201 * @brief Print information about the face into the stream
202 * @param os stream to write information to
203 */
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700204 virtual std::ostream&
205 Print (std::ostream &os) const;
206
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700207 /**
Alexander Afanasyevc57a8772013-03-16 11:55:20 -0700208 * \brief Set face Id
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700209 *
210 * Id is purely informative and should not be used for any other purpose
211 *
212 * \param id id to set
213 */
214 inline void
215 SetId (uint32_t id);
216
217 /**
Alexander Afanasyevc57a8772013-03-16 11:55:20 -0700218 * \brief Get face Id
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700219 *
220 * Id is purely informative and should not be used for any other purpose
221 *
222 * \returns id id to set
223 */
224 inline uint32_t
225 GetId () const;
226
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700227 /**
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 Afanasyev56f79ea2011-08-17 23:54:27 -0700232 bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700233 operator== (const Face &face) const;
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700234
235 /**
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 Afanasyev0845c092012-07-13 17:45:33 -0700240 inline bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700241 operator!= (const Face &face) const;
Alexander Afanasyevdd9fa4f2013-05-15 16:35:04 -0700242
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700243 /**
244 * \brief Compare two faces. Only two faces on the same node could be compared.
245 *
246 * Internal index is used for comparison.
247 */
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700248 bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700249 operator< (const Face &face) const;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700250
Alexander Afanasyeva89bc102013-07-16 10:17:31 -0700251 ///////////////////////////////////////////////////////////////////////////////
252 // Helpers to create wire-formatted data in default wire format, since settings about
253 // the wire format belong to the face
254 /*
255 * @brief Get size of buffer to fit wire-formatted name object
256 */
257 uint32_t
258 NameToWireSize (Ptr<Name> name) const;
259
260 /**
261 * @brief Convert name to wire format
262 */
263 void
264 NameToWire (Buffer::Iterator start, Ptr<const Name> name) const;
265
266 /**
267 * @brief Convert name from wire format
268 */
269 Ptr<Name>
270 NameFromWire (Buffer::Iterator start) const;
271
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800272protected:
273 /**
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700274 * @brief Send packet down to the stack (towards app or network)
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800275 */
Alexander Afanasyev1c0248b2012-07-24 15:59:50 -0700276 virtual bool
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700277 Send (Ptr<Packet> packet);
Alexander Afanasyev5ab2bcd2013-07-15 18:21:19 -0700278
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700279 /**
280 * @brief Send packet up to the stack (towards forwarding strategy)
281 */
282 virtual bool
283 Receive (Ptr<const Packet> p);
Alexander Afanasyevdd9fa4f2013-05-15 16:35:04 -0700284
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700285 /**
286 * @brief Set face flags
287 */
Alexander Afanasyevdd9fa4f2013-05-15 16:35:04 -0700288 void
289 SetFlags (uint32_t flags);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800290
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700291private:
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700292 Face (const Face &); ///< \brief Disabled copy constructor
293 Face& operator= (const Face &); ///< \brief Disabled copy operator
Alexander Afanasyevdd9fa4f2013-05-15 16:35:04 -0700294
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700295protected:
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700296 Ptr<Node> m_node; ///< \brief Smart pointer to Node
Alexander Afanasyevdd9fa4f2013-05-15 16:35:04 -0700297
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700298private:
Alexander Afanasyev5ab2bcd2013-07-15 18:21:19 -0700299 uint32_t m_wireFormat;
300
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700301 InterestHandler m_upstreamInterestHandler;
302 DataHandler m_upstreamDataHandler;
303 bool m_ifup;
Alexander Afanasyevc57a8772013-03-16 11:55:20 -0700304 uint32_t m_id; ///< \brief id of the interface in NDN stack (per-node uniqueness)
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800305 uint32_t m_metric; ///< \brief metric of the face
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700306 uint32_t m_flags; ///< @brief faces flags (e.g., APPLICATION)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700307};
308
Alexander Afanasyevdd9fa4f2013-05-15 16:35:04 -0700309std::ostream&
310operator<< (std::ostream& os, const Face &face);
311
Alexander Afanasyev5bee19e2013-07-10 14:33:57 -0700312inline bool
313Face::IsUp (void) const
314{
315 return m_ifup;
316}
317
318inline void
319Face::SetUp (bool up/* = true*/)
320{
321 m_ifup = up;
322}
323
Alexander Afanasyevdd9fa4f2013-05-15 16:35:04 -0700324inline uint32_t
325Face::GetFlags () const
326{
327 return m_flags;
328}
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700329
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700330inline bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700331operator < (const Ptr<Face> &lhs, const Ptr<Face> &rhs)
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700332{
333 return *lhs < *rhs;
334}
335
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700336void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700337Face::SetId (uint32_t id)
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700338{
339 m_id = id;
340}
341
342uint32_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700343Face::GetId () const
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700344{
345 return m_id;
346}
Alexander Afanasyev98256102011-08-14 01:00:02 -0700347
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700348inline bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700349Face::operator!= (const Face &face) const
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700350{
351 return !(*this == face);
352}
353
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700354} // namespace ndn
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700355} // namespace ns3
356
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700357#endif // NDN_FACE_H