use Name everywhere
diff --git a/src/ccnx-closure.cpp b/src/ccnx-closure.cpp
index 5b24f66..5b30244 100644
--- a/src/ccnx-closure.cpp
+++ b/src/ccnx-closure.cpp
@@ -23,7 +23,7 @@
}
Closure::TimeoutCallbackReturnValue
-Closure::runTimeoutCallback(const string &interest)
+Closure::runTimeoutCallback(const Name &interest)
{
if ((*m_timeoutCallback).empty())
{
@@ -35,7 +35,7 @@
void
-Closure::runDataCallback(const string &name, const Bytes &content)
+Closure::runDataCallback(const Name &name, const Bytes &content)
{
if (m_dataCallback != NULL) {
(*m_dataCallback)(name, content);
diff --git a/src/ccnx-name.cpp b/src/ccnx-name.cpp
index bc7b9bd..ce5f270 100644
--- a/src/ccnx-name.cpp
+++ b/src/ccnx-name.cpp
@@ -4,6 +4,7 @@
#include <boost/algorithm/string/join.hpp>
namespace Ccnx{
+CcnxCharbufPtr CcnxCharbuf::Null;
CcnxCharbuf::CcnxCharbuf()
: m_buf(NULL)
@@ -66,6 +67,40 @@
}
}
+Name &
+Name::operator=(const Name &other)
+{
+ m_comps = other.m_comps;
+ return *this;
+}
+bool
+Name::operator==(const string &str)
+{
+ return this->toString() == str;
+}
+
+bool
+Name::operator!=(const string &str)
+{
+ return !(*this == str);
+}
+
+Name
+operator+(const Name &n1, const Name &n2)
+{
+ vector<Bytes> comps = n1.m_comps;
+ copy(n2.m_comps.begin(), n2.m_comps.end(), back_inserter(comps));
+ return Name(comps);
+}
+
+string
+Name::toString() const
+{
+ stringstream ss(stringstream::out);
+ ss << *this;
+ return ss.str();
+}
+
CcnxCharbufPtr
Name::toCcnxCharbuf() const
{
@@ -131,7 +166,7 @@
Name::getPartialName(int start, int n) const
{
int size = m_comps.size();
- if (start < 0 || start >= size || start + n > size)
+ if (start < 0 || start >= size || n > 0 && start + n > size)
{
stringstream ss(stringstream::out);
ss << "getPartialName() parameter out of range! ";
@@ -142,10 +177,21 @@
}
vector<Bytes> comps;
- for (int i = 0; i < n; i++)
+ int end;
+ if (n > 0)
{
- comps.push_back(m_comps[start + i]);
+ end = start + n;
}
+ else
+ {
+ end = size;
+ }
+
+ for (int i = start; i < end; i++)
+ {
+ comps.push_back(m_comps[i]);
+ }
+
return Name(comps);
}
@@ -163,6 +209,31 @@
return os;
}
+bool
+operator ==(const Name &n1, const Name &n2)
+{
+ stringstream ss1(stringstream::out);
+ stringstream ss2(stringstream::out);
+ ss1 << n1;
+ ss2 << n2;
+ return ss1.str() == ss2.str();
+}
+
+bool
+operator !=(const Name &n1, const Name &n2)
+{
+ return !(n1 == n2);
+}
+
+bool
+operator <(const Name &n1, const Name &n2)
+{
+ stringstream ss1(stringstream::out);
+ stringstream ss2(stringstream::out);
+ ss1 << n1;
+ ss2 << n2;
+ return ss1.str() < ss2.str();
+}
Selectors::Selectors()
: m_maxSuffixComps(-1)
@@ -185,9 +256,36 @@
m_publisherPublicKeyDigest = other.m_publisherPublicKeyDigest;
}
-CcnxCharbufPtr
-Selectors::toCcnxCharbuf()
+bool
+Selectors::operator == (const Selectors &other)
{
+ return m_maxSuffixComps == other.m_maxSuffixComps
+ && m_minSuffixComps == other.m_minSuffixComps
+ && m_answerOriginKind == other.m_answerOriginKind
+ && (m_interestLifetime - other.m_interestLifetime) < 10e-4
+ && m_scope == other.m_scope
+ && m_childSelector == other.m_childSelector;
+}
+
+bool
+Selectors::isEmpty() const
+{
+ return m_maxSuffixComps == -1
+ && m_minSuffixComps == -1
+ && m_answerOriginKind == AOK_DEFAULT
+ && (m_interestLifetime - (-1.0)) < 10e-4
+ && m_scope == -1
+ && m_childSelector == DEFAULT;
+}
+
+
+CcnxCharbufPtr
+Selectors::toCcnxCharbuf() const
+{
+ if (isEmpty())
+ {
+ return CcnxCharbuf::Null;
+ }
CcnxCharbufPtr ptr(new CcnxCharbuf());
ccn_charbuf *cbuf = ptr->getBuf();
ccn_charbuf_append_tt(cbuf, CCN_DTAG_Interest, CCN_DTAG);
diff --git a/src/ccnx-pco.cpp b/src/ccnx-pco.cpp
index 1c24891..dce3bcb 100644
--- a/src/ccnx-pco.cpp
+++ b/src/ccnx-pco.cpp
@@ -46,10 +46,10 @@
return bytes;
}
-string
+Name
ParsedContentObject::name() const
{
- return CcnxWrapper::extractName(head(m_bytes), m_comps);
+ return Name(head(m_bytes), m_comps);
}
}
diff --git a/src/ccnx-tunnel.cpp b/src/ccnx-tunnel.cpp
index 037bd8a..49e28af 100644
--- a/src/ccnx-tunnel.cpp
+++ b/src/ccnx-tunnel.cpp
@@ -18,8 +18,8 @@
void
CcnxTunnel::refreshLocalPrefix()
{
- string newPrefix = getLocalPrefix();
- if (!newPrefix.empty() && m_localPrefix != newPrefix)
+ Name newPrefix = getLocalPrefix();
+ if (!newPrefix.toString().empty() && m_localPrefix != newPrefix)
{
CcnxWrapper::clearInterestFilter(m_localPrefix);
CcnxWrapper::setInterestFilter(newPrefix, bind(&CcnxTunnel::handleTunneledInterest, this, _1));
@@ -28,23 +28,22 @@
}
int
-CcnxTunnel::sendInterest (const string &interest, Closure *closure)
+CcnxTunnel::sendInterest (const Name &interest, Closure *closure, const Selectors &selectors)
{
- string strInterest = interest;
- string tunneledInterest = queryRoutableName(strInterest);
- Closure *cp = new TunnelClosure(closure, this, strInterest);
- sendInterest(tunneledInterest, cp);
+ Name tunneledInterest = queryRoutableName(interest);
+ Closure *cp = new TunnelClosure(closure, this, interest);
+ sendInterest(tunneledInterest, cp, selectors);
}
void
-CcnxTunnel::handleTunneledData(const string &name, const Bytes &tunneledData, const Closure::DataCallback &originalDataCallback)
+CcnxTunnel::handleTunneledData(const Name &name, const Bytes &tunneledData, const Closure::DataCallback &originalDataCallback)
{
ParsedContentObject pco(tunneledData);
originalDataCallback(pco.name(), pco.content());
}
int
-CcnxTunnel::publishData(const string &name, const unsigned char *buf, size_t len, int freshness)
+CcnxTunnel::publishData(const Name &name, const unsigned char *buf, size_t len, int freshness)
{
Bytes content = createContentObject(name, buf, len, freshness);
storeContentObject(name, content);
@@ -53,20 +52,18 @@
}
int
-CcnxTunnel::publishContentObject(const string &name, const Bytes &contentObject, int freshness)
+CcnxTunnel::publishContentObject(const Name &name, const Bytes &contentObject, int freshness)
{
- string tunneledName = m_localPrefix + name;
+ Name tunneledName = m_localPrefix + name;
Bytes tunneledCo = createContentObject(tunneledName, head(contentObject), contentObject.size(), freshness);
return putToCcnd(tunneledCo);
}
void
-CcnxTunnel::handleTunneledInterest(const string &tunneledInterest)
+CcnxTunnel::handleTunneledInterest(const Name &tunneledInterest)
{
// The interest must have m_localPrefix as a prefix (component-wise), otherwise ccnd would not deliver it to us
- string interest = (m_localPrefix == "/")
- ? tunneledInterest
- : tunneledInterest.substr(m_localPrefix.size());
+ Name interest = tunneledInterest.getPartialName(m_localPrefix.size());
ReadLock(m_ritLock);
@@ -82,23 +79,27 @@
}
bool
-CcnxTunnel::isPrefix(const string &prefix, const string &name)
+CcnxTunnel::isPrefix(const Name &prefix, const Name &name)
{
- // prefix is literally prefix of name
- if (name.find(prefix) == 0)
+ if (prefix.size() > name.size())
{
- // name and prefix are exactly the same, or the next character in name
- // is '/'; in both case, prefix is the ccnx prefix of name (component-wise)
- if (name.size() == prefix.size() || name.at(prefix.size()) == '/')
+ return false;
+ }
+
+ int size = prefix.size();
+ for (int i = 0; i < size; i++)
+ {
+ if (prefix.getCompAsString(i) != name.getCompAsString(i))
{
- return true;
+ return false;
}
}
- return false;
+
+ return true;
}
int
-CcnxTunnel::setInterestFilter(const string &prefix, const InterestCallback &interestCallback)
+CcnxTunnel::setInterestFilter(const Name &prefix, const InterestCallback &interestCallback)
{
WriteLock(m_ritLock);
// make sure copy constructor for boost::function works properly
@@ -107,28 +108,28 @@
}
void
-CcnxTunnel::clearInterestFilter(const string &prefix)
+CcnxTunnel::clearInterestFilter(const Name &prefix)
{
WriteLock(m_ritLock);
// remove all
m_rit.erase(prefix);
}
-TunnelClosure::TunnelClosure(int retry, const DataCallback &dataCallback, const TimeoutCallback &timeoutCallback, CcnxTunnel *tunnel, const string &originalInterest)
+TunnelClosure::TunnelClosure(int retry, const DataCallback &dataCallback, const TimeoutCallback &timeoutCallback, CcnxTunnel *tunnel, const Name &originalInterest)
: Closure(retry, dataCallback, timeoutCallback)
, m_tunnel(tunnel)
, m_originalInterest(originalInterest)
{
}
-TunnelClosure::TunnelClosure(const Closure *closure, CcnxTunnel *tunnel, const string &originalInterest)
+TunnelClosure::TunnelClosure(const Closure *closure, CcnxTunnel *tunnel, const Name &originalInterest)
: Closure(*closure)
, m_tunnel(tunnel)
{
}
void
-TunnelClosure::runDataCallback(const string &name, const Bytes &content)
+TunnelClosure::runDataCallback(const Name &name, const Bytes &content)
{
if (m_tunnel != NULL)
{
@@ -137,7 +138,7 @@
}
Closure::TimeoutCallbackReturnValue
-TunnelClosure::runTimeoutCallback(const string &interest)
+TunnelClosure::runTimeoutCallback(const Name &interest)
{
return Closure::runTimeoutCallback(m_originalInterest);
}
diff --git a/src/ccnx-wrapper.cpp b/src/ccnx-wrapper.cpp
index 1980e6e..ac65cba 100644
--- a/src/ccnx-wrapper.cpp
+++ b/src/ccnx-wrapper.cpp
@@ -63,7 +63,7 @@
if (!m_registeredInterests.empty())
{
- for (map<std::string, InterestCallback>::const_iterator it = m_registeredInterests.begin(); it != m_registeredInterests.end(); ++it)
+ for (map<Name, InterestCallback>::const_iterator it = m_registeredInterests.begin(); it != m_registeredInterests.end(); ++it)
{
// clearInterestFilter(it->first);
setInterestFilter(it->first, it->second);
@@ -211,13 +211,13 @@
}
Bytes
-CcnxWrapper::createContentObject(const std::string &name, const unsigned char *buf, size_t len, int freshness)
+CcnxWrapper::createContentObject(const Name &name, const unsigned char *buf, size_t len, int freshness)
{
- ccn_charbuf *pname = ccn_charbuf_create();
+ CcnxCharbufPtr ptr = name.toCcnxCharbuf();
+ ccn_charbuf *pname = ptr->getBuf();
ccn_charbuf *signed_info = ccn_charbuf_create();
ccn_charbuf *content = ccn_charbuf_create();
- ccn_name_from_uri(pname, name.c_str());
ccn_signed_info_create(signed_info,
getPublicKeyDigest(),
getPublicKeyDigestLength(),
@@ -236,7 +236,6 @@
Bytes bytes;
readRaw(bytes, content->buf, content->length);
- ccn_charbuf_destroy (&pname);
ccn_charbuf_destroy (&signed_info);
ccn_charbuf_destroy (&content);
@@ -260,35 +259,18 @@
}
int
-CcnxWrapper::publishData (const string &name, const unsigned char *buf, size_t len, int freshness)
+CcnxWrapper::publishData (const Name &name, const unsigned char *buf, size_t len, int freshness)
{
Bytes co = createContentObject(name, buf, len, freshness);
return putToCcnd(co);
}
int
-CcnxWrapper::publishData (const string &name, const Bytes &content, int freshness)
+CcnxWrapper::publishData (const Name &name, const Bytes &content, int freshness)
{
publishData(name, head(content), content.size(), freshness);
}
-string
-CcnxWrapper::extractName(const unsigned char *data, const ccn_indexbuf *comps)
-{
- ostringstream name (ostringstream::out);
- for (int i = 0; i < comps->n - 1; i++)
- {
- char *comp;
- size_t size;
- name << "/";
- ccn_name_comp_get(data, comps, i, (const unsigned char **)&comp, &size);
- string compStr(comp, size);
- name << compStr;
- }
-
- return name.str();
-}
-
static ccn_upcall_res
incomingInterest(ccn_closure *selfp,
@@ -311,7 +293,7 @@
return CCN_UPCALL_RESULT_OK;
}
- string interest = CcnxWrapper::extractName(info->interest_ccnb, info->interest_comps);
+ Name interest(info->interest_ccnb, info->interest_comps);
(*f) (interest);
return CCN_UPCALL_RESULT_OK;
@@ -341,7 +323,7 @@
return CCN_UPCALL_RESULT_REEXPRESS;
}
- string interest = CcnxWrapper::extractName(info->interest_ccnb, info->interest_comps);
+ Name interest(info->interest_ccnb, info->interest_comps);
Closure::TimeoutCallbackReturnValue rv = cp->runTimeoutCallback(interest);
switch(rv)
{
@@ -363,7 +345,7 @@
// BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("decode ContentObject failed"));
}
- string name = CcnxWrapper::extractName(info->content_ccnb, info->content_comps);
+ Name name(info->content_ccnb, info->content_comps);
Bytes content;
// copy content and do processing on the copy
@@ -375,37 +357,44 @@
return CCN_UPCALL_RESULT_OK;
}
-int CcnxWrapper::sendInterest (const string &interest, Closure *closure)
+int CcnxWrapper::sendInterest (const Name &interest, Closure *closure, const Selectors &selectors)
{
UniqueRecLock(m_mutex);
if (!m_running || !m_connected)
return -1;
- ccn_charbuf *pname = ccn_charbuf_create();
+ CcnxCharbufPtr namePtr = interest.toCcnxCharbuf();
+ ccn_charbuf *pname = namePtr->getBuf();
ccn_closure *dataClosure = new ccn_closure;
- ccn_name_from_uri (pname, interest.c_str());
dataClosure->data = (void *)closure;
dataClosure->p = &incomingData;
- if (ccn_express_interest (m_handle, pname, dataClosure, NULL) < 0)
+
+ CcnxCharbufPtr selectorsPtr = selectors.toCcnxCharbuf();
+ ccn_charbuf *templ = NULL;
+ if (selectorsPtr != CcnxCharbuf::Null)
+ {
+ templ = selectorsPtr->getBuf();
+ }
+
+ if (ccn_express_interest (m_handle, pname, dataClosure, templ) < 0)
{
}
- ccn_charbuf_destroy (&pname);
return 0;
}
-int CcnxWrapper::setInterestFilter (const string &prefix, const InterestCallback &interestCallback)
+int CcnxWrapper::setInterestFilter (const Name &prefix, const InterestCallback &interestCallback)
{
UniqueRecLock(m_mutex);
if (!m_running || !m_connected)
return -1;
- ccn_charbuf *pname = ccn_charbuf_create();
+ CcnxCharbufPtr ptr = prefix.toCcnxCharbuf();
+ ccn_charbuf *pname = ptr->getBuf();
ccn_closure *interestClosure = new ccn_closure;
- ccn_name_from_uri (pname, prefix.c_str());
interestClosure->data = new InterestCallback (interestCallback); // should be removed when closure is removed
interestClosure->p = &incomingInterest;
int ret = ccn_set_interest_filter (m_handle, pname, interestClosure);
@@ -413,37 +402,35 @@
{
}
- m_registeredInterests.insert(pair<std::string, InterestCallback>(prefix, interestCallback));
- ccn_charbuf_destroy(&pname);
+ m_registeredInterests.insert(pair<Name, InterestCallback>(prefix, interestCallback));
}
void
-CcnxWrapper::clearInterestFilter (const std::string &prefix)
+CcnxWrapper::clearInterestFilter (const Name &prefix)
{
UniqueRecLock(m_mutex);
if (!m_running || !m_connected)
return;
- ccn_charbuf *pname = ccn_charbuf_create();
+ CcnxCharbufPtr ptr = prefix.toCcnxCharbuf();
+ ccn_charbuf *pname = ptr->getBuf();
- ccn_name_from_uri (pname, prefix.c_str());
int ret = ccn_set_interest_filter (m_handle, pname, 0);
if (ret < 0)
{
}
m_registeredInterests.erase(prefix);
- ccn_charbuf_destroy(&pname);
}
-string
+Name
CcnxWrapper::getLocalPrefix ()
{
struct ccn * tmp_handle = ccn_create ();
int res = ccn_connect (tmp_handle, NULL);
if (res < 0)
{
- return "";
+ return Name();
}
string retval = "";
@@ -515,7 +502,7 @@
ccn_disconnect (tmp_handle);
ccn_destroy (&tmp_handle);
- return retval;
+ return Name(retval);
}
}