blob: eea5fda286e43416c11ea994e034b9fdf4e0ea73 [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 Afanasyev4aac5572012-08-09 10:49:55 -070025#include "ndn-app.h"
Alexander Afanasyev19426ef2011-11-23 20:55:28 -080026#include "ns3/random-variable.h"
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -070027#include "ns3/ndn-name.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"
Alexander Afanasyevd9a7f192013-03-07 13:58:14 -080030#include "ns3/ndn-rtt-estimator.h"
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 Afanasyev2b4c9472012-08-09 15:00:38 -070047class Consumer: public App
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070048{
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -080049public:
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070050 static TypeId 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 Afanasyev2b4c9472012-08-09 15:00:38 -070056 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 Afanasyevfaa01f92013-07-10 18:34:31 -070064 OnNack (Ptr<const Interest> interest);
Alexander Afanasyev781ea812011-12-15 22:42:09 -080065
66 virtual void
Alexander Afanasyev772f51b2013-08-01 18:53:25 -070067 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
74 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
80 SendPacket ();
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -080081
Alexander Afanasyev79b2fb32013-04-12 11:24:55 -070082 /**
83 * @brief An event that is fired just before an Interest packet is actually send out (send is inevitable)
84 *
85 * The reason for "before" even is that in certain cases (when it is possible to satisfy from the local cache),
86 * the send call will immediately return data, and if "after" even was used, this after would be called after
87 * all processing of incoming data, potentially producing unexpected results.
88 */
89 virtual void
90 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
95 StartApplication ();
96
97 virtual void
98 StopApplication ();
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -080099
Ilya Moiseenko956d0542012-01-02 15:26:40 -0800100 /**
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700101 * \brief Constructs the Interest packet and sends it using a callback to the underlying NDN protocol
Ilya Moiseenko956d0542012-01-02 15:26:40 -0800102 */
Alexander Afanasyev029d38d2012-01-09 13:50:50 -0800103 virtual void
104 ScheduleNextPacket () = 0;
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800105
Ilya Moiseenko956d0542012-01-02 15:26:40 -0800106 /**
107 * \brief Checks if the packet need to be retransmitted becuase of retransmission timer expiration
108 */
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800109 void
110 CheckRetxTimeout ();
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800111
Ilya Moiseenko956d0542012-01-02 15:26:40 -0800112 /**
113 * \brief Modifies the frequency of checking the retransmission timeouts
114 * \param retxTimer Timeout defining how frequent retransmission timeouts should be checked
115 */
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800116 void
117 SetRetxTimer (Time retxTimer);
118
Ilya Moiseenko956d0542012-01-02 15:26:40 -0800119 /**
120 * \brief Returns the frequency of checking the retransmission timeouts
121 * \return Timeout defining how frequent retransmission timeouts should be checked
122 */
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800123 Time
124 GetRetxTimer () const;
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800125
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800126protected:
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700127 UniformVariable m_rand; ///< @brief nonce generator
Alexander Afanasyev029d38d2012-01-09 13:50:50 -0800128
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700129 uint32_t m_seq; ///< @brief currently requested sequence number
130 uint32_t m_seqMax; ///< @brief maximum number of sequence number
131 EventId m_sendEvent; ///< @brief EventId of pending "send packet" event
132 Time m_retxTimer; ///< @brief Currently estimated retransmission timer
133 EventId m_retxEvent; ///< @brief Event to check whether or not retransmission should be performed
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800134
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700135 Ptr<RttEstimator> m_rtt; ///< @brief RTT estimator
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800136
Ilya Moiseenko956d0542012-01-02 15:26:40 -0800137 Time m_offTime; ///< \brief Time interval between packets
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700138 Name m_interestName; ///< \brief NDN Name of the Interest (use Name)
Ilya Moiseenko956d0542012-01-02 15:26:40 -0800139 Time m_interestLifeTime; ///< \brief LifeTime for interest packet
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800140
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800141/// @cond include_hidden
Ilya Moiseenko956d0542012-01-02 15:26:40 -0800142 /**
143 * \struct This struct contains sequence numbers of packets to be retransmitted
144 */
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800145 struct RetxSeqsContainer :
146 public std::set<uint32_t> { };
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800147
Ilya Moiseenko956d0542012-01-02 15:26:40 -0800148 RetxSeqsContainer m_retxSeqs; ///< \brief ordered set of sequence numbers to be retransmitted
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800149
Ilya Moiseenko956d0542012-01-02 15:26:40 -0800150 /**
151 * \struct This struct contains a pair of packet sequence number and its timeout
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800152 */
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800153 struct SeqTimeout
154 {
155 SeqTimeout (uint32_t _seq, Time _time) : seq (_seq), time (_time) { }
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800156
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800157 uint32_t seq;
158 Time time;
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800159 };
Alexander Afanasyev6315ef72012-06-01 20:56:31 -0700160/// @endcond
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800161
Alexander Afanasyev6315ef72012-06-01 20:56:31 -0700162/// @cond include_hidden
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800163 class i_seq { };
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800164 class i_timestamp { };
Alexander Afanasyev6315ef72012-06-01 20:56:31 -0700165/// @endcond
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800166
Alexander Afanasyev6315ef72012-06-01 20:56:31 -0700167/// @cond include_hidden
Ilya Moiseenko956d0542012-01-02 15:26:40 -0800168 /**
169 * \struct This struct contains a multi-index for the set of SeqTimeout structs
170 */
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800171 struct SeqTimeoutsContainer :
172 public boost::multi_index::multi_index_container<
173 SeqTimeout,
174 boost::multi_index::indexed_by<
175 boost::multi_index::ordered_unique<
176 boost::multi_index::tag<i_seq>,
177 boost::multi_index::member<SeqTimeout, uint32_t, &SeqTimeout::seq>
178 >,
179 boost::multi_index::ordered_non_unique<
180 boost::multi_index::tag<i_timestamp>,
181 boost::multi_index::member<SeqTimeout, Time, &SeqTimeout::time>
182 >
183 >
184 > { } ;
185
Ilya Moiseenko956d0542012-01-02 15:26:40 -0800186 SeqTimeoutsContainer m_seqTimeouts; ///< \brief multi-index for the set of SeqTimeout structs
Alexander Afanasyev400aae12013-01-19 13:27:52 -0800187
188 SeqTimeoutsContainer m_seqLastDelay;
189 SeqTimeoutsContainer m_seqFullDelay;
190 std::map<uint32_t, uint32_t> m_seqRetxCounts;
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800191
Alexander Afanasyev1a0fff62013-01-19 14:29:51 -0800192 TracedCallback<Ptr<App> /* app */, uint32_t /* seqno */,
193 Time /* delay */, int32_t /*hop count*/> m_lastRetransmittedInterestDataDelay;
194 TracedCallback<Ptr<App> /* app */, uint32_t /* seqno */,
195 Time /* delay */, uint32_t /*retx count*/,
196 int32_t /*hop count*/> m_firstInterestDataDelay;
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800197
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700198/// @endcond
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700199};
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700200
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700201} // namespace ndn
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700202} // namespace ns3
203
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700204#endif