blob: 30ccc7cb87076984ce37b5052102186c92d7e85a [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"
Mickey Sweatt89046c12014-11-16 20:32:27 -080028
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080029#include "ns3/random-variable.h"
Alexander Afanasyev781ea812011-12-15 22:42:09 -080030#include "ns3/nstime.h"
Alexander Afanasyev011b8592011-12-21 14:45:27 -080031#include "ns3/data-rate.h"
Mickey Sweatt89046c12014-11-16 20:32:27 -080032
33#include "ns3/ndnSIM/model/ndn-common.hpp"
34#include "ns3/ndnSIM/utils/ndn-rtt-estimator.hpp"
35#include "ns3/ndnSIM/utils/ndn-fw-hop-count-tag.hpp"
Alexander Afanasyev781ea812011-12-15 22:42:09 -080036
37#include <set>
Alexander Afanasyev400aae12013-01-19 13:27:52 -080038#include <map>
Alexander Afanasyev781ea812011-12-15 22:42:09 -080039
40#include <boost/multi_index_container.hpp>
41#include <boost/multi_index/tag.hpp>
42#include <boost/multi_index/ordered_index.hpp>
43#include <boost/multi_index/member.hpp>
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070044
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070045namespace ns3 {
46namespace ndn {
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070047
Ilya Moiseenko956d0542012-01-02 15:26:40 -080048/**
Alexander Afanasyev79206512013-07-27 16:49:12 -070049 * @ingroup ndn-apps
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070050 * \brief NDN application for sending out Interest packets
Ilya Moiseenko956d0542012-01-02 15:26:40 -080051 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080052class Consumer : public App {
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -080053public:
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080054 static TypeId
55 GetTypeId();
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -080056
Ilya Moiseenko956d0542012-01-02 15:26:40 -080057 /**
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -080058 * \brief Default constructor
Ilya Moiseenko956d0542012-01-02 15:26:40 -080059 * Sets up randomizer function and packet sequence number
60 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080061 Consumer();
62 virtual ~Consumer(){};
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070063
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070064 // From App
Alexander Afanasyev781ea812011-12-15 22:42:09 -080065 virtual void
Spyridon Mastorakis53e922f2014-10-17 17:29:26 -070066 OnData(shared_ptr<const Data> contentObject);
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080067
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070068 /**
69 * @brief Timeout event
70 * @param sequenceNumber time outed sequence number
71 */
Alexander Afanasyev359bfb72012-01-09 18:42:50 -080072 virtual void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080073 OnTimeout(uint32_t sequenceNumber);
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080074
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -070075 /**
76 * @brief Actually send packet
77 */
Alexander Afanasyev029d38d2012-01-09 13:50:50 -080078 void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080079 SendPacket();
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -080080
Alexander Afanasyev79b2fb32013-04-12 11:24:55 -070081 /**
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080082 * @brief An event that is fired just before an Interest packet is actually send out (send is
83 *inevitable)
Alexander Afanasyev79b2fb32013-04-12 11:24:55 -070084 *
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080085 * The reason for "before" even is that in certain cases (when it is possible to satisfy from the
86 *local cache),
87 * the send call will immediately return data, and if "after" even was used, this after would be
88 *called after
Alexander Afanasyev79b2fb32013-04-12 11:24:55 -070089 * all processing of incoming data, potentially producing unexpected results.
90 */
91 virtual void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080092 WillSendOutInterest(uint32_t sequenceNumber);
93
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070094protected:
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070095 // from App
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080096 virtual void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -080097 StartApplication();
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080098
99 virtual void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800100 StopApplication();
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800101
Ilya Moiseenko956d0542012-01-02 15:26:40 -0800102 /**
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800103 * \brief Constructs the Interest packet and sends it using a callback to the underlying NDN
104 * protocol
Ilya Moiseenko956d0542012-01-02 15:26:40 -0800105 */
Alexander Afanasyev029d38d2012-01-09 13:50:50 -0800106 virtual void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800107 ScheduleNextPacket() = 0;
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800108
Ilya Moiseenko956d0542012-01-02 15:26:40 -0800109 /**
110 * \brief Checks if the packet need to be retransmitted becuase of retransmission timer expiration
111 */
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800112 void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800113 CheckRetxTimeout();
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800114
Ilya Moiseenko956d0542012-01-02 15:26:40 -0800115 /**
116 * \brief Modifies the frequency of checking the retransmission timeouts
117 * \param retxTimer Timeout defining how frequent retransmission timeouts should be checked
118 */
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800119 void
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800120 SetRetxTimer(Time retxTimer);
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800121
Ilya Moiseenko956d0542012-01-02 15:26:40 -0800122 /**
123 * \brief Returns the frequency of checking the retransmission timeouts
124 * \return Timeout defining how frequent retransmission timeouts should be checked
125 */
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800126 Time
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800127 GetRetxTimer() const;
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800128
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800129protected:
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700130 UniformVariable m_rand; ///< @brief nonce generator
Alexander Afanasyev029d38d2012-01-09 13:50:50 -0800131
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800132 uint32_t m_seq; ///< @brief currently requested sequence number
133 uint32_t m_seqMax; ///< @brief maximum number of sequence number
134 EventId m_sendEvent; ///< @brief EventId of pending "send packet" event
135 Time m_retxTimer; ///< @brief Currently estimated retransmission timer
136 EventId m_retxEvent; ///< @brief Event to check whether or not retransmission should be performed
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800137
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700138 Ptr<RttEstimator> m_rtt; ///< @brief RTT estimator
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800139
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800140 Time m_offTime; ///< \brief Time interval between packets
141 Name m_interestName; ///< \brief NDN Name of the Interest (use Name)
142 Time m_interestLifeTime; ///< \brief LifeTime for interest packet
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800143
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800144 /// @cond include_hidden
Ilya Moiseenko956d0542012-01-02 15:26:40 -0800145 /**
146 * \struct This struct contains sequence numbers of packets to be retransmitted
147 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800148 struct RetxSeqsContainer : public std::set<uint32_t> {
149 };
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800150
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800151 RetxSeqsContainer m_retxSeqs; ///< \brief ordered set of sequence numbers to be retransmitted
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800152
Ilya Moiseenko956d0542012-01-02 15:26:40 -0800153 /**
154 * \struct This struct contains a pair of packet sequence number and its timeout
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800155 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800156 struct SeqTimeout {
157 SeqTimeout(uint32_t _seq, Time _time)
158 : seq(_seq)
159 , time(_time)
160 {
161 }
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800162
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800163 uint32_t seq;
164 Time time;
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800165 };
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800166 /// @endcond
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800167
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800168 /// @cond include_hidden
169 class i_seq {
170 };
171 class i_timestamp {
172 };
173 /// @endcond
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800174
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800175 /// @cond include_hidden
Ilya Moiseenko956d0542012-01-02 15:26:40 -0800176 /**
177 * \struct This struct contains a multi-index for the set of SeqTimeout structs
178 */
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800179 struct SeqTimeoutsContainer
180 : public boost::multi_index::
181 multi_index_container<SeqTimeout,
182 boost::multi_index::
183 indexed_by<boost::multi_index::
184 ordered_unique<boost::multi_index::tag<i_seq>,
185 boost::multi_index::
186 member<SeqTimeout, uint32_t,
187 &SeqTimeout::seq>>,
188 boost::multi_index::
189 ordered_non_unique<boost::multi_index::
190 tag<i_timestamp>,
191 boost::multi_index::
192 member<SeqTimeout, Time,
193 &SeqTimeout::time>>>> {
194 };
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800195
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800196 SeqTimeoutsContainer m_seqTimeouts; ///< \brief multi-index for the set of SeqTimeout structs
Alexander Afanasyev400aae12013-01-19 13:27:52 -0800197
198 SeqTimeoutsContainer m_seqLastDelay;
199 SeqTimeoutsContainer m_seqFullDelay;
200 std::map<uint32_t, uint32_t> m_seqRetxCounts;
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800201
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800202 TracedCallback<Ptr<App> /* app */, uint32_t /* seqno */, Time /* delay */, int32_t /*hop count*/>
203 m_lastRetransmittedInterestDataDelay;
204 TracedCallback<Ptr<App> /* app */, uint32_t /* seqno */, Time /* delay */,
205 uint32_t /*retx count*/, int32_t /*hop count*/> m_firstInterestDataDelay;
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800206
Alexander Afanasyevbe55cf62014-12-20 17:51:09 -0800207 /// @endcond
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700208};
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700209
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700210} // namespace ndn
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700211} // namespace ns3
212
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700213#endif