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 | void |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame] | 20 | readRaw(Bytes &bytes, const unsigned char *src, size_t len) |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 21 | { |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame] | 22 | if (len > 0) |
| 23 | { |
| 24 | bytes.resize(len); |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame^] | 25 | memcpy(&bytes[0], src, len); |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame] | 26 | } |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 27 | } |
| 28 | |
Zhenkai Zhu | 974c5a6 | 2012-12-28 14:15:30 -0800 | [diff] [blame] | 29 | const unsigned char * |
| 30 | head(const Bytes &bytes) |
| 31 | { |
| 32 | return &bytes[0]; |
| 33 | } |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame] | 34 | |
Zhenkai Zhu | 9bad2bf | 2012-12-28 15:31:46 -0800 | [diff] [blame] | 35 | void |
| 36 | split(const string &name, Comps &comps) |
| 37 | { |
| 38 | stringstream ss(name); |
| 39 | string comp; |
| 40 | while(getline(ss, comp, '/')) |
| 41 | { |
| 42 | comps.push_back(comp); |
| 43 | } |
| 44 | } |
| 45 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 46 | CcnxWrapper::CcnxWrapper() |
| 47 | : m_handle (0) |
| 48 | , m_keyStore (0) |
| 49 | , m_keyLoactor (0) |
| 50 | , m_running (true) |
| 51 | , m_connected (false) |
| 52 | { |
| 53 | connectCcnd(); |
| 54 | initKeyStore (); |
| 55 | createKeyLocator (); |
| 56 | m_thread = thread (&CcnxWrapper::ccnLoop, this); |
| 57 | } |
| 58 | |
| 59 | void |
| 60 | CcnxWrapper::connectCcnd() |
| 61 | { |
Zhenkai Zhu | 7f4258e | 2012-12-29 19:55:13 -0800 | [diff] [blame] | 62 | UniqueRecLock(m_mutex); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 63 | if (m_handle != 0) { |
| 64 | ccn_disconnect (m_handle); |
| 65 | ccn_destroy (&m_handle); |
| 66 | } |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame^] | 67 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 68 | m_handle = ccn_create (); |
| 69 | if (ccn_connect(m_handle, NULL) < 0) |
| 70 | { |
| 71 | BOOST_THROW_EXCEPTION (CcnxOperationException() << errmsg_info_str("connection to ccnd failed")); |
| 72 | } |
| 73 | m_connected = true; |
| 74 | |
| 75 | if (!m_registeredInterests.empty()) |
| 76 | { |
| 77 | for (map<std::string, InterestCallback>::const_iterator it = m_registeredInterests.begin(); it != m_registeredInterests.end(); ++it) |
| 78 | { |
| 79 | // clearInterestFilter(it->first); |
| 80 | setInterestFilter(it->first, it->second); |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | CcnxWrapper::~CcnxWrapper() |
| 86 | { |
| 87 | { |
Zhenkai Zhu | 7f4258e | 2012-12-29 19:55:13 -0800 | [diff] [blame] | 88 | UniqueRecLock(m_mutex); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 89 | m_running = false; |
| 90 | } |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame^] | 91 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 92 | m_thread.join (); |
| 93 | ccn_disconnect (m_handle); |
| 94 | ccn_destroy (&m_handle); |
| 95 | ccn_charbuf_destroy (&m_keyLoactor); |
| 96 | ccn_keystore_destroy (&m_keyStore); |
| 97 | } |
| 98 | |
| 99 | void |
| 100 | CcnxWrapper::createKeyLocator () |
| 101 | { |
| 102 | m_keyLoactor = ccn_charbuf_create(); |
| 103 | ccn_charbuf_append_tt (m_keyLoactor, CCN_DTAG_KeyLocator, CCN_DTAG); |
| 104 | ccn_charbuf_append_tt (m_keyLoactor, CCN_DTAG_Key, CCN_DTAG); |
| 105 | int res = ccn_append_pubkey_blob (m_keyLoactor, ccn_keystore_public_key(m_keyStore)); |
| 106 | if (res >= 0) |
| 107 | { |
| 108 | ccn_charbuf_append_closer (m_keyLoactor); /* </Key> */ |
| 109 | ccn_charbuf_append_closer (m_keyLoactor); /* </KeyLocator> */ |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | const ccn_pkey* |
| 114 | CcnxWrapper::getPrivateKey () |
| 115 | { |
| 116 | return ccn_keystore_private_key (m_keyStore); |
| 117 | } |
| 118 | |
| 119 | const unsigned char* |
| 120 | CcnxWrapper::getPublicKeyDigest () |
| 121 | { |
| 122 | return ccn_keystore_public_key_digest(m_keyStore); |
| 123 | } |
| 124 | |
| 125 | ssize_t |
| 126 | CcnxWrapper::getPublicKeyDigestLength () |
| 127 | { |
| 128 | return ccn_keystore_public_key_digest_length(m_keyStore); |
| 129 | } |
| 130 | |
| 131 | void |
| 132 | CcnxWrapper::initKeyStore () |
| 133 | { |
| 134 | m_keyStore = ccn_keystore_create (); |
| 135 | string keyStoreFile = string(getenv("HOME")) + string("/.ccnx/.ccnx_keystore"); |
| 136 | if (ccn_keystore_init (m_keyStore, (char *)keyStoreFile.c_str(), (char*)"Th1s1sn0t8g00dp8ssw0rd.") < 0) |
| 137 | BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str(keyStoreFile.c_str())); |
| 138 | } |
| 139 | |
| 140 | void |
| 141 | CcnxWrapper::ccnLoop () |
| 142 | { |
| 143 | static boost::mt19937 randomGenerator (static_cast<unsigned int> (std::time (0))); |
| 144 | static boost::variate_generator<boost::mt19937&, boost::uniform_int<> > rangeUniformRandom (randomGenerator, uniform_int<> (0,1000)); |
| 145 | |
| 146 | while (m_running) |
| 147 | { |
| 148 | try |
| 149 | { |
| 150 | int res = 0; |
| 151 | { |
Zhenkai Zhu | 7f4258e | 2012-12-29 19:55:13 -0800 | [diff] [blame] | 152 | UniqueRecLock(m_mutex); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 153 | res = ccn_run (m_handle, 0); |
| 154 | } |
| 155 | |
| 156 | if (!m_running) break; |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame^] | 157 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 158 | if (res < 0) |
| 159 | BOOST_THROW_EXCEPTION (CcnxOperationException() |
| 160 | << errmsg_info_str("ccn_run returned error")); |
| 161 | |
| 162 | |
| 163 | pollfd pfds[1]; |
| 164 | { |
Zhenkai Zhu | 7f4258e | 2012-12-29 19:55:13 -0800 | [diff] [blame] | 165 | UniqueRecLock(m_mutex); |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame^] | 166 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 167 | pfds[0].fd = ccn_get_connection_fd (m_handle); |
| 168 | pfds[0].events = POLLIN; |
| 169 | if (ccn_output_is_pending (m_handle)) |
| 170 | pfds[0].events |= POLLOUT; |
| 171 | } |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame^] | 172 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 173 | int ret = poll (pfds, 1, 1); |
| 174 | if (ret < 0) |
| 175 | { |
| 176 | BOOST_THROW_EXCEPTION (CcnxOperationException() << errmsg_info_str("ccnd socket failed (probably ccnd got stopped)")); |
| 177 | } |
| 178 | } |
| 179 | catch (CcnxOperationException &e) |
| 180 | { |
| 181 | // do not try reconnect for now |
| 182 | throw e; |
| 183 | /* |
| 184 | m_connected = false; |
| 185 | // probably ccnd has been stopped |
| 186 | // try reconnect with sleep |
| 187 | int interval = 1; |
| 188 | int maxInterval = 32; |
| 189 | while (m_running) |
| 190 | { |
| 191 | try |
| 192 | { |
| 193 | this_thread::sleep (boost::get_system_time () + TIME_SECONDS(interval) + TIME_MILLISECONDS (rangeUniformRandom ())); |
| 194 | |
| 195 | connectCcnd(); |
| 196 | _LOG_DEBUG("reconnect to ccnd succeeded"); |
| 197 | break; |
| 198 | } |
| 199 | catch (CcnxOperationException &e) |
| 200 | { |
| 201 | this_thread::sleep (boost::get_system_time () + TIME_SECONDS(interval) + TIME_MILLISECONDS (rangeUniformRandom ())); |
| 202 | |
| 203 | // do exponential backup for reconnect interval |
| 204 | if (interval < maxInterval) |
| 205 | { |
| 206 | interval *= 2; |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | */ |
| 211 | } |
| 212 | catch (const std::exception &exc) |
| 213 | { |
| 214 | // catch anything thrown within try block that derives from std::exception |
| 215 | std::cerr << exc.what(); |
| 216 | } |
| 217 | catch (...) |
| 218 | { |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame^] | 219 | cout << "UNKNOWN EXCEPTION !!!" << endl; |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 220 | } |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame^] | 221 | } |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 222 | } |
| 223 | |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame] | 224 | Bytes |
Zhenkai Zhu | 974c5a6 | 2012-12-28 14:15:30 -0800 | [diff] [blame] | 225 | CcnxWrapper::createContentObject(const std::string &name, const unsigned char *buf, size_t len, int freshness) |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 226 | { |
| 227 | ccn_charbuf *pname = ccn_charbuf_create(); |
| 228 | ccn_charbuf *signed_info = ccn_charbuf_create(); |
| 229 | ccn_charbuf *content = ccn_charbuf_create(); |
| 230 | |
| 231 | ccn_name_from_uri(pname, name.c_str()); |
| 232 | ccn_signed_info_create(signed_info, |
| 233 | getPublicKeyDigest(), |
| 234 | getPublicKeyDigestLength(), |
| 235 | NULL, |
| 236 | CCN_CONTENT_DATA, |
| 237 | freshness, |
| 238 | NULL, |
| 239 | m_keyLoactor); |
| 240 | if(ccn_encode_ContentObject(content, pname, signed_info, |
Zhenkai Zhu | 974c5a6 | 2012-12-28 14:15:30 -0800 | [diff] [blame] | 241 | buf, len, |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 242 | NULL, getPrivateKey()) < 0) |
| 243 | { |
| 244 | // BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("encode content failed")); |
| 245 | } |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame] | 246 | |
| 247 | Bytes bytes; |
| 248 | readRaw(bytes, content->buf, content->length); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 249 | |
| 250 | ccn_charbuf_destroy (&pname); |
| 251 | ccn_charbuf_destroy (&signed_info); |
| 252 | ccn_charbuf_destroy (&content); |
| 253 | |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame] | 254 | return bytes; |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | int |
Zhenkai Zhu | bad089c | 2012-12-28 10:28:27 -0800 | [diff] [blame] | 258 | CcnxWrapper::putToCcnd (const Bytes &contentObject) |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 259 | { |
Zhenkai Zhu | 7f4258e | 2012-12-29 19:55:13 -0800 | [diff] [blame] | 260 | UniqueRecLock(m_mutex); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 261 | if (!m_running || !m_connected) |
| 262 | return -1; |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame^] | 263 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 264 | |
Zhenkai Zhu | 974c5a6 | 2012-12-28 14:15:30 -0800 | [diff] [blame] | 265 | if (ccn_put(m_handle, head(contentObject), contentObject.size()) < 0) |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 266 | { |
| 267 | // BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("ccnput failed")); |
| 268 | } |
| 269 | |
| 270 | return 0; |
| 271 | } |
| 272 | |
| 273 | int |
Zhenkai Zhu | 974c5a6 | 2012-12-28 14:15:30 -0800 | [diff] [blame] | 274 | CcnxWrapper::publishData (const string &name, const unsigned char *buf, size_t len, int freshness) |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 275 | { |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame^] | 276 | Bytes co = createContentObject(name, buf, len, freshness); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 277 | return putToCcnd(co); |
| 278 | } |
| 279 | |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame] | 280 | int |
| 281 | CcnxWrapper::publishData (const string &name, const Bytes &content, int freshness) |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 282 | { |
Zhenkai Zhu | 974c5a6 | 2012-12-28 14:15:30 -0800 | [diff] [blame] | 283 | publishData(name, head(content), content.size(), freshness); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 284 | } |
| 285 | |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame] | 286 | string |
Zhenkai Zhu | bad089c | 2012-12-28 10:28:27 -0800 | [diff] [blame] | 287 | CcnxWrapper::extractName(const unsigned char *data, const ccn_indexbuf *comps) |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 288 | { |
| 289 | ostringstream name (ostringstream::out); |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame] | 290 | for (int i = 0; i < comps->n - 1; i++) |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 291 | { |
| 292 | char *comp; |
| 293 | size_t size; |
| 294 | name << "/"; |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame] | 295 | ccn_name_comp_get(data, comps, i, (const unsigned char **)&comp, &size); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 296 | string compStr(comp, size); |
| 297 | name << compStr; |
| 298 | } |
| 299 | |
| 300 | return name.str(); |
| 301 | } |
| 302 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 303 | |
| 304 | static ccn_upcall_res |
| 305 | incomingInterest(ccn_closure *selfp, |
| 306 | ccn_upcall_kind kind, |
| 307 | ccn_upcall_info *info) |
| 308 | { |
| 309 | CcnxWrapper::InterestCallback *f = static_cast<CcnxWrapper::InterestCallback*> (selfp->data); |
| 310 | |
| 311 | switch (kind) |
| 312 | { |
| 313 | case CCN_UPCALL_FINAL: // effective in unit tests |
| 314 | delete f; |
| 315 | delete selfp; |
| 316 | return CCN_UPCALL_RESULT_OK; |
| 317 | |
| 318 | case CCN_UPCALL_INTEREST: |
| 319 | break; |
| 320 | |
| 321 | default: |
| 322 | return CCN_UPCALL_RESULT_OK; |
| 323 | } |
| 324 | |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame] | 325 | string interest = CcnxWrapper::extractName(info->interest_ccnb, info->interest_comps); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 326 | |
| 327 | (*f) (interest); |
| 328 | return CCN_UPCALL_RESULT_OK; |
| 329 | } |
| 330 | |
| 331 | static ccn_upcall_res |
| 332 | incomingData(ccn_closure *selfp, |
| 333 | ccn_upcall_kind kind, |
| 334 | ccn_upcall_info *info) |
| 335 | { |
Zhenkai Zhu | 974c5a6 | 2012-12-28 14:15:30 -0800 | [diff] [blame] | 336 | Closure *cp = static_cast<Closure *> (selfp->data); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 337 | |
| 338 | switch (kind) |
| 339 | { |
| 340 | case CCN_UPCALL_FINAL: // effecitve in unit tests |
| 341 | delete cp; |
| 342 | cp = NULL; |
| 343 | delete selfp; |
| 344 | return CCN_UPCALL_RESULT_OK; |
| 345 | |
| 346 | case CCN_UPCALL_CONTENT: |
| 347 | break; |
| 348 | |
| 349 | case CCN_UPCALL_INTEREST_TIMED_OUT: { |
| 350 | if (cp != NULL && cp->getRetry() > 0) { |
| 351 | cp->decRetry(); |
| 352 | return CCN_UPCALL_RESULT_REEXPRESS; |
| 353 | } |
| 354 | |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame] | 355 | string interest = CcnxWrapper::extractName(info->interest_ccnb, info->interest_comps); |
Zhenkai Zhu | 974c5a6 | 2012-12-28 14:15:30 -0800 | [diff] [blame] | 356 | Closure::TimeoutCallbackReturnValue rv = cp->runTimeoutCallback(interest); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 357 | switch(rv) |
| 358 | { |
Zhenkai Zhu | 974c5a6 | 2012-12-28 14:15:30 -0800 | [diff] [blame] | 359 | case Closure::RESULT_OK : return CCN_UPCALL_RESULT_OK; |
| 360 | case Closure::RESULT_REEXPRESS : return CCN_UPCALL_RESULT_REEXPRESS; |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 361 | default : break; |
| 362 | } |
| 363 | return CCN_UPCALL_RESULT_OK; |
| 364 | } |
| 365 | |
| 366 | default: |
| 367 | return CCN_UPCALL_RESULT_OK; |
| 368 | } |
| 369 | |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame] | 370 | const unsigned char *pcontent; |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 371 | size_t len; |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame] | 372 | 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] | 373 | { |
| 374 | // BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("decode ContentObject failed")); |
| 375 | } |
| 376 | |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame] | 377 | string name = CcnxWrapper::extractName(info->content_ccnb, info->content_comps); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 378 | |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame] | 379 | Bytes content; |
| 380 | // copy content and do processing on the copy |
| 381 | // otherwise the pointed memory may have been changed during the processing |
| 382 | readRaw(content, pcontent, len); |
| 383 | |
| 384 | cp->runDataCallback(name, content); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 385 | |
| 386 | return CCN_UPCALL_RESULT_OK; |
| 387 | } |
| 388 | |
Zhenkai Zhu | 9bad2bf | 2012-12-28 15:31:46 -0800 | [diff] [blame] | 389 | int CcnxWrapper::sendInterest (const Interest &interest, Closure *closure) |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 390 | { |
Zhenkai Zhu | 7f4258e | 2012-12-29 19:55:13 -0800 | [diff] [blame] | 391 | UniqueRecLock(m_mutex); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 392 | if (!m_running || !m_connected) |
| 393 | return -1; |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame^] | 394 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 395 | ccn_charbuf *pname = ccn_charbuf_create(); |
| 396 | ccn_closure *dataClosure = new ccn_closure; |
| 397 | |
Zhenkai Zhu | 9bad2bf | 2012-12-28 15:31:46 -0800 | [diff] [blame] | 398 | ccn_name_from_uri (pname, interest.name().c_str()); |
Zhenkai Zhu | 974c5a6 | 2012-12-28 14:15:30 -0800 | [diff] [blame] | 399 | dataClosure->data = (void *)closure; |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 400 | |
| 401 | dataClosure->p = &incomingData; |
| 402 | if (ccn_express_interest (m_handle, pname, dataClosure, NULL) < 0) |
| 403 | { |
| 404 | } |
| 405 | |
| 406 | ccn_charbuf_destroy (&pname); |
| 407 | return 0; |
| 408 | } |
| 409 | |
| 410 | int CcnxWrapper::setInterestFilter (const string &prefix, const InterestCallback &interestCallback) |
| 411 | { |
Zhenkai Zhu | 7f4258e | 2012-12-29 19:55:13 -0800 | [diff] [blame] | 412 | UniqueRecLock(m_mutex); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 413 | if (!m_running || !m_connected) |
| 414 | return -1; |
| 415 | |
| 416 | ccn_charbuf *pname = ccn_charbuf_create(); |
| 417 | ccn_closure *interestClosure = new ccn_closure; |
| 418 | |
| 419 | ccn_name_from_uri (pname, prefix.c_str()); |
| 420 | interestClosure->data = new InterestCallback (interestCallback); // should be removed when closure is removed |
| 421 | interestClosure->p = &incomingInterest; |
| 422 | int ret = ccn_set_interest_filter (m_handle, pname, interestClosure); |
| 423 | if (ret < 0) |
| 424 | { |
| 425 | } |
| 426 | |
| 427 | m_registeredInterests.insert(pair<std::string, InterestCallback>(prefix, interestCallback)); |
| 428 | ccn_charbuf_destroy(&pname); |
| 429 | } |
| 430 | |
| 431 | void |
| 432 | CcnxWrapper::clearInterestFilter (const std::string &prefix) |
| 433 | { |
Zhenkai Zhu | 7f4258e | 2012-12-29 19:55:13 -0800 | [diff] [blame] | 434 | UniqueRecLock(m_mutex); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 435 | if (!m_running || !m_connected) |
| 436 | return; |
| 437 | |
| 438 | ccn_charbuf *pname = ccn_charbuf_create(); |
| 439 | |
| 440 | ccn_name_from_uri (pname, prefix.c_str()); |
| 441 | int ret = ccn_set_interest_filter (m_handle, pname, 0); |
| 442 | if (ret < 0) |
| 443 | { |
| 444 | } |
| 445 | |
| 446 | m_registeredInterests.erase(prefix); |
| 447 | ccn_charbuf_destroy(&pname); |
| 448 | } |
| 449 | |
| 450 | string |
| 451 | CcnxWrapper::getLocalPrefix () |
| 452 | { |
| 453 | struct ccn * tmp_handle = ccn_create (); |
| 454 | int res = ccn_connect (tmp_handle, NULL); |
| 455 | if (res < 0) |
| 456 | { |
| 457 | return ""; |
| 458 | } |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame^] | 459 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 460 | string retval = ""; |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame^] | 461 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 462 | struct ccn_charbuf *templ = ccn_charbuf_create(); |
| 463 | ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG); |
| 464 | ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG); |
| 465 | ccn_charbuf_append_closer(templ); /* </Name> */ |
| 466 | // XXX - use pubid if possible |
| 467 | ccn_charbuf_append_tt(templ, CCN_DTAG_MaxSuffixComponents, CCN_DTAG); |
| 468 | ccnb_append_number(templ, 1); |
| 469 | ccn_charbuf_append_closer(templ); /* </MaxSuffixComponents> */ |
| 470 | ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", 2); |
| 471 | ccn_charbuf_append_closer(templ); /* </Interest> */ |
| 472 | |
| 473 | struct ccn_charbuf *name = ccn_charbuf_create (); |
| 474 | res = ccn_name_from_uri (name, "/local/ndn/prefix"); |
| 475 | if (res < 0) { |
| 476 | } |
| 477 | else |
| 478 | { |
| 479 | struct ccn_fetch *fetch = ccn_fetch_new (tmp_handle); |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame^] | 480 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 481 | 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^] | 482 | NULL, 4, CCN_V_HIGHEST, 0); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 483 | if (stream == NULL) { |
| 484 | } |
| 485 | else |
| 486 | { |
| 487 | ostringstream os; |
| 488 | |
| 489 | int counter = 0; |
| 490 | char buf[256]; |
| 491 | while (true) { |
| 492 | res = ccn_fetch_read (stream, buf, sizeof(buf)); |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame^] | 493 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 494 | if (res == 0) { |
| 495 | break; |
| 496 | } |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame^] | 497 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 498 | if (res > 0) { |
| 499 | os << string(buf, res); |
| 500 | } else if (res == CCN_FETCH_READ_NONE) { |
| 501 | if (counter < 2) |
| 502 | { |
| 503 | ccn_run(tmp_handle, 1000); |
| 504 | counter ++; |
| 505 | } |
| 506 | else |
| 507 | { |
| 508 | break; |
| 509 | } |
| 510 | } else if (res == CCN_FETCH_READ_END) { |
| 511 | break; |
| 512 | } else if (res == CCN_FETCH_READ_TIMEOUT) { |
| 513 | break; |
| 514 | } else { |
| 515 | break; |
| 516 | } |
| 517 | } |
| 518 | retval = os.str (); |
| 519 | stream = ccn_fetch_close(stream); |
| 520 | } |
| 521 | fetch = ccn_fetch_destroy(fetch); |
| 522 | } |
| 523 | |
| 524 | ccn_charbuf_destroy (&name); |
| 525 | |
| 526 | ccn_disconnect (tmp_handle); |
| 527 | ccn_destroy (&tmp_handle); |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame^] | 528 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 529 | return retval; |
| 530 | } |
| 531 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 532 | } |