blob: b63b1e745f771c0521dad5452449f38058754a3b [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/*
3 * Copyright (c) 2005,2006,2007 INRIA
4 *
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 Afanasyev98256102011-08-14 01:00:02 -070021#ifndef CCNX_FACE_H
22#define CCNX_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 Afanasyev09c7deb2011-11-23 14:50:10 -080028#include "ns3/ccnx.h"
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080029#include "ns3/nstime.h"
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070030
31namespace ns3 {
32
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070033class Packet;
34class Node;
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -070035
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070036/**
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070037 * \ingroup ccnx
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -070038 * \defgroup ccnx-face Faces
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070039 */
40/**
41 * \ingroup ccnx-face
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070042 * \brief Virtual class defining CCNx face
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070043 *
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070044 * This class defines basic functionality of CCNx face. Face is core
45 * component responsible for actual delivery of data packet to and
46 * from CCNx stack
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070047 *
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070048 * \see CcnxLocalFace, CcnxNetDeviceFace, CcnxIpv4Face, CcnxUdpFace
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070049 */
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -080050class CcnxFace : public Object
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070051{
52public:
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -080053 static TypeId
54 GetTypeId ();
55
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070056 /**
Alexander Afanasyevbdc0d982011-12-16 01:15:26 -080057 * \brief Ccnx protocol handler
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070058 *
59 * \param face Face from which packet has been received
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080060 * \param packet Original packet
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070061 */
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -070062 typedef Callback<void,const Ptr<CcnxFace>&,const Ptr<const Packet>& > ProtocolHandler;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070063
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070064 /**
65 * \brief Default constructor
66 */
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080067 CcnxFace (Ptr<Node> node);
Alexander Afanasyev98256102011-08-14 01:00:02 -070068 virtual ~CcnxFace();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070069
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070070 ////////////////////////////////////////////////////////////////////
71
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070072 /**
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070073 * \brief Register callback to call when new packet arrives on the face
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070074 *
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070075 * This method should call protocol-dependent registration function
76 */
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080077 virtual void
78 RegisterProtocolHandler (ProtocolHandler handler);
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070079
80 /**
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080081 * @brief Check if Interest limit is reached
82 *
83 * Side effect: if limit is not yet reached, the number of outstanding packets will be increased
84 *
85 * @returns true if Interest limit is not yet reached
86 */
87 bool
88 IsBelowLimit ();
89
90 /**
91 * \brief Send packet on a face
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070092 *
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080093 * This method will be called by lower layers to send data to device or application
94 *
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080095 * \param p smart pointer to a packet to send
96 *
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080097 * @return false if either limit is reached
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080098 */
99 bool
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800100 Send (Ptr<Packet> p);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800101
102 /**
103 * \brief Receive packet from application or another node and forward it to the CCNx stack
104 *
105 * \todo The only reason for this call is to handle tracing, if requested
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700106 */
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800107 bool
108 Receive (const Ptr<const Packet> &p);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800109 ////////////////////////////////////////////////////////////////////
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700110
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700111 // /**
112 // * \Brief Assign routing/forwarding metric with face
113 // *
114 // * \param metric configured routing metric (cost) of this face
115 // */
116 // virtual void SetMetric (uint16_t metric);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700117
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700118 // /**
119 // * \brief Get routing/forwarding metric assigned to the face
120 // *
121 // * \returns configured routing/forwarding metric (cost) of this face
122 // */
123 // virtual uint16_t GetMetric (void) const;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700124
125 /**
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700126 * These are face states and may be distinct from actual lower-layer
127 * device states, such as found in real implementations (where the
128 * device may be down but ccnx face state is still up).
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700129 */
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700130
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700131 /**
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800132 * \brief Enable or disable this face
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700133 */
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800134 virtual void
135 SetUp (bool up = true);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700136
137 /**
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700138 * \brief Returns true if this face is enabled, false otherwise.
139 */
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800140 virtual bool
141 IsUp () const;
Ilya Moiseenkoacac1ea2011-10-28 13:16:53 -0700142
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700143 virtual std::ostream&
144 Print (std::ostream &os) const;
145
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700146 /**
147 * \brief Set node Id
148 *
149 * Id is purely informative and should not be used for any other purpose
150 *
151 * \param id id to set
152 */
153 inline void
154 SetId (uint32_t id);
155
156 /**
157 * \brief Get node Id
158 *
159 * Id is purely informative and should not be used for any other purpose
160 *
161 * \returns id id to set
162 */
163 inline uint32_t
164 GetId () const;
165
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700166 /**
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800167 * @brief Set maximum value for Interest allowance
168 *
169 * @param bucket maximum value for Interest allowance. If < 0, then limit will be disabled
170 */
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800171 void
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800172 SetBucketMax (double bucket);
173
174 /**
Alexander Afanasyev11453142011-11-25 16:13:33 -0800175 * @brief Set a normalized value (one second) for Interest allowance bucket leak
176 */
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800177 void
Alexander Afanasyev11453142011-11-25 16:13:33 -0800178 SetBucketLeak (double leak);
179
180 /**
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800181 * @brief Leak the Interest allowance bucket by (1/interval) * m_bucketMax amount
182 *
183 * @param interval Time interval with which the bucket is leaked
184 */
Alexander Afanasyevb5703a92011-11-25 16:46:15 -0800185 void
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800186 LeakBucket (const Time &interval);
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800187
Alexander Afanasyevc39f0b42011-11-28 12:51:12 -0800188 void
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800189 LeakBucketByOnePacket ();
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800190
191 /**
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700192 * \brief Compare two faces. Only two faces on the same node could be compared.
193 *
194 * Internal index is used for comparison.
195 */
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700196 bool
197 operator== (const CcnxFace &face) const;
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700198
199 /**
200 * \brief Compare two faces. Only two faces on the same node could be compared.
201 *
202 * Internal index is used for comparison.
203 */
204 bool
205 operator< (const CcnxFace &face) const;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700206
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800207protected:
208 /**
209 * \brief Send packet on a face (actual implementation)
210 *
211 * \param p smart pointer to a packet to send
212 */
213 virtual void
214 SendImpl (Ptr<Packet> p) = 0;
215
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700216private:
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700217 CcnxFace (const CcnxFace &); ///< \brief Disabled copy constructor
218 CcnxFace& operator= (const CcnxFace &); ///< \brief Disabled copy operator
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700219
220protected:
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700221 // uint16_t m_metric; ///< \brief Routing/forwarding metric
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700222 Ptr<Node> m_node; ///< \brief Smart pointer to Node
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800223
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800224 double m_bucket; ///< \brief Value representing current size of the Interest allowance for this face
225 double m_bucketMax; ///< \brief Maximum Interest allowance for this face
226 double m_bucketLeak; ///< \brief Normalized amount that should be leaked every second
227
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700228private:
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800229 ProtocolHandler m_protocolHandler; ///< Callback via which packets are getting send to CCNx stack
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700230 bool m_ifup; ///< \brief flag indicating that the interface is UP
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800231 uint32_t m_id; ///< \brief id of the interface in CCNx stack (per-node uniqueness)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700232};
233
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700234std::ostream& operator<< (std::ostream& os, const CcnxFace &face);
235
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700236inline bool
237operator < (const Ptr<CcnxFace> &lhs, const Ptr<CcnxFace> &rhs)
238{
239 return *lhs < *rhs;
240}
241
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700242void
243CcnxFace::SetId (uint32_t id)
244{
245 m_id = id;
246}
247
248uint32_t
249CcnxFace::GetId () const
250{
251 return m_id;
252}
Alexander Afanasyev98256102011-08-14 01:00:02 -0700253
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700254} // namespace ns3
255
Alexander Afanasyev98256102011-08-14 01:00:02 -0700256#endif //CCNX_FACE_H