refactor ccnx-wrapper
diff --git a/src/ccnx-pco.cpp b/src/ccnx-pco.cpp
new file mode 100644
index 0000000..f592f53
--- /dev/null
+++ b/src/ccnx-pco.cpp
@@ -0,0 +1,55 @@
+#include "ccnx-pco.h"
+
+namespace Ccnx {
+
+ParsedContentObject::ParsedContentObject(const unsigned char *data, size_t len)
+ : m_comps(NULL)
+{
+ m_comps = ccn_indexbuf_create();
+ int res = ccn_parse_ContentObject(data, len, &m_pco, m_comps);
+ if (res < 0)
+ {
+ boost::throw_exception(MisformedContentObjectException());
+ }
+ readRaw(m_bytes, data, len);
+}
+
+ParsedContentObject::ParsedContentObject(const Bytes &bytes)
+{
+ ParsedContentObject((const unsigned char *)bytes[0], bytes.size());
+}
+
+ParsedContentObject::ParsedContentObject(const ParsedContentObject &other)
+{
+ ParsedContentObject(other.m_bytes);
+}
+
+ParsedContentObject::~ParsedContentObject()
+{
+ ccn_indexbuf_destroy(&m_comps);
+ m_comps = NULL;
+}
+
+Bytes
+ParsedContentObject::content()
+{
+ const unsigned char *content;
+ size_t len;
+ Bytes bytes;
+ int res = ccn_content_get_value((const unsigned char *)m_bytes[0], m_pco.offset[CCN_PCO_E], &m_pco, &content, &len);
+ if (res < 0)
+ {
+ boost::throw_exception(MisformedContentObjectException());
+ }
+
+ readRaw(bytes, content, len);
+ return bytes;
+}
+
+string
+ParsedContentObject::name()
+{
+ return CcnxWrapper::extractName((const unsigned char *)m_bytes[0], m_comps);
+}
+
+}
diff --git a/src/ccnx-wrapper.cpp b/src/ccnx-wrapper.cpp
index 95d3130..58a2b7c 100644
--- a/src/ccnx-wrapper.cpp
+++ b/src/ccnx-wrapper.cpp
@@ -1,4 +1,7 @@
#include "ccnx-wrapper.h"
+extern "C" {
+#include <ccn/fetch.h>
+}
#include <poll.h>
#include <boost/throw_exception.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
@@ -13,27 +16,17 @@
namespace Ccnx {
-ContentObject::ContentObject(const char *data, size_t len)
-{
- m_data = new char[len];
- m_len = len;
- memcpy(m_data, data, len);
-}
-
-ContentObject::~ContentObject()
-{
- delete m_data;
-}
-
void
-ContentObject::dup(char **data, size_t *len)
+readRaw(Bytes &bytes, const unsigned char *src, size_t len)
{
- char *tmp = new char[m_len];
- memcpy(tmp, m_data, m_len);
- (*data) = tmp;
- (*len) = m_len;
+ if (len > 0)
+ {
+ bytes.resize(len);
+ memcpy(&bytes[0], src, len);
+ }
}
+
CcnxWrapper::CcnxWrapper()
: m_handle (0)
, m_keyStore (0)
@@ -213,8 +206,7 @@
}
}
-
-ContentObjectPtr
+Bytes
CcnxWrapper::createContentObject(const std::string &name, const char *buf, size_t len, int freshness)
{
ccn_charbuf *pname = ccn_charbuf_create();
@@ -236,25 +228,26 @@
{
// BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("encode content failed"));
}
-
- ContentObjectPtr co(new ContentObject((const char*)content->buf, content->length));
+
+ Bytes bytes;
+ readRaw(bytes, content->buf, content->length);
ccn_charbuf_destroy (&pname);
ccn_charbuf_destroy (&signed_info);
ccn_charbuf_destroy (&content);
- return co;
+ return bytes;
}
int
-CcnxWrapper::putToCcnd (ContentObjectPtr co)
+CcnxWrapper::putToCcnd (Bytes &contentObject)
{
recursive_mutex::scoped_lock lock(m_mutex);
if (!m_running || !m_connected)
return -1;
- if (ccn_put(m_handle, co->m_data, co->m_len) < 0)
+ if (ccn_put(m_handle, (const unsigned char *)contentObject[0], contentObject.size()) < 0)
{
// BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("ccnput failed"));
}
@@ -265,37 +258,26 @@
int
CcnxWrapper::publishData (const string &name, const char *buf, size_t len, int freshness)
{
- ContentObjectPtr co = createContentObject(name, buf, len, freshness);
+ Bytes co = createContentObject(name, buf, len, freshness);
return putToCcnd(co);
}
-std::string
-CcnxWrapper::getInterestName(ccn_upcall_info *info)
+int
+CcnxWrapper::publishData (const string &name, const Bytes &content, int freshness)
{
- ostringstream interest (ostringstream::out);
- for (int i = 0; i < info->interest_comps->n - 1; i++)
- {
- char *comp;
- size_t size;
- interest << "/";
- ccn_name_comp_get(info->interest_ccnb, info->interest_comps, i, (const unsigned char **)&comp, &size);
- string compStr(comp, size);
- interest << compStr;
- }
-
- return interest.str();
+ publishData(name, (const char*)content[0], content.size(), freshness);
}
-std::string
-CcnxWrapper::getDataName(ccn_upcall_info *info)
+string
+CcnxWrapper::extractName(const unsigned char *data, ccn_indexbuf *comps)
{
ostringstream name (ostringstream::out);
- for (int i = 0; i < info->content_comps->n - 1; i++)
+ for (int i = 0; i < comps->n - 1; i++)
{
char *comp;
size_t size;
name << "/";
- ccn_name_comp_get(info->content_ccnb, info->content_comps, i, (const unsigned char **)&comp, &size);
+ ccn_name_comp_get(data, comps, i, (const unsigned char **)&comp, &size);
string compStr(comp, size);
name << compStr;
}
@@ -303,23 +285,6 @@
return name.str();
}
-int
-CcnxWrapper::getContentFromContentObject(ContentObjectPtr co, char **content, size_t *len)
-{
- struct ccn_parsed_ContentObject pco;
- struct ccn_indexbuf *comps = ccn_indexbuf_create();
- int res1 = ccn_parse_ContentObject((const unsigned char *)co->m_data, co->m_len, &pco, comps);
- int res2;
-
- if (res1 >= 0)
- {
- res2 = ccn_content_get_value((const unsigned char *)co->m_data, pco.offset[CCN_PCO_E], &pco, (const unsigned char **)content, len);
- }
-
- ccn_indexbuf_destroy(&comps);
-
- return (res1 < res2) ? res1 : res2;
-}
static ccn_upcall_res
incomingInterest(ccn_closure *selfp,
@@ -342,7 +307,7 @@
return CCN_UPCALL_RESULT_OK;
}
- string interest = CcnxWrapper::getInterestName(info);
+ string interest = CcnxWrapper::extractName(info->interest_ccnb, info->interest_comps);
(*f) (interest);
return CCN_UPCALL_RESULT_OK;
@@ -372,7 +337,7 @@
return CCN_UPCALL_RESULT_REEXPRESS;
}
- string interest = CcnxWrapper::getInterestName(info);
+ string interest = CcnxWrapper::extractName(info->interest_ccnb, info->interest_comps);
CcnxWrapper::TimeoutCallbackReturnValue rv = cp->runTimeoutCallback(interest);
switch(rv)
{
@@ -387,16 +352,21 @@
return CCN_UPCALL_RESULT_OK;
}
- char *pcontent;
+ const unsigned char *pcontent;
size_t len;
- if (ccn_content_get_value(info->content_ccnb, info->pco->offset[CCN_PCO_E], info->pco, (const unsigned char **)&pcontent, &len) < 0)
+ if (ccn_content_get_value(info->content_ccnb, info->pco->offset[CCN_PCO_E], info->pco, &pcontent, &len) < 0)
{
// BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("decode ContentObject failed"));
}
- string name = CcnxWrapper::getDataName(info);
+ string name = CcnxWrapper::extractName(info->content_ccnb, info->content_comps);
- cp->runCallback(name, pcontent, len);
+ Bytes content;
+ // copy content and do processing on the copy
+ // otherwise the pointed memory may have been changed during the processing
+ readRaw(content, pcontent, len);
+
+ cp->runDataCallback(name, content);
return CCN_UPCALL_RESULT_OK;
}
@@ -580,10 +550,10 @@
void
-ClosurePass::runCallback(std::string name, const char *data, size_t len)
+ClosurePass::runDataCallback(std::string name, const Bytes &content)
{
- if (m_callback != NULL) {
- (*m_callback)(name, data, len);
+ if (m_dataCallback != NULL) {
+ (*m_dataCallback)(name, content);
}
}