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