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 | { |
| 21 | string newPrefix = getLocalPrefix(); |
| 22 | if (!newPrefix.empty() && m_localPrefix != newPrefix) |
| 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 | 9bad2bf | 2012-12-28 15:31:46 -0800 | [diff] [blame] | 31 | CcnxTunnel::sendInterest (const Interest &interest, Closure *closure) |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 32 | { |
Zhenkai Zhu | 9bad2bf | 2012-12-28 15:31:46 -0800 | [diff] [blame] | 33 | string strInterest = interest.name(); |
| 34 | string tunneledInterest = queryRoutableName(strInterest); |
| 35 | Closure *cp = new TunnelClosure(closure, this, strInterest); |
Zhenkai Zhu | 974c5a6 | 2012-12-28 14:15:30 -0800 | [diff] [blame] | 36 | sendInterest(tunneledInterest, cp); |
Zhenkai Zhu | d492431 | 2012-12-28 11:35:12 -0800 | [diff] [blame] | 37 | } |
| 38 | |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame^] | 39 | void |
Zhenkai Zhu | 974c5a6 | 2012-12-28 14:15:30 -0800 | [diff] [blame] | 40 | CcnxTunnel::handleTunneledData(const string &name, const Bytes &tunneledData, const Closure::DataCallback &originalDataCallback) |
Zhenkai Zhu | d492431 | 2012-12-28 11:35:12 -0800 | [diff] [blame] | 41 | { |
| 42 | ParsedContentObject pco(tunneledData); |
| 43 | originalDataCallback(pco.name(), pco.content()); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | int |
Zhenkai Zhu | 974c5a6 | 2012-12-28 14:15:30 -0800 | [diff] [blame] | 47 | CcnxTunnel::publishData(const string &name, const unsigned char *buf, size_t len, int freshness) |
Zhenkai Zhu | 0f05412 | 2012-12-25 22:22:50 -0800 | [diff] [blame] | 48 | { |
Zhenkai Zhu | d492431 | 2012-12-28 11:35:12 -0800 | [diff] [blame] | 49 | Bytes content = createContentObject(name, buf, len, freshness); |
| 50 | storeContentObject(name, content); |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame^] | 51 | |
Zhenkai Zhu | f118526 | 2012-12-29 17:06:00 -0800 | [diff] [blame] | 52 | return publishContentObject(name, content, freshness); |
| 53 | } |
Zhenkai Zhu | 0f05412 | 2012-12-25 22:22:50 -0800 | [diff] [blame] | 54 | |
Zhenkai Zhu | f118526 | 2012-12-29 17:06:00 -0800 | [diff] [blame] | 55 | int |
| 56 | CcnxTunnel::publishContentObject(const string &name, const Bytes &contentObject, int freshness) |
| 57 | { |
| 58 | string tunneledName = m_localPrefix + name; |
| 59 | Bytes tunneledCo = createContentObject(tunneledName, head(contentObject), contentObject.size(), freshness); |
Zhenkai Zhu | 0f05412 | 2012-12-25 22:22:50 -0800 | [diff] [blame] | 60 | return putToCcnd(tunneledCo); |
| 61 | } |
| 62 | |
| 63 | void |
Zhenkai Zhu | d492431 | 2012-12-28 11:35:12 -0800 | [diff] [blame] | 64 | CcnxTunnel::handleTunneledInterest(const string &tunneledInterest) |
Zhenkai Zhu | 0f05412 | 2012-12-25 22:22:50 -0800 | [diff] [blame] | 65 | { |
| 66 | // The interest must have m_localPrefix as a prefix (component-wise), otherwise ccnd would not deliver it to us |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame^] | 67 | string interest = (m_localPrefix == "/") |
Zhenkai Zhu | 0f05412 | 2012-12-25 22:22:50 -0800 | [diff] [blame] | 68 | ? tunneledInterest |
Zhenkai Zhu | d492431 | 2012-12-28 11:35:12 -0800 | [diff] [blame] | 69 | : tunneledInterest.substr(m_localPrefix.size()); |
Zhenkai Zhu | 0f05412 | 2012-12-25 22:22:50 -0800 | [diff] [blame] | 70 | |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame^] | 71 | ReadLock(m_ritLock); |
| 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 | d492431 | 2012-12-28 11:35:12 -0800 | [diff] [blame] | 85 | CcnxTunnel::isPrefix(const string &prefix, const string &name) |
Zhenkai Zhu | 0f05412 | 2012-12-25 22:22:50 -0800 | [diff] [blame] | 86 | { |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame^] | 87 | // prefix is literally prefix of name |
Zhenkai Zhu | 0f05412 | 2012-12-25 22:22:50 -0800 | [diff] [blame] | 88 | if (name.find(prefix) == 0) |
| 89 | { |
| 90 | // name and prefix are exactly the same, or the next character in name |
| 91 | // is '/'; in both case, prefix is the ccnx prefix of name (component-wise) |
| 92 | if (name.size() == prefix.size() || name.at(prefix.size()) == '/') |
| 93 | { |
| 94 | return true; |
| 95 | } |
| 96 | } |
| 97 | return false; |
| 98 | } |
| 99 | |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame^] | 100 | int |
Zhenkai Zhu | 0f05412 | 2012-12-25 22:22:50 -0800 | [diff] [blame] | 101 | CcnxTunnel::setInterestFilter(const string &prefix, const InterestCallback &interestCallback) |
| 102 | { |
| 103 | WriteLock(m_ritLock); |
Zhenkai Zhu | d492431 | 2012-12-28 11:35:12 -0800 | [diff] [blame] | 104 | // make sure copy constructor for boost::function works properly |
| 105 | m_rit.insert(make_pair(prefix, interestCallback)); |
Zhenkai Zhu | 0f05412 | 2012-12-25 22:22:50 -0800 | [diff] [blame] | 106 | return 0; |
| 107 | } |
| 108 | |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame^] | 109 | void |
Zhenkai Zhu | 0f05412 | 2012-12-25 22:22:50 -0800 | [diff] [blame] | 110 | CcnxTunnel::clearInterestFilter(const string &prefix) |
| 111 | { |
| 112 | WriteLock(m_ritLock); |
| 113 | // remove all |
| 114 | m_rit.erase(prefix); |
| 115 | } |
| 116 | |
Zhenkai Zhu | 974c5a6 | 2012-12-28 14:15:30 -0800 | [diff] [blame] | 117 | TunnelClosure::TunnelClosure(int retry, const DataCallback &dataCallback, const TimeoutCallback &timeoutCallback, CcnxTunnel *tunnel, const string &originalInterest) |
| 118 | : Closure(retry, dataCallback, timeoutCallback) |
Zhenkai Zhu | d492431 | 2012-12-28 11:35:12 -0800 | [diff] [blame] | 119 | , m_tunnel(tunnel) |
| 120 | , m_originalInterest(originalInterest) |
| 121 | { |
Zhenkai Zhu | 0f05412 | 2012-12-25 22:22:50 -0800 | [diff] [blame] | 122 | } |
| 123 | |
Zhenkai Zhu | 974c5a6 | 2012-12-28 14:15:30 -0800 | [diff] [blame] | 124 | TunnelClosure::TunnelClosure(const Closure *closure, CcnxTunnel *tunnel, const string &originalInterest) |
| 125 | : Closure(*closure) |
| 126 | , m_tunnel(tunnel) |
| 127 | { |
| 128 | } |
| 129 | |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame^] | 130 | void |
Zhenkai Zhu | 974c5a6 | 2012-12-28 14:15:30 -0800 | [diff] [blame] | 131 | TunnelClosure::runDataCallback(const string &name, const Bytes &content) |
Zhenkai Zhu | d492431 | 2012-12-28 11:35:12 -0800 | [diff] [blame] | 132 | { |
| 133 | if (m_tunnel != NULL) |
| 134 | { |
| 135 | m_tunnel->handleTunneledData(name, content, (*m_dataCallback)); |
| 136 | } |
| 137 | } |
| 138 | |
Zhenkai Zhu | 974c5a6 | 2012-12-28 14:15:30 -0800 | [diff] [blame] | 139 | Closure::TimeoutCallbackReturnValue |
| 140 | TunnelClosure::runTimeoutCallback(const string &interest) |
Zhenkai Zhu | d492431 | 2012-12-28 11:35:12 -0800 | [diff] [blame] | 141 | { |
Zhenkai Zhu | 974c5a6 | 2012-12-28 14:15:30 -0800 | [diff] [blame] | 142 | return Closure::runTimeoutCallback(m_originalInterest); |
Zhenkai Zhu | d492431 | 2012-12-28 11:35:12 -0800 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | } // Ccnx |