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