blob: 9e4525855688bbc68d5d75ba00a4a4503be46607 [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"
Alexander Afanasyeve3d126f2012-07-16 17:07:31 -070022
23#include "ns3/ccnx-fib.h"
24#include "ns3/ccnx-name-components.h"
25#include "ns3/ccnx-interest-header.h"
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070026
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -070027#include "ns3/simulator.h"
Alexander Afanasyev5a595072011-11-25 14:49:07 -080028#include "ns3/log.h"
29
30#include <boost/lambda/lambda.hpp>
Alexander Afanasyev0a61c342011-12-06 12:48:55 -080031#include <boost/lambda/bind.hpp>
Alexander Afanasyev78057c32012-07-06 15:18:46 -070032#include <boost/foreach.hpp>
Alexander Afanasyev5a595072011-11-25 14:49:07 -080033namespace ll = boost::lambda;
34
35NS_LOG_COMPONENT_DEFINE ("CcnxPitEntry");
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -070036
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070037namespace ns3
38{
39
Alexander Afanasyev36b45772012-07-10 16:57:42 -070040CcnxPitEntry::CcnxPitEntry (CcnxPit &container,
41 Ptr<const CcnxInterestHeader> header,
Alexander Afanasyev11f7bb42012-07-09 17:06:30 -070042 Ptr<CcnxFibEntry> fibEntry)
Alexander Afanasyev36b45772012-07-10 16:57:42 -070043 : m_container (container)
44 , m_prefix (header->GetNamePtr ())
Alexander Afanasyev0560eec2012-07-16 15:44:31 -070045 , m_fibEntry (fibEntry)
Alexander Afanasyevf1e013f2012-07-11 17:59:40 -070046 , m_expireTime (Simulator::Now () + (!header->GetInterestLifetime ().IsZero ()?
47 header->GetInterestLifetime ():
48 Seconds (1.0)))
Alexander Afanasyev0a61c342011-12-06 12:48:55 -080049 , m_maxRetxCount (0)
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070050{
Alexander Afanasyev30f60e32012-07-10 14:21:16 -070051 // note that if interest lifetime is not set, the behavior is undefined
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070052}
53
Alexander Afanasyev0a61c342011-12-06 12:48:55 -080054void
55CcnxPitEntry::UpdateLifetime (const Time &offsetTime)
56{
Alexander Afanasyevff8c5d62012-04-25 15:14:51 -070057 NS_LOG_FUNCTION (offsetTime.ToDouble (Time::S));
58
Alexander Afanasyev0a61c342011-12-06 12:48:55 -080059 Time newExpireTime = Simulator::Now () + offsetTime;
60 if (newExpireTime > m_expireTime)
61 m_expireTime = newExpireTime;
Alexander Afanasyev1aeaf922012-04-23 13:48:09 -070062
Alexander Afanasyevff8c5d62012-04-25 15:14:51 -070063 NS_LOG_INFO ("Updated lifetime to " << m_expireTime.ToDouble (Time::S));
Alexander Afanasyev0a61c342011-12-06 12:48:55 -080064}
65
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -070066CcnxPitEntry::in_iterator
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080067CcnxPitEntry::AddIncoming (Ptr<CcnxFace> face)
68{
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -070069 std::pair<in_iterator,bool> ret =
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080070 m_incoming.insert (CcnxPitEntryIncomingFace (face));
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080071
Alexander Afanasyeve55d1e32012-07-19 15:33:05 -070072 // NS_ASSERT_MSG (ret.second, "Something is wrong");
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080073
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080074 return ret.first;
75}
76
Alexander Afanasyev9d313d42011-11-25 13:36:15 -080077void
78CcnxPitEntry::RemoveIncoming (Ptr<CcnxFace> face)
79{
80 m_incoming.erase (face);
81}
82
83
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -070084CcnxPitEntry::out_iterator
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080085CcnxPitEntry::AddOutgoing (Ptr<CcnxFace> face)
86{
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -070087 std::pair<out_iterator,bool> ret =
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080088 m_outgoing.insert (CcnxPitEntryOutgoingFace (face));
89
Alexander Afanasyev0a61c342011-12-06 12:48:55 -080090 if (!ret.second)
91 { // outgoing face already exists
92 m_outgoing.modify (ret.first,
93 ll::bind (&CcnxPitEntryOutgoingFace::UpdateOnRetransmit, ll::_1));
94 }
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080095
96 return ret.first;
97}
98
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -080099void
100CcnxPitEntry::RemoveAllReferencesToFace (Ptr<CcnxFace> face)
101{
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -0700102 in_iterator incoming = m_incoming.find (face);
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700103
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800104 if (incoming != m_incoming.end ())
105 m_incoming.erase (incoming);
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700106
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -0700107 out_iterator outgoing =
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800108 m_outgoing.find (face);
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700109
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800110 if (outgoing != m_outgoing.end ())
111 m_outgoing.erase (outgoing);
112}
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700113
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700114// void
115// CcnxPitEntry::SetWaitingInVain (CcnxPitEntry::out_iterator face)
116// {
117// NS_LOG_DEBUG (boost::cref (*face->m_face));
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800118
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700119// m_outgoing.modify (face,
120// (&ll::_1)->*&CcnxPitEntryOutgoingFace::m_waitingInVain = true);
121// }
122
123void
124CcnxPitEntry::SetWaitingInVain (Ptr<CcnxFace> face)
125{
126 // NS_LOG_DEBUG (boost::cref (*face->m_face));
127
128 out_iterator item = m_outgoing.find (face);
129 if (item == m_outgoing.end ())
130 return;
131
132 m_outgoing.modify (item,
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800133 (&ll::_1)->*&CcnxPitEntryOutgoingFace::m_waitingInVain = true);
134}
135
Alexander Afanasyev786936a2012-07-17 19:48:15 -0700136
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800137bool
138CcnxPitEntry::AreAllOutgoingInVain () const
139{
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800140 NS_LOG_DEBUG (m_outgoing.size ());
141
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800142 bool inVain = true;
143 std::for_each (m_outgoing.begin (), m_outgoing.end (),
144 ll::var(inVain) &= (&ll::_1)->*&CcnxPitEntryOutgoingFace::m_waitingInVain);
145
146 NS_LOG_DEBUG ("inVain " << inVain);
147 return inVain;
148}
149
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800150bool
151CcnxPitEntry::AreTherePromisingOutgoingFacesExcept (Ptr<CcnxFace> face) const
152{
153 bool inVain = true;
154 std::for_each (m_outgoing.begin (), m_outgoing.end (),
155 ll::var(inVain) &=
156 ((&ll::_1)->*&CcnxPitEntryOutgoingFace::m_face == face ||
157 (&ll::_1)->*&CcnxPitEntryOutgoingFace::m_waitingInVain));
158
159 return !inVain;
160}
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800161
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800162void
163CcnxPitEntry::IncreaseAllowedRetxCount ()
164{
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800165 NS_LOG_ERROR (this);
Alexander Afanasyev9a517db2012-04-30 13:58:47 -0700166 if (Simulator::Now () - m_lastRetransmission >= MilliSeconds (100))
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700167 {
168 // cheat:
Alexander Afanasyev9a517db2012-04-30 13:58:47 -0700169 // don't allow retransmission faster than every 100ms
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700170 m_maxRetxCount++;
Alexander Afanasyev9a517db2012-04-30 13:58:47 -0700171 m_lastRetransmission = Simulator::Now ();
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700172 }
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800173}
174
Alexander Afanasyeva95b7392012-03-09 10:54:10 -0800175std::ostream& operator<< (std::ostream& os, const CcnxPitEntry &entry)
176{
177 os << "Prefix: " << *entry.m_prefix << "\n";
178 os << "In: ";
179 bool first = true;
180 BOOST_FOREACH (const CcnxPitEntryIncomingFace &face, entry.m_incoming)
181 {
182 if (!first)
183 os << ",";
184 else
185 first = false;
186
187 os << *face.m_face;
188 }
189
190 os << "\nOut: ";
191 first = true;
192 BOOST_FOREACH (const CcnxPitEntryOutgoingFace &face, entry.m_outgoing)
193 {
194 if (!first)
195 os << ",";
196 else
197 first = false;
198
199 os << *face.m_face;
200 }
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700201 os << "\nNonces: ";
202 first = true;
203 BOOST_FOREACH (uint32_t nonce, entry.m_seenNonces)
204 {
205 if (!first)
206 os << ",";
207 else
208 first = false;
209
210 os << nonce;
211 }
Alexander Afanasyeva95b7392012-03-09 10:54:10 -0800212
213 return os;
214}
215
216
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800217}