blob: 665e20fe76f468d8bc7d2fe50acb3c4f3402c565 [file] [log] [blame]
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2011-2015 Regents of the University of California.
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -07004 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08005 * This file is part of ndnSIM. See AUTHORS for complete list of ndnSIM authors and
6 * contributors.
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -07007 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -08008 * ndnSIM is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070011 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -080012 * ndnSIM is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070015 *
Alexander Afanasyev60a7b622014-12-20 17:04:07 -080016 * You should have received a copy of the GNU General Public License along with
17 * ndnSIM, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 **/
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070019
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070020#ifndef NDN_CONSUMER_H
21#define NDN_CONSUMER_H
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070022
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -070023#include "ns3/ndnSIM/model/ndn-common.hpp"
24
Alexander Afanasyev0c395372014-12-20 15:54:02 -080025#include "ndn-app.hpp"
Mickey Sweatt89046c12014-11-16 20:32:27 -080026
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080027#include "ns3/random-variable.h"
Alexander Afanasyev781ea812011-12-15 22:42:09 -080028#include "ns3/nstime.h"
Alexander Afanasyev011b8592011-12-21 14:45:27 -080029#include "ns3/data-rate.h"
Mickey Sweatt89046c12014-11-16 20:32:27 -080030
31#include "ns3/ndnSIM/model/ndn-common.hpp"
32#include "ns3/ndnSIM/utils/ndn-rtt-estimator.hpp"
33#include "ns3/ndnSIM/utils/ndn-fw-hop-count-tag.hpp"
Alexander Afanasyev781ea812011-12-15 22:42:09 -080034
35#include <set>
Alexander Afanasyev400aae12013-01-19 13:27:52 -080036#include <map>
Alexander Afanasyev781ea812011-12-15 22:42:09 -080037
38#include <boost/multi_index_container.hpp>
39#include <boost/multi_index/tag.hpp>
40#include <boost/multi_index/ordered_index.hpp>
41#include <boost/multi_index/member.hpp>
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070042
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070043namespace ns3 {
44namespace ndn {
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070045
Ilya Moiseenko956d0542012-01-02 15:26:40 -080046/**
Alexander Afanasyev79206512013-07-27 16:49:12 -070047 * @ingroup ndn-apps
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070048 * \brief NDN application for sending out Interest packets
Ilya Moiseenko956d0542012-01-02 15:26:40 -080049 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080050class Consumer : public App {
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -080051public:
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080052 static TypeId
53 GetTypeId();
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -080054
Ilya Moiseenko956d0542012-01-02 15:26:40 -080055 /**
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -080056 * \brief Default constructor
Ilya Moiseenko956d0542012-01-02 15:26:40 -080057 * Sets up randomizer function and packet sequence number
58 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080059 Consumer();
60 virtual ~Consumer(){};
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070061
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070062 // From App
Alexander Afanasyev781ea812011-12-15 22:42:09 -080063 virtual void
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -070064 OnData(shared_ptr<const Data> contentObject);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080065
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070066 /**
67 * @brief Timeout event
68 * @param sequenceNumber time outed sequence number
69 */
Alexander Afanasyev359bfb72012-01-09 18:42:50 -080070 virtual void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080071 OnTimeout(uint32_t sequenceNumber);
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080072
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070073 /**
74 * @brief Actually send packet
75 */
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080076 void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080077 SendPacket();
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -080078
Alexander Afanasyev79b2fb32013-04-12 11:24:55 -070079 /**
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080080 * @brief An event that is fired just before an Interest packet is actually send out (send is
81 *inevitable)
Alexander Afanasyev79b2fb32013-04-12 11:24:55 -070082 *
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080083 * The reason for "before" even is that in certain cases (when it is possible to satisfy from the
84 *local cache),
85 * the send call will immediately return data, and if "after" even was used, this after would be
86 *called after
Alexander Afanasyev79b2fb32013-04-12 11:24:55 -070087 * all processing of incoming data, potentially producing unexpected results.
88 */
89 virtual void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080090 WillSendOutInterest(uint32_t sequenceNumber);
91
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070092protected:
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070093 // from App
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080094 virtual void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080095 StartApplication();
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080096
97 virtual void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080098 StopApplication();
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -080099
Ilya Moiseenko956d0542012-01-02 15:26:40 -0800100 /**
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800101 * \brief Constructs the Interest packet and sends it using a callback to the underlying NDN
102 * protocol
Ilya Moiseenko956d0542012-01-02 15:26:40 -0800103 */
Alexander Afanasyev029d38d2012-01-09 13:50:50 -0800104 virtual void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800105 ScheduleNextPacket() = 0;
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800106
Ilya Moiseenko956d0542012-01-02 15:26:40 -0800107 /**
108 * \brief Checks if the packet need to be retransmitted becuase of retransmission timer expiration
109 */
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800110 void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800111 CheckRetxTimeout();
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800112
Ilya Moiseenko956d0542012-01-02 15:26:40 -0800113 /**
114 * \brief Modifies the frequency of checking the retransmission timeouts
115 * \param retxTimer Timeout defining how frequent retransmission timeouts should be checked
116 */
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800117 void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800118 SetRetxTimer(Time retxTimer);
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800119
Ilya Moiseenko956d0542012-01-02 15:26:40 -0800120 /**
121 * \brief Returns the frequency of checking the retransmission timeouts
122 * \return Timeout defining how frequent retransmission timeouts should be checked
123 */
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800124 Time
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800125 GetRetxTimer() const;
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800126
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800127protected:
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700128 UniformVariable m_rand; ///< @brief nonce generator
Alexander Afanasyev029d38d2012-01-09 13:50:50 -0800129
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800130 uint32_t m_seq; ///< @brief currently requested sequence number
131 uint32_t m_seqMax; ///< @brief maximum number of sequence number
132 EventId m_sendEvent; ///< @brief EventId of pending "send packet" event
133 Time m_retxTimer; ///< @brief Currently estimated retransmission timer
134 EventId m_retxEvent; ///< @brief Event to check whether or not retransmission should be performed
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800135
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700136 Ptr<RttEstimator> m_rtt; ///< @brief RTT estimator
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800137
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800138 Time m_offTime; ///< \brief Time interval between packets
139 Name m_interestName; ///< \brief NDN Name of the Interest (use Name)
140 Time m_interestLifeTime; ///< \brief LifeTime for interest packet
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800141
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800142 /// @cond include_hidden
Ilya Moiseenko956d0542012-01-02 15:26:40 -0800143 /**
144 * \struct This struct contains sequence numbers of packets to be retransmitted
145 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800146 struct RetxSeqsContainer : public std::set<uint32_t> {
147 };
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800148
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800149 RetxSeqsContainer m_retxSeqs; ///< \brief ordered set of sequence numbers to be retransmitted
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800150
Ilya Moiseenko956d0542012-01-02 15:26:40 -0800151 /**
152 * \struct This struct contains a pair of packet sequence number and its timeout
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800153 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800154 struct SeqTimeout {
155 SeqTimeout(uint32_t _seq, Time _time)
156 : seq(_seq)
157 , time(_time)
158 {
159 }
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800160
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800161 uint32_t seq;
162 Time time;
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800163 };
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800164 /// @endcond
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800165
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800166 /// @cond include_hidden
167 class i_seq {
168 };
169 class i_timestamp {
170 };
171 /// @endcond
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800172
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800173 /// @cond include_hidden
Ilya Moiseenko956d0542012-01-02 15:26:40 -0800174 /**
175 * \struct This struct contains a multi-index for the set of SeqTimeout structs
176 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800177 struct SeqTimeoutsContainer
178 : public boost::multi_index::
179 multi_index_container<SeqTimeout,
180 boost::multi_index::
181 indexed_by<boost::multi_index::
182 ordered_unique<boost::multi_index::tag<i_seq>,
183 boost::multi_index::
184 member<SeqTimeout, uint32_t,
185 &SeqTimeout::seq>>,
186 boost::multi_index::
187 ordered_non_unique<boost::multi_index::
188 tag<i_timestamp>,
189 boost::multi_index::
190 member<SeqTimeout, Time,
191 &SeqTimeout::time>>>> {
192 };
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800193
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800194 SeqTimeoutsContainer m_seqTimeouts; ///< \brief multi-index for the set of SeqTimeout structs
Alexander Afanasyev400aae12013-01-19 13:27:52 -0800195
196 SeqTimeoutsContainer m_seqLastDelay;
197 SeqTimeoutsContainer m_seqFullDelay;
198 std::map<uint32_t, uint32_t> m_seqRetxCounts;
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800199
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800200 TracedCallback<Ptr<App> /* app */, uint32_t /* seqno */, Time /* delay */, int32_t /*hop count*/>
201 m_lastRetransmittedInterestDataDelay;
202 TracedCallback<Ptr<App> /* app */, uint32_t /* seqno */, Time /* delay */,
203 uint32_t /*retx count*/, int32_t /*hop count*/> m_firstInterestDataDelay;
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800204
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800205 /// @endcond
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700206};
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700207
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700208} // namespace ndn
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700209} // namespace ns3
210
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700211#endif