blob: 83ece12607764e7f2dd28fee4c585a4d73e31422 [file] [log] [blame]
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -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: Ilya Moiseenko <iliamo@cs.ucla.edu>
Alexander Afanasyevb3e4b852011-12-23 15:58:20 -080019 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070020 */
21
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070022#ifndef NDN_CONSUMER_H
23#define NDN_CONSUMER_H
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070024
Alexander Afanasyev0c395372014-12-20 15:54:02 -080025#include "ndn-app.hpp"
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080026#include "ns3/random-variable.h"
Alexander Afanasyev0c395372014-12-20 15:54:02 -080027#include "ns3/ndn-name.hpp"
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"
Alexander Afanasyev0c395372014-12-20 15:54:02 -080030#include "ns3/ndn-rtt-estimator.hpp"
Alexander Afanasyev781ea812011-12-15 22:42:09 -080031
32#include <set>
Alexander Afanasyev400aae12013-01-19 13:27:52 -080033#include <map>
Alexander Afanasyev781ea812011-12-15 22:42:09 -080034
35#include <boost/multi_index_container.hpp>
36#include <boost/multi_index/tag.hpp>
37#include <boost/multi_index/ordered_index.hpp>
38#include <boost/multi_index/member.hpp>
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070039
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070040namespace ns3 {
41namespace ndn {
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070042
Ilya Moiseenko956d0542012-01-02 15:26:40 -080043/**
Alexander Afanasyev79206512013-07-27 16:49:12 -070044 * @ingroup ndn-apps
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070045 * \brief NDN application for sending out Interest packets
Ilya Moiseenko956d0542012-01-02 15:26:40 -080046 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080047class Consumer : public App {
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -080048public:
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080049 static TypeId
50 GetTypeId();
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -080051
Ilya Moiseenko956d0542012-01-02 15:26:40 -080052 /**
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -080053 * \brief Default constructor
Ilya Moiseenko956d0542012-01-02 15:26:40 -080054 * Sets up randomizer function and packet sequence number
55 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080056 Consumer();
57 virtual ~Consumer(){};
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070058
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070059 // From App
Alexander Afanasyev781ea812011-12-15 22:42:09 -080060 // virtual void
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070061 // OnInterest (const Ptr<const Interest> &interest);
Alexander Afanasyev781ea812011-12-15 22:42:09 -080062
63 virtual void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080064 OnNack(Ptr<const Interest> interest);
Alexander Afanasyev781ea812011-12-15 22:42:09 -080065
66 virtual void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080067 OnData(Ptr<const Data> contentObject);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080068
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070069 /**
70 * @brief Timeout event
71 * @param sequenceNumber time outed sequence number
72 */
Alexander Afanasyev359bfb72012-01-09 18:42:50 -080073 virtual void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080074 OnTimeout(uint32_t sequenceNumber);
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080075
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070076 /**
77 * @brief Actually send packet
78 */
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080079 void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080080 SendPacket();
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -080081
Alexander Afanasyev79b2fb32013-04-12 11:24:55 -070082 /**
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080083 * @brief An event that is fired just before an Interest packet is actually send out (send is
84 *inevitable)
Alexander Afanasyev79b2fb32013-04-12 11:24:55 -070085 *
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080086 * The reason for "before" even is that in certain cases (when it is possible to satisfy from the
87 *local cache),
88 * the send call will immediately return data, and if "after" even was used, this after would be
89 *called after
Alexander Afanasyev79b2fb32013-04-12 11:24:55 -070090 * all processing of incoming data, potentially producing unexpected results.
91 */
92 virtual void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080093 WillSendOutInterest(uint32_t sequenceNumber);
94
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070095protected:
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070096 // from App
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080097 virtual void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080098 StartApplication();
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080099
100 virtual void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800101 StopApplication();
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800102
Ilya Moiseenko956d0542012-01-02 15:26:40 -0800103 /**
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800104 * \brief Constructs the Interest packet and sends it using a callback to the underlying NDN
105 * protocol
Ilya Moiseenko956d0542012-01-02 15:26:40 -0800106 */
Alexander Afanasyev029d38d2012-01-09 13:50:50 -0800107 virtual void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800108 ScheduleNextPacket() = 0;
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800109
Ilya Moiseenko956d0542012-01-02 15:26:40 -0800110 /**
111 * \brief Checks if the packet need to be retransmitted becuase of retransmission timer expiration
112 */
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800113 void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800114 CheckRetxTimeout();
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800115
Ilya Moiseenko956d0542012-01-02 15:26:40 -0800116 /**
117 * \brief Modifies the frequency of checking the retransmission timeouts
118 * \param retxTimer Timeout defining how frequent retransmission timeouts should be checked
119 */
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800120 void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800121 SetRetxTimer(Time retxTimer);
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800122
Ilya Moiseenko956d0542012-01-02 15:26:40 -0800123 /**
124 * \brief Returns the frequency of checking the retransmission timeouts
125 * \return Timeout defining how frequent retransmission timeouts should be checked
126 */
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800127 Time
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800128 GetRetxTimer() const;
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800129
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800130protected:
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700131 UniformVariable m_rand; ///< @brief nonce generator
Alexander Afanasyev029d38d2012-01-09 13:50:50 -0800132
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800133 uint32_t m_seq; ///< @brief currently requested sequence number
134 uint32_t m_seqMax; ///< @brief maximum number of sequence number
135 EventId m_sendEvent; ///< @brief EventId of pending "send packet" event
136 Time m_retxTimer; ///< @brief Currently estimated retransmission timer
137 EventId m_retxEvent; ///< @brief Event to check whether or not retransmission should be performed
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800138
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700139 Ptr<RttEstimator> m_rtt; ///< @brief RTT estimator
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800140
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800141 Time m_offTime; ///< \brief Time interval between packets
142 Name m_interestName; ///< \brief NDN Name of the Interest (use Name)
143 Time m_interestLifeTime; ///< \brief LifeTime for interest packet
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800144
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800145 /// @cond include_hidden
Ilya Moiseenko956d0542012-01-02 15:26:40 -0800146 /**
147 * \struct This struct contains sequence numbers of packets to be retransmitted
148 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800149 struct RetxSeqsContainer : public std::set<uint32_t> {
150 };
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800151
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800152 RetxSeqsContainer m_retxSeqs; ///< \brief ordered set of sequence numbers to be retransmitted
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800153
Ilya Moiseenko956d0542012-01-02 15:26:40 -0800154 /**
155 * \struct This struct contains a pair of packet sequence number and its timeout
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800156 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800157 struct SeqTimeout {
158 SeqTimeout(uint32_t _seq, Time _time)
159 : seq(_seq)
160 , time(_time)
161 {
162 }
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800163
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800164 uint32_t seq;
165 Time time;
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800166 };
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800167 /// @endcond
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800168
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800169 /// @cond include_hidden
170 class i_seq {
171 };
172 class i_timestamp {
173 };
174 /// @endcond
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800175
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800176 /// @cond include_hidden
Ilya Moiseenko956d0542012-01-02 15:26:40 -0800177 /**
178 * \struct This struct contains a multi-index for the set of SeqTimeout structs
179 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800180 struct SeqTimeoutsContainer
181 : public boost::multi_index::
182 multi_index_container<SeqTimeout,
183 boost::multi_index::
184 indexed_by<boost::multi_index::
185 ordered_unique<boost::multi_index::tag<i_seq>,
186 boost::multi_index::
187 member<SeqTimeout, uint32_t,
188 &SeqTimeout::seq>>,
189 boost::multi_index::
190 ordered_non_unique<boost::multi_index::
191 tag<i_timestamp>,
192 boost::multi_index::
193 member<SeqTimeout, Time,
194 &SeqTimeout::time>>>> {
195 };
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800196
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800197 SeqTimeoutsContainer m_seqTimeouts; ///< \brief multi-index for the set of SeqTimeout structs
Alexander Afanasyev400aae12013-01-19 13:27:52 -0800198
199 SeqTimeoutsContainer m_seqLastDelay;
200 SeqTimeoutsContainer m_seqFullDelay;
201 std::map<uint32_t, uint32_t> m_seqRetxCounts;
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800202
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800203 TracedCallback<Ptr<App> /* app */, uint32_t /* seqno */, Time /* delay */, int32_t /*hop count*/>
204 m_lastRetransmittedInterestDataDelay;
205 TracedCallback<Ptr<App> /* app */, uint32_t /* seqno */, Time /* delay */,
206 uint32_t /*retx count*/, int32_t /*hop count*/> m_firstInterestDataDelay;
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800207
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800208 /// @endcond
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700209};
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700210
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700211} // namespace ndn
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700212} // namespace ns3
213
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700214#endif