blob: 4b075aba879e79ed122d898a56996121968da6fb [file] [log] [blame]
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -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: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
19 */
20
21#include "ccnx-pit-entry.h"
22#include "ccnx-name-components.h"
23#include "ccnx-fib.h"
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070024#include "ccnx-interest-header.h"
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070025
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -070026#include "ns3/simulator.h"
Alexander Afanasyev5a595072011-11-25 14:49:07 -080027#include "ns3/log.h"
28
29#include <boost/lambda/lambda.hpp>
Alexander Afanasyev0a61c342011-12-06 12:48:55 -080030#include <boost/lambda/bind.hpp>
Alexander Afanasyev78057c32012-07-06 15:18:46 -070031#include <boost/foreach.hpp>
Alexander Afanasyev5a595072011-11-25 14:49:07 -080032namespace ll = boost::lambda;
33
34NS_LOG_COMPONENT_DEFINE ("CcnxPitEntry");
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -070035
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070036namespace ns3
37{
38
Alexander Afanasyev36b45772012-07-10 16:57:42 -070039CcnxPitEntry::CcnxPitEntry (CcnxPit &container,
40 Ptr<const CcnxInterestHeader> header,
Alexander Afanasyev11f7bb42012-07-09 17:06:30 -070041 Ptr<CcnxFibEntry> fibEntry)
Alexander Afanasyev36b45772012-07-10 16:57:42 -070042 : m_container (container)
43 , m_prefix (header->GetNamePtr ())
Alexander Afanasyevf1e013f2012-07-11 17:59:40 -070044 , m_expireTime (Simulator::Now () + (!header->GetInterestLifetime ().IsZero ()?
45 header->GetInterestLifetime ():
46 Seconds (1.0)))
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070047 , m_fibEntry (fibEntry)
Alexander Afanasyev0a61c342011-12-06 12:48:55 -080048 , m_maxRetxCount (0)
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070049{
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070050 // note that if interest lifetime is not set, the behavior is undefined
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070051}
52
Alexander Afanasyev0a61c342011-12-06 12:48:55 -080053void
54CcnxPitEntry::UpdateLifetime (const Time &offsetTime)
55{
Alexander Afanasyevff8c5d62012-04-25 15:14:51 -070056 NS_LOG_FUNCTION (offsetTime.ToDouble (Time::S));
57
Alexander Afanasyev0a61c342011-12-06 12:48:55 -080058 Time newExpireTime = Simulator::Now () + offsetTime;
59 if (newExpireTime > m_expireTime)
60 m_expireTime = newExpireTime;
Alexander Afanasyev1aeaf922012-04-23 13:48:09 -070061
Alexander Afanasyevff8c5d62012-04-25 15:14:51 -070062 NS_LOG_INFO ("Updated lifetime to " << m_expireTime.ToDouble (Time::S));
Alexander Afanasyev0a61c342011-12-06 12:48:55 -080063}
64
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -070065CcnxPitEntry::in_iterator
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080066CcnxPitEntry::AddIncoming (Ptr<CcnxFace> face)
67{
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -070068 std::pair<in_iterator,bool> ret =
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080069 m_incoming.insert (CcnxPitEntryIncomingFace (face));
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080070
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080071 NS_ASSERT_MSG (ret.second, "Something is wrong");
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080072
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080073 return ret.first;
74}
75
Alexander Afanasyev9d313d42011-11-25 13:36:15 -080076void
77CcnxPitEntry::RemoveIncoming (Ptr<CcnxFace> face)
78{
79 m_incoming.erase (face);
80}
81
82
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -070083CcnxPitEntry::out_iterator
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080084CcnxPitEntry::AddOutgoing (Ptr<CcnxFace> face)
85{
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -070086 std::pair<out_iterator,bool> ret =
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080087 m_outgoing.insert (CcnxPitEntryOutgoingFace (face));
88
Alexander Afanasyev0a61c342011-12-06 12:48:55 -080089 if (!ret.second)
90 { // outgoing face already exists
91 m_outgoing.modify (ret.first,
92 ll::bind (&CcnxPitEntryOutgoingFace::UpdateOnRetransmit, ll::_1));
93 }
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080094
95 return ret.first;
96}
97
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080098void
99CcnxPitEntry::RemoveAllReferencesToFace (Ptr<CcnxFace> face)
100{
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -0700101 in_iterator incoming = m_incoming.find (face);
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700102
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800103 if (incoming != m_incoming.end ())
104 m_incoming.erase (incoming);
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700105
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -0700106 out_iterator outgoing =
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800107 m_outgoing.find (face);
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700108
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800109 if (outgoing != m_outgoing.end ())
110 m_outgoing.erase (outgoing);
111}
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700112
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800113void
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -0700114CcnxPitEntry::SetWaitingInVain (CcnxPitEntry::out_iterator face)
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800115{
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800116 NS_LOG_DEBUG (boost::cref (*face->m_face));
117
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800118 m_outgoing.modify (face,
119 (&ll::_1)->*&CcnxPitEntryOutgoingFace::m_waitingInVain = true);
120}
121
122bool
123CcnxPitEntry::AreAllOutgoingInVain () const
124{
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800125 NS_LOG_DEBUG (m_outgoing.size ());
126
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800127 bool inVain = true;
128 std::for_each (m_outgoing.begin (), m_outgoing.end (),
129 ll::var(inVain) &= (&ll::_1)->*&CcnxPitEntryOutgoingFace::m_waitingInVain);
130
131 NS_LOG_DEBUG ("inVain " << inVain);
132 return inVain;
133}
134
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800135bool
136CcnxPitEntry::AreTherePromisingOutgoingFacesExcept (Ptr<CcnxFace> face) const
137{
138 bool inVain = true;
139 std::for_each (m_outgoing.begin (), m_outgoing.end (),
140 ll::var(inVain) &=
141 ((&ll::_1)->*&CcnxPitEntryOutgoingFace::m_face == face ||
142 (&ll::_1)->*&CcnxPitEntryOutgoingFace::m_waitingInVain));
143
144 return !inVain;
145}
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800146
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800147void
148CcnxPitEntry::IncreaseAllowedRetxCount ()
149{
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800150 NS_LOG_ERROR (this);
Alexander Afanasyev9a517db2012-04-30 13:58:47 -0700151 if (Simulator::Now () - m_lastRetransmission >= MilliSeconds (100))
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700152 {
153 // cheat:
Alexander Afanasyev9a517db2012-04-30 13:58:47 -0700154 // don't allow retransmission faster than every 100ms
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700155 m_maxRetxCount++;
Alexander Afanasyev9a517db2012-04-30 13:58:47 -0700156 m_lastRetransmission = Simulator::Now ();
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700157 }
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800158}
159
Alexander Afanasyeva95b7392012-03-09 10:54:10 -0800160std::ostream& operator<< (std::ostream& os, const CcnxPitEntry &entry)
161{
162 os << "Prefix: " << *entry.m_prefix << "\n";
163 os << "In: ";
164 bool first = true;
165 BOOST_FOREACH (const CcnxPitEntryIncomingFace &face, entry.m_incoming)
166 {
167 if (!first)
168 os << ",";
169 else
170 first = false;
171
172 os << *face.m_face;
173 }
174
175 os << "\nOut: ";
176 first = true;
177 BOOST_FOREACH (const CcnxPitEntryOutgoingFace &face, entry.m_outgoing)
178 {
179 if (!first)
180 os << ",";
181 else
182 first = false;
183
184 os << *face.m_face;
185 }
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700186 os << "\nNonces: ";
187 first = true;
188 BOOST_FOREACH (uint32_t nonce, entry.m_seenNonces)
189 {
190 if (!first)
191 os << ",";
192 else
193 first = false;
194
195 os << nonce;
196 }
Alexander Afanasyeva95b7392012-03-09 10:54:10 -0800197
198 return os;
199}
200
201
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800202}