blob: bc6769ebd915009a48a86b42e67ee2a5bafb8951 [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>
25
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070026#include "ns3/ptr.h"
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080027#include "ns3/ccnx.h"
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070028
29namespace ns3 {
30
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070031class Packet;
32class Node;
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -070033
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070034/**
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070035 * \ingroup ccnx
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -070036 * \defgroup ccnx-face Faces
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -070037 */
38/**
39 * \ingroup ccnx-face
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070040 * \brief Virtual class defining CCNx face
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070041 *
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070042 * This class defines basic functionality of CCNx face. Face is core
43 * component responsible for actual delivery of data packet to and
44 * from CCNx stack
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070045 *
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070046 * \see CcnxLocalFace, CcnxNetDeviceFace, CcnxIpv4Face, CcnxUdpFace
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070047 */
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070048class CcnxFace : public SimpleRefCount<CcnxFace>
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070049{
50public:
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070051 /**
52 * \brief Ccnx protocol hanler
53 *
54 * \param face Face from which packet has been received
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080055 * \param packet Original packet
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070056 */
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -070057 typedef Callback<void,const Ptr<CcnxFace>&,const Ptr<const Packet>& > ProtocolHandler;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070058
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070059 /**
60 * \brief Default constructor
61 */
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080062 CcnxFace (Ptr<Node> node);
Alexander Afanasyev98256102011-08-14 01:00:02 -070063 virtual ~CcnxFace();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070064
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070065 ////////////////////////////////////////////////////////////////////
66
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070067 /**
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070068 * \brief Register callback to call when new packet arrives on the face
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070069 *
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070070 * This method should call protocol-dependent registration function
71 */
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080072 virtual void
73 RegisterProtocolHandler (ProtocolHandler handler);
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070074
75 /**
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080076 * \brief Send packet on a face with regard Interest limits
77 *
78 * This method will be called by lower layers to send data to device or application
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070079 *
80 * \param p smart pointer to a packet to send
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080081 *
82 * @return false if either limit is reached or face is down
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070083 */
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080084 bool
85 SendWithLimit (Ptr<Packet> p);
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070086
87 /**
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080088 * \brief Send content packet on a face without regard to limits
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070089 *
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080090 * This method will be called by lower layers to send data to device or application
91 *
92 * !!! The only difference between this call and SendInterest is that the former check Interest limit !!!
93 *
94 * \param p smart pointer to a packet to send
95 *
96 * @return false if face is down
97 */
98 bool
99 SendWithoutLimits (Ptr<Packet> p);
100
101 /**
102 * \brief Receive packet from application or another node and forward it to the CCNx stack
103 *
104 * \todo The only reason for this call is to handle tracing, if requested
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700105 */
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800106 void
107 Receive (Ptr<const Packet> p);
108 ////////////////////////////////////////////////////////////////////
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700109
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700110 // /**
111 // * \Brief Assign routing/forwarding metric with face
112 // *
113 // * \param metric configured routing metric (cost) of this face
114 // */
115 // virtual void SetMetric (uint16_t metric);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700116
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700117 // /**
118 // * \brief Get routing/forwarding metric assigned to the face
119 // *
120 // * \returns configured routing/forwarding metric (cost) of this face
121 // */
122 // virtual uint16_t GetMetric (void) const;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700123
124 /**
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700125 * These are face states and may be distinct from actual lower-layer
126 * device states, such as found in real implementations (where the
127 * device may be down but ccnx face state is still up).
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700128 */
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700129
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700130 /**
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800131 * \brief Enable or disable this face
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700132 */
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800133 virtual void
134 SetUp (bool up = true);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700135
136 /**
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700137 * \brief Returns true if this face is enabled, false otherwise.
138 */
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800139 virtual bool
140 IsUp () const;
Ilya Moiseenkoacac1ea2011-10-28 13:16:53 -0700141
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700142 virtual std::ostream&
143 Print (std::ostream &os) const;
144
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700145 /**
146 * \brief Set node Id
147 *
148 * Id is purely informative and should not be used for any other purpose
149 *
150 * \param id id to set
151 */
152 inline void
153 SetId (uint32_t id);
154
155 /**
156 * \brief Get node Id
157 *
158 * Id is purely informative and should not be used for any other purpose
159 *
160 * \returns id id to set
161 */
162 inline uint32_t
163 GetId () const;
164
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700165 /**
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800166 * @brief Set maximum value for Interest allowance
167 *
168 * @param bucket maximum value for Interest allowance. If < 0, then limit will be disabled
169 */
170 inline void
171 SetBucketMax (double bucket);
172
173 /**
174 * @brief Leak the Interest allowance bucket by (1/interval) * m_bucketMax amount
175 *
176 * @param interval Time interval with which the bucket is leaked
177 */
178 inline void
179 LeakBucket (const Time &interval);
180
181 /**
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700182 * \brief Compare two faces. Only two faces on the same node could be compared.
183 *
184 * Internal index is used for comparison.
185 */
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700186 bool
187 operator== (const CcnxFace &face) const;
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700188
189 /**
190 * \brief Compare two faces. Only two faces on the same node could be compared.
191 *
192 * Internal index is used for comparison.
193 */
194 bool
195 operator< (const CcnxFace &face) const;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700196
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800197protected:
198 /**
199 * \brief Send packet on a face (actual implementation)
200 *
201 * \param p smart pointer to a packet to send
202 */
203 virtual void
204 SendImpl (Ptr<Packet> p) = 0;
205
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700206private:
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700207 CcnxFace (const CcnxFace &); ///< \brief Disabled copy constructor
208 CcnxFace& operator= (const CcnxFace &); ///< \brief Disabled copy operator
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700209
210protected:
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700211 // uint16_t m_metric; ///< \brief Routing/forwarding metric
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700212 Ptr<Node> m_node; ///< \brief Smart pointer to Node
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800213
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800214 double m_bucket; ///< \brief Value representing current size of the Interest allowance for this face
215 double m_bucketMax; ///< \brief Maximum Interest allowance for this face
216 double m_bucketLeak; ///< \brief Normalized amount that should be leaked every second
217
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700218private:
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800219 ProtocolHandler m_protocolHandler; ///< Callback via which packets are getting send to CCNx stack
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700220 bool m_ifup; ///< \brief flag indicating that the interface is UP
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800221 uint32_t m_id; ///< \brief id of the interface in CCNx stack (per-node uniqueness)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700222};
223
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700224std::ostream& operator<< (std::ostream& os, const CcnxFace &face);
225
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700226inline bool
227operator < (const Ptr<CcnxFace> &lhs, const Ptr<CcnxFace> &rhs)
228{
229 return *lhs < *rhs;
230}
231
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700232void
233CcnxFace::SetId (uint32_t id)
234{
235 m_id = id;
236}
237
238uint32_t
239CcnxFace::GetId () const
240{
241 return m_id;
242}
Alexander Afanasyev98256102011-08-14 01:00:02 -0700243
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800244void
245CcnxFace::SetBucketMax (double bucket)
246{
247 m_bucketMax = bucket;
248}
249
250void
251CcnxFace::LeakBucket (const Time &interval)
252{
253 const double leak = m_bucketLeak * 1.0 / interval.ToDouble (Time::S);
254 m_bucket -= std::max (0, m_bucket-leak);
255}
256
257
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700258} // namespace ns3
259
Alexander Afanasyev98256102011-08-14 01:00:02 -0700260#endif //CCNX_FACE_H