Zhenkai Zhu | 0f05412 | 2012-12-25 22:22:50 -0800 | [diff] [blame] | 1 | #include "ccnx-tunnel.h" |
| 2 | |
| 3 | namespace Ccnx |
| 4 | { |
| 5 | |
| 6 | CcnxTunnel::CcnxTunnel() |
| 7 | : CcnxWrapper() |
| 8 | , m_localPrefix("/") |
| 9 | { |
| 10 | refreshLocalPrefix(); |
| 11 | } |
| 12 | |
| 13 | CcnxTunnel::~CcnxTunnel() |
| 14 | { |
| 15 | } |
| 16 | |
| 17 | CcnxTunnel::refreshLocalPrefix() |
| 18 | { |
| 19 | string newPrefix = getLocalPrefix(); |
| 20 | if (!newPrefix.empty() && m_localPrefix != newPrefix) |
| 21 | { |
| 22 | CcnxWrapper::clearInterestFilter(m_localPrefix); |
| 23 | CcnxWrapper::setInterestFilter(newPrefix, bind(&CcnxTunnel::handleTunneledInterest, this, _1)); |
| 24 | m_localPrefix = newPrefix; |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | int |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame^] | 29 | sendInterest(const string &interest, void *dataPass) |
| 30 | { |
| 31 | string tunneledInterest = queryRoutableName(interest); |
| 32 | } |
| 33 | |
| 34 | int |
Zhenkai Zhu | 0f05412 | 2012-12-25 22:22:50 -0800 | [diff] [blame] | 35 | CccnxTunnel::publishRawData(const string &name, const char *buf, size_t len, int freshness) |
| 36 | { |
| 37 | ContentObjectPtr co = createContentObject(name, buf, len, freshness); |
| 38 | storeContentObject(name, co); |
| 39 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame^] | 40 | string tunneledName = m_localPrefix + name; |
Zhenkai Zhu | 0f05412 | 2012-12-25 22:22:50 -0800 | [diff] [blame] | 41 | ContentObjectPtr tunneledCo = createContentObject(tunneledName, co->m_data, co->m_len, freshness); |
| 42 | |
| 43 | return putToCcnd(tunneledCo); |
| 44 | } |
| 45 | |
| 46 | void |
| 47 | CcnxTunnel::handleTunneledInterest(string tunneledInterest) |
| 48 | { |
| 49 | // The interest must have m_localPrefix as a prefix (component-wise), otherwise ccnd would not deliver it to us |
| 50 | string interest = (m_localPrefix == "/") |
| 51 | ? tunneledInterest |
| 52 | : tunneldInterest.substr(m_localPrefix.size()); |
| 53 | |
| 54 | ReadLock(m_ritLock); |
| 55 | |
| 56 | // 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) |
| 57 | for (RitIter it = m_rit.begin(); it != m_rit.end(); it++) |
| 58 | { |
| 59 | // evoke callback for any prefix that is the prefix of the interest |
| 60 | if (isPrefix(it->first(), interest)) |
| 61 | { |
| 62 | (it->second())(interest); |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | bool |
| 68 | CcnxTunnel::isPrefix(string &prefix, string &name) |
| 69 | { |
| 70 | // prefix is literally prefix of name |
| 71 | if (name.find(prefix) == 0) |
| 72 | { |
| 73 | // name and prefix are exactly the same, or the next character in name |
| 74 | // is '/'; in both case, prefix is the ccnx prefix of name (component-wise) |
| 75 | if (name.size() == prefix.size() || name.at(prefix.size()) == '/') |
| 76 | { |
| 77 | return true; |
| 78 | } |
| 79 | } |
| 80 | return false; |
| 81 | } |
| 82 | |
| 83 | int |
| 84 | CcnxTunnel::setInterestFilter(const string &prefix, const InterestCallback &interestCallback) |
| 85 | { |
| 86 | WriteLock(m_ritLock); |
| 87 | m_rit.insert(make_pair(prefix, new InterestCallback(interestCallback))); |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | void |
| 92 | CcnxTunnel::clearInterestFilter(const string &prefix) |
| 93 | { |
| 94 | WriteLock(m_ritLock); |
| 95 | // remove all |
| 96 | m_rit.erase(prefix); |
| 97 | } |
| 98 | |
| 99 | } |
| 100 | |