blob: 7741c1ed57105bf719cf8863cba47d422aff353f [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 Afanasyevea9b3e62012-08-13 19:02:54 -070032#include "ns3/ndn-limits.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 Afanasyev08d984e2011-08-13 19:20:22 -070041/**
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070042 * \ingroup ndn
43 * \defgroup ndn-face Faces
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070044 */
45/**
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070046 * \ingroup ndn-face
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070047 * \brief Virtual class defining NDN face
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070048 *
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070049 * This class defines basic functionality of NDN face. Face is core
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070050 * component responsible for actual delivery of data packet to and
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070051 * from NDN stack
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070052 *
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070053 * \see ndn::LocalFace, ndn::NetDeviceFace, ndn::Ipv4Face, ndn::UdpFace
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070054 */
Alexander Afanasyevcf6dc922012-08-10 16:55:27 -070055class Face :
56 public Object
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070057{
58public:
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -080059 static TypeId
60 GetTypeId ();
61
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070062 /**
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070063 * \brief Ndn protocol handler
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070064 *
65 * \param face Face from which packet has been received
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080066 * \param packet Original packet
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070067 */
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070068 typedef Callback<void,const Ptr<Face>&,const Ptr<const Packet>& > ProtocolHandler;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070069
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070070 /**
71 * \brief Default constructor
72 */
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070073 Face (Ptr<Node> node);
74 virtual ~Face();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070075
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070076 /**
77 * @brief Get node to which this face is associated
78 */
Alexander Afanasyeve9c9d722012-01-19 16:59:30 -080079 Ptr<Node>
80 GetNode () const;
81
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070082 ////////////////////////////////////////////////////////////////////
83
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070084 /**
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070085 * \brief Register callback to call when new packet arrives on the face
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070086 *
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070087 * This method should call protocol-dependent registration function
88 */
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080089 virtual void
90 RegisterProtocolHandler (ProtocolHandler handler);
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070091
92 /**
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -070093 * @brief Get reference to Limits object
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080094 */
Alexander Afanasyev6a3bb132012-08-15 09:47:35 -070095 inline Limits&
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -070096 GetLimits ();
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080097
98 /**
99 * \brief Send packet on a face
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700100 *
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800101 * This method will be called by lower layers to send data to device or application
102 *
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800103 * \param p smart pointer to a packet to send
104 *
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800105 * @return false if either limit is reached
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800106 */
107 bool
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800108 Send (Ptr<Packet> p);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800109
110 /**
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700111 * \brief Receive packet from application or another node and forward it to the Ndn stack
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800112 *
113 * \todo The only reason for this call is to handle tracing, if requested
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700114 */
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800115 bool
116 Receive (const Ptr<const Packet> &p);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800117 ////////////////////////////////////////////////////////////////////
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700118
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800119 /**
120 * \Brief Assign routing/forwarding metric with face
121 *
122 * \param metric configured routing metric (cost) of this face
123 */
124 virtual void SetMetric (uint16_t metric);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700125
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800126 /**
127 * \brief Get routing/forwarding metric assigned to the face
128 *
129 * \returns configured routing/forwarding metric (cost) of this face
130 */
131 virtual uint16_t GetMetric (void) const;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700132
133 /**
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700134 * These are face states and may be distinct from actual lower-layer
135 * device states, such as found in real implementations (where the
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700136 * device may be down but ndn face state is still up).
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700137 */
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700138
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700139 /**
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800140 * \brief Enable or disable this face
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700141 */
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800142 virtual void
143 SetUp (bool up = true);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700144
145 /**
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700146 * \brief Returns true if this face is enabled, false otherwise.
147 */
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800148 virtual bool
149 IsUp () const;
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700150
151 /**
152 * @brief Print information about the face into the stream
153 * @param os stream to write information to
154 */
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700155 virtual std::ostream&
156 Print (std::ostream &os) const;
157
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700158 /**
159 * \brief Set node Id
160 *
161 * Id is purely informative and should not be used for any other purpose
162 *
163 * \param id id to set
164 */
165 inline void
166 SetId (uint32_t id);
167
168 /**
169 * \brief Get node Id
170 *
171 * Id is purely informative and should not be used for any other purpose
172 *
173 * \returns id id to set
174 */
175 inline uint32_t
176 GetId () const;
177
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700178 /**
179 * \brief Compare two faces. Only two faces on the same node could be compared.
180 *
181 * Internal index is used for comparison.
182 */
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700183 bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700184 operator== (const Face &face) const;
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700185
186 /**
187 * \brief Compare two faces. Only two faces on the same node could be compared.
188 *
189 * Internal index is used for comparison.
190 */
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700191 inline bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700192 operator!= (const Face &face) const;
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700193
194 /**
195 * \brief Compare two faces. Only two faces on the same node could be compared.
196 *
197 * Internal index is used for comparison.
198 */
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700199 bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700200 operator< (const Face &face) const;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700201
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800202protected:
203 /**
204 * \brief Send packet on a face (actual implementation)
205 *
206 * \param p smart pointer to a packet to send
207 */
Alexander Afanasyev1c0248b2012-07-24 15:59:50 -0700208 virtual bool
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800209 SendImpl (Ptr<Packet> p) = 0;
210
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700211private:
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700212 Face (const Face &); ///< \brief Disabled copy constructor
213 Face& operator= (const Face &); ///< \brief Disabled copy operator
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700214
215protected:
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700216 Ptr<Node> m_node; ///< \brief Smart pointer to Node
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800217
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700218private:
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700219 ProtocolHandler m_protocolHandler; ///< Callback via which packets are getting send to Ndn stack
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700220 bool m_ifup; ///< \brief flag indicating that the interface is UP
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700221 uint32_t m_id; ///< \brief id of the interface in Ndn stack (per-node uniqueness)
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800222 uint32_t m_metric; ///< \brief metric of the face
223
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700224 Ptr<Limits> m_limits;
Alexander Afanasyev30c33e32012-06-05 21:28:44 -0700225 // bool m_enableMetricTagging;
226
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700227 TracedCallback<Ptr<const Packet> > m_txTrace;
228 TracedCallback<Ptr<const Packet> > m_rxTrace;
229 TracedCallback<Ptr<const Packet> > m_dropTrace;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700230};
231
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700232std::ostream& operator<< (std::ostream& os, const Face &face);
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700233
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700234inline bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700235operator < (const Ptr<Face> &lhs, const Ptr<Face> &rhs)
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700236{
237 return *lhs < *rhs;
238}
239
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700240void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700241Face::SetId (uint32_t id)
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700242{
243 m_id = id;
244}
245
246uint32_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700247Face::GetId () const
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700248{
249 return m_id;
250}
Alexander Afanasyev98256102011-08-14 01:00:02 -0700251
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700252inline bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700253Face::operator!= (const Face &face) const
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700254{
255 return !(*this == face);
256}
257
Alexander Afanasyev6a3bb132012-08-15 09:47:35 -0700258inline Limits&
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700259Face::GetLimits ()
260{
Alexander Afanasyev6a3bb132012-08-15 09:47:35 -0700261 return *m_limits;
Alexander Afanasyevea9b3e62012-08-13 19:02:54 -0700262}
263
264
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700265} // namespace ndn
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700266} // namespace ns3
267
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700268#endif // NDN_FACE_H