blob: 2ac604af1d3520cb4b7bfd8f242f31eab5ec385d [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
29CccnxTunnel::publishRawData(const string &name, const char *buf, size_t len, int freshness)
30{
31 ContentObjectPtr co = createContentObject(name, buf, len, freshness);
32 storeContentObject(name, co);
33
34 string tunneledName = queryRoutableName(name);
35 ContentObjectPtr tunneledCo = createContentObject(tunneledName, co->m_data, co->m_len, freshness);
36
37 return putToCcnd(tunneledCo);
38}
39
40void
41CcnxTunnel::handleTunneledInterest(string tunneledInterest)
42{
43 // The interest must have m_localPrefix as a prefix (component-wise), otherwise ccnd would not deliver it to us
44 string interest = (m_localPrefix == "/")
45 ? tunneledInterest
46 : tunneldInterest.substr(m_localPrefix.size());
47
48 ReadLock(m_ritLock);
49
50 // 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)
51 for (RitIter it = m_rit.begin(); it != m_rit.end(); it++)
52 {
53 // evoke callback for any prefix that is the prefix of the interest
54 if (isPrefix(it->first(), interest))
55 {
56 (it->second())(interest);
57 }
58 }
59}
60
61bool
62CcnxTunnel::isPrefix(string &prefix, string &name)
63{
64 // prefix is literally prefix of name
65 if (name.find(prefix) == 0)
66 {
67 // name and prefix are exactly the same, or the next character in name
68 // is '/'; in both case, prefix is the ccnx prefix of name (component-wise)
69 if (name.size() == prefix.size() || name.at(prefix.size()) == '/')
70 {
71 return true;
72 }
73 }
74 return false;
75}
76
77int
78CcnxTunnel::setInterestFilter(const string &prefix, const InterestCallback &interestCallback)
79{
80 WriteLock(m_ritLock);
81 m_rit.insert(make_pair(prefix, new InterestCallback(interestCallback)));
82 return 0;
83}
84
85void
86CcnxTunnel::clearInterestFilter(const string &prefix)
87{
88 WriteLock(m_ritLock);
89 // remove all
90 m_rit.erase(prefix);
91}
92
93}
94