blob: 49e28afe163ba2d0c109981c5b19123c7a85a6c5 [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{
11 refreshLocalPrefix();
12}
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);
35 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 Zhuf1185262012-12-29 17:06:00 -080058 Bytes tunneledCo = createContentObject(tunneledName, head(contentObject), contentObject.size(), freshness);
Zhenkai Zhu0f054122012-12-25 22:22:50 -080059 return putToCcnd(tunneledCo);
60}
61
62void
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080063CcnxTunnel::handleTunneledInterest(const Name &tunneledInterest)
Zhenkai Zhu0f054122012-12-25 22:22:50 -080064{
65 // 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 -080066 Name interest = tunneledInterest.getPartialName(m_localPrefix.size());
Zhenkai Zhu0f054122012-12-25 22:22:50 -080067
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -080068 ReadLock(m_ritLock);
69
Zhenkai Zhu0f054122012-12-25 22:22:50 -080070 // 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)
71 for (RitIter it = m_rit.begin(); it != m_rit.end(); it++)
72 {
73 // evoke callback for any prefix that is the prefix of the interest
Zhenkai Zhud4924312012-12-28 11:35:12 -080074 if (isPrefix(it->first, interest))
Zhenkai Zhu0f054122012-12-25 22:22:50 -080075 {
Zhenkai Zhud4924312012-12-28 11:35:12 -080076 (it->second)(interest);
Zhenkai Zhu0f054122012-12-25 22:22:50 -080077 }
78 }
79}
80
81bool
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080082CcnxTunnel::isPrefix(const Name &prefix, const Name &name)
Zhenkai Zhu0f054122012-12-25 22:22:50 -080083{
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080084 if (prefix.size() > name.size())
Zhenkai Zhu0f054122012-12-25 22:22:50 -080085 {
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080086 return false;
87 }
88
89 int size = prefix.size();
90 for (int i = 0; i < size; i++)
91 {
92 if (prefix.getCompAsString(i) != name.getCompAsString(i))
Zhenkai Zhu0f054122012-12-25 22:22:50 -080093 {
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080094 return false;
Zhenkai Zhu0f054122012-12-25 22:22:50 -080095 }
96 }
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -080097
98 return true;
Zhenkai Zhu0f054122012-12-25 22:22:50 -080099}
100
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800101int
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800102CcnxTunnel::setInterestFilter(const Name &prefix, const InterestCallback &interestCallback)
Zhenkai Zhu0f054122012-12-25 22:22:50 -0800103{
104 WriteLock(m_ritLock);
Zhenkai Zhud4924312012-12-28 11:35:12 -0800105 // make sure copy constructor for boost::function works properly
106 m_rit.insert(make_pair(prefix, interestCallback));
Zhenkai Zhu0f054122012-12-25 22:22:50 -0800107 return 0;
108}
109
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800110void
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800111CcnxTunnel::clearInterestFilter(const Name &prefix)
Zhenkai Zhu0f054122012-12-25 22:22:50 -0800112{
113 WriteLock(m_ritLock);
114 // remove all
115 m_rit.erase(prefix);
116}
117
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800118TunnelClosure::TunnelClosure(int retry, const DataCallback &dataCallback, const TimeoutCallback &timeoutCallback, CcnxTunnel *tunnel, const Name &originalInterest)
Zhenkai Zhu974c5a62012-12-28 14:15:30 -0800119 : Closure(retry, dataCallback, timeoutCallback)
Zhenkai Zhud4924312012-12-28 11:35:12 -0800120 , m_tunnel(tunnel)
121 , m_originalInterest(originalInterest)
122{
Zhenkai Zhu0f054122012-12-25 22:22:50 -0800123}
124
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800125TunnelClosure::TunnelClosure(const Closure *closure, CcnxTunnel *tunnel, const Name &originalInterest)
Zhenkai Zhu974c5a62012-12-28 14:15:30 -0800126 : Closure(*closure)
127 , m_tunnel(tunnel)
128{
129}
130
Zhenkai Zhu0d8f5d52012-12-30 12:54:07 -0800131void
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800132TunnelClosure::runDataCallback(const Name &name, const Bytes &content)
Zhenkai Zhud4924312012-12-28 11:35:12 -0800133{
134 if (m_tunnel != NULL)
135 {
136 m_tunnel->handleTunneledData(name, content, (*m_dataCallback));
137 }
138}
139
Zhenkai Zhu974c5a62012-12-28 14:15:30 -0800140Closure::TimeoutCallbackReturnValue
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800141TunnelClosure::runTimeoutCallback(const Name &interest)
Zhenkai Zhud4924312012-12-28 11:35:12 -0800142{
Zhenkai Zhu974c5a62012-12-28 14:15:30 -0800143 return Closure::runTimeoutCallback(m_originalInterest);
Zhenkai Zhud4924312012-12-28 11:35:12 -0800144}
145
146} // Ccnx