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