blob: 9c8c389c77851535ac26cfc75fc05bb9e439c1e4 [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"
24
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -070025#include "ns3/simulator.h"
Alexander Afanasyev5a595072011-11-25 14:49:07 -080026#include "ns3/log.h"
27
28#include <boost/lambda/lambda.hpp>
Alexander Afanasyev0a61c342011-12-06 12:48:55 -080029#include <boost/lambda/bind.hpp>
Alexander Afanasyev5a595072011-11-25 14:49:07 -080030namespace ll = boost::lambda;
31
32NS_LOG_COMPONENT_DEFINE ("CcnxPitEntry");
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -070033
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070034namespace ns3
35{
36
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080037CcnxPitEntry::CcnxPitEntry (Ptr<CcnxNameComponents> prefix,
38 const Time &expireTime,
39 const CcnxFibEntry &fibEntry)
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070040 : m_prefix (prefix)
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070041 , m_fibEntry (fibEntry)
Alexander Afanasyev0a61c342011-12-06 12:48:55 -080042 , m_expireTime (Simulator::Now () + expireTime)
43 // , m_timerExpired (false)
44 // , m_counterExpirations (0)
45 , m_maxRetxCount (0)
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070046{
47}
48
Alexander Afanasyev0a61c342011-12-06 12:48:55 -080049void
Alexander Afanasyev1aeaf922012-04-23 13:48:09 -070050CcnxPitEntry::SetExpireTime (const Time &expireTime)
51{
Alexander Afanasyevff8c5d62012-04-25 15:14:51 -070052 NS_LOG_FUNCTION (expireTime.ToDouble (Time::S));
Alexander Afanasyev1aeaf922012-04-23 13:48:09 -070053 m_expireTime = expireTime;
54}
55
56
57void
Alexander Afanasyev0a61c342011-12-06 12:48:55 -080058CcnxPitEntry::UpdateLifetime (const Time &offsetTime)
59{
Alexander Afanasyevff8c5d62012-04-25 15:14:51 -070060 NS_LOG_FUNCTION (offsetTime.ToDouble (Time::S));
61
Alexander Afanasyev0a61c342011-12-06 12:48:55 -080062 Time newExpireTime = Simulator::Now () + offsetTime;
63 if (newExpireTime > m_expireTime)
64 m_expireTime = newExpireTime;
Alexander Afanasyev1aeaf922012-04-23 13:48:09 -070065
Alexander Afanasyevff8c5d62012-04-25 15:14:51 -070066 NS_LOG_INFO ("Updated lifetime to " << m_expireTime.ToDouble (Time::S));
Alexander Afanasyev0a61c342011-12-06 12:48:55 -080067}
68
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080069CcnxPitEntryIncomingFaceContainer::type::iterator
70CcnxPitEntry::AddIncoming (Ptr<CcnxFace> face)
71{
72 std::pair<CcnxPitEntryIncomingFaceContainer::type::iterator,bool> ret =
73 m_incoming.insert (CcnxPitEntryIncomingFace (face));
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080074
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080075 NS_ASSERT_MSG (ret.second, "Something is wrong");
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080076
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080077 return ret.first;
78}
79
Alexander Afanasyev9d313d42011-11-25 13:36:15 -080080void
81CcnxPitEntry::RemoveIncoming (Ptr<CcnxFace> face)
82{
83 m_incoming.erase (face);
84}
85
86
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080087CcnxPitEntryOutgoingFaceContainer::type::iterator
88CcnxPitEntry::AddOutgoing (Ptr<CcnxFace> face)
89{
90 std::pair<CcnxPitEntryOutgoingFaceContainer::type::iterator,bool> ret =
91 m_outgoing.insert (CcnxPitEntryOutgoingFace (face));
92
Alexander Afanasyev0a61c342011-12-06 12:48:55 -080093 if (!ret.second)
94 { // outgoing face already exists
95 m_outgoing.modify (ret.first,
96 ll::bind (&CcnxPitEntryOutgoingFace::UpdateOnRetransmit, ll::_1));
97 }
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080098
99 return ret.first;
100}
101
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800102void
103CcnxPitEntry::RemoveAllReferencesToFace (Ptr<CcnxFace> face)
104{
105 CcnxPitEntryIncomingFaceContainer::type::iterator incoming =
106 m_incoming.find (face);
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700107
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800108 if (incoming != m_incoming.end ())
109 m_incoming.erase (incoming);
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700110
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800111 CcnxPitEntryOutgoingFaceContainer::type::iterator outgoing =
112 m_outgoing.find (face);
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700113
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800114 if (outgoing != m_outgoing.end ())
115 m_outgoing.erase (outgoing);
116}
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700117
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800118void
119CcnxPitEntry::SetWaitingInVain (CcnxPitEntryOutgoingFaceContainer::type::iterator face)
120{
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800121 NS_LOG_DEBUG (boost::cref (*face->m_face));
122
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800123 m_outgoing.modify (face,
124 (&ll::_1)->*&CcnxPitEntryOutgoingFace::m_waitingInVain = true);
125}
126
127bool
128CcnxPitEntry::AreAllOutgoingInVain () const
129{
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800130 NS_LOG_DEBUG (m_outgoing.size ());
131
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800132 bool inVain = true;
133 std::for_each (m_outgoing.begin (), m_outgoing.end (),
134 ll::var(inVain) &= (&ll::_1)->*&CcnxPitEntryOutgoingFace::m_waitingInVain);
135
136 NS_LOG_DEBUG ("inVain " << inVain);
137 return inVain;
138}
139
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800140bool
141CcnxPitEntry::AreTherePromisingOutgoingFacesExcept (Ptr<CcnxFace> face) const
142{
143 bool inVain = true;
144 std::for_each (m_outgoing.begin (), m_outgoing.end (),
145 ll::var(inVain) &=
146 ((&ll::_1)->*&CcnxPitEntryOutgoingFace::m_face == face ||
147 (&ll::_1)->*&CcnxPitEntryOutgoingFace::m_waitingInVain));
148
149 return !inVain;
150}
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800151
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800152void
153CcnxPitEntry::IncreaseAllowedRetxCount ()
154{
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800155 NS_LOG_ERROR (this);
Alexander Afanasyev9a517db2012-04-30 13:58:47 -0700156 if (Simulator::Now () - m_lastRetransmission >= MilliSeconds (100))
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700157 {
158 // cheat:
Alexander Afanasyev9a517db2012-04-30 13:58:47 -0700159 // don't allow retransmission faster than every 100ms
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700160 m_maxRetxCount++;
Alexander Afanasyev9a517db2012-04-30 13:58:47 -0700161 m_lastRetransmission = Simulator::Now ();
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700162 }
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800163}
164
Alexander Afanasyeva95b7392012-03-09 10:54:10 -0800165std::ostream& operator<< (std::ostream& os, const CcnxPitEntry &entry)
166{
167 os << "Prefix: " << *entry.m_prefix << "\n";
168 os << "In: ";
169 bool first = true;
170 BOOST_FOREACH (const CcnxPitEntryIncomingFace &face, entry.m_incoming)
171 {
172 if (!first)
173 os << ",";
174 else
175 first = false;
176
177 os << *face.m_face;
178 }
179
180 os << "\nOut: ";
181 first = true;
182 BOOST_FOREACH (const CcnxPitEntryOutgoingFace &face, entry.m_outgoing)
183 {
184 if (!first)
185 os << ",";
186 else
187 first = false;
188
189 os << *face.m_face;
190 }
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700191 os << "\nNonces: ";
192 first = true;
193 BOOST_FOREACH (uint32_t nonce, entry.m_seenNonces)
194 {
195 if (!first)
196 os << ",";
197 else
198 first = false;
199
200 os << nonce;
201 }
Alexander Afanasyeva95b7392012-03-09 10:54:10 -0800202
203 return os;
204}
205
206
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800207}