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