blob: dd90b9f255a56f38332d86918d6f85b53d709aae [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 Afanasyeva98cdd22011-08-29 17:32:37 -070051/**
Alexander Afanasyev79206512013-07-27 16:49:12 -070052 * @ingroup ndn-pit
53 * @brief structure for PIT entry
Alexander Afanasyev36b45772012-07-10 16:57:42 -070054 *
55 * All set-methods are virtual, in case index rearrangement is necessary in the derived classes
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070056 */
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070057class Entry : public SimpleRefCount<Entry>
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070058{
59public:
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070060 typedef std::set< IncomingFace > in_container; ///< @brief incoming faces container type
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -070061 typedef in_container::iterator in_iterator; ///< @brief iterator to incoming faces
62
Alexander Afanasyevc202fd92012-09-03 21:46:00 -070063 // typedef OutgoingFaceContainer::type out_container; ///< @brief outgoing faces container type
64 typedef std::set< OutgoingFace > out_container; ///< @brief outgoing faces container type
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -070065 typedef out_container::iterator out_iterator; ///< @brief iterator to outgoing faces
66
67 typedef std::set< uint32_t > nonce_container; ///< @brief nonce container type
Alexander Afanasyeveec66292013-02-06 16:23:21 -080068
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070069 /**
70 * \brief PIT entry constructor
71 * \param prefix Prefix of the PIT entry
Alexander Afanasyev0a61c342011-12-06 12:48:55 -080072 * \param offsetTime Relative time to the current moment, representing PIT entry lifetime
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070073 * \param fibEntry A FIB entry associated with the PIT entry
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070074 */
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070075 Entry (Pit &container, Ptr<const Interest> header, Ptr<fib::Entry> fibEntry);
Alexander Afanasyev36b45772012-07-10 16:57:42 -070076
77 /**
78 * @brief Virtual destructor
79 */
Alexander Afanasyev31cb4692012-08-17 13:08:20 -070080 virtual ~Entry ();
Alexander Afanasyeveec66292013-02-06 16:23:21 -080081
Alexander Afanasyev0a61c342011-12-06 12:48:55 -080082 /**
83 * @brief Update lifetime of PIT entry
84 *
Alexander Afanasyeveec66292013-02-06 16:23:21 -080085 * @param lifetime desired lifetime of the pit entry (relative to the Simulator::Now ())
Alexander Afanasyev0a61c342011-12-06 12:48:55 -080086 *
Alexander Afanasyeveec66292013-02-06 16:23:21 -080087 * This function will update PIT entry lifetime to the maximum of the current lifetime and
88 * the lifetime Simulator::Now () + lifetime
Alexander Afanasyev0a61c342011-12-06 12:48:55 -080089 */
Alexander Afanasyev36b45772012-07-10 16:57:42 -070090 virtual void
Alexander Afanasyev08b7d9e2012-08-23 10:53:46 -070091 UpdateLifetime (const Time &lifetime);
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070092
93 /**
Alexander Afanasyev08b7d9e2012-08-23 10:53:46 -070094 * @brief Offset the currently set PIT lifetime (allowed both negative and positive offsets)
Alexander Afanasyeveec66292013-02-06 16:23:21 -080095 *
Alexander Afanasyev08b7d9e2012-08-23 10:53:46 -070096 * @param offsetTime positive or negative offset for the PIT lifetime.
97 *
98 * If PIT expire time becomes less than Simulator::Now, then it is adjusted to Simulator::Now.
99 */
100 virtual void
101 OffsetLifetime (const Time &offsetTime);
Alexander Afanasyeveec66292013-02-06 16:23:21 -0800102
Alexander Afanasyev08b7d9e2012-08-23 10:53:46 -0700103 /**
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700104 * @brief Get prefix of the PIT entry
105 */
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700106 const Name &
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700107 GetPrefix () const;
Alexander Afanasyeveec66292013-02-06 16:23:21 -0800108
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800109 /**
110 * @brief Get current expiration time of the record
111 *
112 * @returns current expiration time of the record
113 */
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700114 const Time &
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700115 GetExpireTime () const;
Alexander Afanasyeveec66292013-02-06 16:23:21 -0800116
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800117 /**
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800118 * @brief Check if nonce `nonce` for the same prefix has already been seen
119 *
120 * @param nonce Nonce to check
121 */
Alexander Afanasyeva46844b2011-11-21 19:13:26 -0800122 bool
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700123 IsNonceSeen (uint32_t nonce) const;
Alexander Afanasyeveec66292013-02-06 16:23:21 -0800124
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800125 /**
126 * @brief Add `nonce` to the list of seen nonces
127 *
128 * @param nonce nonce to add to the list of seen nonces
129 *
130 * All nonces are stored for the lifetime of the PIT entry
131 */
Alexander Afanasyev36b45772012-07-10 16:57:42 -0700132 virtual void
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700133 AddSeenNonce (uint32_t nonce);
Alexander Afanasyeveec66292013-02-06 16:23:21 -0800134
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800135 /**
136 * @brief Add `face` to the list of incoming faces
137 *
138 * @param face Face to add to the list of incoming faces
139 * @returns iterator to the added entry
140 */
Alexander Afanasyev36b45772012-07-10 16:57:42 -0700141 virtual in_iterator
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700142 AddIncoming (Ptr<Face> face);
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800143
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800144 /**
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800145 * @brief Remove incoming entry for face `face`
146 */
Alexander Afanasyev36b45772012-07-10 16:57:42 -0700147 virtual void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700148 RemoveIncoming (Ptr<Face> face);
Alexander Afanasyev9d313d42011-11-25 13:36:15 -0800149
150 /**
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800151 * @brief Clear all incoming faces either after all of them were satisfied or NACKed
152 */
Alexander Afanasyev36b45772012-07-10 16:57:42 -0700153 virtual void
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700154 ClearIncoming ();
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -0800155
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800156 /**
157 * @brief Add `face` to the list of outgoing faces
158 *
159 * @param face Face to add to the list of outgoing faces
160 * @returns iterator to the added entry
161 */
Alexander Afanasyev36b45772012-07-10 16:57:42 -0700162 virtual out_iterator
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700163 AddOutgoing (Ptr<Face> face);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800164
165 /**
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800166 * @brief Clear all incoming faces either after all of them were satisfied or NACKed
167 */
Alexander Afanasyev36b45772012-07-10 16:57:42 -0700168 virtual void
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700169 ClearOutgoing ();
Alexander Afanasyeveec66292013-02-06 16:23:21 -0800170
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800171 /**
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800172 * @brief Remove all references to face.
Alexander Afanasyeveec66292013-02-06 16:23:21 -0800173 *
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800174 * This method should be called before face is completely removed from the stack.
175 * Face is removed from the lists of incoming and outgoing faces
176 */
Alexander Afanasyev36b45772012-07-10 16:57:42 -0700177 virtual void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700178 RemoveAllReferencesToFace (Ptr<Face> face);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800179
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800180 /**
181 * @brief Flag outgoing face as hopeless
182 */
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700183 // virtual void
184 // SetWaitingInVain (out_iterator face);
Alexander Afanasyev36b45772012-07-10 16:57:42 -0700185 virtual void
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700186 SetWaitingInVain (Ptr<Face> face);
Alexander Afanasyeveec66292013-02-06 16:23:21 -0800187
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800188 /**
189 * @brief Check if all outgoing faces are NACKed
190 */
191 bool
192 AreAllOutgoingInVain () const;
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800193
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700194 /**
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800195 * @brief Similar to AreAllOutgoingInVain, but ignores `face`
196 * \see AreAllOutgoingInVain
197 **/
198 bool
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700199 AreTherePromisingOutgoingFacesExcept (Ptr<Face> face) const;
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800200
201 /**
202 * @brief Increase maximum limit of allowed retransmission per outgoing face
203 */
Alexander Afanasyev36b45772012-07-10 16:57:42 -0700204 virtual void
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800205 IncreaseAllowedRetxCount ();
Alexander Afanasyev36b45772012-07-10 16:57:42 -0700206
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700207 /**
208 * @brief Get maximum allowed number of retransmissions via outgoing faces
209 */
Alexander Afanasyev36b45772012-07-10 16:57:42 -0700210 uint32_t
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700211 GetMaxRetxCount () const;
212
213 /**
214 * @brief Get associated FIB entry
215 */
216 Ptr<fib::Entry>
217 GetFibEntry ();
218
219 /**
220 * @brief Get associated list (const reference) of incoming faces
221 */
222 const in_container &
223 GetIncoming () const;
224
225 /**
226 * @brief Get associated list (const reference) of outgoing faces
227 */
228 const out_container &
229 GetOutgoing () const;
Alexander Afanasyev36b45772012-07-10 16:57:42 -0700230
Alexander Afanasyeved449cc2012-08-21 11:10:33 -0700231 /**
Alexander Afanasyevff033952012-08-23 15:45:52 -0700232 * @brief Get number of outgoing faces (needed for python bindings)
233 */
234 uint32_t
235 GetOutgoingCount () const;
Alexander Afanasyeveec66292013-02-06 16:23:21 -0800236
Alexander Afanasyevff033952012-08-23 15:45:52 -0700237 /**
Alexander Afanasyeved449cc2012-08-21 11:10:33 -0700238 * @brief Add new forwarding strategy tag
239 */
240 inline void
241 AddFwTag (boost::shared_ptr< fw::Tag > tag);
242
243 /**
244 * @brief Get forwarding strategy tag (tag is not removed)
245 */
246 template<class T>
247 inline boost::shared_ptr< T >
248 GetFwTag ();
249
250 /**
251 * @brief Remove the forwarding strategy tag
252 */
253 template<class T>
254 inline void
255 RemoveFwTag ();
256
Alexander Afanasyev91e11282012-08-21 17:23:11 -0700257 /**
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700258 * @brief Get Interest (if several interests are received, then nonce is from the first Interest)
Alexander Afanasyev91e11282012-08-21 17:23:11 -0700259 */
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700260 Ptr<const Interest>
Alexander Afanasyev91e11282012-08-21 17:23:11 -0700261 GetInterest () const;
262
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700263private:
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700264 friend std::ostream& operator<< (std::ostream& os, const Entry &entry);
Alexander Afanasyeveec66292013-02-06 16:23:21 -0800265
Alexander Afanasyev36b45772012-07-10 16:57:42 -0700266protected:
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700267 Pit &m_container; ///< @brief Reference to the container (to rearrange indexes, if necessary)
Alexander Afanasyev31cb4692012-08-17 13:08:20 -0700268
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -0700269 Ptr<const Interest> 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 -0700270 Ptr<fib::Entry> m_fibEntry; ///< \brief FIB entry related to this prefix
Alexander Afanasyeveec66292013-02-06 16:23:21 -0800271
272 nonce_container m_seenNonces; ///< \brief map of nonces that were seen for this prefix
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -0700273 in_container m_incoming; ///< \brief container for incoming interests
274 out_container m_outgoing; ///< \brief container for outgoing interests
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700275
276 Time m_expireTime; ///< \brief Time when PIT entry will be removed
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800277
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700278 Time m_lastRetransmission; ///< @brief Last time when number of retransmissions were increased
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800279 uint32_t m_maxRetxCount; ///< @brief Maximum allowed number of retransmissions via outgoing faces
Alexander Afanasyeved449cc2012-08-21 11:10:33 -0700280
281 std::list< boost::shared_ptr<fw::Tag> > m_fwTags; ///< @brief Forwarding strategy tags
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700282};
283
Alexander Afanasyev79206512013-07-27 16:49:12 -0700284/// @cond include_hidden
Alexander Afanasyeveec66292013-02-06 16:23:21 -0800285struct EntryIsNotEmpty
286{
287 bool
288 operator () (Ptr<Entry> entry)
289 {
290 return !entry->GetIncoming ().empty ();
291 }
292};
Alexander Afanasyev79206512013-07-27 16:49:12 -0700293/// @endcond
Alexander Afanasyeveec66292013-02-06 16:23:21 -0800294
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700295std::ostream& operator<< (std::ostream& os, const Entry &entry);
Alexander Afanasyeva95b7392012-03-09 10:54:10 -0800296
Alexander Afanasyeved449cc2012-08-21 11:10:33 -0700297inline void
298Entry::AddFwTag (boost::shared_ptr< fw::Tag > tag)
299{
300 m_fwTags.push_back (tag);
301}
302
303/**
304 * @brief Get and remove forwarding strategy tag
305 */
306template<class T>
307inline boost::shared_ptr< T >
308Entry::GetFwTag ()
309{
310 for (std::list< boost::shared_ptr<fw::Tag> >::iterator item = m_fwTags.begin ();
311 item != m_fwTags.end ();
312 item ++)
313 {
314 boost::shared_ptr< T > retPtr = boost::dynamic_pointer_cast<T> (*item);
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -0700315 if (retPtr != boost::shared_ptr< T > ())
Alexander Afanasyeved449cc2012-08-21 11:10:33 -0700316 {
317 return retPtr;
318 }
319 }
320
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -0700321 return boost::shared_ptr< T > ();
Alexander Afanasyeved449cc2012-08-21 11:10:33 -0700322}
323
324// /**
325// * @brief Peek the forwarding strategy tag
326// */
327// template<class T>
328// inline boost::shared_ptr< const T >
329// Entry::PeekFwTag () const
330// {
331// for (std::list< boost::shared_ptr<fw::Tag> >::const_iterator item = m_fwTags.begin ();
332// item != m_fwTags.end ();
333// item ++)
334// {
335// boost::shared_ptr< const T > retPtr = boost::dynamic_pointer_cast<const T> (*item);
336// if (retPtr != 0)
337// {
338// return retPtr;
339// }
340// }
341
342// return 0;
343// }
344
345template<class T>
346inline void
347Entry::RemoveFwTag ()
348{
349 for (std::list< boost::shared_ptr<fw::Tag> >::iterator item = m_fwTags.begin ();
350 item != m_fwTags.end ();
351 item ++)
352 {
353 boost::shared_ptr< T > retPtr = boost::dynamic_pointer_cast< T > (*item);
Alexander Afanasyev38ba9b22012-08-21 13:32:43 -0700354 if (retPtr != boost::shared_ptr< T > ())
Alexander Afanasyeved449cc2012-08-21 11:10:33 -0700355 {
356 m_fwTags.erase (item);
357 return;
358 }
359 }
360}
361
362
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700363} // namespace pit
364} // namespace ndn
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700365} // namespace ns3
366
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700367#endif // _NDN_PIT_ENTRY_H_