blob: 7f4ef0ee1ec1f47048cf20199170f7c02467a852 [file] [log] [blame]
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -07001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2011 University of California, Los Angeles
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 *
18 * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
19 */
20
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070021#ifndef _NDN_PIT_ENTRY_H_
22#define _NDN_PIT_ENTRY_H_
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070023
24#include "ns3/ptr.h"
Alexander Afanasyev11f7bb42012-07-09 17:06:30 -070025#include "ns3/simple-ref-count.h"
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070026
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070027#include "ns3/ndn-fib.h"
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070028
29#include "ns3/ndn-pit-entry-incoming-face.h"
30#include "ns3/ndn-pit-entry-outgoing-face.h"
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070031
32#include <boost/multi_index_container.hpp>
33#include <boost/multi_index/tag.hpp>
34#include <boost/multi_index/ordered_index.hpp>
Alexander Afanasyev9a989702012-06-29 17:44:00 -070035// #include <boost/multi_index/composite_key.hpp>
36// #include <boost/multi_index/hashed_index.hpp>
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070037#include <boost/multi_index/member.hpp>
Alexander Afanasyev9a989702012-06-29 17:44:00 -070038// #include <boost/multi_index/mem_fun.hpp>
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080039#include <set>
Alexander Afanasyeved449cc2012-08-21 11:10:33 -070040#include <boost/shared_ptr.hpp>
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070041
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070042namespace ns3 {
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070043namespace ndn {
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070044
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070045class Pit;
46
Alexander Afanasyeved449cc2012-08-21 11:10:33 -070047namespace fw { class Tag; }
48
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070049namespace pit {
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070050
Alexander Afanasyev6315ef72012-06-01 20:56:31 -070051/// @cond include_hidden
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070052class i_face {};
Alexander Afanasyev0a61c342011-12-06 12:48:55 -080053class i_retx {};
Alexander Afanasyev6315ef72012-06-01 20:56:31 -070054/// @endcond
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070055
56/**
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070057 * \ingroup ndn
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070058 * \brief Typedef for indexed face container of PitEntryOutgoingFace
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070059 *
60 * Indexes:
61 * - by face (may be it will be possible to replace with just the std::map)
62 */
Alexander Afanasyevc202fd92012-09-03 21:46:00 -070063// struct OutgoingFaceContainer
64// {
65// /// @cond include_hidden
66// typedef boost::multi_index::multi_index_container<
67// OutgoingFace,
68// boost::multi_index::indexed_by<
69// // For fast access to elements using NdnFace
70// boost::multi_index::ordered_unique<
71// boost::multi_index::tag<i_face>,
72// boost::multi_index::member<OutgoingFace, Ptr<Face>, &OutgoingFace::m_face>
73// >
74// // ,
75// // boost::multi_index::ordered_non_unique<
76// // boost::multi_index::tag<i_retx>,
77// // boost::multi_index::member<OutgoingFace, uint32_t, &OutgoingFace::m_retxCount>
78// // >
79// >
80// > type;
81// /// @endcond
82// };
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070083
84
85/**
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070086 * \ingroup ndn
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070087 * \brief structure for PIT entry
Alexander Afanasyev36b45772012-07-10 16:57:42 -070088 *
89 * All set-methods are virtual, in case index rearrangement is necessary in the derived classes
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070090 */
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070091class Entry : public SimpleRefCount<Entry>
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070092{
93public:
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070094 typedef std::set< IncomingFace > in_container; ///< @brief incoming faces container type
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -070095 typedef in_container::iterator in_iterator; ///< @brief iterator to incoming faces
96
Alexander Afanasyevc202fd92012-09-03 21:46:00 -070097 // typedef OutgoingFaceContainer::type out_container; ///< @brief outgoing faces container type
98 typedef std::set< OutgoingFace > out_container; ///< @brief outgoing faces container type
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -070099 typedef out_container::iterator out_iterator; ///< @brief iterator to outgoing faces
100
101 typedef std::set< uint32_t > nonce_container; ///< @brief nonce container type
102
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700103 /**
104 * \brief PIT entry constructor
105 * \param prefix Prefix of the PIT entry
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800106 * \param offsetTime Relative time to the current moment, representing PIT entry lifetime
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700107 * \param fibEntry A FIB entry associated with the PIT entry
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700108 */
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700109 Entry (Pit &container, Ptr<const InterestHeader> header, Ptr<fib::Entry> fibEntry);
Alexander Afanasyev36b45772012-07-10 16:57:42 -0700110
111 /**
112 * @brief Virtual destructor
113 */
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700114 virtual ~Entry ();
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700115
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800116 /**
117 * @brief Update lifetime of PIT entry
118 *
119 * This function will update PIT entry lifetime to the maximum of the current lifetime and
120 * the lifetime Simulator::Now () + offsetTime
121 *
Alexander Afanasyev08b7d9e2012-08-23 10:53:46 -0700122 * @param lifetime Relative time to the current moment, representing PIT entry lifetime
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800123 */
Alexander Afanasyev36b45772012-07-10 16:57:42 -0700124 virtual void
Alexander Afanasyev08b7d9e2012-08-23 10:53:46 -0700125 UpdateLifetime (const Time &lifetime);
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700126
127 /**
Alexander Afanasyev08b7d9e2012-08-23 10:53:46 -0700128 * @brief Offset the currently set PIT lifetime (allowed both negative and positive offsets)
129 * @param offsetTime positive or negative offset for the PIT lifetime.
130 *
131 * If PIT expire time becomes less than Simulator::Now, then it is adjusted to Simulator::Now.
132 */
133 virtual void
134 OffsetLifetime (const Time &offsetTime);
135
136 /**
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700137 * @brief Get prefix of the PIT entry
138 */
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700139 const NameComponents &
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700140 GetPrefix () const;
141
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800142 /**
143 * @brief Get current expiration time of the record
144 *
145 * @returns current expiration time of the record
146 */
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700147 const Time &
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700148 GetExpireTime () const;
149
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800150 /**
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800151 * @brief Check if nonce `nonce` for the same prefix has already been seen
152 *
153 * @param nonce Nonce to check
154 */
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800155 bool
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700156 IsNonceSeen (uint32_t nonce) const;
157
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800158 /**
159 * @brief Add `nonce` to the list of seen nonces
160 *
161 * @param nonce nonce to add to the list of seen nonces
162 *
163 * All nonces are stored for the lifetime of the PIT entry
164 */
Alexander Afanasyev36b45772012-07-10 16:57:42 -0700165 virtual void
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700166 AddSeenNonce (uint32_t nonce);
167
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800168 /**
169 * @brief Add `face` to the list of incoming faces
170 *
171 * @param face Face to add to the list of incoming faces
172 * @returns iterator to the added entry
173 */
Alexander Afanasyev36b45772012-07-10 16:57:42 -0700174 virtual in_iterator
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700175 AddIncoming (Ptr<Face> face);
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800176
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800177 /**
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800178 * @brief Remove incoming entry for face `face`
179 */
Alexander Afanasyev36b45772012-07-10 16:57:42 -0700180 virtual void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700181 RemoveIncoming (Ptr<Face> face);
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800182
183 /**
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800184 * @brief Clear all incoming faces either after all of them were satisfied or NACKed
185 */
Alexander Afanasyev36b45772012-07-10 16:57:42 -0700186 virtual void
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700187 ClearIncoming ();
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800188
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800189 /**
190 * @brief Add `face` to the list of outgoing faces
191 *
192 * @param face Face to add to the list of outgoing faces
193 * @returns iterator to the added entry
194 */
Alexander Afanasyev36b45772012-07-10 16:57:42 -0700195 virtual out_iterator
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700196 AddOutgoing (Ptr<Face> face);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800197
198 /**
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800199 * @brief Clear all incoming faces either after all of them were satisfied or NACKed
200 */
Alexander Afanasyev36b45772012-07-10 16:57:42 -0700201 virtual void
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700202 ClearOutgoing ();
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800203
204 /**
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800205 * @brief Remove all references to face.
206 *
207 * This method should be called before face is completely removed from the stack.
208 * Face is removed from the lists of incoming and outgoing faces
209 */
Alexander Afanasyev36b45772012-07-10 16:57:42 -0700210 virtual void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700211 RemoveAllReferencesToFace (Ptr<Face> face);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800212
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800213 /**
214 * @brief Flag outgoing face as hopeless
215 */
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700216 // virtual void
217 // SetWaitingInVain (out_iterator face);
Alexander Afanasyev36b45772012-07-10 16:57:42 -0700218 virtual void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700219 SetWaitingInVain (Ptr<Face> face);
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800220
221 /**
222 * @brief Check if all outgoing faces are NACKed
223 */
224 bool
225 AreAllOutgoingInVain () const;
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800226
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700227 /**
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800228 * @brief Similar to AreAllOutgoingInVain, but ignores `face`
229 * \see AreAllOutgoingInVain
230 **/
231 bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700232 AreTherePromisingOutgoingFacesExcept (Ptr<Face> face) const;
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800233
234 /**
235 * @brief Increase maximum limit of allowed retransmission per outgoing face
236 */
Alexander Afanasyev36b45772012-07-10 16:57:42 -0700237 virtual void
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800238 IncreaseAllowedRetxCount ();
Alexander Afanasyev36b45772012-07-10 16:57:42 -0700239
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700240 /**
241 * @brief Get maximum allowed number of retransmissions via outgoing faces
242 */
Alexander Afanasyev36b45772012-07-10 16:57:42 -0700243 uint32_t
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700244 GetMaxRetxCount () const;
245
246 /**
247 * @brief Get associated FIB entry
248 */
249 Ptr<fib::Entry>
250 GetFibEntry ();
251
252 /**
253 * @brief Get associated list (const reference) of incoming faces
254 */
255 const in_container &
256 GetIncoming () const;
257
258 /**
259 * @brief Get associated list (const reference) of outgoing faces
260 */
261 const out_container &
262 GetOutgoing () const;
Alexander Afanasyev36b45772012-07-10 16:57:42 -0700263
Alexander Afanasyeved449cc2012-08-21 11:10:33 -0700264 /**
265 * @brief Add new forwarding strategy tag
266 */
267 inline void
268 AddFwTag (boost::shared_ptr< fw::Tag > tag);
269
270 /**
271 * @brief Get forwarding strategy tag (tag is not removed)
272 */
273 template<class T>
274 inline boost::shared_ptr< T >
275 GetFwTag ();
276
277 /**
278 * @brief Remove the forwarding strategy tag
279 */
280 template<class T>
281 inline void
282 RemoveFwTag ();
283
Alexander Afanasyev91e11282012-08-21 17:23:11 -0700284 /**
285 * @brief Get InterestHeader (if several interests are received, then nonce is from the first Interest)
286 */
287 Ptr<const InterestHeader>
288 GetInterest () const;
289
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700290private:
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700291 friend std::ostream& operator<< (std::ostream& os, const Entry &entry);
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700292
Alexander Afanasyev36b45772012-07-10 16:57:42 -0700293protected:
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700294 Pit &m_container; ///< @brief Reference to the container (to rearrange indexes, if necessary)
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700295
296 Ptr<const InterestHeader> m_interest; ///< \brief Interest of the PIT entry (if several interests are received, then nonce is from the first Interest)
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700297 Ptr<fib::Entry> m_fibEntry; ///< \brief FIB entry related to this prefix
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700298
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -0700299 nonce_container m_seenNonces; ///< \brief map of nonces that were seen for this prefix
300 in_container m_incoming; ///< \brief container for incoming interests
301 out_container m_outgoing; ///< \brief container for outgoing interests
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700302
303 Time m_expireTime; ///< \brief Time when PIT entry will be removed
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800304
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700305 Time m_lastRetransmission; ///< @brief Last time when number of retransmissions were increased
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800306 uint32_t m_maxRetxCount; ///< @brief Maximum allowed number of retransmissions via outgoing faces
Alexander Afanasyeved449cc2012-08-21 11:10:33 -0700307
308 std::list< boost::shared_ptr<fw::Tag> > m_fwTags; ///< @brief Forwarding strategy tags
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700309};
310
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700311std::ostream& operator<< (std::ostream& os, const Entry &entry);
Alexander Afanasyeva95b7392012-03-09 10:54:10 -0800312
Alexander Afanasyeved449cc2012-08-21 11:10:33 -0700313inline void
314Entry::AddFwTag (boost::shared_ptr< fw::Tag > tag)
315{
316 m_fwTags.push_back (tag);
317}
318
319/**
320 * @brief Get and remove forwarding strategy tag
321 */
322template<class T>
323inline boost::shared_ptr< T >
324Entry::GetFwTag ()
325{
326 for (std::list< boost::shared_ptr<fw::Tag> >::iterator item = m_fwTags.begin ();
327 item != m_fwTags.end ();
328 item ++)
329 {
330 boost::shared_ptr< T > retPtr = boost::dynamic_pointer_cast<T> (*item);
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -0700331 if (retPtr != boost::shared_ptr< T > ())
Alexander Afanasyeved449cc2012-08-21 11:10:33 -0700332 {
333 return retPtr;
334 }
335 }
336
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -0700337 return boost::shared_ptr< T > ();
Alexander Afanasyeved449cc2012-08-21 11:10:33 -0700338}
339
340// /**
341// * @brief Peek the forwarding strategy tag
342// */
343// template<class T>
344// inline boost::shared_ptr< const T >
345// Entry::PeekFwTag () const
346// {
347// for (std::list< boost::shared_ptr<fw::Tag> >::const_iterator item = m_fwTags.begin ();
348// item != m_fwTags.end ();
349// item ++)
350// {
351// boost::shared_ptr< const T > retPtr = boost::dynamic_pointer_cast<const T> (*item);
352// if (retPtr != 0)
353// {
354// return retPtr;
355// }
356// }
357
358// return 0;
359// }
360
361template<class T>
362inline void
363Entry::RemoveFwTag ()
364{
365 for (std::list< boost::shared_ptr<fw::Tag> >::iterator item = m_fwTags.begin ();
366 item != m_fwTags.end ();
367 item ++)
368 {
369 boost::shared_ptr< T > retPtr = boost::dynamic_pointer_cast< T > (*item);
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -0700370 if (retPtr != boost::shared_ptr< T > ())
Alexander Afanasyeved449cc2012-08-21 11:10:33 -0700371 {
372 m_fwTags.erase (item);
373 return;
374 }
375 }
376}
377
378
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700379} // namespace pit
380} // namespace ndn
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700381} // namespace ns3
382
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700383#endif // _NDN_PIT_ENTRY_H_