blob: ad81781a6adc2d3b8599cd23979c34b1fb50c7d4 [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 Afanasyev78057c32012-07-06 15:18:46 -070030#include <boost/foreach.hpp>
Alexander Afanasyev5a595072011-11-25 14:49:07 -080031namespace ll = boost::lambda;
32
33NS_LOG_COMPONENT_DEFINE ("CcnxPitEntry");
Alexander Afanasyev78cf0c92011-09-01 19:57:14 -070034
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070035namespace ns3
36{
37
Alexander Afanasyev11f7bb42012-07-09 17:06:30 -070038CcnxPitEntry::CcnxPitEntry (Ptr<const CcnxNameComponents> prefix,
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080039 const Time &expireTime,
Alexander Afanasyev11f7bb42012-07-09 17:06:30 -070040 Ptr<CcnxFibEntry> fibEntry)
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070041 : m_prefix (prefix)
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070042 , m_fibEntry (fibEntry)
Alexander Afanasyev0a61c342011-12-06 12:48:55 -080043 , m_expireTime (Simulator::Now () + expireTime)
Alexander Afanasyev0a61c342011-12-06 12:48:55 -080044 , m_maxRetxCount (0)
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -070045{
46}
47
Alexander Afanasyev0a61c342011-12-06 12:48:55 -080048void
Alexander Afanasyev1aeaf922012-04-23 13:48:09 -070049CcnxPitEntry::SetExpireTime (const Time &expireTime)
50{
Alexander Afanasyevff8c5d62012-04-25 15:14:51 -070051 NS_LOG_FUNCTION (expireTime.ToDouble (Time::S));
Alexander Afanasyev1aeaf922012-04-23 13:48:09 -070052 m_expireTime = expireTime;
53}
54
Alexander Afanasyev1aeaf922012-04-23 13:48:09 -070055void
Alexander Afanasyev0a61c342011-12-06 12:48:55 -080056CcnxPitEntry::UpdateLifetime (const Time &offsetTime)
57{
Alexander Afanasyevff8c5d62012-04-25 15:14:51 -070058 NS_LOG_FUNCTION (offsetTime.ToDouble (Time::S));
59
Alexander Afanasyev0a61c342011-12-06 12:48:55 -080060 Time newExpireTime = Simulator::Now () + offsetTime;
61 if (newExpireTime > m_expireTime)
62 m_expireTime = newExpireTime;
Alexander Afanasyev1aeaf922012-04-23 13:48:09 -070063
Alexander Afanasyevff8c5d62012-04-25 15:14:51 -070064 NS_LOG_INFO ("Updated lifetime to " << m_expireTime.ToDouble (Time::S));
Alexander Afanasyev0a61c342011-12-06 12:48:55 -080065}
66
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -070067CcnxPitEntry::in_iterator
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080068CcnxPitEntry::AddIncoming (Ptr<CcnxFace> face)
69{
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -070070 std::pair<in_iterator,bool> ret =
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080071 m_incoming.insert (CcnxPitEntryIncomingFace (face));
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080072
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080073 NS_ASSERT_MSG (ret.second, "Something is wrong");
Alexander Afanasyeva46844b2011-11-21 19:13:26 -080074
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080075 return ret.first;
76}
77
Alexander Afanasyev9d313d42011-11-25 13:36:15 -080078void
79CcnxPitEntry::RemoveIncoming (Ptr<CcnxFace> face)
80{
81 m_incoming.erase (face);
82}
83
84
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -070085CcnxPitEntry::out_iterator
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080086CcnxPitEntry::AddOutgoing (Ptr<CcnxFace> face)
87{
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -070088 std::pair<out_iterator,bool> ret =
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080089 m_outgoing.insert (CcnxPitEntryOutgoingFace (face));
90
Alexander Afanasyev0a61c342011-12-06 12:48:55 -080091 if (!ret.second)
92 { // outgoing face already exists
93 m_outgoing.modify (ret.first,
94 ll::bind (&CcnxPitEntryOutgoingFace::UpdateOnRetransmit, ll::_1));
95 }
Alexander Afanasyeva5bbe0e2011-11-22 17:28:39 -080096
97 return ret.first;
98}
99
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800100void
101CcnxPitEntry::RemoveAllReferencesToFace (Ptr<CcnxFace> face)
102{
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -0700103 in_iterator incoming = m_incoming.find (face);
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700104
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800105 if (incoming != m_incoming.end ())
106 m_incoming.erase (incoming);
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700107
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -0700108 out_iterator outgoing =
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800109 m_outgoing.find (face);
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700110
Alexander Afanasyev09c7deb2011-11-23 14:50:10 -0800111 if (outgoing != m_outgoing.end ())
112 m_outgoing.erase (outgoing);
113}
Alexander Afanasyeva98cdd22011-08-29 17:32:37 -0700114
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800115void
Alexander Afanasyevf034cbd2012-06-29 14:28:31 -0700116CcnxPitEntry::SetWaitingInVain (CcnxPitEntry::out_iterator face)
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800117{
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800118 NS_LOG_DEBUG (boost::cref (*face->m_face));
119
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800120 m_outgoing.modify (face,
121 (&ll::_1)->*&CcnxPitEntryOutgoingFace::m_waitingInVain = true);
122}
123
124bool
125CcnxPitEntry::AreAllOutgoingInVain () const
126{
Alexander Afanasyev23d2b542011-12-07 18:54:46 -0800127 NS_LOG_DEBUG (m_outgoing.size ());
128
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800129 bool inVain = true;
130 std::for_each (m_outgoing.begin (), m_outgoing.end (),
131 ll::var(inVain) &= (&ll::_1)->*&CcnxPitEntryOutgoingFace::m_waitingInVain);
132
133 NS_LOG_DEBUG ("inVain " << inVain);
134 return inVain;
135}
136
Alexander Afanasyeva7a2b8b2011-11-28 18:19:09 -0800137bool
138CcnxPitEntry::AreTherePromisingOutgoingFacesExcept (Ptr<CcnxFace> face) const
139{
140 bool inVain = true;
141 std::for_each (m_outgoing.begin (), m_outgoing.end (),
142 ll::var(inVain) &=
143 ((&ll::_1)->*&CcnxPitEntryOutgoingFace::m_face == face ||
144 (&ll::_1)->*&CcnxPitEntryOutgoingFace::m_waitingInVain));
145
146 return !inVain;
147}
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800148
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800149void
150CcnxPitEntry::IncreaseAllowedRetxCount ()
151{
Alexander Afanasyev120bf312011-12-19 01:24:47 -0800152 NS_LOG_ERROR (this);
Alexander Afanasyev9a517db2012-04-30 13:58:47 -0700153 if (Simulator::Now () - m_lastRetransmission >= MilliSeconds (100))
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700154 {
155 // cheat:
Alexander Afanasyev9a517db2012-04-30 13:58:47 -0700156 // don't allow retransmission faster than every 100ms
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700157 m_maxRetxCount++;
Alexander Afanasyev9a517db2012-04-30 13:58:47 -0700158 m_lastRetransmission = Simulator::Now ();
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700159 }
Alexander Afanasyev0a61c342011-12-06 12:48:55 -0800160}
161
Alexander Afanasyeva95b7392012-03-09 10:54:10 -0800162std::ostream& operator<< (std::ostream& os, const CcnxPitEntry &entry)
163{
164 os << "Prefix: " << *entry.m_prefix << "\n";
165 os << "In: ";
166 bool first = true;
167 BOOST_FOREACH (const CcnxPitEntryIncomingFace &face, entry.m_incoming)
168 {
169 if (!first)
170 os << ",";
171 else
172 first = false;
173
174 os << *face.m_face;
175 }
176
177 os << "\nOut: ";
178 first = true;
179 BOOST_FOREACH (const CcnxPitEntryOutgoingFace &face, entry.m_outgoing)
180 {
181 if (!first)
182 os << ",";
183 else
184 first = false;
185
186 os << *face.m_face;
187 }
Alexander Afanasyev7f3e49e2012-04-30 00:17:07 -0700188 os << "\nNonces: ";
189 first = true;
190 BOOST_FOREACH (uint32_t nonce, entry.m_seenNonces)
191 {
192 if (!first)
193 os << ",";
194 else
195 first = false;
196
197 os << nonce;
198 }
Alexander Afanasyeva95b7392012-03-09 10:54:10 -0800199
200 return os;
201}
202
203
Alexander Afanasyev5a595072011-11-25 14:49:07 -0800204}