blob: 6ed40bc882692192728574fc53e975f5ff04f15e [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 Afanasyev08d984e2011-08-13 19:20:22 -070032
33namespace ns3 {
34
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070035class Packet;
36class Node;
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070037
38namespace ndn {
39
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070040/**
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070041 * \ingroup ndn
42 * \defgroup ndn-face Faces
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070043 */
44/**
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070045 * \ingroup ndn-face
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070046 * \brief Virtual class defining NDN face
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070047 *
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070048 * This class defines basic functionality of NDN face. Face is core
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070049 * component responsible for actual delivery of data packet to and
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070050 * from NDN stack
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070051 *
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070052 * \see ndn::LocalFace, ndn::NetDeviceFace, ndn::Ipv4Face, ndn::UdpFace
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070053 */
Alexander Afanasyevcf6dc922012-08-10 16:55:27 -070054class Face :
55 public Object
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070056{
57public:
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -080058 static TypeId
59 GetTypeId ();
60
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070061 /**
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070062 * \brief Ndn protocol handler
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070063 *
64 * \param face Face from which packet has been received
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080065 * \param packet Original packet
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070066 */
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070067 typedef Callback<void,const Ptr<Face>&,const Ptr<const Packet>& > ProtocolHandler;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070068
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070069 /**
70 * \brief Default constructor
71 */
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070072 Face (Ptr<Node> node);
73 virtual ~Face();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070074
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070075 /**
76 * @brief Get node to which this face is associated
77 */
Alexander Afanasyeve9c9d722012-01-19 16:59:30 -080078 Ptr<Node>
79 GetNode () const;
80
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070081 ////////////////////////////////////////////////////////////////////
82
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070083 /**
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070084 * \brief Register callback to call when new packet arrives on the face
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070085 *
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070086 * This method should call protocol-dependent registration function
87 */
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080088 virtual void
89 RegisterProtocolHandler (ProtocolHandler handler);
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070090
91 /**
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080092 */
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080093
94 /**
95 * \brief Send packet on a face
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070096 *
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080097 * This method will be called by lower layers to send data to device or application
98 *
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080099 * \param p smart pointer to a packet to send
100 *
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800101 * @return false if either limit is reached
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800102 */
103 bool
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800104 Send (Ptr<Packet> p);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800105
106 /**
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700107 * \brief Receive packet from application or another node and forward it to the Ndn stack
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800108 *
109 * \todo The only reason for this call is to handle tracing, if requested
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700110 */
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800111 bool
112 Receive (const Ptr<const Packet> &p);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800113 ////////////////////////////////////////////////////////////////////
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700114
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800115 /**
116 * \Brief Assign routing/forwarding metric with face
117 *
118 * \param metric configured routing metric (cost) of this face
119 */
120 virtual void SetMetric (uint16_t metric);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700121
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800122 /**
123 * \brief Get routing/forwarding metric assigned to the face
124 *
125 * \returns configured routing/forwarding metric (cost) of this face
126 */
127 virtual uint16_t GetMetric (void) const;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700128
129 /**
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700130 * These are face states and may be distinct from actual lower-layer
131 * device states, such as found in real implementations (where the
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700132 * device may be down but ndn face state is still up).
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700133 */
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700134
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700135 /**
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800136 * \brief Enable or disable this face
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700137 */
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800138 virtual void
139 SetUp (bool up = true);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700140
141 /**
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700142 * \brief Returns true if this face is enabled, false otherwise.
143 */
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800144 virtual bool
145 IsUp () const;
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700146
147 /**
148 * @brief Print information about the face into the stream
149 * @param os stream to write information to
150 */
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700151 virtual std::ostream&
152 Print (std::ostream &os) const;
153
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700154 /**
155 * \brief Set node Id
156 *
157 * Id is purely informative and should not be used for any other purpose
158 *
159 * \param id id to set
160 */
161 inline void
162 SetId (uint32_t id);
163
164 /**
165 * \brief Get node Id
166 *
167 * Id is purely informative and should not be used for any other purpose
168 *
169 * \returns id id to set
170 */
171 inline uint32_t
172 GetId () const;
173
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700174 /**
175 * \brief Compare two faces. Only two faces on the same node could be compared.
176 *
177 * Internal index is used for comparison.
178 */
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700179 bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700180 operator== (const Face &face) const;
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700181
182 /**
183 * \brief Compare two faces. Only two faces on the same node could be compared.
184 *
185 * Internal index is used for comparison.
186 */
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700187 inline bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700188 operator!= (const Face &face) const;
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700189
190 /**
191 * \brief Compare two faces. Only two faces on the same node could be compared.
192 *
193 * Internal index is used for comparison.
194 */
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700195 bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700196 operator< (const Face &face) const;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700197
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800198protected:
199 /**
200 * \brief Send packet on a face (actual implementation)
201 *
202 * \param p smart pointer to a packet to send
203 */
Alexander Afanasyev1c0248b2012-07-24 15:59:50 -0700204 virtual bool
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800205 SendImpl (Ptr<Packet> p) = 0;
206
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700207private:
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700208 Face (const Face &); ///< \brief Disabled copy constructor
209 Face& operator= (const Face &); ///< \brief Disabled copy operator
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700210
211protected:
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700212 Ptr<Node> m_node; ///< \brief Smart pointer to Node
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800213
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700214private:
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700215 ProtocolHandler m_protocolHandler; ///< Callback via which packets are getting send to Ndn stack
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700216 bool m_ifup; ///< \brief flag indicating that the interface is UP
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700217 uint32_t m_id; ///< \brief id of the interface in Ndn stack (per-node uniqueness)
Alexander Afanasyev8e0d2812012-01-19 22:38:14 -0800218 uint32_t m_metric; ///< \brief metric of the face
219
Alexander Afanasyev30c33e32012-06-05 21:28:44 -0700220 // bool m_enableMetricTagging;
221
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700222 TracedCallback<Ptr<const Packet> > m_txTrace;
223 TracedCallback<Ptr<const Packet> > m_rxTrace;
224 TracedCallback<Ptr<const Packet> > m_dropTrace;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700225};
226
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700227std::ostream& operator<< (std::ostream& os, const Face &face);
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700228
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700229inline bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700230operator < (const Ptr<Face> &lhs, const Ptr<Face> &rhs)
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700231{
232 return *lhs < *rhs;
233}
234
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700235void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700236Face::SetId (uint32_t id)
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700237{
238 m_id = id;
239}
240
241uint32_t
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700242Face::GetId () const
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700243{
244 return m_id;
245}
Alexander Afanasyev98256102011-08-14 01:00:02 -0700246
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700247inline bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700248Face::operator!= (const Face &face) const
Alexander Afanasyev0845c092012-07-13 17:45:33 -0700249{
250 return !(*this == face);
251}
252
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700253} // namespace ndn
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700254} // namespace ns3
255
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700256#endif // NDN_FACE_H