blob: 36b8f6f9d884007eaf1849297ec6a266aa1bc7bc [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 Zhub7aba362013-01-04 20:58:51 -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 Zhub7aba362013-01-04 20:58:51 -080035 cout << "send interest in Tunnel" << endl;
36 CcnxWrapper::sendInterest(tunneledInterest, cp, selectors);
Zhenkai Zhud4924312012-12-28 11:35:12 -080037}
38
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -080039void
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080040CcnxTunnel::handleTunneledData(const Name &name, const Bytes &tunneledData, const Closure::DataCallback &originalDataCallback)
Zhenkai Zhud4924312012-12-28 11:35:12 -080041{
42 ParsedContentObject pco(tunneledData);
Zhenkai Zhub7aba362013-01-04 20:58:51 -080043 Name n = pco.name();
44 Bytes b = pco.content();
45 cout <<"Parsed ContentObject" << n << b.size() << endl;
Zhenkai Zhud4924312012-12-28 11:35:12 -080046 originalDataCallback(pco.name(), pco.content());
Zhenkai Zhu1ddeb6f2012-12-27 14:04:18 -080047}
48
49int
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080050CcnxTunnel::publishData(const Name &name, const unsigned char *buf, size_t len, int freshness)
Zhenkai Zhu0f054122012-12-25 22:22:50 -080051{
Zhenkai Zhud4924312012-12-28 11:35:12 -080052 Bytes content = createContentObject(name, buf, len, freshness);
53 storeContentObject(name, content);
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -080054
Zhenkai Zhuf1185262012-12-29 17:06:00 -080055 return publishContentObject(name, content, freshness);
56}
Zhenkai Zhu0f054122012-12-25 22:22:50 -080057
Zhenkai Zhuf1185262012-12-29 17:06:00 -080058int
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080059CcnxTunnel::publishContentObject(const Name &name, const Bytes &contentObject, int freshness)
Zhenkai Zhuf1185262012-12-29 17:06:00 -080060{
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080061 Name tunneledName = m_localPrefix + name;
Zhenkai Zhu0ccc9ec2013-01-04 19:24:09 -080062 cout << "Outer name " << tunneledName;
Zhenkai Zhuf1185262012-12-29 17:06:00 -080063 Bytes tunneledCo = createContentObject(tunneledName, head(contentObject), contentObject.size(), freshness);
Zhenkai Zhu0f054122012-12-25 22:22:50 -080064 return putToCcnd(tunneledCo);
65}
66
67void
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080068CcnxTunnel::handleTunneledInterest(const Name &tunneledInterest)
Zhenkai Zhu0f054122012-12-25 22:22:50 -080069{
70 // 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 -080071 Name interest = tunneledInterest.getPartialName(m_localPrefix.size());
Zhenkai Zhu0f054122012-12-25 22:22:50 -080072
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -080073 ReadLock(m_ritLock);
74
Zhenkai Zhu0f054122012-12-25 22:22:50 -080075 // 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 Zhud4924312012-12-28 11:35:12 -080079 if (isPrefix(it->first, interest))
Zhenkai Zhu0f054122012-12-25 22:22:50 -080080 {
Zhenkai Zhud4924312012-12-28 11:35:12 -080081 (it->second)(interest);
Zhenkai Zhu0f054122012-12-25 22:22:50 -080082 }
83 }
84}
85
86bool
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080087CcnxTunnel::isPrefix(const Name &prefix, const Name &name)
Zhenkai Zhu0f054122012-12-25 22:22:50 -080088{
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080089 if (prefix.size() > name.size())
Zhenkai Zhu0f054122012-12-25 22:22:50 -080090 {
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080091 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 Zhu0f054122012-12-25 22:22:50 -080098 {
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080099 return false;
Zhenkai Zhu0f054122012-12-25 22:22:50 -0800100 }
101 }
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800102
103 return true;
Zhenkai Zhu0f054122012-12-25 22:22:50 -0800104}
105
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800106int
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800107CcnxTunnel::setInterestFilter(const Name &prefix, const InterestCallback &interestCallback)
Zhenkai Zhu0f054122012-12-25 22:22:50 -0800108{
109 WriteLock(m_ritLock);
Zhenkai Zhud4924312012-12-28 11:35:12 -0800110 // make sure copy constructor for boost::function works properly
111 m_rit.insert(make_pair(prefix, interestCallback));
Zhenkai Zhu0f054122012-12-25 22:22:50 -0800112 return 0;
113}
114
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800115void
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800116CcnxTunnel::clearInterestFilter(const Name &prefix)
Zhenkai Zhu0f054122012-12-25 22:22:50 -0800117{
118 WriteLock(m_ritLock);
119 // remove all
120 m_rit.erase(prefix);
121}
122
Zhenkai Zhu0ccc9ec2013-01-04 19:24:09 -0800123TunnelClosure::TunnelClosure(int retry, const DataCallback &dataCallback, CcnxTunnel *tunnel, const Name &originalInterest, const TimeoutCallback &timeoutCallback)
Zhenkai Zhu974c5a62012-12-28 14:15:30 -0800124 : Closure(retry, dataCallback, timeoutCallback)
Zhenkai Zhud4924312012-12-28 11:35:12 -0800125 , m_tunnel(tunnel)
126 , m_originalInterest(originalInterest)
127{
Zhenkai Zhu0f054122012-12-25 22:22:50 -0800128}
129
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800130TunnelClosure::TunnelClosure(const Closure *closure, CcnxTunnel *tunnel, const Name &originalInterest)
Zhenkai Zhu974c5a62012-12-28 14:15:30 -0800131 : Closure(*closure)
132 , m_tunnel(tunnel)
133{
134}
135
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800136void
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800137TunnelClosure::runDataCallback(const Name &name, const Bytes &content)
Zhenkai Zhud4924312012-12-28 11:35:12 -0800138{
139 if (m_tunnel != NULL)
140 {
141 m_tunnel->handleTunneledData(name, content, (*m_dataCallback));
142 }
143}
144
Zhenkai Zhu974c5a62012-12-28 14:15:30 -0800145Closure::TimeoutCallbackReturnValue
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800146TunnelClosure::runTimeoutCallback(const Name &interest)
Zhenkai Zhud4924312012-12-28 11:35:12 -0800147{
Zhenkai Zhu974c5a62012-12-28 14:15:30 -0800148 return Closure::runTimeoutCallback(m_originalInterest);
Zhenkai Zhud4924312012-12-28 11:35:12 -0800149}
150
151} // Ccnx