blob: 670a76ac0ca0f5acb0459b83ca5da35fe63afa87 [file] [log] [blame]
Zhenkai Zhu0f054122012-12-25 22:22:50 -08001#include "ccnx-tunnel.h"
2
3namespace Ccnx
4{
5
6CcnxTunnel::CcnxTunnel()
7 : CcnxWrapper()
8 , m_localPrefix("/")
9{
10 refreshLocalPrefix();
11}
12
13CcnxTunnel::~CcnxTunnel()
14{
15}
16
17CcnxTunnel::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
28int
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080029sendInterest(const string &interest, void *dataPass)
30{
31 string tunneledInterest = queryRoutableName(interest);
32}
33
34int
Zhenkai Zhu0f054122012-12-25 22:22:50 -080035CccnxTunnel::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 Zhu1ddeb6f2012-12-27 14:04:18 -080040 string tunneledName = m_localPrefix + name;
Zhenkai Zhu0f054122012-12-25 22:22:50 -080041 ContentObjectPtr tunneledCo = createContentObject(tunneledName, co->m_data, co->m_len, freshness);
42
43 return putToCcnd(tunneledCo);
44}
45
46void
47CcnxTunnel::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
67bool
68CcnxTunnel::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
83int
84CcnxTunnel::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
91void
92CcnxTunnel::clearInterestFilter(const string &prefix)
93{
94 WriteLock(m_ritLock);
95 // remove all
96 m_rit.erase(prefix);
97}
98
99}
100