blob: 94b48ab644fd983dbac2a1aa3b5913297765d5d9 [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 /**
Alexander Afanasyev11453142011-11-25 16:13:33 -0800172 * @brief Set a normalized value (one second) for Interest allowance bucket leak
173 */
174 inline void
175 SetBucketLeak (double leak);
176
177 /**
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800178 * @brief Leak the Interest allowance bucket by (1/interval) * m_bucketMax amount
179 *
180 * @param interval Time interval with which the bucket is leaked
181 */
182 inline void
183 LeakBucket (const Time &interval);
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800184
185 inline void
186 LeakBucketByOnePacket ();
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800187
188 /**
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700189 * \brief Compare two faces. Only two faces on the same node could be compared.
190 *
191 * Internal index is used for comparison.
192 */
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700193 bool
194 operator== (const CcnxFace &face) const;
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700195
196 /**
197 * \brief Compare two faces. Only two faces on the same node could be compared.
198 *
199 * Internal index is used for comparison.
200 */
201 bool
202 operator< (const CcnxFace &face) const;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700203
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800204protected:
205 /**
206 * \brief Send packet on a face (actual implementation)
207 *
208 * \param p smart pointer to a packet to send
209 */
210 virtual void
211 SendImpl (Ptr<Packet> p) = 0;
212
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700213private:
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700214 CcnxFace (const CcnxFace &); ///< \brief Disabled copy constructor
215 CcnxFace& operator= (const CcnxFace &); ///< \brief Disabled copy operator
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700216
217protected:
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700218 // uint16_t m_metric; ///< \brief Routing/forwarding metric
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700219 Ptr<Node> m_node; ///< \brief Smart pointer to Node
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800220
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800221 double m_bucket; ///< \brief Value representing current size of the Interest allowance for this face
222 double m_bucketMax; ///< \brief Maximum Interest allowance for this face
223 double m_bucketLeak; ///< \brief Normalized amount that should be leaked every second
224
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700225private:
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800226 ProtocolHandler m_protocolHandler; ///< Callback via which packets are getting send to CCNx stack
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700227 bool m_ifup; ///< \brief flag indicating that the interface is UP
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800228 uint32_t m_id; ///< \brief id of the interface in CCNx stack (per-node uniqueness)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700229};
230
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700231std::ostream& operator<< (std::ostream& os, const CcnxFace &face);
232
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700233inline bool
234operator < (const Ptr<CcnxFace> &lhs, const Ptr<CcnxFace> &rhs)
235{
236 return *lhs < *rhs;
237}
238
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700239void
240CcnxFace::SetId (uint32_t id)
241{
242 m_id = id;
243}
244
245uint32_t
246CcnxFace::GetId () const
247{
248 return m_id;
249}
Alexander Afanasyev98256102011-08-14 01:00:02 -0700250
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800251void
252CcnxFace::SetBucketMax (double bucket)
253{
254 m_bucketMax = bucket;
255}
256
257void
Alexander Afanasyev11453142011-11-25 16:13:33 -0800258CcnxFace::SetBucketLeak (double leak)
259{
260 m_bucketLeak = leak;
261}
262
263void
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800264CcnxFace::LeakBucket (const Time &interval)
265{
266 const double leak = m_bucketLeak * 1.0 / interval.ToDouble (Time::S);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800267 m_bucket -= std::max (0.0, m_bucket-leak);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800268}
269
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800270void
271CcnxFace::LeakBucketByOnePacket ()
272{
273 m_bucket -= std::max (0.0, m_bucket-1.0);
274}
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800275
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700276} // namespace ns3
277
Alexander Afanasyev98256102011-08-14 01:00:02 -0700278#endif //CCNX_FACE_H