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