blob: dfefc8b7263e6a4cb51bce09ceb1232975bbef69 [file] [log] [blame]
Zhenkai Zhu0f054122012-12-25 22:22:50 -08001#include "ccnx-tunnel.h"
Zhenkai Zhud4924312012-12-28 11:35:12 -08002#include "ccnx-pco.h"
Zhenkai Zhu0f054122012-12-25 22:22:50 -08003
4namespace Ccnx
5{
6
7CcnxTunnel::CcnxTunnel()
8 : CcnxWrapper()
9 , m_localPrefix("/")
10{
Zhenkai Zhu19f81de2013-01-04 22:27:47 -080011 refreshLocalPrefix();
Zhenkai Zhu0f054122012-12-25 22:22:50 -080012}
13
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -080014CcnxTunnel::~CcnxTunnel()
Zhenkai Zhu0f054122012-12-25 22:22:50 -080015{
16}
17
Zhenkai Zhud4924312012-12-28 11:35:12 -080018void
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -080019CcnxTunnel::refreshLocalPrefix()
Zhenkai Zhu0f054122012-12-25 22:22:50 -080020{
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080021 Name newPrefix = getLocalPrefix();
22 if (!newPrefix.toString().empty() && m_localPrefix != newPrefix)
Zhenkai Zhu0f054122012-12-25 22:22:50 -080023 {
24 CcnxWrapper::clearInterestFilter(m_localPrefix);
25 CcnxWrapper::setInterestFilter(newPrefix, bind(&CcnxTunnel::handleTunneledInterest, this, _1));
26 m_localPrefix = newPrefix;
27 }
28}
29
30int
Zhenkai Zhu19f81de2013-01-04 22:27:47 -080031CcnxTunnel::sendInterest (const Name &interest, const Closure *closure, const Selectors &selectors)
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080032{
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080033 Name tunneledInterest = queryRoutableName(interest);
34 Closure *cp = new TunnelClosure(closure, this, interest);
Zhenkai Zhuf3047f12013-01-04 21:48:31 -080035 CcnxWrapper::sendInterest(tunneledInterest, cp, selectors);
Zhenkai Zhu19f81de2013-01-04 22:27:47 -080036 delete cp;
Alexander Afanasyevdcfa9632013-01-07 16:38:19 -080037
38 return 0;
Zhenkai Zhud4924312012-12-28 11:35:12 -080039}
40
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -080041void
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080042CcnxTunnel::handleTunneledData(const Name &name, const Bytes &tunneledData, const Closure::DataCallback &originalDataCallback)
Zhenkai Zhud4924312012-12-28 11:35:12 -080043{
44 ParsedContentObject pco(tunneledData);
45 originalDataCallback(pco.name(), pco.content());
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080046}
47
48int
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080049CcnxTunnel::publishData(const Name &name, const unsigned char *buf, size_t len, int freshness)
Zhenkai Zhu0f054122012-12-25 22:22:50 -080050{
Zhenkai Zhud4924312012-12-28 11:35:12 -080051 Bytes content = createContentObject(name, buf, len, freshness);
52 storeContentObject(name, content);
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -080053
Zhenkai Zhuf1185262012-12-29 17:06:00 -080054 return publishContentObject(name, content, freshness);
55}
Zhenkai Zhu0f054122012-12-25 22:22:50 -080056
Zhenkai Zhuf1185262012-12-29 17:06:00 -080057int
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080058CcnxTunnel::publishContentObject(const Name &name, const Bytes &contentObject, int freshness)
Zhenkai Zhuf1185262012-12-29 17:06:00 -080059{
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080060 Name tunneledName = m_localPrefix + name;
Zhenkai Zhuf1185262012-12-29 17:06:00 -080061 Bytes tunneledCo = createContentObject(tunneledName, head(contentObject), contentObject.size(), freshness);
Zhenkai Zhu0f054122012-12-25 22:22:50 -080062 return putToCcnd(tunneledCo);
63}
64
65void
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080066CcnxTunnel::handleTunneledInterest(const Name &tunneledInterest)
Zhenkai Zhu0f054122012-12-25 22:22:50 -080067{
68 // The interest must have m_localPrefix as a prefix (component-wise), otherwise ccnd would not deliver it to us
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080069 Name interest = tunneledInterest.getPartialName(m_localPrefix.size());
Zhenkai Zhu0f054122012-12-25 22:22:50 -080070
Zhenkai Zhu4865bd62013-01-17 12:08:29 -080071 ReadLock lock(m_ritLock);
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -080072
Zhenkai Zhu0f054122012-12-25 22:22:50 -080073 // 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)
74 for (RitIter it = m_rit.begin(); it != m_rit.end(); it++)
75 {
76 // evoke callback for any prefix that is the prefix of the interest
Zhenkai Zhud4924312012-12-28 11:35:12 -080077 if (isPrefix(it->first, interest))
Zhenkai Zhu0f054122012-12-25 22:22:50 -080078 {
Zhenkai Zhud4924312012-12-28 11:35:12 -080079 (it->second)(interest);
Zhenkai Zhu0f054122012-12-25 22:22:50 -080080 }
81 }
82}
83
84bool
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080085CcnxTunnel::isPrefix(const Name &prefix, const Name &name)
Zhenkai Zhu0f054122012-12-25 22:22:50 -080086{
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080087 if (prefix.size() > name.size())
Zhenkai Zhu0f054122012-12-25 22:22:50 -080088 {
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080089 return false;
90 }
91
92 int size = prefix.size();
93 for (int i = 0; i < size; i++)
94 {
95 if (prefix.getCompAsString(i) != name.getCompAsString(i))
Zhenkai Zhu0f054122012-12-25 22:22:50 -080096 {
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080097 return false;
Zhenkai Zhu0f054122012-12-25 22:22:50 -080098 }
99 }
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800100
101 return true;
Zhenkai Zhu0f054122012-12-25 22:22:50 -0800102}
103
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800104int
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800105CcnxTunnel::setInterestFilter(const Name &prefix, const InterestCallback &interestCallback)
Zhenkai Zhu0f054122012-12-25 22:22:50 -0800106{
Zhenkai Zhu4865bd62013-01-17 12:08:29 -0800107 WriteLock lock(m_ritLock);
Zhenkai Zhud4924312012-12-28 11:35:12 -0800108 // make sure copy constructor for boost::function works properly
109 m_rit.insert(make_pair(prefix, interestCallback));
Zhenkai Zhu0f054122012-12-25 22:22:50 -0800110 return 0;
111}
112
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800113void
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800114CcnxTunnel::clearInterestFilter(const Name &prefix)
Zhenkai Zhu0f054122012-12-25 22:22:50 -0800115{
Zhenkai Zhu4865bd62013-01-17 12:08:29 -0800116 WriteLock lock(m_ritLock);
Zhenkai Zhu0f054122012-12-25 22:22:50 -0800117 // remove all
118 m_rit.erase(prefix);
119}
120
Zhenkai Zhu0ccc9ec2013-01-04 19:24:09 -0800121TunnelClosure::TunnelClosure(int retry, const DataCallback &dataCallback, CcnxTunnel *tunnel, const Name &originalInterest, const TimeoutCallback &timeoutCallback)
Zhenkai Zhu974c5a62012-12-28 14:15:30 -0800122 : Closure(retry, dataCallback, timeoutCallback)
Zhenkai Zhud4924312012-12-28 11:35:12 -0800123 , m_tunnel(tunnel)
124 , m_originalInterest(originalInterest)
125{
Zhenkai Zhu0f054122012-12-25 22:22:50 -0800126}
127
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800128TunnelClosure::TunnelClosure(const Closure *closure, CcnxTunnel *tunnel, const Name &originalInterest)
Zhenkai Zhu974c5a62012-12-28 14:15:30 -0800129 : Closure(*closure)
130 , m_tunnel(tunnel)
131{
132}
133
Zhenkai Zhu19f81de2013-01-04 22:27:47 -0800134Closure *
135TunnelClosure::dup() const
136{
137 Closure *closure = new TunnelClosure(m_retry, *m_dataCallback, m_tunnel, m_originalInterest, *m_timeoutCallback);
138 return closure;
139}
140
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800141void
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800142TunnelClosure::runDataCallback(const Name &name, const Bytes &content)
Zhenkai Zhud4924312012-12-28 11:35:12 -0800143{
144 if (m_tunnel != NULL)
145 {
146 m_tunnel->handleTunneledData(name, content, (*m_dataCallback));
147 }
148}
149
Zhenkai Zhu974c5a62012-12-28 14:15:30 -0800150Closure::TimeoutCallbackReturnValue
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800151TunnelClosure::runTimeoutCallback(const Name &interest)
Zhenkai Zhud4924312012-12-28 11:35:12 -0800152{
Zhenkai Zhu974c5a62012-12-28 14:15:30 -0800153 return Closure::runTimeoutCallback(m_originalInterest);
Zhenkai Zhud4924312012-12-28 11:35:12 -0800154}
155
156} // Ccnx