blob: 397a5b03fe9cc204a75f0031b2419b996523a8eb [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 Afanasyevc5a23e22011-09-07 00:37:36 -070050class CcnxFace : public SimpleRefCount<CcnxFace>
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070051{
52public:
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070053 /**
54 * \brief Ccnx protocol hanler
55 *
56 * \param face Face from which packet has been received
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080057 * \param packet Original packet
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070058 */
Alexander Afanasyev0ab833e2011-08-18 15:49:13 -070059 typedef Callback<void,const Ptr<CcnxFace>&,const Ptr<const Packet>& > ProtocolHandler;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070060
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070061 /**
62 * \brief Default constructor
63 */
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080064 CcnxFace (Ptr<Node> node);
Alexander Afanasyev98256102011-08-14 01:00:02 -070065 virtual ~CcnxFace();
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070066
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070067 ////////////////////////////////////////////////////////////////////
68
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070069 /**
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070070 * \brief Register callback to call when new packet arrives on the face
Alexander Afanasyev08d984e2011-08-13 19:20:22 -070071 *
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070072 * This method should call protocol-dependent registration function
73 */
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080074 virtual void
75 RegisterProtocolHandler (ProtocolHandler handler);
Alexander Afanasyevab1d5602011-08-17 19:17:18 -070076
77 /**
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080078 * @brief Check if Interest limit is reached
79 *
80 * Side effect: if limit is not yet reached, the number of outstanding packets will be increased
81 *
82 * @returns true if Interest limit is not yet reached
83 */
84 bool
85 IsBelowLimit ();
86
87 /**
88 * \brief Send packet on a face
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 *
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080092 * \param p smart pointer to a packet to send
93 *
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080094 * @return false if either limit is reached
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080095 */
96 bool
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080097 Send (Ptr<Packet> p);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080098
99 /**
100 * \brief Receive packet from application or another node and forward it to the CCNx stack
101 *
102 * \todo The only reason for this call is to handle tracing, if requested
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700103 */
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800104 bool
105 Receive (const Ptr<const Packet> &p);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800106 ////////////////////////////////////////////////////////////////////
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700107
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700108 // /**
109 // * \Brief Assign routing/forwarding metric with face
110 // *
111 // * \param metric configured routing metric (cost) of this face
112 // */
113 // virtual void SetMetric (uint16_t metric);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700114
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700115 // /**
116 // * \brief Get routing/forwarding metric assigned to the face
117 // *
118 // * \returns configured routing/forwarding metric (cost) of this face
119 // */
120 // virtual uint16_t GetMetric (void) const;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700121
122 /**
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700123 * These are face states and may be distinct from actual lower-layer
124 * device states, such as found in real implementations (where the
125 * device may be down but ccnx face state is still up).
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700126 */
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700127
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700128 /**
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800129 * \brief Enable or disable this face
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700130 */
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800131 virtual void
132 SetUp (bool up = true);
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700133
134 /**
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700135 * \brief Returns true if this face is enabled, false otherwise.
136 */
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800137 virtual bool
138 IsUp () const;
Ilya Moiseenkoacac1ea2011-10-28 13:16:53 -0700139
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700140 virtual std::ostream&
141 Print (std::ostream &os) const;
142
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700143 /**
144 * \brief Set node Id
145 *
146 * Id is purely informative and should not be used for any other purpose
147 *
148 * \param id id to set
149 */
150 inline void
151 SetId (uint32_t id);
152
153 /**
154 * \brief Get node Id
155 *
156 * Id is purely informative and should not be used for any other purpose
157 *
158 * \returns id id to set
159 */
160 inline uint32_t
161 GetId () const;
162
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700163 /**
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800164 * @brief Set maximum value for Interest allowance
165 *
166 * @param bucket maximum value for Interest allowance. If < 0, then limit will be disabled
167 */
168 inline void
169 SetBucketMax (double bucket);
170
171 /**
172 * @brief Leak the Interest allowance bucket by (1/interval) * m_bucketMax amount
173 *
174 * @param interval Time interval with which the bucket is leaked
175 */
176 inline void
177 LeakBucket (const Time &interval);
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800178
179 inline void
180 LeakBucketByOnePacket ();
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800181
182 /**
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700183 * \brief Compare two faces. Only two faces on the same node could be compared.
184 *
185 * Internal index is used for comparison.
186 */
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700187 bool
188 operator== (const CcnxFace &face) const;
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -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 */
195 bool
196 operator< (const CcnxFace &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 */
204 virtual void
205 SendImpl (Ptr<Packet> p) = 0;
206
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700207private:
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700208 CcnxFace (const CcnxFace &); ///< \brief Disabled copy constructor
209 CcnxFace& operator= (const CcnxFace &); ///< \brief Disabled copy operator
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700210
211protected:
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700212 // uint16_t m_metric; ///< \brief Routing/forwarding metric
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700213 Ptr<Node> m_node; ///< \brief Smart pointer to Node
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800214
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800215 double m_bucket; ///< \brief Value representing current size of the Interest allowance for this face
216 double m_bucketMax; ///< \brief Maximum Interest allowance for this face
217 double m_bucketLeak; ///< \brief Normalized amount that should be leaked every second
218
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700219private:
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800220 ProtocolHandler m_protocolHandler; ///< Callback via which packets are getting send to CCNx stack
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700221 bool m_ifup; ///< \brief flag indicating that the interface is UP
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800222 uint32_t m_id; ///< \brief id of the interface in CCNx stack (per-node uniqueness)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700223};
224
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700225std::ostream& operator<< (std::ostream& os, const CcnxFace &face);
226
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700227inline bool
228operator < (const Ptr<CcnxFace> &lhs, const Ptr<CcnxFace> &rhs)
229{
230 return *lhs < *rhs;
231}
232
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700233void
234CcnxFace::SetId (uint32_t id)
235{
236 m_id = id;
237}
238
239uint32_t
240CcnxFace::GetId () const
241{
242 return m_id;
243}
Alexander Afanasyev98256102011-08-14 01:00:02 -0700244
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800245void
246CcnxFace::SetBucketMax (double bucket)
247{
248 m_bucketMax = bucket;
249}
250
251void
252CcnxFace::LeakBucket (const Time &interval)
253{
254 const double leak = m_bucketLeak * 1.0 / interval.ToDouble (Time::S);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800255 m_bucket -= std::max (0.0, m_bucket-leak);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800256}
257
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800258void
259CcnxFace::LeakBucketByOnePacket ()
260{
261 m_bucket -= std::max (0.0, m_bucket-1.0);
262}
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800263
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700264} // namespace ns3
265
Alexander Afanasyev98256102011-08-14 01:00:02 -0700266#endif //CCNX_FACE_H