Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 1 | #include "ccnx-wrapper.h" |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame] | 2 | extern "C" { |
| 3 | #include <ccn/fetch.h> |
| 4 | } |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 5 | #include <poll.h> |
| 6 | #include <boost/throw_exception.hpp> |
| 7 | #include <boost/date_time/posix_time/posix_time.hpp> |
| 8 | #include <boost/random.hpp> |
| 9 | #include <sstream> |
| 10 | |
| 11 | typedef boost::error_info<struct tag_errmsg, std::string> errmsg_info_str; |
| 12 | typedef boost::error_info<struct tag_errmsg, int> errmsg_info_int; |
| 13 | |
| 14 | using namespace std; |
| 15 | using namespace boost; |
| 16 | |
| 17 | namespace Ccnx { |
| 18 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 19 | CcnxWrapper::CcnxWrapper() |
| 20 | : m_handle (0) |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 21 | , m_running (true) |
| 22 | , m_connected (false) |
| 23 | { |
| 24 | connectCcnd(); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 25 | m_thread = thread (&CcnxWrapper::ccnLoop, this); |
| 26 | } |
| 27 | |
| 28 | void |
| 29 | CcnxWrapper::connectCcnd() |
| 30 | { |
Zhenkai Zhu | 3b82d43 | 2013-01-03 22:48:40 -0800 | [diff] [blame] | 31 | //if (m_handle != 0) { |
| 32 | //ccn_disconnect (m_handle); |
| 33 | //ccn_destroy (&m_handle); |
| 34 | //} |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 35 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 36 | m_handle = ccn_create (); |
Alexander Afanasyev | 20bc8e2 | 2013-01-17 10:56:04 -0800 | [diff] [blame] | 37 | UniqueRecLock lock(m_mutex); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 38 | if (ccn_connect(m_handle, NULL) < 0) |
| 39 | { |
| 40 | BOOST_THROW_EXCEPTION (CcnxOperationException() << errmsg_info_str("connection to ccnd failed")); |
| 41 | } |
| 42 | m_connected = true; |
| 43 | |
Zhenkai Zhu | 3b82d43 | 2013-01-03 22:48:40 -0800 | [diff] [blame] | 44 | //if (!m_registeredInterests.empty()) |
| 45 | //{ |
| 46 | // for (map<Name, InterestCallback>::const_iterator it = m_registeredInterests.begin(); it != m_registeredInterests.end(); ++it) |
| 47 | //{ |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 48 | // clearInterestFilter(it->first); |
Zhenkai Zhu | 3b82d43 | 2013-01-03 22:48:40 -0800 | [diff] [blame] | 49 | // setInterestFilter(it->first, it->second); |
| 50 | //} |
| 51 | //} |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | CcnxWrapper::~CcnxWrapper() |
| 55 | { |
| 56 | { |
Alexander Afanasyev | 20bc8e2 | 2013-01-17 10:56:04 -0800 | [diff] [blame] | 57 | UniqueRecLock lock(m_mutex); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 58 | m_running = false; |
| 59 | } |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 60 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 61 | m_thread.join (); |
| 62 | ccn_disconnect (m_handle); |
Zhenkai Zhu | e29616f | 2013-01-14 15:40:57 -0800 | [diff] [blame] | 63 | //ccn_destroy (&m_handle); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | void |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 67 | CcnxWrapper::ccnLoop () |
| 68 | { |
| 69 | static boost::mt19937 randomGenerator (static_cast<unsigned int> (std::time (0))); |
| 70 | static boost::variate_generator<boost::mt19937&, boost::uniform_int<> > rangeUniformRandom (randomGenerator, uniform_int<> (0,1000)); |
| 71 | |
| 72 | while (m_running) |
| 73 | { |
| 74 | try |
| 75 | { |
| 76 | int res = 0; |
| 77 | { |
Alexander Afanasyev | 20bc8e2 | 2013-01-17 10:56:04 -0800 | [diff] [blame] | 78 | UniqueRecLock lock(m_mutex); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 79 | res = ccn_run (m_handle, 0); |
| 80 | } |
| 81 | |
| 82 | if (!m_running) break; |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 83 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 84 | if (res < 0) |
| 85 | BOOST_THROW_EXCEPTION (CcnxOperationException() |
| 86 | << errmsg_info_str("ccn_run returned error")); |
| 87 | |
| 88 | |
| 89 | pollfd pfds[1]; |
| 90 | { |
Alexander Afanasyev | 20bc8e2 | 2013-01-17 10:56:04 -0800 | [diff] [blame] | 91 | UniqueRecLock lock(m_mutex); |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 92 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 93 | pfds[0].fd = ccn_get_connection_fd (m_handle); |
| 94 | pfds[0].events = POLLIN; |
| 95 | if (ccn_output_is_pending (m_handle)) |
| 96 | pfds[0].events |= POLLOUT; |
| 97 | } |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 98 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 99 | int ret = poll (pfds, 1, 1); |
| 100 | if (ret < 0) |
| 101 | { |
| 102 | BOOST_THROW_EXCEPTION (CcnxOperationException() << errmsg_info_str("ccnd socket failed (probably ccnd got stopped)")); |
| 103 | } |
| 104 | } |
| 105 | catch (CcnxOperationException &e) |
| 106 | { |
| 107 | // do not try reconnect for now |
Zhenkai Zhu | 6e7d4d2 | 2013-01-15 18:18:18 -0800 | [diff] [blame] | 108 | cout << *get_error_info<errmsg_info_str> (e) << endl; |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 109 | throw e; |
| 110 | /* |
| 111 | m_connected = false; |
| 112 | // probably ccnd has been stopped |
| 113 | // try reconnect with sleep |
| 114 | int interval = 1; |
| 115 | int maxInterval = 32; |
| 116 | while (m_running) |
| 117 | { |
| 118 | try |
| 119 | { |
| 120 | this_thread::sleep (boost::get_system_time () + TIME_SECONDS(interval) + TIME_MILLISECONDS (rangeUniformRandom ())); |
| 121 | |
| 122 | connectCcnd(); |
| 123 | _LOG_DEBUG("reconnect to ccnd succeeded"); |
| 124 | break; |
| 125 | } |
| 126 | catch (CcnxOperationException &e) |
| 127 | { |
| 128 | this_thread::sleep (boost::get_system_time () + TIME_SECONDS(interval) + TIME_MILLISECONDS (rangeUniformRandom ())); |
| 129 | |
| 130 | // do exponential backup for reconnect interval |
| 131 | if (interval < maxInterval) |
| 132 | { |
| 133 | interval *= 2; |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | */ |
| 138 | } |
| 139 | catch (const std::exception &exc) |
| 140 | { |
| 141 | // catch anything thrown within try block that derives from std::exception |
| 142 | std::cerr << exc.what(); |
| 143 | } |
| 144 | catch (...) |
| 145 | { |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 146 | cout << "UNKNOWN EXCEPTION !!!" << endl; |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 147 | } |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 148 | } |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 149 | } |
| 150 | |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame] | 151 | Bytes |
Alexander Afanasyev | dcfa963 | 2013-01-07 16:38:19 -0800 | [diff] [blame] | 152 | CcnxWrapper::createContentObject(const Name &name, const void *buf, size_t len, int freshness) |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 153 | { |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 154 | CcnxCharbufPtr ptr = name.toCcnxCharbuf(); |
| 155 | ccn_charbuf *pname = ptr->getBuf(); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 156 | ccn_charbuf *content = ccn_charbuf_create(); |
| 157 | |
Zhenkai Zhu | d9d03f6 | 2013-01-04 16:57:46 -0800 | [diff] [blame] | 158 | struct ccn_signing_params sp = CCN_SIGNING_PARAMS_INIT; |
| 159 | sp.freshness = freshness; |
| 160 | |
| 161 | if (ccn_sign_content(m_handle, content, pname, &sp, buf, len) != 0) |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 162 | { |
Zhenkai Zhu | d9d03f6 | 2013-01-04 16:57:46 -0800 | [diff] [blame] | 163 | BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("sign content failed")); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 164 | } |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame] | 165 | |
| 166 | Bytes bytes; |
| 167 | readRaw(bytes, content->buf, content->length); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 168 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 169 | ccn_charbuf_destroy (&content); |
| 170 | |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame] | 171 | return bytes; |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | int |
Zhenkai Zhu | bad089c | 2012-12-28 10:28:27 -0800 | [diff] [blame] | 175 | CcnxWrapper::putToCcnd (const Bytes &contentObject) |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 176 | { |
Alexander Afanasyev | 20bc8e2 | 2013-01-17 10:56:04 -0800 | [diff] [blame] | 177 | UniqueRecLock lock(m_mutex); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 178 | if (!m_running || !m_connected) |
| 179 | return -1; |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 180 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 181 | |
Zhenkai Zhu | 974c5a6 | 2012-12-28 14:15:30 -0800 | [diff] [blame] | 182 | if (ccn_put(m_handle, head(contentObject), contentObject.size()) < 0) |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 183 | { |
| 184 | // BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("ccnput failed")); |
| 185 | } |
| 186 | |
| 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | int |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 191 | CcnxWrapper::publishData (const Name &name, const unsigned char *buf, size_t len, int freshness) |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 192 | { |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 193 | Bytes co = createContentObject(name, buf, len, freshness); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 194 | return putToCcnd(co); |
| 195 | } |
| 196 | |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame] | 197 | int |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 198 | CcnxWrapper::publishData (const Name &name, const Bytes &content, int freshness) |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 199 | { |
Alexander Afanasyev | dcfa963 | 2013-01-07 16:38:19 -0800 | [diff] [blame] | 200 | return publishData(name, head(content), content.size(), freshness); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 201 | } |
| 202 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 203 | |
| 204 | static ccn_upcall_res |
| 205 | incomingInterest(ccn_closure *selfp, |
| 206 | ccn_upcall_kind kind, |
| 207 | ccn_upcall_info *info) |
| 208 | { |
| 209 | CcnxWrapper::InterestCallback *f = static_cast<CcnxWrapper::InterestCallback*> (selfp->data); |
| 210 | |
| 211 | switch (kind) |
| 212 | { |
| 213 | case CCN_UPCALL_FINAL: // effective in unit tests |
| 214 | delete f; |
| 215 | delete selfp; |
| 216 | return CCN_UPCALL_RESULT_OK; |
| 217 | |
| 218 | case CCN_UPCALL_INTEREST: |
| 219 | break; |
| 220 | |
| 221 | default: |
| 222 | return CCN_UPCALL_RESULT_OK; |
| 223 | } |
| 224 | |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 225 | Name interest(info->interest_ccnb, info->interest_comps); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 226 | |
| 227 | (*f) (interest); |
| 228 | return CCN_UPCALL_RESULT_OK; |
| 229 | } |
| 230 | |
| 231 | static ccn_upcall_res |
| 232 | incomingData(ccn_closure *selfp, |
| 233 | ccn_upcall_kind kind, |
| 234 | ccn_upcall_info *info) |
| 235 | { |
Zhenkai Zhu | 974c5a6 | 2012-12-28 14:15:30 -0800 | [diff] [blame] | 236 | Closure *cp = static_cast<Closure *> (selfp->data); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 237 | |
| 238 | switch (kind) |
| 239 | { |
| 240 | case CCN_UPCALL_FINAL: // effecitve in unit tests |
Zhenkai Zhu | 19f81de | 2013-01-04 22:27:47 -0800 | [diff] [blame] | 241 | delete cp; |
| 242 | cp = NULL; |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 243 | delete selfp; |
| 244 | return CCN_UPCALL_RESULT_OK; |
| 245 | |
| 246 | case CCN_UPCALL_CONTENT: |
| 247 | break; |
| 248 | |
| 249 | case CCN_UPCALL_INTEREST_TIMED_OUT: { |
| 250 | if (cp != NULL && cp->getRetry() > 0) { |
| 251 | cp->decRetry(); |
| 252 | return CCN_UPCALL_RESULT_REEXPRESS; |
| 253 | } |
| 254 | |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 255 | Name interest(info->interest_ccnb, info->interest_comps); |
Zhenkai Zhu | 974c5a6 | 2012-12-28 14:15:30 -0800 | [diff] [blame] | 256 | Closure::TimeoutCallbackReturnValue rv = cp->runTimeoutCallback(interest); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 257 | switch(rv) |
| 258 | { |
Zhenkai Zhu | 974c5a6 | 2012-12-28 14:15:30 -0800 | [diff] [blame] | 259 | case Closure::RESULT_OK : return CCN_UPCALL_RESULT_OK; |
| 260 | case Closure::RESULT_REEXPRESS : return CCN_UPCALL_RESULT_REEXPRESS; |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 261 | default : break; |
| 262 | } |
| 263 | return CCN_UPCALL_RESULT_OK; |
| 264 | } |
| 265 | |
| 266 | default: |
| 267 | return CCN_UPCALL_RESULT_OK; |
| 268 | } |
| 269 | |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame] | 270 | const unsigned char *pcontent; |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 271 | size_t len; |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame] | 272 | if (ccn_content_get_value(info->content_ccnb, info->pco->offset[CCN_PCO_E], info->pco, &pcontent, &len) < 0) |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 273 | { |
| 274 | // BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("decode ContentObject failed")); |
| 275 | } |
| 276 | |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 277 | Name name(info->content_ccnb, info->content_comps); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 278 | |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame] | 279 | Bytes content; |
| 280 | // copy content and do processing on the copy |
| 281 | // otherwise the pointed memory may have been changed during the processing |
| 282 | readRaw(content, pcontent, len); |
| 283 | |
| 284 | cp->runDataCallback(name, content); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 285 | |
| 286 | return CCN_UPCALL_RESULT_OK; |
| 287 | } |
| 288 | |
Zhenkai Zhu | 19f81de | 2013-01-04 22:27:47 -0800 | [diff] [blame] | 289 | int CcnxWrapper::sendInterest (const Name &interest, const Closure *closure, const Selectors &selectors) |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 290 | { |
Alexander Afanasyev | 20bc8e2 | 2013-01-17 10:56:04 -0800 | [diff] [blame] | 291 | UniqueRecLock lock(m_mutex); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 292 | if (!m_running || !m_connected) |
| 293 | return -1; |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 294 | |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 295 | CcnxCharbufPtr namePtr = interest.toCcnxCharbuf(); |
| 296 | ccn_charbuf *pname = namePtr->getBuf(); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 297 | ccn_closure *dataClosure = new ccn_closure; |
| 298 | |
Zhenkai Zhu | 19f81de | 2013-01-04 22:27:47 -0800 | [diff] [blame] | 299 | Closure *myClosure = closure->dup(); |
| 300 | dataClosure->data = (void *)myClosure; |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 301 | |
| 302 | dataClosure->p = &incomingData; |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 303 | |
| 304 | CcnxCharbufPtr selectorsPtr = selectors.toCcnxCharbuf(); |
| 305 | ccn_charbuf *templ = NULL; |
| 306 | if (selectorsPtr != CcnxCharbuf::Null) |
| 307 | { |
| 308 | templ = selectorsPtr->getBuf(); |
| 309 | } |
| 310 | |
| 311 | if (ccn_express_interest (m_handle, pname, dataClosure, templ) < 0) |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 312 | { |
| 313 | } |
| 314 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 315 | return 0; |
| 316 | } |
| 317 | |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 318 | int CcnxWrapper::setInterestFilter (const Name &prefix, const InterestCallback &interestCallback) |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 319 | { |
Alexander Afanasyev | 20bc8e2 | 2013-01-17 10:56:04 -0800 | [diff] [blame] | 320 | UniqueRecLock lock(m_mutex); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 321 | if (!m_running || !m_connected) |
| 322 | return -1; |
| 323 | |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 324 | CcnxCharbufPtr ptr = prefix.toCcnxCharbuf(); |
| 325 | ccn_charbuf *pname = ptr->getBuf(); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 326 | ccn_closure *interestClosure = new ccn_closure; |
| 327 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 328 | interestClosure->data = new InterestCallback (interestCallback); // should be removed when closure is removed |
| 329 | interestClosure->p = &incomingInterest; |
| 330 | int ret = ccn_set_interest_filter (m_handle, pname, interestClosure); |
| 331 | if (ret < 0) |
| 332 | { |
| 333 | } |
| 334 | |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 335 | m_registeredInterests.insert(pair<Name, InterestCallback>(prefix, interestCallback)); |
Alexander Afanasyev | dcfa963 | 2013-01-07 16:38:19 -0800 | [diff] [blame] | 336 | |
| 337 | return ret; |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | void |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 341 | CcnxWrapper::clearInterestFilter (const Name &prefix) |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 342 | { |
Alexander Afanasyev | 20bc8e2 | 2013-01-17 10:56:04 -0800 | [diff] [blame] | 343 | UniqueRecLock lock(m_mutex); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 344 | if (!m_running || !m_connected) |
| 345 | return; |
| 346 | |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 347 | CcnxCharbufPtr ptr = prefix.toCcnxCharbuf(); |
| 348 | ccn_charbuf *pname = ptr->getBuf(); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 349 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 350 | int ret = ccn_set_interest_filter (m_handle, pname, 0); |
| 351 | if (ret < 0) |
| 352 | { |
| 353 | } |
| 354 | |
| 355 | m_registeredInterests.erase(prefix); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 356 | } |
| 357 | |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 358 | Name |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 359 | CcnxWrapper::getLocalPrefix () |
| 360 | { |
| 361 | struct ccn * tmp_handle = ccn_create (); |
| 362 | int res = ccn_connect (tmp_handle, NULL); |
| 363 | if (res < 0) |
| 364 | { |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 365 | return Name(); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 366 | } |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 367 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 368 | string retval = ""; |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 369 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 370 | struct ccn_charbuf *templ = ccn_charbuf_create(); |
| 371 | ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG); |
| 372 | ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG); |
| 373 | ccn_charbuf_append_closer(templ); /* </Name> */ |
| 374 | // XXX - use pubid if possible |
| 375 | ccn_charbuf_append_tt(templ, CCN_DTAG_MaxSuffixComponents, CCN_DTAG); |
| 376 | ccnb_append_number(templ, 1); |
| 377 | ccn_charbuf_append_closer(templ); /* </MaxSuffixComponents> */ |
| 378 | ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", 2); |
| 379 | ccn_charbuf_append_closer(templ); /* </Interest> */ |
| 380 | |
| 381 | struct ccn_charbuf *name = ccn_charbuf_create (); |
| 382 | res = ccn_name_from_uri (name, "/local/ndn/prefix"); |
| 383 | if (res < 0) { |
| 384 | } |
| 385 | else |
| 386 | { |
| 387 | struct ccn_fetch *fetch = ccn_fetch_new (tmp_handle); |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 388 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 389 | struct ccn_fetch_stream *stream = ccn_fetch_open (fetch, name, "/local/ndn/prefix", |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 390 | NULL, 4, CCN_V_HIGHEST, 0); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 391 | if (stream == NULL) { |
| 392 | } |
| 393 | else |
| 394 | { |
| 395 | ostringstream os; |
| 396 | |
| 397 | int counter = 0; |
| 398 | char buf[256]; |
| 399 | while (true) { |
| 400 | res = ccn_fetch_read (stream, buf, sizeof(buf)); |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 401 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 402 | if (res == 0) { |
| 403 | break; |
| 404 | } |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 405 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 406 | if (res > 0) { |
| 407 | os << string(buf, res); |
| 408 | } else if (res == CCN_FETCH_READ_NONE) { |
| 409 | if (counter < 2) |
| 410 | { |
| 411 | ccn_run(tmp_handle, 1000); |
| 412 | counter ++; |
| 413 | } |
| 414 | else |
| 415 | { |
| 416 | break; |
| 417 | } |
| 418 | } else if (res == CCN_FETCH_READ_END) { |
| 419 | break; |
| 420 | } else if (res == CCN_FETCH_READ_TIMEOUT) { |
| 421 | break; |
| 422 | } else { |
| 423 | break; |
| 424 | } |
| 425 | } |
| 426 | retval = os.str (); |
| 427 | stream = ccn_fetch_close(stream); |
| 428 | } |
| 429 | fetch = ccn_fetch_destroy(fetch); |
| 430 | } |
| 431 | |
| 432 | ccn_charbuf_destroy (&name); |
| 433 | |
| 434 | ccn_disconnect (tmp_handle); |
| 435 | ccn_destroy (&tmp_handle); |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 436 | |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 437 | return Name(retval); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 438 | } |
| 439 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 440 | } |