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