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