Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2013 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: Zhenkai Zhu <zhenkai@cs.ucla.edu> |
| 19 | * Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 20 | */ |
| 21 | |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 22 | #include "ndnx-tunnel.h" |
| 23 | #include "ndnx-pco.h" |
Zhenkai Zhu | 0f05412 | 2012-12-25 22:22:50 -0800 | [diff] [blame] | 24 | |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 25 | namespace Ndnx |
Zhenkai Zhu | 0f05412 | 2012-12-25 22:22:50 -0800 | [diff] [blame] | 26 | { |
| 27 | |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 28 | NdnxTunnel::NdnxTunnel() |
| 29 | : NdnxWrapper() |
Alexander Afanasyev | 66f4c49 | 2013-01-20 23:32:50 -0800 | [diff] [blame] | 30 | , m_localPrefix("/") |
Zhenkai Zhu | 0f05412 | 2012-12-25 22:22:50 -0800 | [diff] [blame] | 31 | { |
Zhenkai Zhu | 19f81de | 2013-01-04 22:27:47 -0800 | [diff] [blame] | 32 | refreshLocalPrefix(); |
Zhenkai Zhu | 0f05412 | 2012-12-25 22:22:50 -0800 | [diff] [blame] | 33 | } |
| 34 | |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 35 | NdnxTunnel::~NdnxTunnel() |
Zhenkai Zhu | 0f05412 | 2012-12-25 22:22:50 -0800 | [diff] [blame] | 36 | { |
| 37 | } |
| 38 | |
Zhenkai Zhu | d492431 | 2012-12-28 11:35:12 -0800 | [diff] [blame] | 39 | void |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 40 | NdnxTunnel::refreshLocalPrefix() |
Zhenkai Zhu | 0f05412 | 2012-12-25 22:22:50 -0800 | [diff] [blame] | 41 | { |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 42 | Name newPrefix = getLocalPrefix(); |
| 43 | if (!newPrefix.toString().empty() && m_localPrefix != newPrefix) |
Zhenkai Zhu | 0f05412 | 2012-12-25 22:22:50 -0800 | [diff] [blame] | 44 | { |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 45 | NdnxWrapper::clearInterestFilter(m_localPrefix); |
| 46 | NdnxWrapper::setInterestFilter(newPrefix, bind(&NdnxTunnel::handleTunneledInterest, this, _1)); |
Zhenkai Zhu | 0f05412 | 2012-12-25 22:22:50 -0800 | [diff] [blame] | 47 | m_localPrefix = newPrefix; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | int |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 52 | NdnxTunnel::sendInterest (const Name &interest, const Closure &closure, const Selectors &selectors) |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 53 | { |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 54 | Name tunneledInterest = queryRoutableName(interest); |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 55 | |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 56 | NdnxWrapper::sendInterest(tunneledInterest, |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 57 | TunnelClosure(closure, *this, interest), |
| 58 | selectors); |
Alexander Afanasyev | dcfa963 | 2013-01-07 16:38:19 -0800 | [diff] [blame] | 59 | |
| 60 | return 0; |
Zhenkai Zhu | d492431 | 2012-12-28 11:35:12 -0800 | [diff] [blame] | 61 | } |
| 62 | |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 63 | void |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 64 | NdnxTunnel::handleTunneledData(const Name &name, const Bytes &tunneledData, const Closure::DataCallback &originalDataCallback) |
Zhenkai Zhu | d492431 | 2012-12-28 11:35:12 -0800 | [diff] [blame] | 65 | { |
| 66 | ParsedContentObject pco(tunneledData); |
| 67 | originalDataCallback(pco.name(), pco.content()); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | int |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 71 | NdnxTunnel::publishData(const Name &name, const unsigned char *buf, size_t len, int freshness) |
Zhenkai Zhu | 0f05412 | 2012-12-25 22:22:50 -0800 | [diff] [blame] | 72 | { |
Zhenkai Zhu | d492431 | 2012-12-28 11:35:12 -0800 | [diff] [blame] | 73 | Bytes content = createContentObject(name, buf, len, freshness); |
| 74 | storeContentObject(name, content); |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 75 | |
Zhenkai Zhu | f118526 | 2012-12-29 17:06:00 -0800 | [diff] [blame] | 76 | return publishContentObject(name, content, freshness); |
| 77 | } |
Zhenkai Zhu | 0f05412 | 2012-12-25 22:22:50 -0800 | [diff] [blame] | 78 | |
Zhenkai Zhu | f118526 | 2012-12-29 17:06:00 -0800 | [diff] [blame] | 79 | int |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 80 | NdnxTunnel::publishContentObject(const Name &name, const Bytes &contentObject, int freshness) |
Zhenkai Zhu | f118526 | 2012-12-29 17:06:00 -0800 | [diff] [blame] | 81 | { |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 82 | Name tunneledName = m_localPrefix + name; |
Zhenkai Zhu | f118526 | 2012-12-29 17:06:00 -0800 | [diff] [blame] | 83 | Bytes tunneledCo = createContentObject(tunneledName, head(contentObject), contentObject.size(), freshness); |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 84 | return putToNdnd(tunneledCo); |
Zhenkai Zhu | 0f05412 | 2012-12-25 22:22:50 -0800 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | void |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 88 | NdnxTunnel::handleTunneledInterest(const Name &tunneledInterest) |
Zhenkai Zhu | 0f05412 | 2012-12-25 22:22:50 -0800 | [diff] [blame] | 89 | { |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 90 | // The interest must have m_localPrefix as a prefix (component-wise), otherwise ndnd would not deliver it to us |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 91 | Name interest = tunneledInterest.getPartialName(m_localPrefix.size()); |
Zhenkai Zhu | 0f05412 | 2012-12-25 22:22:50 -0800 | [diff] [blame] | 92 | |
Zhenkai Zhu | 4865bd6 | 2013-01-17 12:08:29 -0800 | [diff] [blame] | 93 | ReadLock lock(m_ritLock); |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 94 | |
Zhenkai Zhu | 0f05412 | 2012-12-25 22:22:50 -0800 | [diff] [blame] | 95 | // This is linear scan, but should be acceptable under the assumption that the caller wouldn't be listening to a lot prefixes (as of now, most app listen to one or two prefixes) |
| 96 | for (RitIter it = m_rit.begin(); it != m_rit.end(); it++) |
| 97 | { |
| 98 | // evoke callback for any prefix that is the prefix of the interest |
Zhenkai Zhu | d492431 | 2012-12-28 11:35:12 -0800 | [diff] [blame] | 99 | if (isPrefix(it->first, interest)) |
Zhenkai Zhu | 0f05412 | 2012-12-25 22:22:50 -0800 | [diff] [blame] | 100 | { |
Zhenkai Zhu | d492431 | 2012-12-28 11:35:12 -0800 | [diff] [blame] | 101 | (it->second)(interest); |
Zhenkai Zhu | 0f05412 | 2012-12-25 22:22:50 -0800 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | bool |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 107 | NdnxTunnel::isPrefix(const Name &prefix, const Name &name) |
Zhenkai Zhu | 0f05412 | 2012-12-25 22:22:50 -0800 | [diff] [blame] | 108 | { |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 109 | if (prefix.size() > name.size()) |
Zhenkai Zhu | 0f05412 | 2012-12-25 22:22:50 -0800 | [diff] [blame] | 110 | { |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 111 | return false; |
| 112 | } |
| 113 | |
| 114 | int size = prefix.size(); |
| 115 | for (int i = 0; i < size; i++) |
| 116 | { |
| 117 | if (prefix.getCompAsString(i) != name.getCompAsString(i)) |
Zhenkai Zhu | 0f05412 | 2012-12-25 22:22:50 -0800 | [diff] [blame] | 118 | { |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 119 | return false; |
Zhenkai Zhu | 0f05412 | 2012-12-25 22:22:50 -0800 | [diff] [blame] | 120 | } |
| 121 | } |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 122 | |
| 123 | return true; |
Zhenkai Zhu | 0f05412 | 2012-12-25 22:22:50 -0800 | [diff] [blame] | 124 | } |
| 125 | |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 126 | int |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 127 | NdnxTunnel::setInterestFilter(const Name &prefix, const InterestCallback &interestCallback) |
Zhenkai Zhu | 0f05412 | 2012-12-25 22:22:50 -0800 | [diff] [blame] | 128 | { |
Zhenkai Zhu | 4865bd6 | 2013-01-17 12:08:29 -0800 | [diff] [blame] | 129 | WriteLock lock(m_ritLock); |
Zhenkai Zhu | d492431 | 2012-12-28 11:35:12 -0800 | [diff] [blame] | 130 | // make sure copy constructor for boost::function works properly |
| 131 | m_rit.insert(make_pair(prefix, interestCallback)); |
Zhenkai Zhu | 0f05412 | 2012-12-25 22:22:50 -0800 | [diff] [blame] | 132 | return 0; |
| 133 | } |
| 134 | |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 135 | void |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 136 | NdnxTunnel::clearInterestFilter(const Name &prefix) |
Zhenkai Zhu | 0f05412 | 2012-12-25 22:22:50 -0800 | [diff] [blame] | 137 | { |
Zhenkai Zhu | 4865bd6 | 2013-01-17 12:08:29 -0800 | [diff] [blame] | 138 | WriteLock lock(m_ritLock); |
Zhenkai Zhu | 0f05412 | 2012-12-25 22:22:50 -0800 | [diff] [blame] | 139 | // remove all |
| 140 | m_rit.erase(prefix); |
| 141 | } |
| 142 | |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 143 | TunnelClosure::TunnelClosure(const DataCallback &dataCallback, NdnxTunnel &tunnel, |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 144 | const Name &originalInterest, const TimeoutCallback &timeoutCallback) |
| 145 | : Closure(dataCallback, timeoutCallback) |
| 146 | , m_tunnel(tunnel) |
| 147 | , m_originalInterest(originalInterest) |
Zhenkai Zhu | d492431 | 2012-12-28 11:35:12 -0800 | [diff] [blame] | 148 | { |
Zhenkai Zhu | 0f05412 | 2012-12-25 22:22:50 -0800 | [diff] [blame] | 149 | } |
| 150 | |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 151 | TunnelClosure::TunnelClosure(const Closure &closure, NdnxTunnel &tunnel, const Name &originalInterest) |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 152 | : Closure(closure) |
| 153 | , m_tunnel(tunnel) |
Zhenkai Zhu | 974c5a6 | 2012-12-28 14:15:30 -0800 | [diff] [blame] | 154 | { |
| 155 | } |
| 156 | |
Zhenkai Zhu | 19f81de | 2013-01-04 22:27:47 -0800 | [diff] [blame] | 157 | Closure * |
| 158 | TunnelClosure::dup() const |
| 159 | { |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 160 | return new TunnelClosure (*this); |
Zhenkai Zhu | 19f81de | 2013-01-04 22:27:47 -0800 | [diff] [blame] | 161 | } |
| 162 | |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 163 | void |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 164 | TunnelClosure::runDataCallback(const Name &name, const Bytes &content) |
Zhenkai Zhu | d492431 | 2012-12-28 11:35:12 -0800 | [diff] [blame] | 165 | { |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 166 | m_tunnel.handleTunneledData(name, content, m_dataCallback); |
Zhenkai Zhu | d492431 | 2012-12-28 11:35:12 -0800 | [diff] [blame] | 167 | } |
| 168 | |
Zhenkai Zhu | 974c5a6 | 2012-12-28 14:15:30 -0800 | [diff] [blame] | 169 | Closure::TimeoutCallbackReturnValue |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 170 | TunnelClosure::runTimeoutCallback(const Name &interest) |
Zhenkai Zhu | d492431 | 2012-12-28 11:35:12 -0800 | [diff] [blame] | 171 | { |
Zhenkai Zhu | 974c5a6 | 2012-12-28 14:15:30 -0800 | [diff] [blame] | 172 | return Closure::runTimeoutCallback(m_originalInterest); |
Zhenkai Zhu | d492431 | 2012-12-28 11:35:12 -0800 | [diff] [blame] | 173 | } |
| 174 | |
Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [diff] [blame] | 175 | } // Ndnx |