blob: 940647452b68fa38c3ebe32daa976494c0c1dc1c [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 Zhu90611802013-01-04 21:30:24 -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 Zhucb2d0dd2013-01-03 14:10:48 -080031CcnxTunnel::sendInterest (const Name &interest, 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 Zhu90611802013-01-04 21:30:24 -080035 sendInterest(tunneledInterest, cp, selectors);
Zhenkai Zhud4924312012-12-28 11:35:12 -080036}
37
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -080038void
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080039CcnxTunnel::handleTunneledData(const Name &name, const Bytes &tunneledData, const Closure::DataCallback &originalDataCallback)
Zhenkai Zhud4924312012-12-28 11:35:12 -080040{
41 ParsedContentObject pco(tunneledData);
42 originalDataCallback(pco.name(), pco.content());
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080043}
44
45int
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080046CcnxTunnel::publishData(const Name &name, const unsigned char *buf, size_t len, int freshness)
Zhenkai Zhu0f054122012-12-25 22:22:50 -080047{
Zhenkai Zhud4924312012-12-28 11:35:12 -080048 Bytes content = createContentObject(name, buf, len, freshness);
49 storeContentObject(name, content);
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -080050
Zhenkai Zhuf1185262012-12-29 17:06:00 -080051 return publishContentObject(name, content, freshness);
52}
Zhenkai Zhu0f054122012-12-25 22:22:50 -080053
Zhenkai Zhuf1185262012-12-29 17:06:00 -080054int
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080055CcnxTunnel::publishContentObject(const Name &name, const Bytes &contentObject, int freshness)
Zhenkai Zhuf1185262012-12-29 17:06:00 -080056{
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080057 Name tunneledName = m_localPrefix + name;
Zhenkai Zhu0ccc9ec2013-01-04 19:24:09 -080058 cout << "Outer name " << tunneledName;
Zhenkai Zhuf1185262012-12-29 17:06:00 -080059 Bytes tunneledCo = createContentObject(tunneledName, head(contentObject), contentObject.size(), freshness);
Zhenkai Zhu0f054122012-12-25 22:22:50 -080060 return putToCcnd(tunneledCo);
61}
62
63void
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080064CcnxTunnel::handleTunneledInterest(const Name &tunneledInterest)
Zhenkai Zhu0f054122012-12-25 22:22:50 -080065{
66 // 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 -080067 Name interest = tunneledInterest.getPartialName(m_localPrefix.size());
Zhenkai Zhu0f054122012-12-25 22:22:50 -080068
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -080069 ReadLock(m_ritLock);
70
Zhenkai Zhu0f054122012-12-25 22:22:50 -080071 // 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)
72 for (RitIter it = m_rit.begin(); it != m_rit.end(); it++)
73 {
74 // evoke callback for any prefix that is the prefix of the interest
Zhenkai Zhud4924312012-12-28 11:35:12 -080075 if (isPrefix(it->first, interest))
Zhenkai Zhu0f054122012-12-25 22:22:50 -080076 {
Zhenkai Zhud4924312012-12-28 11:35:12 -080077 (it->second)(interest);
Zhenkai Zhu0f054122012-12-25 22:22:50 -080078 }
79 }
80}
81
82bool
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080083CcnxTunnel::isPrefix(const Name &prefix, const Name &name)
Zhenkai Zhu0f054122012-12-25 22:22:50 -080084{
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080085 if (prefix.size() > name.size())
Zhenkai Zhu0f054122012-12-25 22:22:50 -080086 {
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080087 return false;
88 }
89
90 int size = prefix.size();
91 for (int i = 0; i < size; i++)
92 {
93 if (prefix.getCompAsString(i) != name.getCompAsString(i))
Zhenkai Zhu0f054122012-12-25 22:22:50 -080094 {
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080095 return false;
Zhenkai Zhu0f054122012-12-25 22:22:50 -080096 }
97 }
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080098
99 return true;
Zhenkai Zhu0f054122012-12-25 22:22:50 -0800100}
101
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800102int
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800103CcnxTunnel::setInterestFilter(const Name &prefix, const InterestCallback &interestCallback)
Zhenkai Zhu0f054122012-12-25 22:22:50 -0800104{
105 WriteLock(m_ritLock);
Zhenkai Zhud4924312012-12-28 11:35:12 -0800106 // make sure copy constructor for boost::function works properly
107 m_rit.insert(make_pair(prefix, interestCallback));
Zhenkai Zhu0f054122012-12-25 22:22:50 -0800108 return 0;
109}
110
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800111void
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800112CcnxTunnel::clearInterestFilter(const Name &prefix)
Zhenkai Zhu0f054122012-12-25 22:22:50 -0800113{
114 WriteLock(m_ritLock);
115 // remove all
116 m_rit.erase(prefix);
117}
118
Zhenkai Zhu0ccc9ec2013-01-04 19:24:09 -0800119TunnelClosure::TunnelClosure(int retry, const DataCallback &dataCallback, CcnxTunnel *tunnel, const Name &originalInterest, const TimeoutCallback &timeoutCallback)
Zhenkai Zhu974c5a62012-12-28 14:15:30 -0800120 : Closure(retry, dataCallback, timeoutCallback)
Zhenkai Zhud4924312012-12-28 11:35:12 -0800121 , m_tunnel(tunnel)
122 , m_originalInterest(originalInterest)
123{
Zhenkai Zhu0f054122012-12-25 22:22:50 -0800124}
125
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800126TunnelClosure::TunnelClosure(const Closure *closure, CcnxTunnel *tunnel, const Name &originalInterest)
Zhenkai Zhu974c5a62012-12-28 14:15:30 -0800127 : Closure(*closure)
128 , m_tunnel(tunnel)
129{
130}
131
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800132void
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800133TunnelClosure::runDataCallback(const Name &name, const Bytes &content)
Zhenkai Zhud4924312012-12-28 11:35:12 -0800134{
135 if (m_tunnel != NULL)
136 {
137 m_tunnel->handleTunneledData(name, content, (*m_dataCallback));
138 }
139}
140
Zhenkai Zhu974c5a62012-12-28 14:15:30 -0800141Closure::TimeoutCallbackReturnValue
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800142TunnelClosure::runTimeoutCallback(const Name &interest)
Zhenkai Zhud4924312012-12-28 11:35:12 -0800143{
Zhenkai Zhu974c5a62012-12-28 14:15:30 -0800144 return Closure::runTimeoutCallback(m_originalInterest);
Zhenkai Zhud4924312012-12-28 11:35:12 -0800145}
146
147} // Ccnx