blob: 7dd4cdcc1ab614f7b47a18c541ea8b77a5110a67 [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 Afanasyev4aac5572012-08-09 10:49:55 -070044 * @ingroup ndn
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 Afanasyeveae83ee2013-03-15 15:01:10 -070064 OnNack (const Ptr<const Interest> &interest, Ptr<Packet> packet);
Alexander Afanasyev781ea812011-12-15 22:42:09 -080065
66 virtual void
Alexander Afanasyeveae83ee2013-03-15 15:01:10 -070067 OnContentObject (const Ptr<const ContentObject> &contentObject,
Alexander Afanasyeve9c9d722012-01-19 16:59:30 -080068 Ptr<Packet> payload);
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
75 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
81 SendPacket ();
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -080082
Alexander Afanasyev79b2fb32013-04-12 11:24:55 -070083 /**
84 * @brief An event that is fired just before an Interest packet is actually send out (send is inevitable)
85 *
86 * The reason for "before" even is that in certain cases (when it is possible to satisfy from the local cache),
87 * the send call will immediately return data, and if "after" even was used, this after would be called after
88 * all processing of incoming data, potentially producing unexpected results.
89 */
90 virtual void
91 WillSendOutInterest (uint32_t sequenceNumber);
92
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -070093protected:
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -070094 // from App
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080095 virtual void
96 StartApplication ();
97
98 virtual void
99 StopApplication ();
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800100
Ilya Moiseenko956d0542012-01-02 15:26:40 -0800101 /**
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700102 * \brief Constructs the Interest packet and sends it using a callback to the underlying NDN protocol
Ilya Moiseenko956d0542012-01-02 15:26:40 -0800103 */
Alexander Afanasyev029d38d2012-01-09 13:50:50 -0800104 virtual void
105 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
111 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
118 SetRetxTimer (Time retxTimer);
119
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
125 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 Afanasyevb4fee8b2012-06-06 12:54:26 -0700130 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
Ilya Moiseenko956d0542012-01-02 15:26:40 -0800138 Time m_offTime; ///< \brief Time interval between packets
Alexander Afanasyevcfdc14f2013-03-15 14:38:44 -0700139 Name m_interestName; ///< \brief NDN Name of the Interest (use Name)
Ilya Moiseenko956d0542012-01-02 15:26:40 -0800140 Time m_interestLifeTime; ///< \brief LifeTime for interest packet
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800141
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -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 Afanasyev781ea812011-12-15 22:42:09 -0800146 struct RetxSeqsContainer :
147 public std::set<uint32_t> { };
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800148
Ilya Moiseenko956d0542012-01-02 15:26:40 -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 Afanasyev781ea812011-12-15 22:42:09 -0800154 struct SeqTimeout
155 {
156 SeqTimeout (uint32_t _seq, Time _time) : seq (_seq), time (_time) { }
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800157
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800158 uint32_t seq;
159 Time time;
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800160 };
Alexander Afanasyev6315ef72012-06-01 20:56:31 -0700161/// @endcond
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800162
Alexander Afanasyev6315ef72012-06-01 20:56:31 -0700163/// @cond include_hidden
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800164 class i_seq { };
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800165 class i_timestamp { };
Alexander Afanasyev6315ef72012-06-01 20:56:31 -0700166/// @endcond
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800167
Alexander Afanasyev6315ef72012-06-01 20:56:31 -0700168/// @cond include_hidden
Ilya Moiseenko956d0542012-01-02 15:26:40 -0800169 /**
170 * \struct This struct contains a multi-index for the set of SeqTimeout structs
171 */
Alexander Afanasyev781ea812011-12-15 22:42:09 -0800172 struct SeqTimeoutsContainer :
173 public boost::multi_index::multi_index_container<
174 SeqTimeout,
175 boost::multi_index::indexed_by<
176 boost::multi_index::ordered_unique<
177 boost::multi_index::tag<i_seq>,
178 boost::multi_index::member<SeqTimeout, uint32_t, &SeqTimeout::seq>
179 >,
180 boost::multi_index::ordered_non_unique<
181 boost::multi_index::tag<i_timestamp>,
182 boost::multi_index::member<SeqTimeout, Time, &SeqTimeout::time>
183 >
184 >
185 > { } ;
186
Ilya Moiseenko956d0542012-01-02 15:26:40 -0800187 SeqTimeoutsContainer m_seqTimeouts; ///< \brief multi-index for the set of SeqTimeout structs
Alexander Afanasyev400aae12013-01-19 13:27:52 -0800188
189 SeqTimeoutsContainer m_seqLastDelay;
190 SeqTimeoutsContainer m_seqFullDelay;
191 std::map<uint32_t, uint32_t> m_seqRetxCounts;
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800192
Alexander Afanasyev1a0fff62013-01-19 14:29:51 -0800193 TracedCallback<Ptr<App> /* app */, uint32_t /* seqno */,
194 Time /* delay */, int32_t /*hop count*/> m_lastRetransmittedInterestDataDelay;
195 TracedCallback<Ptr<App> /* app */, uint32_t /* seqno */,
196 Time /* delay */, uint32_t /*retx count*/,
197 int32_t /*hop count*/> m_firstInterestDataDelay;
Alexander Afanasyev3a3ce1a2013-01-31 11:26:11 -0800198
Alexander Afanasyevb4fee8b2012-06-06 12:54:26 -0700199/// @endcond
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700200};
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700201
Alexander Afanasyev2b4c9472012-08-09 15:00:38 -0700202} // namespace ndn
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700203} // namespace ns3
204
Ilya Moiseenko8196d2e2011-08-29 13:03:22 -0700205#endif