Alexander Afanasyev | 5bee19e | 2013-07-10 14:33:57 -0700 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2012 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> |
Alexander Afanasyev | 5bee19e | 2013-07-10 14:33:57 -0700 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | #include "ndn-api-face.h" |
Alexander Afanasyev | a4e7428 | 2013-07-11 15:23:20 -0700 | [diff] [blame] | 22 | #include "detail/pending-interests-container.h" |
| 23 | #include "detail/registered-prefix-container.h" |
Alexander Afanasyev | 5bee19e | 2013-07-10 14:33:57 -0700 | [diff] [blame] | 24 | |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 25 | #include <ns3/random-variable.h> |
Alexander Afanasyev | 5bee19e | 2013-07-10 14:33:57 -0700 | [diff] [blame] | 26 | |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 27 | #include <ns3/ndn-l3-protocol.h> |
Alexander Afanasyev | 5bee19e | 2013-07-10 14:33:57 -0700 | [diff] [blame] | 28 | #include <ns3/ndn-interest.h> |
Alexander Afanasyev | 6eba36f | 2013-08-07 17:42:54 -0700 | [diff] [blame] | 29 | #include <ns3/ndn-data.h> |
Alexander Afanasyev | 5bee19e | 2013-07-10 14:33:57 -0700 | [diff] [blame] | 30 | #include <ns3/ndn-face.h> |
| 31 | #include <ns3/ndn-fib.h> |
Alexander Afanasyev | 5bee19e | 2013-07-10 14:33:57 -0700 | [diff] [blame] | 32 | |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 33 | #include <ns3/packet.h> |
| 34 | #include <ns3/log.h> |
Alexander Afanasyev | 5bee19e | 2013-07-10 14:33:57 -0700 | [diff] [blame] | 35 | |
| 36 | using namespace std; |
| 37 | using namespace boost; |
| 38 | using namespace ns3; |
| 39 | |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 40 | NS_LOG_COMPONENT_DEFINE ("ndn.ApiFace"); |
Alexander Afanasyev | 5bee19e | 2013-07-10 14:33:57 -0700 | [diff] [blame] | 41 | |
| 42 | namespace ns3 { |
| 43 | namespace ndn { |
| 44 | |
Alexander Afanasyev | a4e7428 | 2013-07-11 15:23:20 -0700 | [diff] [blame] | 45 | using namespace detail; |
| 46 | |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 47 | class ApiFacePriv |
| 48 | { |
| 49 | public: |
| 50 | ApiFacePriv () |
| 51 | : m_rand (0, std::numeric_limits<uint32_t>::max ()) |
| 52 | { |
| 53 | } |
Alexander Afanasyev | f0bda2f | 2013-08-12 16:38:13 -0700 | [diff] [blame] | 54 | |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 55 | ns3::UniformVariable m_rand; // nonce generator |
Alexander Afanasyev | 5bee19e | 2013-07-10 14:33:57 -0700 | [diff] [blame] | 56 | |
Alexander Afanasyev | a4e7428 | 2013-07-11 15:23:20 -0700 | [diff] [blame] | 57 | PendingInterestContainer m_pendingInterests; |
| 58 | RegisteredPrefixContainer m_expectedInterests; |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 59 | }; |
Alexander Afanasyev | 5bee19e | 2013-07-10 14:33:57 -0700 | [diff] [blame] | 60 | |
| 61 | |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 62 | ApiFace::ApiFace (Ptr<Node> node) |
| 63 | : Face (node) |
| 64 | , m_this (new ApiFacePriv ()) |
| 65 | { |
Alexander Afanasyev | f0bda2f | 2013-08-12 16:38:13 -0700 | [diff] [blame] | 66 | NS_LOG_FUNCTION (this << boost::cref (*this)); |
| 67 | |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 68 | NS_ASSERT_MSG (GetNode ()->GetObject<L3Protocol> () != 0, |
| 69 | "NDN stack should be installed on the node " << GetNode ()); |
Alexander Afanasyev | 5bee19e | 2013-07-10 14:33:57 -0700 | [diff] [blame] | 70 | |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 71 | GetNode ()->GetObject<L3Protocol> ()->AddFace (this); |
| 72 | this->SetUp (true); |
| 73 | this->SetFlags (APPLICATION); |
| 74 | } |
Alexander Afanasyev | 5bee19e | 2013-07-10 14:33:57 -0700 | [diff] [blame] | 75 | |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 76 | ApiFace::~ApiFace () |
| 77 | { |
Alexander Afanasyev | f0bda2f | 2013-08-12 16:38:13 -0700 | [diff] [blame] | 78 | NS_LOG_FUNCTION (this << boost::cref (*this)); |
| 79 | |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 80 | delete m_this; |
| 81 | } |
Alexander Afanasyev | 5bee19e | 2013-07-10 14:33:57 -0700 | [diff] [blame] | 82 | |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 83 | void |
Alexander Afanasyev | a4e7428 | 2013-07-11 15:23:20 -0700 | [diff] [blame] | 84 | ApiFace::Shutdown () |
| 85 | { |
Alexander Afanasyev | f0bda2f | 2013-08-12 16:38:13 -0700 | [diff] [blame] | 86 | NS_LOG_FUNCTION (this << boost::cref (*this)); |
Alexander Afanasyev | a4e7428 | 2013-07-11 15:23:20 -0700 | [diff] [blame] | 87 | |
| 88 | if (!IsUp ()) |
| 89 | { |
| 90 | return; |
| 91 | } |
Alexander Afanasyev | f0bda2f | 2013-08-12 16:38:13 -0700 | [diff] [blame] | 92 | |
Alexander Afanasyev | a4e7428 | 2013-07-11 15:23:20 -0700 | [diff] [blame] | 93 | this->SetUp (false); |
| 94 | |
| 95 | m_this->m_pendingInterests.clear (); |
| 96 | m_this->m_expectedInterests.clear (); |
| 97 | |
| 98 | GetNode ()->GetObject<L3Protocol> ()->RemoveFace (this); |
| 99 | } |
| 100 | |
| 101 | void |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 102 | ApiFace::ExpressInterest (Ptr<Interest> interest, |
| 103 | DataCallback onData, |
| 104 | TimeoutCallback onTimeout/* = MakeNullCallback< void, Ptr<Interest> > ()*/) |
| 105 | { |
| 106 | NS_LOG_INFO (">> I " << interest->GetName ()); |
Alexander Afanasyev | 5bee19e | 2013-07-10 14:33:57 -0700 | [diff] [blame] | 107 | |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 108 | if (interest->GetNonce () == 0) |
| 109 | { |
| 110 | interest->SetNonce (m_this->m_rand.GetValue ()); |
| 111 | } |
Alexander Afanasyev | f0bda2f | 2013-08-12 16:38:13 -0700 | [diff] [blame] | 112 | |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 113 | // Record the callback |
Alexander Afanasyev | baccf7d | 2013-08-09 12:38:44 -0700 | [diff] [blame] | 114 | bool needToActuallyExpressInterest = false; |
Alexander Afanasyev | a4e7428 | 2013-07-11 15:23:20 -0700 | [diff] [blame] | 115 | PendingInterestContainer::iterator entry = m_this->m_pendingInterests.find_exact (interest->GetName ()); |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 116 | if (entry == m_this->m_pendingInterests.end ()) |
| 117 | { |
Alexander Afanasyev | a4e7428 | 2013-07-11 15:23:20 -0700 | [diff] [blame] | 118 | pair<PendingInterestContainer::iterator, bool> status = |
| 119 | m_this->m_pendingInterests.insert (interest->GetName (), Create <PendingInterestEntry> (interest)); |
Alexander Afanasyev | 5bee19e | 2013-07-10 14:33:57 -0700 | [diff] [blame] | 120 | |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 121 | entry = status.first; |
Alexander Afanasyev | baccf7d | 2013-08-09 12:38:44 -0700 | [diff] [blame] | 122 | |
| 123 | needToActuallyExpressInterest = true; |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 124 | } |
Alexander Afanasyev | a4e7428 | 2013-07-11 15:23:20 -0700 | [diff] [blame] | 125 | entry->payload ()->AddCallbacks (onData, onTimeout); |
Alexander Afanasyev | 5bee19e | 2013-07-10 14:33:57 -0700 | [diff] [blame] | 126 | |
Alexander Afanasyev | baccf7d | 2013-08-09 12:38:44 -0700 | [diff] [blame] | 127 | if (needToActuallyExpressInterest) |
| 128 | { |
| 129 | Simulator::ScheduleNow (&Face::ReceiveInterest, this, interest); |
| 130 | } |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | void |
| 134 | ApiFace::SetInterestFilter (Ptr<const Name> prefix, InterestCallback onInterest) |
| 135 | { |
| 136 | NS_LOG_DEBUG ("== setInterestFilter " << *prefix << " (" << GetNode ()->GetId () << ")"); |
| 137 | |
Alexander Afanasyev | a4e7428 | 2013-07-11 15:23:20 -0700 | [diff] [blame] | 138 | RegisteredPrefixContainer::iterator entry = m_this->m_expectedInterests.find_exact (*prefix); |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 139 | if (entry == m_this->m_expectedInterests.end ()) |
| 140 | { |
Alexander Afanasyev | a4e7428 | 2013-07-11 15:23:20 -0700 | [diff] [blame] | 141 | pair<RegisteredPrefixContainer::iterator, bool> status = |
| 142 | m_this->m_expectedInterests.insert (*prefix, Create < RegisteredPrefixEntry > (prefix)); |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 143 | |
| 144 | entry = status.first; |
| 145 | } |
| 146 | |
| 147 | entry->payload ()->AddCallback (onInterest); |
| 148 | |
| 149 | // creating actual face |
| 150 | Ptr<ndn::Fib> fib = GetNode ()->GetObject<ndn::Fib> (); |
| 151 | Ptr<ndn::fib::Entry> fibEntry = fib->Add (prefix, this, 0); |
| 152 | fibEntry->UpdateStatus (this, ndn::fib::FaceMetric::NDN_FIB_GREEN); |
| 153 | } |
| 154 | |
| 155 | void |
| 156 | ApiFace::ClearInterestFilter (Ptr<const Name> prefix) |
| 157 | { |
Alexander Afanasyev | a4e7428 | 2013-07-11 15:23:20 -0700 | [diff] [blame] | 158 | RegisteredPrefixContainer::iterator entry = m_this->m_expectedInterests.find_exact (*prefix); |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 159 | if (entry == m_this->m_expectedInterests.end ()) |
| 160 | return; |
| 161 | |
| 162 | entry->payload ()->ClearCallback (); |
| 163 | m_this->m_expectedInterests.erase (entry); |
| 164 | } |
| 165 | |
| 166 | void |
Alexander Afanasyev | 772f51b | 2013-08-01 18:53:25 -0700 | [diff] [blame] | 167 | ApiFace::Put (Ptr<Data> data) |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 168 | { |
| 169 | NS_LOG_INFO (">> D " << data->GetName ()); |
Alexander Afanasyev | f0bda2f | 2013-08-12 16:38:13 -0700 | [diff] [blame] | 170 | |
Alexander Afanasyev | baccf7d | 2013-08-09 12:38:44 -0700 | [diff] [blame] | 171 | Simulator::ScheduleNow (&Face::ReceiveData, this, data); |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 172 | } |
Alexander Afanasyev | 5bee19e | 2013-07-10 14:33:57 -0700 | [diff] [blame] | 173 | |
Alexander Afanasyev | 5bee19e | 2013-07-10 14:33:57 -0700 | [diff] [blame] | 174 | |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 175 | /////////////////////////////////////////////// |
| 176 | // private stuff |
Alexander Afanasyev | 5bee19e | 2013-07-10 14:33:57 -0700 | [diff] [blame] | 177 | |
Alexander Afanasyev | 5bee19e | 2013-07-10 14:33:57 -0700 | [diff] [blame] | 178 | |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 179 | bool |
| 180 | ApiFace::SendInterest (Ptr<const Interest> interest) |
| 181 | { |
| 182 | NS_LOG_FUNCTION (this << interest); |
Alexander Afanasyev | 5bee19e | 2013-07-10 14:33:57 -0700 | [diff] [blame] | 183 | |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 184 | NS_LOG_DEBUG ("<< I " << interest->GetName ()); |
Alexander Afanasyev | f0bda2f | 2013-08-12 16:38:13 -0700 | [diff] [blame] | 185 | |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 186 | if (!IsUp ()) |
| 187 | { |
| 188 | return false; |
| 189 | } |
Alexander Afanasyev | 5bee19e | 2013-07-10 14:33:57 -0700 | [diff] [blame] | 190 | |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 191 | // the app cannot set several filters for the same prefix |
Alexander Afanasyev | a4e7428 | 2013-07-11 15:23:20 -0700 | [diff] [blame] | 192 | RegisteredPrefixContainer::iterator entry = m_this->m_expectedInterests.longest_prefix_match (interest->GetName ()); |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 193 | if (entry == m_this->m_expectedInterests.end ()) |
| 194 | { |
| 195 | return false; |
| 196 | } |
Alexander Afanasyev | e4795ae | 2013-07-11 20:01:31 -0700 | [diff] [blame] | 197 | |
| 198 | if (!entry->payload ()->m_callback.IsNull ()) |
| 199 | entry->payload ()->m_callback (entry->payload ()->GetPrefix (), interest); |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 200 | return true; |
| 201 | } |
Alexander Afanasyev | 5bee19e | 2013-07-10 14:33:57 -0700 | [diff] [blame] | 202 | |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 203 | bool |
Alexander Afanasyev | 772f51b | 2013-08-01 18:53:25 -0700 | [diff] [blame] | 204 | ApiFace::SendData (Ptr<const Data> data) |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 205 | { |
| 206 | // data has been send out from NDN stack towards the application |
| 207 | NS_LOG_DEBUG ("<< D " << data->GetName ()); |
Alexander Afanasyev | baccf7d | 2013-08-09 12:38:44 -0700 | [diff] [blame] | 208 | // NS_LOG_FUNCTION (this << data); |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 209 | |
| 210 | if (!IsUp ()) |
| 211 | { |
| 212 | return false; |
| 213 | } |
| 214 | |
Alexander Afanasyev | a4e7428 | 2013-07-11 15:23:20 -0700 | [diff] [blame] | 215 | PendingInterestContainer::iterator entry = m_this->m_pendingInterests.longest_prefix_match (data->GetName ()); |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 216 | if (entry == m_this->m_pendingInterests.end ()) |
| 217 | { |
| 218 | return false; |
| 219 | } |
| 220 | |
| 221 | while (entry != m_this->m_pendingInterests.end ()) |
| 222 | { |
Alexander Afanasyev | baccf7d | 2013-08-09 12:38:44 -0700 | [diff] [blame] | 223 | entry->payload ()->ProcessOnData (entry->payload ()->GetInterest (), data); |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 224 | m_this->m_pendingInterests.erase (entry); |
| 225 | |
| 226 | entry = m_this->m_pendingInterests.longest_prefix_match (data->GetName ()); |
| 227 | } |
| 228 | m_this->m_pendingInterests.erase (entry); |
Alexander Afanasyev | f0bda2f | 2013-08-12 16:38:13 -0700 | [diff] [blame] | 229 | |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 230 | return true; |
| 231 | } |
| 232 | |
| 233 | std::ostream& |
| 234 | ApiFace::Print (std::ostream &os) const |
| 235 | { |
| 236 | os << "dev=ApiFace(" << GetId () << ")"; |
| 237 | return os; |
| 238 | } |
| 239 | |
Alexander Afanasyev | 5bee19e | 2013-07-10 14:33:57 -0700 | [diff] [blame] | 240 | |
| 241 | } |
| 242 | } |