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 | a4e7428 | 2013-07-11 15:23:20 -0700 | [diff] [blame] | 110 | PendingInterestContainer::iterator entry = m_this->m_pendingInterests.find_exact (interest->GetName ()); |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 111 | if (entry == m_this->m_pendingInterests.end ()) |
| 112 | { |
Alexander Afanasyev | a4e7428 | 2013-07-11 15:23:20 -0700 | [diff] [blame] | 113 | pair<PendingInterestContainer::iterator, bool> status = |
| 114 | m_this->m_pendingInterests.insert (interest->GetName (), Create <PendingInterestEntry> (interest)); |
Alexander Afanasyev | 5bee19e | 2013-07-10 14:33:57 -0700 | [diff] [blame] | 115 | |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 116 | entry = status.first; |
| 117 | } |
Alexander Afanasyev | a4e7428 | 2013-07-11 15:23:20 -0700 | [diff] [blame] | 118 | entry->payload ()->AddCallbacks (onData, onTimeout); |
Alexander Afanasyev | 5bee19e | 2013-07-10 14:33:57 -0700 | [diff] [blame] | 119 | |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 120 | ReceiveInterest (interest); |
| 121 | } |
| 122 | |
| 123 | void |
| 124 | ApiFace::SetInterestFilter (Ptr<const Name> prefix, InterestCallback onInterest) |
| 125 | { |
| 126 | NS_LOG_DEBUG ("== setInterestFilter " << *prefix << " (" << GetNode ()->GetId () << ")"); |
| 127 | |
Alexander Afanasyev | a4e7428 | 2013-07-11 15:23:20 -0700 | [diff] [blame] | 128 | RegisteredPrefixContainer::iterator entry = m_this->m_expectedInterests.find_exact (*prefix); |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 129 | if (entry == m_this->m_expectedInterests.end ()) |
| 130 | { |
Alexander Afanasyev | a4e7428 | 2013-07-11 15:23:20 -0700 | [diff] [blame] | 131 | pair<RegisteredPrefixContainer::iterator, bool> status = |
| 132 | m_this->m_expectedInterests.insert (*prefix, Create < RegisteredPrefixEntry > (prefix)); |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 133 | |
| 134 | entry = status.first; |
| 135 | } |
| 136 | |
| 137 | entry->payload ()->AddCallback (onInterest); |
| 138 | |
| 139 | // creating actual face |
| 140 | Ptr<ndn::Fib> fib = GetNode ()->GetObject<ndn::Fib> (); |
| 141 | Ptr<ndn::fib::Entry> fibEntry = fib->Add (prefix, this, 0); |
| 142 | fibEntry->UpdateStatus (this, ndn::fib::FaceMetric::NDN_FIB_GREEN); |
| 143 | } |
| 144 | |
| 145 | void |
| 146 | ApiFace::ClearInterestFilter (Ptr<const Name> prefix) |
| 147 | { |
Alexander Afanasyev | a4e7428 | 2013-07-11 15:23:20 -0700 | [diff] [blame] | 148 | RegisteredPrefixContainer::iterator entry = m_this->m_expectedInterests.find_exact (*prefix); |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 149 | if (entry == m_this->m_expectedInterests.end ()) |
| 150 | return; |
| 151 | |
| 152 | entry->payload ()->ClearCallback (); |
| 153 | m_this->m_expectedInterests.erase (entry); |
| 154 | } |
| 155 | |
| 156 | void |
Alexander Afanasyev | 772f51b | 2013-08-01 18:53:25 -0700 | [diff] [blame] | 157 | ApiFace::Put (Ptr<Data> data) |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 158 | { |
| 159 | NS_LOG_INFO (">> D " << data->GetName ()); |
Alexander Afanasyev | 5bee19e | 2013-07-10 14:33:57 -0700 | [diff] [blame] | 160 | |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 161 | ReceiveData (data); |
| 162 | } |
Alexander Afanasyev | 5bee19e | 2013-07-10 14:33:57 -0700 | [diff] [blame] | 163 | |
Alexander Afanasyev | 5bee19e | 2013-07-10 14:33:57 -0700 | [diff] [blame] | 164 | |
Alexander Afanasyev | 5bee19e | 2013-07-10 14:33:57 -0700 | [diff] [blame] | 165 | |
Alexander Afanasyev | 5bee19e | 2013-07-10 14:33:57 -0700 | [diff] [blame] | 166 | |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 167 | /////////////////////////////////////////////// |
| 168 | // private stuff |
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 | bool |
| 172 | ApiFace::SendInterest (Ptr<const Interest> interest) |
| 173 | { |
| 174 | NS_LOG_FUNCTION (this << interest); |
Alexander Afanasyev | 5bee19e | 2013-07-10 14:33:57 -0700 | [diff] [blame] | 175 | |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 176 | NS_LOG_DEBUG ("<< I " << interest->GetName ()); |
Alexander Afanasyev | 5bee19e | 2013-07-10 14:33:57 -0700 | [diff] [blame] | 177 | |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 178 | if (!IsUp ()) |
| 179 | { |
| 180 | return false; |
| 181 | } |
Alexander Afanasyev | 5bee19e | 2013-07-10 14:33:57 -0700 | [diff] [blame] | 182 | |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 183 | // the app cannot set several filters for the same prefix |
Alexander Afanasyev | a4e7428 | 2013-07-11 15:23:20 -0700 | [diff] [blame] | 184 | RegisteredPrefixContainer::iterator entry = m_this->m_expectedInterests.longest_prefix_match (interest->GetName ()); |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 185 | if (entry == m_this->m_expectedInterests.end ()) |
| 186 | { |
| 187 | return false; |
| 188 | } |
Alexander Afanasyev | e4795ae | 2013-07-11 20:01:31 -0700 | [diff] [blame] | 189 | |
| 190 | if (!entry->payload ()->m_callback.IsNull ()) |
| 191 | entry->payload ()->m_callback (entry->payload ()->GetPrefix (), interest); |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 192 | return true; |
| 193 | } |
Alexander Afanasyev | 5bee19e | 2013-07-10 14:33:57 -0700 | [diff] [blame] | 194 | |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 195 | bool |
Alexander Afanasyev | 772f51b | 2013-08-01 18:53:25 -0700 | [diff] [blame] | 196 | ApiFace::SendData (Ptr<const Data> data) |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 197 | { |
| 198 | // data has been send out from NDN stack towards the application |
| 199 | NS_LOG_DEBUG ("<< D " << data->GetName ()); |
Alexander Afanasyev | 5bee19e | 2013-07-10 14:33:57 -0700 | [diff] [blame] | 200 | |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 201 | NS_LOG_FUNCTION (this << data); |
| 202 | |
| 203 | if (!IsUp ()) |
| 204 | { |
| 205 | return false; |
| 206 | } |
| 207 | |
Alexander Afanasyev | a4e7428 | 2013-07-11 15:23:20 -0700 | [diff] [blame] | 208 | PendingInterestContainer::iterator entry = m_this->m_pendingInterests.longest_prefix_match (data->GetName ()); |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 209 | if (entry == m_this->m_pendingInterests.end ()) |
| 210 | { |
| 211 | return false; |
| 212 | } |
| 213 | |
| 214 | while (entry != m_this->m_pendingInterests.end ()) |
| 215 | { |
Alexander Afanasyev | e4795ae | 2013-07-11 20:01:31 -0700 | [diff] [blame] | 216 | if (!entry->payload ()->m_dataCallback.IsNull ()) |
| 217 | entry->payload ()->m_dataCallback (entry->payload ()->GetInterest (), data); |
Alexander Afanasyev | 7960606 | 2013-07-11 00:57:28 -0700 | [diff] [blame] | 218 | m_this->m_pendingInterests.erase (entry); |
| 219 | |
| 220 | entry = m_this->m_pendingInterests.longest_prefix_match (data->GetName ()); |
| 221 | } |
| 222 | m_this->m_pendingInterests.erase (entry); |
| 223 | |
| 224 | return true; |
| 225 | } |
| 226 | |
| 227 | std::ostream& |
| 228 | ApiFace::Print (std::ostream &os) const |
| 229 | { |
| 230 | os << "dev=ApiFace(" << GetId () << ")"; |
| 231 | return os; |
| 232 | } |
| 233 | |
Alexander Afanasyev | 5bee19e | 2013-07-10 14:33:57 -0700 | [diff] [blame] | 234 | |
| 235 | } |
| 236 | } |