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 | |
| 14 | CcnxTunnel::~CcnxTunnel() |
| 15 | { |
| 16 | } |
| 17 | |
Zhenkai Zhu | d492431 | 2012-12-28 11:35:12 -0800 | [diff] [blame] | 18 | void |
Zhenkai Zhu | 0f05412 | 2012-12-25 22:22:50 -0800 | [diff] [blame] | 19 | CcnxTunnel::refreshLocalPrefix() |
| 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 | 974c5a6 | 2012-12-28 14:15:30 -0800 | [diff] [blame^] | 31 | CcnxTunnel::sendInterest (const std::string &interest, Closure *closure) |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 32 | { |
| 33 | string tunneledInterest = queryRoutableName(interest); |
Zhenkai Zhu | 974c5a6 | 2012-12-28 14:15:30 -0800 | [diff] [blame^] | 34 | Closure *cp = new TunnelClosure(closure, this, interest); |
| 35 | sendInterest(tunneledInterest, cp); |
Zhenkai Zhu | d492431 | 2012-12-28 11:35:12 -0800 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | void |
Zhenkai Zhu | 974c5a6 | 2012-12-28 14:15:30 -0800 | [diff] [blame^] | 39 | CcnxTunnel::handleTunneledData(const string &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 | 974c5a6 | 2012-12-28 14:15:30 -0800 | [diff] [blame^] | 46 | 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] | 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 | 0f05412 | 2012-12-25 22:22:50 -0800 | [diff] [blame] | 50 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 51 | string tunneledName = m_localPrefix + name; |
Zhenkai Zhu | 974c5a6 | 2012-12-28 14:15:30 -0800 | [diff] [blame^] | 52 | Bytes tunneledCo = createContentObject(tunneledName, head(content), content.size(), freshness); |
Zhenkai Zhu | 0f05412 | 2012-12-25 22:22:50 -0800 | [diff] [blame] | 53 | |
| 54 | return putToCcnd(tunneledCo); |
| 55 | } |
| 56 | |
| 57 | void |
Zhenkai Zhu | d492431 | 2012-12-28 11:35:12 -0800 | [diff] [blame] | 58 | CcnxTunnel::handleTunneledInterest(const string &tunneledInterest) |
Zhenkai Zhu | 0f05412 | 2012-12-25 22:22:50 -0800 | [diff] [blame] | 59 | { |
| 60 | // The interest must have m_localPrefix as a prefix (component-wise), otherwise ccnd would not deliver it to us |
| 61 | string interest = (m_localPrefix == "/") |
| 62 | ? tunneledInterest |
Zhenkai Zhu | d492431 | 2012-12-28 11:35:12 -0800 | [diff] [blame] | 63 | : tunneledInterest.substr(m_localPrefix.size()); |
Zhenkai Zhu | 0f05412 | 2012-12-25 22:22:50 -0800 | [diff] [blame] | 64 | |
| 65 | ReadLock(m_ritLock); |
| 66 | |
| 67 | // 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) |
| 68 | for (RitIter it = m_rit.begin(); it != m_rit.end(); it++) |
| 69 | { |
| 70 | // evoke callback for any prefix that is the prefix of the interest |
Zhenkai Zhu | d492431 | 2012-12-28 11:35:12 -0800 | [diff] [blame] | 71 | if (isPrefix(it->first, interest)) |
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 | (it->second)(interest); |
Zhenkai Zhu | 0f05412 | 2012-12-25 22:22:50 -0800 | [diff] [blame] | 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | bool |
Zhenkai Zhu | d492431 | 2012-12-28 11:35:12 -0800 | [diff] [blame] | 79 | CcnxTunnel::isPrefix(const string &prefix, const string &name) |
Zhenkai Zhu | 0f05412 | 2012-12-25 22:22:50 -0800 | [diff] [blame] | 80 | { |
| 81 | // prefix is literally prefix of name |
| 82 | if (name.find(prefix) == 0) |
| 83 | { |
| 84 | // name and prefix are exactly the same, or the next character in name |
| 85 | // is '/'; in both case, prefix is the ccnx prefix of name (component-wise) |
| 86 | if (name.size() == prefix.size() || name.at(prefix.size()) == '/') |
| 87 | { |
| 88 | return true; |
| 89 | } |
| 90 | } |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | int |
| 95 | CcnxTunnel::setInterestFilter(const string &prefix, const InterestCallback &interestCallback) |
| 96 | { |
| 97 | WriteLock(m_ritLock); |
Zhenkai Zhu | d492431 | 2012-12-28 11:35:12 -0800 | [diff] [blame] | 98 | // make sure copy constructor for boost::function works properly |
| 99 | m_rit.insert(make_pair(prefix, interestCallback)); |
Zhenkai Zhu | 0f05412 | 2012-12-25 22:22:50 -0800 | [diff] [blame] | 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | void |
| 104 | CcnxTunnel::clearInterestFilter(const string &prefix) |
| 105 | { |
| 106 | WriteLock(m_ritLock); |
| 107 | // remove all |
| 108 | m_rit.erase(prefix); |
| 109 | } |
| 110 | |
Zhenkai Zhu | 974c5a6 | 2012-12-28 14:15:30 -0800 | [diff] [blame^] | 111 | TunnelClosure::TunnelClosure(int retry, const DataCallback &dataCallback, const TimeoutCallback &timeoutCallback, CcnxTunnel *tunnel, const string &originalInterest) |
| 112 | : Closure(retry, dataCallback, timeoutCallback) |
Zhenkai Zhu | d492431 | 2012-12-28 11:35:12 -0800 | [diff] [blame] | 113 | , m_tunnel(tunnel) |
| 114 | , m_originalInterest(originalInterest) |
| 115 | { |
Zhenkai Zhu | 0f05412 | 2012-12-25 22:22:50 -0800 | [diff] [blame] | 116 | } |
| 117 | |
Zhenkai Zhu | 974c5a6 | 2012-12-28 14:15:30 -0800 | [diff] [blame^] | 118 | TunnelClosure::TunnelClosure(const Closure *closure, CcnxTunnel *tunnel, const string &originalInterest) |
| 119 | : Closure(*closure) |
| 120 | , m_tunnel(tunnel) |
| 121 | { |
| 122 | } |
| 123 | |
Zhenkai Zhu | d492431 | 2012-12-28 11:35:12 -0800 | [diff] [blame] | 124 | void |
Zhenkai Zhu | 974c5a6 | 2012-12-28 14:15:30 -0800 | [diff] [blame^] | 125 | TunnelClosure::runDataCallback(const string &name, const Bytes &content) |
Zhenkai Zhu | d492431 | 2012-12-28 11:35:12 -0800 | [diff] [blame] | 126 | { |
| 127 | if (m_tunnel != NULL) |
| 128 | { |
| 129 | m_tunnel->handleTunneledData(name, content, (*m_dataCallback)); |
| 130 | } |
| 131 | } |
| 132 | |
Zhenkai Zhu | 974c5a6 | 2012-12-28 14:15:30 -0800 | [diff] [blame^] | 133 | Closure::TimeoutCallbackReturnValue |
| 134 | TunnelClosure::runTimeoutCallback(const string &interest) |
Zhenkai Zhu | d492431 | 2012-12-28 11:35:12 -0800 | [diff] [blame] | 135 | { |
Zhenkai Zhu | 974c5a6 | 2012-12-28 14:15:30 -0800 | [diff] [blame^] | 136 | return Closure::runTimeoutCallback(m_originalInterest); |
Zhenkai Zhu | d492431 | 2012-12-28 11:35:12 -0800 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | } // Ccnx |