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