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 | 5bee19e | 2013-07-10 14:33:57 -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 | { |
| 66 | NS_ASSERT_MSG (GetNode ()->GetObject<L3Protocol> () != 0, |
| 67 | "NDN stack should be installed on the node " << GetNode ()); |
Alexander Afanasyev | 5bee19e | 2013-07-10 14:33:57 -0700 | [diff] [blame] | 68 | |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 69 | GetNode ()->GetObject<L3Protocol> ()->AddFace (this); |
| 70 | this->SetUp (true); |
| 71 | this->SetFlags (APPLICATION); |
| 72 | } |
Alexander Afanasyev | 5bee19e | 2013-07-10 14:33:57 -0700 | [diff] [blame] | 73 | |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 74 | ApiFace::~ApiFace () |
| 75 | { |
| 76 | delete m_this; |
| 77 | } |
Alexander Afanasyev | 5bee19e | 2013-07-10 14:33:57 -0700 | [diff] [blame] | 78 | |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 79 | void |
Alexander Afanasyev | a4e7428 | 2013-07-11 15:23:20 -0700 | [diff] [blame] | 80 | ApiFace::Shutdown () |
| 81 | { |
| 82 | NS_LOG_FUNCTION (this); |
| 83 | |
| 84 | if (!IsUp ()) |
| 85 | { |
| 86 | return; |
| 87 | } |
| 88 | |
| 89 | this->SetUp (false); |
| 90 | |
| 91 | m_this->m_pendingInterests.clear (); |
| 92 | m_this->m_expectedInterests.clear (); |
| 93 | |
| 94 | GetNode ()->GetObject<L3Protocol> ()->RemoveFace (this); |
| 95 | } |
| 96 | |
| 97 | void |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 98 | ApiFace::ExpressInterest (Ptr<Interest> interest, |
| 99 | DataCallback onData, |
| 100 | TimeoutCallback onTimeout/* = MakeNullCallback< void, Ptr<Interest> > ()*/) |
| 101 | { |
| 102 | NS_LOG_INFO (">> I " << interest->GetName ()); |
Alexander Afanasyev | 5bee19e | 2013-07-10 14:33:57 -0700 | [diff] [blame] | 103 | |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 104 | if (interest->GetNonce () == 0) |
| 105 | { |
| 106 | interest->SetNonce (m_this->m_rand.GetValue ()); |
| 107 | } |
Alexander Afanasyev | 5bee19e | 2013-07-10 14:33:57 -0700 | [diff] [blame] | 108 | |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 109 | // Record the callback |
Alexander Afanasyev | baccf7d | 2013-08-09 12:38:44 -0700 | [diff] [blame] | 110 | bool needToActuallyExpressInterest = false; |
Alexander Afanasyev | a4e7428 | 2013-07-11 15:23:20 -0700 | [diff] [blame] | 111 | PendingInterestContainer::iterator entry = m_this->m_pendingInterests.find_exact (interest->GetName ()); |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 112 | if (entry == m_this->m_pendingInterests.end ()) |
| 113 | { |
Alexander Afanasyev | a4e7428 | 2013-07-11 15:23:20 -0700 | [diff] [blame] | 114 | pair<PendingInterestContainer::iterator, bool> status = |
| 115 | m_this->m_pendingInterests.insert (interest->GetName (), Create <PendingInterestEntry> (interest)); |
Alexander Afanasyev | 5bee19e | 2013-07-10 14:33:57 -0700 | [diff] [blame] | 116 | |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 117 | entry = status.first; |
Alexander Afanasyev | baccf7d | 2013-08-09 12:38:44 -0700 | [diff] [blame] | 118 | |
| 119 | needToActuallyExpressInterest = true; |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 120 | } |
Alexander Afanasyev | a4e7428 | 2013-07-11 15:23:20 -0700 | [diff] [blame] | 121 | entry->payload ()->AddCallbacks (onData, onTimeout); |
Alexander Afanasyev | 5bee19e | 2013-07-10 14:33:57 -0700 | [diff] [blame] | 122 | |
Alexander Afanasyev | baccf7d | 2013-08-09 12:38:44 -0700 | [diff] [blame] | 123 | if (needToActuallyExpressInterest) |
| 124 | { |
| 125 | Simulator::ScheduleNow (&Face::ReceiveInterest, this, interest); |
| 126 | } |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | void |
| 130 | ApiFace::SetInterestFilter (Ptr<const Name> prefix, InterestCallback onInterest) |
| 131 | { |
| 132 | NS_LOG_DEBUG ("== setInterestFilter " << *prefix << " (" << GetNode ()->GetId () << ")"); |
| 133 | |
Alexander Afanasyev | a4e7428 | 2013-07-11 15:23:20 -0700 | [diff] [blame] | 134 | RegisteredPrefixContainer::iterator entry = m_this->m_expectedInterests.find_exact (*prefix); |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 135 | if (entry == m_this->m_expectedInterests.end ()) |
| 136 | { |
Alexander Afanasyev | a4e7428 | 2013-07-11 15:23:20 -0700 | [diff] [blame] | 137 | pair<RegisteredPrefixContainer::iterator, bool> status = |
| 138 | m_this->m_expectedInterests.insert (*prefix, Create < RegisteredPrefixEntry > (prefix)); |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 139 | |
| 140 | entry = status.first; |
| 141 | } |
| 142 | |
| 143 | entry->payload ()->AddCallback (onInterest); |
| 144 | |
| 145 | // creating actual face |
| 146 | Ptr<ndn::Fib> fib = GetNode ()->GetObject<ndn::Fib> (); |
| 147 | Ptr<ndn::fib::Entry> fibEntry = fib->Add (prefix, this, 0); |
| 148 | fibEntry->UpdateStatus (this, ndn::fib::FaceMetric::NDN_FIB_GREEN); |
| 149 | } |
| 150 | |
| 151 | void |
| 152 | ApiFace::ClearInterestFilter (Ptr<const Name> prefix) |
| 153 | { |
Alexander Afanasyev | a4e7428 | 2013-07-11 15:23:20 -0700 | [diff] [blame] | 154 | RegisteredPrefixContainer::iterator entry = m_this->m_expectedInterests.find_exact (*prefix); |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 155 | if (entry == m_this->m_expectedInterests.end ()) |
| 156 | return; |
| 157 | |
| 158 | entry->payload ()->ClearCallback (); |
| 159 | m_this->m_expectedInterests.erase (entry); |
| 160 | } |
| 161 | |
| 162 | void |
Alexander Afanasyev | 772f51b | 2013-08-01 18:53:25 -0700 | [diff] [blame] | 163 | ApiFace::Put (Ptr<Data> data) |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 164 | { |
| 165 | NS_LOG_INFO (">> D " << data->GetName ()); |
Alexander Afanasyev | 5bee19e | 2013-07-10 14:33:57 -0700 | [diff] [blame] | 166 | |
Alexander Afanasyev | baccf7d | 2013-08-09 12:38:44 -0700 | [diff] [blame] | 167 | Simulator::ScheduleNow (&Face::ReceiveData, this, data); |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 168 | } |
Alexander Afanasyev | 5bee19e | 2013-07-10 14:33:57 -0700 | [diff] [blame] | 169 | |
Alexander Afanasyev | 5bee19e | 2013-07-10 14:33:57 -0700 | [diff] [blame] | 170 | |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 171 | /////////////////////////////////////////////// |
| 172 | // private stuff |
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 | bool |
| 176 | ApiFace::SendInterest (Ptr<const Interest> interest) |
| 177 | { |
| 178 | NS_LOG_FUNCTION (this << interest); |
Alexander Afanasyev | 5bee19e | 2013-07-10 14:33:57 -0700 | [diff] [blame] | 179 | |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 180 | NS_LOG_DEBUG ("<< I " << interest->GetName ()); |
Alexander Afanasyev | 5bee19e | 2013-07-10 14:33:57 -0700 | [diff] [blame] | 181 | |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 182 | if (!IsUp ()) |
| 183 | { |
| 184 | return false; |
| 185 | } |
Alexander Afanasyev | 5bee19e | 2013-07-10 14:33:57 -0700 | [diff] [blame] | 186 | |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 187 | // the app cannot set several filters for the same prefix |
Alexander Afanasyev | a4e7428 | 2013-07-11 15:23:20 -0700 | [diff] [blame] | 188 | RegisteredPrefixContainer::iterator entry = m_this->m_expectedInterests.longest_prefix_match (interest->GetName ()); |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 189 | if (entry == m_this->m_expectedInterests.end ()) |
| 190 | { |
| 191 | return false; |
| 192 | } |
Alexander Afanasyev | e4795ae | 2013-07-11 20:01:31 -0700 | [diff] [blame] | 193 | |
| 194 | if (!entry->payload ()->m_callback.IsNull ()) |
| 195 | entry->payload ()->m_callback (entry->payload ()->GetPrefix (), interest); |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 196 | return true; |
| 197 | } |
Alexander Afanasyev | 5bee19e | 2013-07-10 14:33:57 -0700 | [diff] [blame] | 198 | |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 199 | bool |
Alexander Afanasyev | 772f51b | 2013-08-01 18:53:25 -0700 | [diff] [blame] | 200 | ApiFace::SendData (Ptr<const Data> data) |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 201 | { |
| 202 | // data has been send out from NDN stack towards the application |
| 203 | NS_LOG_DEBUG ("<< D " << data->GetName ()); |
Alexander Afanasyev | baccf7d | 2013-08-09 12:38:44 -0700 | [diff] [blame] | 204 | // NS_LOG_FUNCTION (this << data); |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 205 | |
| 206 | if (!IsUp ()) |
| 207 | { |
| 208 | return false; |
| 209 | } |
| 210 | |
Alexander Afanasyev | a4e7428 | 2013-07-11 15:23:20 -0700 | [diff] [blame] | 211 | PendingInterestContainer::iterator entry = m_this->m_pendingInterests.longest_prefix_match (data->GetName ()); |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 212 | if (entry == m_this->m_pendingInterests.end ()) |
| 213 | { |
| 214 | return false; |
| 215 | } |
| 216 | |
| 217 | while (entry != m_this->m_pendingInterests.end ()) |
| 218 | { |
Alexander Afanasyev | baccf7d | 2013-08-09 12:38:44 -0700 | [diff] [blame] | 219 | entry->payload ()->ProcessOnData (entry->payload ()->GetInterest (), data); |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 220 | m_this->m_pendingInterests.erase (entry); |
| 221 | |
| 222 | entry = m_this->m_pendingInterests.longest_prefix_match (data->GetName ()); |
| 223 | } |
| 224 | m_this->m_pendingInterests.erase (entry); |
| 225 | |
| 226 | return true; |
| 227 | } |
| 228 | |
| 229 | std::ostream& |
| 230 | ApiFace::Print (std::ostream &os) const |
| 231 | { |
| 232 | os << "dev=ApiFace(" << GetId () << ")"; |
| 233 | return os; |
| 234 | } |
| 235 | |
Alexander Afanasyev | 5bee19e | 2013-07-10 14:33:57 -0700 | [diff] [blame] | 236 | |
| 237 | } |
| 238 | } |