more refactoring. Tunnel finishes
diff --git a/src/ccnx-closure.cpp b/src/ccnx-closure.cpp
new file mode 100644
index 0000000..43b0d65
--- /dev/null
+++ b/src/ccnx-closure.cpp
@@ -0,0 +1,45 @@
+#include "ccnx-closure.h"
+
+namespace Ccnx {
+
+Closure::Closure(int retry, const DataCallback &dataCallback, const TimeoutCallback &timeoutCallback)
+ : m_retry(retry), m_timeoutCallback(NULL)
+{
+ m_timeoutCallback = new TimeoutCallback (timeoutCallback);
+ m_dataCallback = new DataCallback (dataCallback);
+}
+
+Closure::Closure(const Closure &other)
+{
+ Closure(other.m_retry, *(other.m_dataCallback), *(other.m_timeoutCallback));
+}
+
+Closure::~Closure ()
+{
+ delete m_dataCallback;
+ delete m_timeoutCallback;
+ m_dataCallback = NULL;
+ m_timeoutCallback = NULL;
+}
+
+Closure::TimeoutCallbackReturnValue
+Closure::runTimeoutCallback(const string &interest)
+{
+ if ((*m_timeoutCallback).empty())
+ {
+ return RESULT_OK;
+ }
+
+ return (*m_timeoutCallback)(interest);
+}
+
+
+void
+Closure::runDataCallback(const string &name, const Bytes &content)
+{
+ if (m_dataCallback != NULL) {
+ (*m_dataCallback)(name, content);
+ }
+}
+
+} // Ccnx
diff --git a/src/ccnx-pco.cpp b/src/ccnx-pco.cpp
index e2db766..df3bd06 100644
--- a/src/ccnx-pco.cpp
+++ b/src/ccnx-pco.cpp
@@ -16,7 +16,7 @@
ParsedContentObject::ParsedContentObject(const Bytes &bytes)
{
- ParsedContentObject((const unsigned char *)bytes[0], bytes.size());
+ ParsedContentObject(head(bytes), bytes.size());
}
ParsedContentObject::ParsedContentObject(const ParsedContentObject &other)
@@ -36,7 +36,7 @@
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);
+ int res = ccn_content_get_value(head(m_bytes), m_pco.offset[CCN_PCO_E], &m_pco, &content, &len);
if (res < 0)
{
boost::throw_exception(MisformedContentObjectException());
@@ -49,7 +49,7 @@
string
ParsedContentObject::name() const
{
- return CcnxWrapper::extractName((const unsigned char *)m_bytes[0], m_comps);
+ return CcnxWrapper::extractName(head(m_bytes), m_comps);
}
}
diff --git a/src/ccnx-tunnel.cpp b/src/ccnx-tunnel.cpp
index f12e2d5..467f7a1 100644
--- a/src/ccnx-tunnel.cpp
+++ b/src/ccnx-tunnel.cpp
@@ -28,28 +28,28 @@
}
int
-CcnxTunnel::sendInterest (const std::string &interest, const DataCallback &dataCallback, int retry, const TimeoutCallback &timeoutCallback)
+CcnxTunnel::sendInterest (const std::string &interest, Closure *closure)
{
string tunneledInterest = queryRoutableName(interest);
- ClosurePass *cp = new TunnelClosurePass(retry, dataCallback, timeoutCallback, this, interest);
- CcnxWrapper::sendInterest(tunneledInterest, cp);
+ Closure *cp = new TunnelClosure(closure, this, interest);
+ sendInterest(tunneledInterest, cp);
}
void
-CcnxTunnel::handleTunneledData(const string &name, const Bytes &tunneledData, const DataCallback &originalDataCallback)
+CcnxTunnel::handleTunneledData(const string &name, const Bytes &tunneledData, const Closure::DataCallback &originalDataCallback)
{
ParsedContentObject pco(tunneledData);
originalDataCallback(pco.name(), pco.content());
}
int
-CcnxTunnel::publishData(const string &name, const char *buf, size_t len, int freshness)
+CcnxTunnel::publishData(const string &name, const unsigned char *buf, size_t len, int freshness)
{
Bytes content = createContentObject(name, buf, len, freshness);
storeContentObject(name, content);
string tunneledName = m_localPrefix + name;
- Bytes tunneledCo = createContentObject(tunneledName, (const char*)&content[0], content.size(), freshness);
+ Bytes tunneledCo = createContentObject(tunneledName, head(content), content.size(), freshness);
return putToCcnd(tunneledCo);
}
@@ -108,15 +108,21 @@
m_rit.erase(prefix);
}
-TunnelClosurePass::TunnelClosurePass(int retry, const CcnxWrapper::DataCallback &dataCallback, const CcnxWrapper::TimeoutCallback &timeoutCallback, CcnxTunnel *tunnel, const string &originalInterest)
- : ClosurePass(retry, dataCallback, timeoutCallback)
+TunnelClosure::TunnelClosure(int retry, const DataCallback &dataCallback, const TimeoutCallback &timeoutCallback, CcnxTunnel *tunnel, const string &originalInterest)
+ : Closure(retry, dataCallback, timeoutCallback)
, m_tunnel(tunnel)
, m_originalInterest(originalInterest)
{
}
+TunnelClosure::TunnelClosure(const Closure *closure, CcnxTunnel *tunnel, const string &originalInterest)
+ : Closure(*closure)
+ , m_tunnel(tunnel)
+{
+}
+
void
-TunnelClosurePass::runDataCallback(const string &name, const Bytes &content)
+TunnelClosure::runDataCallback(const string &name, const Bytes &content)
{
if (m_tunnel != NULL)
{
@@ -124,10 +130,10 @@
}
}
-CcnxWrapper::TimeoutCallbackReturnValue
-TunnelClosurePass::runTimeoutCallback(const string &interest)
+Closure::TimeoutCallbackReturnValue
+TunnelClosure::runTimeoutCallback(const string &interest)
{
- return ClosurePass::runTimeoutCallback(m_originalInterest);
+ return Closure::runTimeoutCallback(m_originalInterest);
}
} // Ccnx
diff --git a/src/ccnx-wrapper.cpp b/src/ccnx-wrapper.cpp
index 7549e3a..0710c67 100644
--- a/src/ccnx-wrapper.cpp
+++ b/src/ccnx-wrapper.cpp
@@ -26,6 +26,11 @@
}
}
+const unsigned char *
+head(const Bytes &bytes)
+{
+ return &bytes[0];
+}
CcnxWrapper::CcnxWrapper()
: m_handle (0)
@@ -207,7 +212,7 @@
}
Bytes
-CcnxWrapper::createContentObject(const std::string &name, const char *buf, size_t len, int freshness)
+CcnxWrapper::createContentObject(const std::string &name, const unsigned char *buf, size_t len, int freshness)
{
ccn_charbuf *pname = ccn_charbuf_create();
ccn_charbuf *signed_info = ccn_charbuf_create();
@@ -223,7 +228,7 @@
NULL,
m_keyLoactor);
if(ccn_encode_ContentObject(content, pname, signed_info,
- (const unsigned char *)buf, len,
+ buf, len,
NULL, getPrivateKey()) < 0)
{
// BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("encode content failed"));
@@ -247,7 +252,7 @@
return -1;
- if (ccn_put(m_handle, (const unsigned char *)contentObject[0], contentObject.size()) < 0)
+ if (ccn_put(m_handle, head(contentObject), contentObject.size()) < 0)
{
// BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("ccnput failed"));
}
@@ -256,7 +261,7 @@
}
int
-CcnxWrapper::publishData (const string &name, const char *buf, size_t len, int freshness)
+CcnxWrapper::publishData (const string &name, const unsigned char *buf, size_t len, int freshness)
{
Bytes co = createContentObject(name, buf, len, freshness);
return putToCcnd(co);
@@ -265,7 +270,7 @@
int
CcnxWrapper::publishData (const string &name, const Bytes &content, int freshness)
{
- publishData(name, (const char*)content[0], content.size(), freshness);
+ publishData(name, head(content), content.size(), freshness);
}
string
@@ -318,7 +323,7 @@
ccn_upcall_kind kind,
ccn_upcall_info *info)
{
- ClosurePass *cp = static_cast<ClosurePass *> (selfp->data);
+ Closure *cp = static_cast<Closure *> (selfp->data);
switch (kind)
{
@@ -338,11 +343,11 @@
}
string interest = CcnxWrapper::extractName(info->interest_ccnb, info->interest_comps);
- CcnxWrapper::TimeoutCallbackReturnValue rv = cp->runTimeoutCallback(interest);
+ Closure::TimeoutCallbackReturnValue rv = cp->runTimeoutCallback(interest);
switch(rv)
{
- case CcnxWrapper::RESULT_OK : return CCN_UPCALL_RESULT_OK;
- case CcnxWrapper::RESULT_REEXPRESS : return CCN_UPCALL_RESULT_REEXPRESS;
+ case Closure::RESULT_OK : return CCN_UPCALL_RESULT_OK;
+ case Closure::RESULT_REEXPRESS : return CCN_UPCALL_RESULT_REEXPRESS;
default : break;
}
return CCN_UPCALL_RESULT_OK;
@@ -371,14 +376,7 @@
return CCN_UPCALL_RESULT_OK;
}
-
-int CcnxWrapper::sendInterest (const string &strInterest, const DataCallback &dataCallback, int retry, const TimeoutCallback &timeoutCallback)
-{
- ClosurePass * pass = new ClosurePass(retry, dataCallback, timeoutCallback);
- sendInterest(strInterest, pass);
-}
-
-int CcnxWrapper::sendInterest (const string &strInterest, void *dataPass)
+int CcnxWrapper::sendInterest (const string &strInterest, Closure *closure)
{
recursive_mutex::scoped_lock lock(m_mutex);
if (!m_running || !m_connected)
@@ -388,7 +386,7 @@
ccn_closure *dataClosure = new ccn_closure;
ccn_name_from_uri (pname, strInterest.c_str());
- dataClosure->data = dataPass;
+ dataClosure->data = (void *)closure;
dataClosure->p = &incomingData;
if (ccn_express_interest (m_handle, pname, dataClosure, NULL) < 0)
@@ -521,40 +519,4 @@
return retval;
}
-
-ClosurePass::ClosurePass(int retry, const CcnxWrapper::DataCallback &dataCallback, const CcnxWrapper::TimeoutCallback &timeoutCallback)
- : m_retry(retry), m_timeoutCallback(NULL)
-{
- m_timeoutCallback = new CcnxWrapper::TimeoutCallback (timeoutCallback);
- m_dataCallback = new CcnxWrapper::DataCallback (dataCallback);
-}
-
-ClosurePass::~ClosurePass ()
-{
- delete m_dataCallback;
- delete m_timeoutCallback;
- m_dataCallback = NULL;
- m_timeoutCallback = NULL;
-}
-
-CcnxWrapper::TimeoutCallbackReturnValue
-ClosurePass::runTimeoutCallback(const string &interest)
-{
- if ((*m_timeoutCallback).empty())
- {
- return CcnxWrapper::RESULT_OK;
- }
-
- return (*m_timeoutCallback)(interest);
-}
-
-
-void
-ClosurePass::runDataCallback(const string &name, const Bytes &content)
-{
- if (m_dataCallback != NULL) {
- (*m_dataCallback)(name, content);
- }
-}
-
}