blob: 132ef3636eddeffc8a300ba3a83c9c9e248fc4e6 [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);
178
179 /**
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700180 * \brief Compare two faces. Only two faces on the same node could be compared.
181 *
182 * Internal index is used for comparison.
183 */
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700184 bool
185 operator== (const CcnxFace &face) const;
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700186
187 /**
188 * \brief Compare two faces. Only two faces on the same node could be compared.
189 *
190 * Internal index is used for comparison.
191 */
192 bool
193 operator< (const CcnxFace &face) const;
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700194
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800195protected:
196 /**
197 * \brief Send packet on a face (actual implementation)
198 *
199 * \param p smart pointer to a packet to send
200 */
201 virtual void
202 SendImpl (Ptr<Packet> p) = 0;
203
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700204private:
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700205 CcnxFace (const CcnxFace &); ///< \brief Disabled copy constructor
206 CcnxFace& operator= (const CcnxFace &); ///< \brief Disabled copy operator
Alexander Afanasyevab1d5602011-08-17 19:17:18 -0700207
208protected:
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700209 // uint16_t m_metric; ///< \brief Routing/forwarding metric
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700210 Ptr<Node> m_node; ///< \brief Smart pointer to Node
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800211
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800212 double m_bucket; ///< \brief Value representing current size of the Interest allowance for this face
213 double m_bucketMax; ///< \brief Maximum Interest allowance for this face
214 double m_bucketLeak; ///< \brief Normalized amount that should be leaked every second
215
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700216private:
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800217 ProtocolHandler m_protocolHandler; ///< Callback via which packets are getting send to CCNx stack
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700218 bool m_ifup; ///< \brief flag indicating that the interface is UP
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800219 uint32_t m_id; ///< \brief id of the interface in CCNx stack (per-node uniqueness)
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700220};
221
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700222std::ostream& operator<< (std::ostream& os, const CcnxFace &face);
223
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -0700224inline bool
225operator < (const Ptr<CcnxFace> &lhs, const Ptr<CcnxFace> &rhs)
226{
227 return *lhs < *rhs;
228}
229
Alexander Afanasyev56f79ea2011-08-17 23:54:27 -0700230void
231CcnxFace::SetId (uint32_t id)
232{
233 m_id = id;
234}
235
236uint32_t
237CcnxFace::GetId () const
238{
239 return m_id;
240}
Alexander Afanasyev98256102011-08-14 01:00:02 -0700241
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800242void
243CcnxFace::SetBucketMax (double bucket)
244{
245 m_bucketMax = bucket;
246}
247
248void
249CcnxFace::LeakBucket (const Time &interval)
250{
251 const double leak = m_bucketLeak * 1.0 / interval.ToDouble (Time::S);
Alexander Afanasyev19426ef2011-11-23 20:55:28 -0800252 m_bucket -= std::max (0.0, m_bucket-leak);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800253}
254
255
Alexander Afanasyev08d984e2011-08-13 19:20:22 -0700256} // namespace ns3
257
Alexander Afanasyev98256102011-08-14 01:00:02 -0700258#endif //CCNX_FACE_H