Zhenkai Zhu | 0f05412 | 2012-12-25 22:22:50 -0800 | [diff] [blame] | 1 | #include "ccnx-tunnel.h" |
Zhenkai Zhu | d492431 | 2012-12-28 11:35:12 -0800 | [diff] [blame] | 2 | #include "ccnx-pco.h" |
Zhenkai Zhu | 0f05412 | 2012-12-25 22:22:50 -0800 | [diff] [blame] | 3 | |
| 4 | namespace Ccnx |
| 5 | { |
| 6 | |
| 7 | CcnxTunnel::CcnxTunnel() |
| 8 | : CcnxWrapper() |
| 9 | , m_localPrefix("/") |
| 10 | { |
| 11 | refreshLocalPrefix(); |
| 12 | } |
| 13 | |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 14 | CcnxTunnel::~CcnxTunnel() |
Zhenkai Zhu | 0f05412 | 2012-12-25 22:22:50 -0800 | [diff] [blame] | 15 | { |
| 16 | } |
| 17 | |
Zhenkai Zhu | d492431 | 2012-12-28 11:35:12 -0800 | [diff] [blame] | 18 | void |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 19 | CcnxTunnel::refreshLocalPrefix() |
Zhenkai Zhu | 0f05412 | 2012-12-25 22:22:50 -0800 | [diff] [blame] | 20 | { |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame^] | 21 | Name newPrefix = getLocalPrefix(); |
| 22 | if (!newPrefix.toString().empty() && m_localPrefix != newPrefix) |
Zhenkai Zhu | 0f05412 | 2012-12-25 22:22:50 -0800 | [diff] [blame] | 23 | { |
| 24 | CcnxWrapper::clearInterestFilter(m_localPrefix); |
| 25 | CcnxWrapper::setInterestFilter(newPrefix, bind(&CcnxTunnel::handleTunneledInterest, this, _1)); |
| 26 | m_localPrefix = newPrefix; |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | int |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame^] | 31 | CcnxTunnel::sendInterest (const Name &interest, Closure *closure, const Selectors &selectors) |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 32 | { |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame^] | 33 | Name tunneledInterest = queryRoutableName(interest); |
| 34 | Closure *cp = new TunnelClosure(closure, this, interest); |
| 35 | sendInterest(tunneledInterest, cp, selectors); |
Zhenkai Zhu | d492431 | 2012-12-28 11:35:12 -0800 | [diff] [blame] | 36 | } |
| 37 | |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 38 | void |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame^] | 39 | CcnxTunnel::handleTunneledData(const Name &name, const Bytes &tunneledData, const Closure::DataCallback &originalDataCallback) |
Zhenkai Zhu | d492431 | 2012-12-28 11:35:12 -0800 | [diff] [blame] | 40 | { |
| 41 | ParsedContentObject pco(tunneledData); |
| 42 | originalDataCallback(pco.name(), pco.content()); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | int |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame^] | 46 | CcnxTunnel::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] | 47 | { |
Zhenkai Zhu | d492431 | 2012-12-28 11:35:12 -0800 | [diff] [blame] | 48 | Bytes content = createContentObject(name, buf, len, freshness); |
| 49 | storeContentObject(name, content); |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 50 | |
Zhenkai Zhu | f118526 | 2012-12-29 17:06:00 -0800 | [diff] [blame] | 51 | return publishContentObject(name, content, freshness); |
| 52 | } |
Zhenkai Zhu | 0f05412 | 2012-12-25 22:22:50 -0800 | [diff] [blame] | 53 | |
Zhenkai Zhu | f118526 | 2012-12-29 17:06:00 -0800 | [diff] [blame] | 54 | int |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame^] | 55 | CcnxTunnel::publishContentObject(const Name &name, const Bytes &contentObject, int freshness) |
Zhenkai Zhu | f118526 | 2012-12-29 17:06:00 -0800 | [diff] [blame] | 56 | { |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame^] | 57 | Name tunneledName = m_localPrefix + name; |
Zhenkai Zhu | f118526 | 2012-12-29 17:06:00 -0800 | [diff] [blame] | 58 | Bytes tunneledCo = createContentObject(tunneledName, head(contentObject), contentObject.size(), freshness); |
Zhenkai Zhu | 0f05412 | 2012-12-25 22:22:50 -0800 | [diff] [blame] | 59 | return putToCcnd(tunneledCo); |
| 60 | } |
| 61 | |
| 62 | void |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame^] | 63 | CcnxTunnel::handleTunneledInterest(const Name &tunneledInterest) |
Zhenkai Zhu | 0f05412 | 2012-12-25 22:22:50 -0800 | [diff] [blame] | 64 | { |
| 65 | // The interest must have m_localPrefix as a prefix (component-wise), otherwise ccnd would not deliver it to us |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame^] | 66 | Name interest = tunneledInterest.getPartialName(m_localPrefix.size()); |
Zhenkai Zhu | 0f05412 | 2012-12-25 22:22:50 -0800 | [diff] [blame] | 67 | |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 68 | ReadLock(m_ritLock); |
| 69 | |
Zhenkai Zhu | 0f05412 | 2012-12-25 22:22:50 -0800 | [diff] [blame] | 70 | // 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) |
| 71 | for (RitIter it = m_rit.begin(); it != m_rit.end(); it++) |
| 72 | { |
| 73 | // evoke callback for any prefix that is the prefix of the interest |
Zhenkai Zhu | d492431 | 2012-12-28 11:35:12 -0800 | [diff] [blame] | 74 | if (isPrefix(it->first, interest)) |
Zhenkai Zhu | 0f05412 | 2012-12-25 22:22:50 -0800 | [diff] [blame] | 75 | { |
Zhenkai Zhu | d492431 | 2012-12-28 11:35:12 -0800 | [diff] [blame] | 76 | (it->second)(interest); |
Zhenkai Zhu | 0f05412 | 2012-12-25 22:22:50 -0800 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | bool |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame^] | 82 | CcnxTunnel::isPrefix(const Name &prefix, const Name &name) |
Zhenkai Zhu | 0f05412 | 2012-12-25 22:22:50 -0800 | [diff] [blame] | 83 | { |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame^] | 84 | if (prefix.size() > name.size()) |
Zhenkai Zhu | 0f05412 | 2012-12-25 22:22:50 -0800 | [diff] [blame] | 85 | { |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame^] | 86 | return false; |
| 87 | } |
| 88 | |
| 89 | int size = prefix.size(); |
| 90 | for (int i = 0; i < size; i++) |
| 91 | { |
| 92 | if (prefix.getCompAsString(i) != name.getCompAsString(i)) |
Zhenkai Zhu | 0f05412 | 2012-12-25 22:22:50 -0800 | [diff] [blame] | 93 | { |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame^] | 94 | return false; |
Zhenkai Zhu | 0f05412 | 2012-12-25 22:22:50 -0800 | [diff] [blame] | 95 | } |
| 96 | } |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame^] | 97 | |
| 98 | return true; |
Zhenkai Zhu | 0f05412 | 2012-12-25 22:22:50 -0800 | [diff] [blame] | 99 | } |
| 100 | |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 101 | int |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame^] | 102 | CcnxTunnel::setInterestFilter(const Name &prefix, const InterestCallback &interestCallback) |
Zhenkai Zhu | 0f05412 | 2012-12-25 22:22:50 -0800 | [diff] [blame] | 103 | { |
| 104 | WriteLock(m_ritLock); |
Zhenkai Zhu | d492431 | 2012-12-28 11:35:12 -0800 | [diff] [blame] | 105 | // make sure copy constructor for boost::function works properly |
| 106 | m_rit.insert(make_pair(prefix, interestCallback)); |
Zhenkai Zhu | 0f05412 | 2012-12-25 22:22:50 -0800 | [diff] [blame] | 107 | return 0; |
| 108 | } |
| 109 | |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 110 | void |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame^] | 111 | CcnxTunnel::clearInterestFilter(const Name &prefix) |
Zhenkai Zhu | 0f05412 | 2012-12-25 22:22:50 -0800 | [diff] [blame] | 112 | { |
| 113 | WriteLock(m_ritLock); |
| 114 | // remove all |
| 115 | m_rit.erase(prefix); |
| 116 | } |
| 117 | |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame^] | 118 | TunnelClosure::TunnelClosure(int retry, const DataCallback &dataCallback, const TimeoutCallback &timeoutCallback, CcnxTunnel *tunnel, const Name &originalInterest) |
Zhenkai Zhu | 974c5a6 | 2012-12-28 14:15:30 -0800 | [diff] [blame] | 119 | : Closure(retry, dataCallback, timeoutCallback) |
Zhenkai Zhu | d492431 | 2012-12-28 11:35:12 -0800 | [diff] [blame] | 120 | , m_tunnel(tunnel) |
| 121 | , m_originalInterest(originalInterest) |
| 122 | { |
Zhenkai Zhu | 0f05412 | 2012-12-25 22:22:50 -0800 | [diff] [blame] | 123 | } |
| 124 | |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame^] | 125 | TunnelClosure::TunnelClosure(const Closure *closure, CcnxTunnel *tunnel, const Name &originalInterest) |
Zhenkai Zhu | 974c5a6 | 2012-12-28 14:15:30 -0800 | [diff] [blame] | 126 | : Closure(*closure) |
| 127 | , m_tunnel(tunnel) |
| 128 | { |
| 129 | } |
| 130 | |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 131 | void |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame^] | 132 | TunnelClosure::runDataCallback(const Name &name, const Bytes &content) |
Zhenkai Zhu | d492431 | 2012-12-28 11:35:12 -0800 | [diff] [blame] | 133 | { |
| 134 | if (m_tunnel != NULL) |
| 135 | { |
| 136 | m_tunnel->handleTunneledData(name, content, (*m_dataCallback)); |
| 137 | } |
| 138 | } |
| 139 | |
Zhenkai Zhu | 974c5a6 | 2012-12-28 14:15:30 -0800 | [diff] [blame] | 140 | Closure::TimeoutCallbackReturnValue |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame^] | 141 | TunnelClosure::runTimeoutCallback(const Name &interest) |
Zhenkai Zhu | d492431 | 2012-12-28 11:35:12 -0800 | [diff] [blame] | 142 | { |
Zhenkai Zhu | 974c5a6 | 2012-12-28 14:15:30 -0800 | [diff] [blame] | 143 | return Closure::runTimeoutCallback(m_originalInterest); |
Zhenkai Zhu | d492431 | 2012-12-28 11:35:12 -0800 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | } // Ccnx |