blob: 25de4cdf103d1f8a99164b0c339d9cd5aaacb4ff [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
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -070025#include "ns3/ndnSIM/model/ndn-common.hpp"
26
Alexander Afanasyev0c395372014-12-20 15:54:02 -080027#include "ndn-app.hpp"
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080028#include "ns3/random-variable.h"
Alexander Afanasyev781ea812011-12-15 22:42:09 -080029#include "ns3/nstime.h"
Alexander Afanasyev011b8592011-12-21 14:45:27 -080030#include "ns3/data-rate.h"
Alexander Afanasyev0c395372014-12-20 15:54:02 -080031#include "ns3/ndn-rtt-estimator.hpp"
Alexander Afanasyev781ea812011-12-15 22:42:09 -080032
33#include <set>
Alexander Afanasyev400aae12013-01-19 13:27:52 -080034#include <map>
Alexander Afanasyev781ea812011-12-15 22:42:09 -080035
36#include <boost/multi_index_container.hpp>
37#include <boost/multi_index/tag.hpp>
38#include <boost/multi_index/ordered_index.hpp>
39#include <boost/multi_index/member.hpp>
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070040
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070041namespace ns3 {
42namespace ndn {
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070043
Ilya Moiseenko956d0542012-01-02 15:26:40 -080044/**
Alexander Afanasyev79206512013-07-27 16:49:12 -070045 * @ingroup ndn-apps
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070046 * \brief NDN application for sending out Interest packets
Ilya Moiseenko956d0542012-01-02 15:26:40 -080047 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080048class Consumer : public App {
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -080049public:
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080050 static TypeId
51 GetTypeId();
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -080052
Ilya Moiseenko956d0542012-01-02 15:26:40 -080053 /**
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -080054 * \brief Default constructor
Ilya Moiseenko956d0542012-01-02 15:26:40 -080055 * Sets up randomizer function and packet sequence number
56 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080057 Consumer();
58 virtual ~Consumer(){};
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070059
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070060 // From App
Alexander Afanasyev781ea812011-12-15 22:42:09 -080061 // virtual void
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -070062 // OnInterest (const shared_ptr<const Interest> &interest);
Alexander Afanasyev781ea812011-12-15 22:42:09 -080063
64 virtual void
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -070065 OnNack(shared_ptr<const Interest> interest);
Alexander Afanasyev781ea812011-12-15 22:42:09 -080066
67 virtual void
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -070068 OnData(shared_ptr<const Data> contentObject);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080069
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070070 /**
71 * @brief Timeout event
72 * @param sequenceNumber time outed sequence number
73 */
Alexander Afanasyev359bfb72012-01-09 18:42:50 -080074 virtual void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080075 OnTimeout(uint32_t sequenceNumber);
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080076
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070077 /**
78 * @brief Actually send packet
79 */
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080080 void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080081 SendPacket();
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -080082
Alexander Afanasyev79b2fb32013-04-12 11:24:55 -070083 /**
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080084 * @brief An event that is fired just before an Interest packet is actually send out (send is
85 *inevitable)
Alexander Afanasyev79b2fb32013-04-12 11:24:55 -070086 *
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080087 * The reason for "before" even is that in certain cases (when it is possible to satisfy from the
88 *local cache),
89 * the send call will immediately return data, and if "after" even was used, this after would be
90 *called after
Alexander Afanasyev79b2fb32013-04-12 11:24:55 -070091 * all processing of incoming data, potentially producing unexpected results.
92 */
93 virtual void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080094 WillSendOutInterest(uint32_t sequenceNumber);
95
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070096protected:
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070097 // from App
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080098 virtual void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080099 StartApplication();
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800100
101 virtual void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800102 StopApplication();
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800103
Ilya Moiseenko956d0542012-01-02 15:26:40 -0800104 /**
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800105 * \brief Constructs the Interest packet and sends it using a callback to the underlying NDN
106 * protocol
Ilya Moiseenko956d0542012-01-02 15:26:40 -0800107 */
Alexander Afanasyev029d38d2012-01-09 13:50:50 -0800108 virtual void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800109 ScheduleNextPacket() = 0;
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800110
Ilya Moiseenko956d0542012-01-02 15:26:40 -0800111 /**
112 * \brief Checks if the packet need to be retransmitted becuase of retransmission timer expiration
113 */
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800114 void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800115 CheckRetxTimeout();
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800116
Ilya Moiseenko956d0542012-01-02 15:26:40 -0800117 /**
118 * \brief Modifies the frequency of checking the retransmission timeouts
119 * \param retxTimer Timeout defining how frequent retransmission timeouts should be checked
120 */
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800121 void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800122 SetRetxTimer(Time retxTimer);
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800123
Ilya Moiseenko956d0542012-01-02 15:26:40 -0800124 /**
125 * \brief Returns the frequency of checking the retransmission timeouts
126 * \return Timeout defining how frequent retransmission timeouts should be checked
127 */
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800128 Time
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800129 GetRetxTimer() const;
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800130
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800131protected:
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700132 UniformVariable m_rand; ///< @brief nonce generator
Alexander Afanasyev029d38d2012-01-09 13:50:50 -0800133
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800134 uint32_t m_seq; ///< @brief currently requested sequence number
135 uint32_t m_seqMax; ///< @brief maximum number of sequence number
136 EventId m_sendEvent; ///< @brief EventId of pending "send packet" event
137 Time m_retxTimer; ///< @brief Currently estimated retransmission timer
138 EventId m_retxEvent; ///< @brief Event to check whether or not retransmission should be performed
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800139
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700140 Ptr<RttEstimator> m_rtt; ///< @brief RTT estimator
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800141
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800142 Time m_offTime; ///< \brief Time interval between packets
143 Name m_interestName; ///< \brief NDN Name of the Interest (use Name)
144 Time m_interestLifeTime; ///< \brief LifeTime for interest packet
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800145
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800146 /// @cond include_hidden
Ilya Moiseenko956d0542012-01-02 15:26:40 -0800147 /**
148 * \struct This struct contains sequence numbers of packets to be retransmitted
149 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800150 struct RetxSeqsContainer : public std::set<uint32_t> {
151 };
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800152
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800153 RetxSeqsContainer m_retxSeqs; ///< \brief ordered set of sequence numbers to be retransmitted
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800154
Ilya Moiseenko956d0542012-01-02 15:26:40 -0800155 /**
156 * \struct This struct contains a pair of packet sequence number and its timeout
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800157 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800158 struct SeqTimeout {
159 SeqTimeout(uint32_t _seq, Time _time)
160 : seq(_seq)
161 , time(_time)
162 {
163 }
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800164
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800165 uint32_t seq;
166 Time time;
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800167 };
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800168 /// @endcond
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800169
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800170 /// @cond include_hidden
171 class i_seq {
172 };
173 class i_timestamp {
174 };
175 /// @endcond
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800176
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800177 /// @cond include_hidden
Ilya Moiseenko956d0542012-01-02 15:26:40 -0800178 /**
179 * \struct This struct contains a multi-index for the set of SeqTimeout structs
180 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800181 struct SeqTimeoutsContainer
182 : public boost::multi_index::
183 multi_index_container<SeqTimeout,
184 boost::multi_index::
185 indexed_by<boost::multi_index::
186 ordered_unique<boost::multi_index::tag<i_seq>,
187 boost::multi_index::
188 member<SeqTimeout, uint32_t,
189 &SeqTimeout::seq>>,
190 boost::multi_index::
191 ordered_non_unique<boost::multi_index::
192 tag<i_timestamp>,
193 boost::multi_index::
194 member<SeqTimeout, Time,
195 &SeqTimeout::time>>>> {
196 };
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800197
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800198 SeqTimeoutsContainer m_seqTimeouts; ///< \brief multi-index for the set of SeqTimeout structs
Alexander Afanasyev400aae12013-01-19 13:27:52 -0800199
200 SeqTimeoutsContainer m_seqLastDelay;
201 SeqTimeoutsContainer m_seqFullDelay;
202 std::map<uint32_t, uint32_t> m_seqRetxCounts;
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800203
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800204 TracedCallback<Ptr<App> /* app */, uint32_t /* seqno */, Time /* delay */, int32_t /*hop count*/>
205 m_lastRetransmittedInterestDataDelay;
206 TracedCallback<Ptr<App> /* app */, uint32_t /* seqno */, Time /* delay */,
207 uint32_t /*retx count*/, int32_t /*hop count*/> m_firstInterestDataDelay;
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800208
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800209 /// @endcond
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700210};
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700211
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700212} // namespace ndn
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700213} // namespace ns3
214
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700215#endif