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 | |
Alexander Afanasyev | 848a8e4 | 2013-02-07 21:19:18 -0800 | [diff] [blame] | 46 | // hack to enable fake signatures |
| 47 | // min length for signature field is 16, as defined in ccn_buf_decoder.c:728 |
| 48 | const int DEFAULT_SIGNATURE_SIZE = 16; |
| 49 | |
| 50 | // Although ccn_buf_decoder.c:745 defines minimum length 16, something else is checking and only 32-byte fake value is accepted by ccnd |
| 51 | const int PUBLISHER_KEY_SIZE = 32; |
| 52 | |
Zhenkai Zhu | 5acebd7 | 2013-02-07 19:59:25 -0800 | [diff] [blame] | 53 | static int |
Alexander Afanasyev | 848a8e4 | 2013-02-07 21:19:18 -0800 | [diff] [blame] | 54 | ccn_encode_garbage_Signature(struct ccn_charbuf *buf) |
Zhenkai Zhu | 5acebd7 | 2013-02-07 19:59:25 -0800 | [diff] [blame] | 55 | { |
| 56 | int res = 0; |
| 57 | |
Zhenkai Zhu | 5acebd7 | 2013-02-07 19:59:25 -0800 | [diff] [blame] | 58 | res |= ccn_charbuf_append_tt(buf, CCN_DTAG_Signature, CCN_DTAG); |
| 59 | |
Alexander Afanasyev | 848a8e4 | 2013-02-07 21:19:18 -0800 | [diff] [blame] | 60 | // Let's cheat more. Default signing algorithm in ccnd is SHA256, so we just need add 32 bytes of garbage |
| 61 | static char garbage [DEFAULT_SIGNATURE_SIZE]; |
Zhenkai Zhu | 5acebd7 | 2013-02-07 19:59:25 -0800 | [diff] [blame] | 62 | |
Alexander Afanasyev | 848a8e4 | 2013-02-07 21:19:18 -0800 | [diff] [blame] | 63 | // digest and witness fields are optional, so use default ones |
Zhenkai Zhu | 5acebd7 | 2013-02-07 19:59:25 -0800 | [diff] [blame] | 64 | |
| 65 | res |= ccn_charbuf_append_tt(buf, CCN_DTAG_SignatureBits, CCN_DTAG); |
Alexander Afanasyev | 848a8e4 | 2013-02-07 21:19:18 -0800 | [diff] [blame] | 66 | res |= ccn_charbuf_append_tt(buf, DEFAULT_SIGNATURE_SIZE, CCN_BLOB); |
| 67 | res |= ccn_charbuf_append(buf, garbage, DEFAULT_SIGNATURE_SIZE); |
Zhenkai Zhu | 5acebd7 | 2013-02-07 19:59:25 -0800 | [diff] [blame] | 68 | res |= ccn_charbuf_append_closer(buf); |
Alexander Afanasyev | 848a8e4 | 2013-02-07 21:19:18 -0800 | [diff] [blame] | 69 | |
Zhenkai Zhu | 5acebd7 | 2013-02-07 19:59:25 -0800 | [diff] [blame] | 70 | res |= ccn_charbuf_append_closer(buf); |
| 71 | |
| 72 | return(res == 0 ? 0 : -1); |
| 73 | } |
| 74 | |
| 75 | static int |
Alexander Afanasyev | 848a8e4 | 2013-02-07 21:19:18 -0800 | [diff] [blame] | 76 | ccn_pack_unsigned_ContentObject(struct ccn_charbuf *buf, |
| 77 | const struct ccn_charbuf *Name, |
| 78 | const struct ccn_charbuf *SignedInfo, |
| 79 | const void *data, |
| 80 | size_t size) |
Zhenkai Zhu | 5acebd7 | 2013-02-07 19:59:25 -0800 | [diff] [blame] | 81 | { |
| 82 | int res = 0; |
Zhenkai Zhu | 5acebd7 | 2013-02-07 19:59:25 -0800 | [diff] [blame] | 83 | struct ccn_charbuf *content_header; |
| 84 | size_t closer_start; |
| 85 | |
| 86 | content_header = ccn_charbuf_create(); |
| 87 | res |= ccn_charbuf_append_tt(content_header, CCN_DTAG_Content, CCN_DTAG); |
| 88 | if (size != 0) |
| 89 | res |= ccn_charbuf_append_tt(content_header, size, CCN_BLOB); |
| 90 | closer_start = content_header->length; |
| 91 | res |= ccn_charbuf_append_closer(content_header); |
| 92 | if (res < 0) |
| 93 | return(-1); |
Alexander Afanasyev | 848a8e4 | 2013-02-07 21:19:18 -0800 | [diff] [blame] | 94 | |
Zhenkai Zhu | 5acebd7 | 2013-02-07 19:59:25 -0800 | [diff] [blame] | 95 | res |= ccn_charbuf_append_tt(buf, CCN_DTAG_ContentObject, CCN_DTAG); |
| 96 | |
Alexander Afanasyev | 848a8e4 | 2013-02-07 21:19:18 -0800 | [diff] [blame] | 97 | res |= ccn_encode_garbage_Signature(buf); |
| 98 | |
Zhenkai Zhu | 5acebd7 | 2013-02-07 19:59:25 -0800 | [diff] [blame] | 99 | res |= ccn_charbuf_append_charbuf(buf, Name); |
| 100 | res |= ccn_charbuf_append_charbuf(buf, SignedInfo); |
| 101 | res |= ccnb_append_tagged_blob(buf, CCN_DTAG_Content, data, size); |
| 102 | res |= ccn_charbuf_append_closer(buf); |
Alexander Afanasyev | 848a8e4 | 2013-02-07 21:19:18 -0800 | [diff] [blame] | 103 | |
Zhenkai Zhu | 5acebd7 | 2013-02-07 19:59:25 -0800 | [diff] [blame] | 104 | ccn_charbuf_destroy(&content_header); |
| 105 | return(res == 0 ? 0 : -1); |
| 106 | } |
| 107 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 108 | CcnxWrapper::CcnxWrapper() |
| 109 | : m_handle (0) |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 110 | , m_running (true) |
| 111 | , m_connected (false) |
Zhenkai Zhu | 1888f74 | 2013-01-28 12:47:33 -0800 | [diff] [blame] | 112 | , m_executor (new Executor(1)) |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 113 | { |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 114 | start (); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | void |
| 118 | CcnxWrapper::connectCcnd() |
| 119 | { |
Zhenkai Zhu | 3b82d43 | 2013-01-03 22:48:40 -0800 | [diff] [blame] | 120 | //if (m_handle != 0) { |
| 121 | //ccn_disconnect (m_handle); |
| 122 | //ccn_destroy (&m_handle); |
| 123 | //} |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 124 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 125 | m_handle = ccn_create (); |
Zhenkai Zhu | 7599db4 | 2013-01-28 10:32:53 -0800 | [diff] [blame] | 126 | //UniqueRecLock lock(m_mutex); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 127 | if (ccn_connect(m_handle, NULL) < 0) |
| 128 | { |
| 129 | BOOST_THROW_EXCEPTION (CcnxOperationException() << errmsg_info_str("connection to ccnd failed")); |
| 130 | } |
| 131 | m_connected = true; |
| 132 | |
Zhenkai Zhu | 3b82d43 | 2013-01-03 22:48:40 -0800 | [diff] [blame] | 133 | //if (!m_registeredInterests.empty()) |
| 134 | //{ |
| 135 | // for (map<Name, InterestCallback>::const_iterator it = m_registeredInterests.begin(); it != m_registeredInterests.end(); ++it) |
| 136 | //{ |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 137 | // clearInterestFilter(it->first); |
Zhenkai Zhu | 3b82d43 | 2013-01-03 22:48:40 -0800 | [diff] [blame] | 138 | // setInterestFilter(it->first, it->second); |
| 139 | //} |
| 140 | //} |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | CcnxWrapper::~CcnxWrapper() |
| 144 | { |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 145 | shutdown (); |
| 146 | } |
| 147 | |
| 148 | void |
| 149 | CcnxWrapper::start () // called automatically in constructor |
| 150 | { |
| 151 | connectCcnd(); |
| 152 | m_thread = thread (&CcnxWrapper::ccnLoop, this); |
| 153 | m_executor->start(); |
| 154 | } |
| 155 | |
| 156 | void |
| 157 | CcnxWrapper::shutdown () // called in destructor, but can called manually |
| 158 | { |
| 159 | m_executor->shutdown(); |
| 160 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 161 | { |
Alexander Afanasyev | 20bc8e2 | 2013-01-17 10:56:04 -0800 | [diff] [blame] | 162 | UniqueRecLock lock(m_mutex); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 163 | m_running = false; |
| 164 | } |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 165 | |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 166 | _LOG_DEBUG ("+++++++++SHUTDOWN+++++++"); |
| 167 | if (m_connected) |
| 168 | { |
| 169 | m_thread.join (); |
Zhenkai Zhu | 1888f74 | 2013-01-28 12:47:33 -0800 | [diff] [blame] | 170 | |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 171 | ccn_disconnect (m_handle); |
| 172 | //ccn_destroy (&m_handle); |
| 173 | m_connected = false; |
| 174 | } |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | void |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 178 | CcnxWrapper::ccnLoop () |
| 179 | { |
| 180 | static boost::mt19937 randomGenerator (static_cast<unsigned int> (std::time (0))); |
| 181 | static boost::variate_generator<boost::mt19937&, boost::uniform_int<> > rangeUniformRandom (randomGenerator, uniform_int<> (0,1000)); |
| 182 | |
| 183 | while (m_running) |
| 184 | { |
| 185 | try |
| 186 | { |
| 187 | int res = 0; |
| 188 | { |
Alexander Afanasyev | 20bc8e2 | 2013-01-17 10:56:04 -0800 | [diff] [blame] | 189 | UniqueRecLock lock(m_mutex); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 190 | res = ccn_run (m_handle, 0); |
| 191 | } |
| 192 | |
| 193 | if (!m_running) break; |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 194 | |
Alexander Afanasyev | 9c9c301 | 2013-01-25 23:44:25 -0800 | [diff] [blame] | 195 | if (res < 0) { |
| 196 | _LOG_ERROR ("ccn_run returned negative status: " << res); |
| 197 | usleep (100000); |
| 198 | continue; |
| 199 | // BOOST_THROW_EXCEPTION (CcnxOperationException() |
| 200 | // << errmsg_info_str("ccn_run returned error")); |
| 201 | } |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 202 | |
| 203 | |
| 204 | pollfd pfds[1]; |
| 205 | { |
Alexander Afanasyev | 20bc8e2 | 2013-01-17 10:56:04 -0800 | [diff] [blame] | 206 | UniqueRecLock lock(m_mutex); |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 207 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 208 | pfds[0].fd = ccn_get_connection_fd (m_handle); |
| 209 | pfds[0].events = POLLIN; |
| 210 | if (ccn_output_is_pending (m_handle)) |
| 211 | pfds[0].events |= POLLOUT; |
| 212 | } |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 213 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 214 | int ret = poll (pfds, 1, 1); |
| 215 | if (ret < 0) |
| 216 | { |
| 217 | BOOST_THROW_EXCEPTION (CcnxOperationException() << errmsg_info_str("ccnd socket failed (probably ccnd got stopped)")); |
| 218 | } |
| 219 | } |
| 220 | catch (CcnxOperationException &e) |
| 221 | { |
| 222 | // do not try reconnect for now |
Zhenkai Zhu | 6e7d4d2 | 2013-01-15 18:18:18 -0800 | [diff] [blame] | 223 | cout << *get_error_info<errmsg_info_str> (e) << endl; |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 224 | throw e; |
| 225 | /* |
| 226 | m_connected = false; |
| 227 | // probably ccnd has been stopped |
| 228 | // try reconnect with sleep |
| 229 | int interval = 1; |
| 230 | int maxInterval = 32; |
| 231 | while (m_running) |
| 232 | { |
| 233 | try |
| 234 | { |
| 235 | this_thread::sleep (boost::get_system_time () + TIME_SECONDS(interval) + TIME_MILLISECONDS (rangeUniformRandom ())); |
| 236 | |
| 237 | connectCcnd(); |
| 238 | _LOG_DEBUG("reconnect to ccnd succeeded"); |
| 239 | break; |
| 240 | } |
| 241 | catch (CcnxOperationException &e) |
| 242 | { |
| 243 | this_thread::sleep (boost::get_system_time () + TIME_SECONDS(interval) + TIME_MILLISECONDS (rangeUniformRandom ())); |
| 244 | |
| 245 | // do exponential backup for reconnect interval |
| 246 | if (interval < maxInterval) |
| 247 | { |
| 248 | interval *= 2; |
| 249 | } |
| 250 | } |
| 251 | } |
| 252 | */ |
| 253 | } |
| 254 | catch (const std::exception &exc) |
| 255 | { |
| 256 | // catch anything thrown within try block that derives from std::exception |
| 257 | std::cerr << exc.what(); |
| 258 | } |
| 259 | catch (...) |
| 260 | { |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 261 | cout << "UNKNOWN EXCEPTION !!!" << endl; |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 262 | } |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 263 | } |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 264 | } |
| 265 | |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame] | 266 | Bytes |
Alexander Afanasyev | dcfa963 | 2013-01-07 16:38:19 -0800 | [diff] [blame] | 267 | 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] | 268 | { |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 269 | { |
| 270 | UniqueRecLock lock(m_mutex); |
| 271 | if (!m_running || !m_connected) |
| 272 | { |
| 273 | _LOG_TRACE ("<< not running or connected"); |
| 274 | return Bytes (); |
| 275 | } |
| 276 | } |
| 277 | |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 278 | CcnxCharbufPtr ptr = name.toCcnxCharbuf(); |
| 279 | ccn_charbuf *pname = ptr->getBuf(); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 280 | ccn_charbuf *content = ccn_charbuf_create(); |
| 281 | |
Zhenkai Zhu | d9d03f6 | 2013-01-04 16:57:46 -0800 | [diff] [blame] | 282 | struct ccn_signing_params sp = CCN_SIGNING_PARAMS_INIT; |
| 283 | sp.freshness = freshness; |
| 284 | |
| 285 | if (ccn_sign_content(m_handle, content, pname, &sp, buf, len) != 0) |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 286 | { |
Zhenkai Zhu | d9d03f6 | 2013-01-04 16:57:46 -0800 | [diff] [blame] | 287 | BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("sign content failed")); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 288 | } |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame] | 289 | |
| 290 | Bytes bytes; |
| 291 | readRaw(bytes, content->buf, content->length); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 292 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 293 | ccn_charbuf_destroy (&content); |
| 294 | |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame] | 295 | return bytes; |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | int |
Zhenkai Zhu | bad089c | 2012-12-28 10:28:27 -0800 | [diff] [blame] | 299 | CcnxWrapper::putToCcnd (const Bytes &contentObject) |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 300 | { |
Alexander Afanasyev | d982666 | 2013-01-28 11:18:06 -0800 | [diff] [blame] | 301 | _LOG_TRACE (">> putToCcnd"); |
Alexander Afanasyev | 20bc8e2 | 2013-01-17 10:56:04 -0800 | [diff] [blame] | 302 | UniqueRecLock lock(m_mutex); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 303 | if (!m_running || !m_connected) |
Alexander Afanasyev | d982666 | 2013-01-28 11:18:06 -0800 | [diff] [blame] | 304 | { |
| 305 | _LOG_TRACE ("<< not running or connected"); |
| 306 | return -1; |
| 307 | } |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 308 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 309 | |
Zhenkai Zhu | 974c5a6 | 2012-12-28 14:15:30 -0800 | [diff] [blame] | 310 | if (ccn_put(m_handle, head(contentObject), contentObject.size()) < 0) |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 311 | { |
Alexander Afanasyev | d982666 | 2013-01-28 11:18:06 -0800 | [diff] [blame] | 312 | _LOG_ERROR ("ccn_put failed"); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 313 | // BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("ccnput failed")); |
| 314 | } |
Alexander Afanasyev | d982666 | 2013-01-28 11:18:06 -0800 | [diff] [blame] | 315 | else |
| 316 | { |
| 317 | _LOG_DEBUG ("<< putToCcnd"); |
| 318 | } |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 319 | |
| 320 | return 0; |
| 321 | } |
| 322 | |
| 323 | int |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 324 | 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] | 325 | { |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 326 | Bytes co = createContentObject(name, buf, len, freshness); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 327 | return putToCcnd(co); |
| 328 | } |
| 329 | |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame] | 330 | int |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 331 | CcnxWrapper::publishData (const Name &name, const Bytes &content, int freshness) |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 332 | { |
Alexander Afanasyev | dcfa963 | 2013-01-07 16:38:19 -0800 | [diff] [blame] | 333 | return publishData(name, head(content), content.size(), freshness); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 334 | } |
| 335 | |
Zhenkai Zhu | 5acebd7 | 2013-02-07 19:59:25 -0800 | [diff] [blame] | 336 | int |
| 337 | CcnxWrapper::publishUnsignedData(const Name &name, const Bytes &content, int freshness) |
| 338 | { |
| 339 | return publishUnsignedData(name, head(content), content.size(), freshness); |
| 340 | } |
| 341 | |
| 342 | int |
| 343 | CcnxWrapper::publishUnsignedData(const Name &name, const unsigned char *buf, size_t len, int freshness) |
| 344 | { |
| 345 | { |
| 346 | UniqueRecLock lock(m_mutex); |
| 347 | if (!m_running || !m_connected) |
| 348 | { |
| 349 | _LOG_TRACE ("<< not running or connected"); |
| 350 | return -1; |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | CcnxCharbufPtr ptr = name.toCcnxCharbuf(); |
| 355 | ccn_charbuf *pname = ptr->getBuf(); |
| 356 | ccn_charbuf *content = ccn_charbuf_create(); |
| 357 | ccn_charbuf *signed_info = ccn_charbuf_create(); |
| 358 | |
Alexander Afanasyev | 848a8e4 | 2013-02-07 21:19:18 -0800 | [diff] [blame] | 359 | static char fakeKey[PUBLISHER_KEY_SIZE]; |
Zhenkai Zhu | 5acebd7 | 2013-02-07 19:59:25 -0800 | [diff] [blame] | 360 | |
| 361 | int res = ccn_signed_info_create(signed_info, |
Alexander Afanasyev | 848a8e4 | 2013-02-07 21:19:18 -0800 | [diff] [blame] | 362 | fakeKey, PUBLISHER_KEY_SIZE, |
| 363 | NULL, |
| 364 | CCN_CONTENT_DATA, |
| 365 | freshness, |
| 366 | NULL, |
| 367 | NULL // ccnd is happy with absent key locator and key itself... ha ha |
| 368 | ); |
| 369 | ccn_pack_unsigned_ContentObject(content, pname, signed_info, buf, len); |
Zhenkai Zhu | 5acebd7 | 2013-02-07 19:59:25 -0800 | [diff] [blame] | 370 | |
| 371 | Bytes bytes; |
| 372 | readRaw(bytes, content->buf, content->length); |
| 373 | |
| 374 | ccn_charbuf_destroy (&content); |
| 375 | ccn_charbuf_destroy (&signed_info); |
| 376 | |
| 377 | return putToCcnd (bytes); |
| 378 | } |
| 379 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 380 | |
Alexander Afanasyev | 816251e | 2013-01-28 16:16:49 -0800 | [diff] [blame] | 381 | static void |
| 382 | deleterInInterestTuple (tuple<CcnxWrapper::InterestCallback *, ExecutorPtr> *tuple) |
| 383 | { |
| 384 | delete tuple->get<0> (); |
| 385 | delete tuple; |
| 386 | } |
| 387 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 388 | static ccn_upcall_res |
| 389 | incomingInterest(ccn_closure *selfp, |
| 390 | ccn_upcall_kind kind, |
| 391 | ccn_upcall_info *info) |
| 392 | { |
Alexander Afanasyev | 816251e | 2013-01-28 16:16:49 -0800 | [diff] [blame] | 393 | CcnxWrapper::InterestCallback *f; |
| 394 | ExecutorPtr executor; |
| 395 | tuple<CcnxWrapper::InterestCallback *, ExecutorPtr> *realData = reinterpret_cast< tuple<CcnxWrapper::InterestCallback *, ExecutorPtr>* > (selfp->data); |
| 396 | tie (f, executor) = *realData; |
| 397 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 398 | switch (kind) |
| 399 | { |
| 400 | case CCN_UPCALL_FINAL: // effective in unit tests |
Alexander Afanasyev | 816251e | 2013-01-28 16:16:49 -0800 | [diff] [blame] | 401 | // delete closure; |
| 402 | executor->execute (bind (deleterInInterestTuple, realData)); |
| 403 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 404 | delete selfp; |
Alexander Afanasyev | d982666 | 2013-01-28 11:18:06 -0800 | [diff] [blame] | 405 | _LOG_TRACE ("<< incomingInterest with CCN_UPCALL_FINAL"); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 406 | return CCN_UPCALL_RESULT_OK; |
| 407 | |
| 408 | case CCN_UPCALL_INTEREST: |
Alexander Afanasyev | 6aec3bf | 2013-02-05 11:25:52 -0800 | [diff] [blame] | 409 | _LOG_TRACE (">> incomingInterest upcall: " << Name(info->interest_ccnb, info->interest_comps)); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 410 | break; |
| 411 | |
| 412 | default: |
Alexander Afanasyev | 6aec3bf | 2013-02-05 11:25:52 -0800 | [diff] [blame] | 413 | _LOG_TRACE ("<< incomingInterest with CCN_UPCALL_RESULT_OK: " << Name(info->interest_ccnb, info->interest_comps)); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 414 | return CCN_UPCALL_RESULT_OK; |
| 415 | } |
| 416 | |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 417 | Name interest(info->interest_ccnb, info->interest_comps); |
Zhenkai Zhu | ff4fa8a | 2013-01-28 22:02:40 -0800 | [diff] [blame] | 418 | Selectors selectors(info->pi); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 419 | |
Zhenkai Zhu | ff4fa8a | 2013-01-28 22:02:40 -0800 | [diff] [blame] | 420 | executor->execute (bind (*f, interest, selectors)); |
Zhenkai Zhu | 1888f74 | 2013-01-28 12:47:33 -0800 | [diff] [blame] | 421 | // this will be run in executor |
Alexander Afanasyev | 816251e | 2013-01-28 16:16:49 -0800 | [diff] [blame] | 422 | // (*f) (interest); |
| 423 | // closure->runInterestCallback(interest); |
Alexander Afanasyev | d982666 | 2013-01-28 11:18:06 -0800 | [diff] [blame] | 424 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 425 | return CCN_UPCALL_RESULT_OK; |
| 426 | } |
| 427 | |
Alexander Afanasyev | 816251e | 2013-01-28 16:16:49 -0800 | [diff] [blame] | 428 | static void |
Zhenkai Zhu | ff4fa8a | 2013-01-28 22:02:40 -0800 | [diff] [blame] | 429 | deleterInDataTuple (tuple<Closure *, ExecutorPtr, Selectors> *tuple) |
Alexander Afanasyev | 816251e | 2013-01-28 16:16:49 -0800 | [diff] [blame] | 430 | { |
| 431 | delete tuple->get<0> (); |
| 432 | delete tuple; |
| 433 | } |
| 434 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 435 | static ccn_upcall_res |
| 436 | incomingData(ccn_closure *selfp, |
| 437 | ccn_upcall_kind kind, |
| 438 | ccn_upcall_info *info) |
| 439 | { |
Alexander Afanasyev | 816251e | 2013-01-28 16:16:49 -0800 | [diff] [blame] | 440 | // Closure *cp = static_cast<Closure *> (selfp->data); |
| 441 | Closure *cp; |
| 442 | ExecutorPtr executor; |
Zhenkai Zhu | ff4fa8a | 2013-01-28 22:02:40 -0800 | [diff] [blame] | 443 | Selectors selectors; |
| 444 | tuple<Closure *, ExecutorPtr, Selectors> *realData = reinterpret_cast< tuple<Closure*, ExecutorPtr, Selectors>* > (selfp->data); |
| 445 | tie (cp, executor, selectors) = *realData; |
Alexander Afanasyev | 816251e | 2013-01-28 16:16:49 -0800 | [diff] [blame] | 446 | |
Zhenkai Zhu | 79264a4 | 2013-02-07 21:49:42 -0800 | [diff] [blame] | 447 | bool verified = false; |
| 448 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 449 | switch (kind) |
| 450 | { |
| 451 | case CCN_UPCALL_FINAL: // effecitve in unit tests |
Alexander Afanasyev | 816251e | 2013-01-28 16:16:49 -0800 | [diff] [blame] | 452 | executor->execute (bind (deleterInDataTuple, realData)); |
| 453 | |
Zhenkai Zhu | 19f81de | 2013-01-04 22:27:47 -0800 | [diff] [blame] | 454 | cp = NULL; |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 455 | delete selfp; |
Alexander Afanasyev | d982666 | 2013-01-28 11:18:06 -0800 | [diff] [blame] | 456 | _LOG_TRACE ("<< incomingData with CCN_UPCALL_FINAL"); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 457 | return CCN_UPCALL_RESULT_OK; |
| 458 | |
| 459 | case CCN_UPCALL_CONTENT: |
Zhenkai Zhu | 79264a4 | 2013-02-07 21:49:42 -0800 | [diff] [blame] | 460 | verified = true; |
Alexander Afanasyev | 6aec3bf | 2013-02-05 11:25:52 -0800 | [diff] [blame] | 461 | _LOG_TRACE (">> incomingData content upcall: " << Name (info->content_ccnb, info->content_comps)); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 462 | break; |
| 463 | |
Zhenkai Zhu | 5acebd7 | 2013-02-07 19:59:25 -0800 | [diff] [blame] | 464 | case CCN_UPCALL_CONTENT_UNVERIFIED: |
| 465 | _LOG_TRACE (">> incomingData content unverified upcall: " << Name (info->content_ccnb, info->content_comps)); |
| 466 | break; |
| 467 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 468 | case CCN_UPCALL_INTEREST_TIMED_OUT: { |
Zhenkai Zhu | 8339e91 | 2013-01-18 18:10:17 -0800 | [diff] [blame] | 469 | if (cp != NULL) |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 470 | { |
Zhenkai Zhu | 8339e91 | 2013-01-18 18:10:17 -0800 | [diff] [blame] | 471 | Name interest(info->interest_ccnb, info->interest_comps); |
Alexander Afanasyev | 6aec3bf | 2013-02-05 11:25:52 -0800 | [diff] [blame] | 472 | _LOG_TRACE ("<< incomingData timeout: " << Name (info->interest_ccnb, info->interest_comps)); |
Zhenkai Zhu | ff4fa8a | 2013-01-28 22:02:40 -0800 | [diff] [blame] | 473 | executor->execute (bind (&Closure::runTimeoutCallback, cp, interest, *cp, selectors)); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 474 | } |
Alexander Afanasyev | d982666 | 2013-01-28 11:18:06 -0800 | [diff] [blame] | 475 | else |
| 476 | { |
Alexander Afanasyev | 6aec3bf | 2013-02-05 11:25:52 -0800 | [diff] [blame] | 477 | _LOG_TRACE ("<< incomingData timeout, but callback is not set...: " << Name (info->interest_ccnb, info->interest_comps)); |
Alexander Afanasyev | d982666 | 2013-01-28 11:18:06 -0800 | [diff] [blame] | 478 | } |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 479 | return CCN_UPCALL_RESULT_OK; |
| 480 | } |
| 481 | |
| 482 | default: |
Zhenkai Zhu | 5acebd7 | 2013-02-07 19:59:25 -0800 | [diff] [blame] | 483 | _LOG_TRACE(">> unknown upcall type"); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 484 | return CCN_UPCALL_RESULT_OK; |
| 485 | } |
| 486 | |
Zhenkai Zhu | 79264a4 | 2013-02-07 21:49:42 -0800 | [diff] [blame] | 487 | PcoPtr pco = make_shared<ParsedContentObject> (info->content_ccnb, info->pco->offset[CCN_PCO_E], verified); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 488 | |
Zhenkai Zhu | 1888f74 | 2013-01-28 12:47:33 -0800 | [diff] [blame] | 489 | // this will be run in executor |
Alexander Afanasyev | 816251e | 2013-01-28 16:16:49 -0800 | [diff] [blame] | 490 | executor->execute (bind (&Closure::runDataCallback, cp, pco->name (), pco)); |
Alexander Afanasyev | d982666 | 2013-01-28 11:18:06 -0800 | [diff] [blame] | 491 | _LOG_TRACE (">> incomingData"); |
| 492 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 493 | return CCN_UPCALL_RESULT_OK; |
| 494 | } |
| 495 | |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 496 | int CcnxWrapper::sendInterest (const Name &interest, const Closure &closure, const Selectors &selectors) |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 497 | { |
Alexander Afanasyev | 6aec3bf | 2013-02-05 11:25:52 -0800 | [diff] [blame] | 498 | _LOG_TRACE (">> sendInterest: " << interest); |
Zhenkai Zhu | 7599db4 | 2013-01-28 10:32:53 -0800 | [diff] [blame] | 499 | { |
| 500 | UniqueRecLock lock(m_mutex); |
| 501 | if (!m_running || !m_connected) |
Alexander Afanasyev | b3d4cd5 | 2013-01-28 11:20:52 -0800 | [diff] [blame] | 502 | { |
| 503 | _LOG_ERROR ("<< sendInterest: not running or connected"); |
| 504 | return -1; |
| 505 | } |
Zhenkai Zhu | 7599db4 | 2013-01-28 10:32:53 -0800 | [diff] [blame] | 506 | } |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 507 | |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 508 | CcnxCharbufPtr namePtr = interest.toCcnxCharbuf(); |
| 509 | ccn_charbuf *pname = namePtr->getBuf(); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 510 | ccn_closure *dataClosure = new ccn_closure; |
| 511 | |
Alexander Afanasyev | 816251e | 2013-01-28 16:16:49 -0800 | [diff] [blame] | 512 | // Closure *myClosure = new ExecutorClosure(closure, m_executor); |
| 513 | Closure *myClosure = closure.dup (); |
Zhenkai Zhu | ff4fa8a | 2013-01-28 22:02:40 -0800 | [diff] [blame] | 514 | dataClosure->data = new tuple<Closure*, ExecutorPtr, Selectors> (myClosure, m_executor, selectors); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 515 | |
| 516 | dataClosure->p = &incomingData; |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 517 | |
| 518 | CcnxCharbufPtr selectorsPtr = selectors.toCcnxCharbuf(); |
| 519 | ccn_charbuf *templ = NULL; |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 520 | if (selectorsPtr) |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 521 | { |
| 522 | templ = selectorsPtr->getBuf(); |
| 523 | } |
| 524 | |
Zhenkai Zhu | 7599db4 | 2013-01-28 10:32:53 -0800 | [diff] [blame] | 525 | UniqueRecLock lock(m_mutex); |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 526 | if (ccn_express_interest (m_handle, pname, dataClosure, templ) < 0) |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 527 | { |
Alexander Afanasyev | d982666 | 2013-01-28 11:18:06 -0800 | [diff] [blame] | 528 | _LOG_ERROR ("<< sendInterest: ccn_express_interest FAILED!!!"); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 529 | } |
| 530 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 531 | return 0; |
| 532 | } |
| 533 | |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 534 | int CcnxWrapper::setInterestFilter (const Name &prefix, const InterestCallback &interestCallback) |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 535 | { |
Alexander Afanasyev | d982666 | 2013-01-28 11:18:06 -0800 | [diff] [blame] | 536 | _LOG_TRACE (">> setInterestFilter"); |
Alexander Afanasyev | 20bc8e2 | 2013-01-17 10:56:04 -0800 | [diff] [blame] | 537 | UniqueRecLock lock(m_mutex); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 538 | if (!m_running || !m_connected) |
Zhenkai Zhu | 1888f74 | 2013-01-28 12:47:33 -0800 | [diff] [blame] | 539 | { |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 540 | return -1; |
Zhenkai Zhu | 7599db4 | 2013-01-28 10:32:53 -0800 | [diff] [blame] | 541 | } |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 542 | |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 543 | CcnxCharbufPtr ptr = prefix.toCcnxCharbuf(); |
| 544 | ccn_charbuf *pname = ptr->getBuf(); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 545 | ccn_closure *interestClosure = new ccn_closure; |
| 546 | |
Alexander Afanasyev | 816251e | 2013-01-28 16:16:49 -0800 | [diff] [blame] | 547 | // interestClosure->data = new ExecutorInterestClosure(interestCallback, m_executor); |
| 548 | |
| 549 | interestClosure->data = new tuple<CcnxWrapper::InterestCallback *, ExecutorPtr> (new InterestCallback (interestCallback), m_executor); // should be removed when closure is removed |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 550 | interestClosure->p = &incomingInterest; |
Zhenkai Zhu | 7599db4 | 2013-01-28 10:32:53 -0800 | [diff] [blame] | 551 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 552 | int ret = ccn_set_interest_filter (m_handle, pname, interestClosure); |
| 553 | if (ret < 0) |
| 554 | { |
Alexander Afanasyev | d982666 | 2013-01-28 11:18:06 -0800 | [diff] [blame] | 555 | _LOG_ERROR ("<< setInterestFilter: ccn_set_interest_filter FAILED"); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 556 | } |
| 557 | |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 558 | m_registeredInterests.insert(pair<Name, InterestCallback>(prefix, interestCallback)); |
Alexander Afanasyev | 816251e | 2013-01-28 16:16:49 -0800 | [diff] [blame] | 559 | |
Alexander Afanasyev | d982666 | 2013-01-28 11:18:06 -0800 | [diff] [blame] | 560 | _LOG_TRACE ("<< setInterestFilter"); |
Alexander Afanasyev | dcfa963 | 2013-01-07 16:38:19 -0800 | [diff] [blame] | 561 | |
| 562 | return ret; |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | void |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 566 | CcnxWrapper::clearInterestFilter (const Name &prefix) |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 567 | { |
Alexander Afanasyev | d982666 | 2013-01-28 11:18:06 -0800 | [diff] [blame] | 568 | _LOG_TRACE (">> clearInterestFilter"); |
Alexander Afanasyev | 20bc8e2 | 2013-01-17 10:56:04 -0800 | [diff] [blame] | 569 | UniqueRecLock lock(m_mutex); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 570 | if (!m_running || !m_connected) |
| 571 | return; |
| 572 | |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 573 | CcnxCharbufPtr ptr = prefix.toCcnxCharbuf(); |
| 574 | ccn_charbuf *pname = ptr->getBuf(); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 575 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 576 | int ret = ccn_set_interest_filter (m_handle, pname, 0); |
| 577 | if (ret < 0) |
| 578 | { |
| 579 | } |
| 580 | |
| 581 | m_registeredInterests.erase(prefix); |
Alexander Afanasyev | d982666 | 2013-01-28 11:18:06 -0800 | [diff] [blame] | 582 | |
| 583 | _LOG_TRACE ("<< clearInterestFilter"); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 584 | } |
| 585 | |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 586 | Name |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 587 | CcnxWrapper::getLocalPrefix () |
| 588 | { |
| 589 | struct ccn * tmp_handle = ccn_create (); |
| 590 | int res = ccn_connect (tmp_handle, NULL); |
| 591 | if (res < 0) |
| 592 | { |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 593 | return Name(); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 594 | } |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 595 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 596 | string retval = ""; |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 597 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 598 | struct ccn_charbuf *templ = ccn_charbuf_create(); |
| 599 | ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG); |
| 600 | ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG); |
| 601 | ccn_charbuf_append_closer(templ); /* </Name> */ |
| 602 | // XXX - use pubid if possible |
| 603 | ccn_charbuf_append_tt(templ, CCN_DTAG_MaxSuffixComponents, CCN_DTAG); |
| 604 | ccnb_append_number(templ, 1); |
| 605 | ccn_charbuf_append_closer(templ); /* </MaxSuffixComponents> */ |
| 606 | ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", 2); |
| 607 | ccn_charbuf_append_closer(templ); /* </Interest> */ |
| 608 | |
| 609 | struct ccn_charbuf *name = ccn_charbuf_create (); |
| 610 | res = ccn_name_from_uri (name, "/local/ndn/prefix"); |
| 611 | if (res < 0) { |
| 612 | } |
| 613 | else |
| 614 | { |
| 615 | struct ccn_fetch *fetch = ccn_fetch_new (tmp_handle); |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 616 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 617 | 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] | 618 | NULL, 4, CCN_V_HIGHEST, 0); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 619 | if (stream == NULL) { |
| 620 | } |
| 621 | else |
| 622 | { |
| 623 | ostringstream os; |
| 624 | |
| 625 | int counter = 0; |
| 626 | char buf[256]; |
| 627 | while (true) { |
| 628 | res = ccn_fetch_read (stream, buf, sizeof(buf)); |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 629 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 630 | if (res == 0) { |
| 631 | break; |
| 632 | } |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 633 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 634 | if (res > 0) { |
| 635 | os << string(buf, res); |
| 636 | } else if (res == CCN_FETCH_READ_NONE) { |
| 637 | if (counter < 2) |
| 638 | { |
| 639 | ccn_run(tmp_handle, 1000); |
| 640 | counter ++; |
| 641 | } |
| 642 | else |
| 643 | { |
| 644 | break; |
| 645 | } |
| 646 | } else if (res == CCN_FETCH_READ_END) { |
| 647 | break; |
| 648 | } else if (res == CCN_FETCH_READ_TIMEOUT) { |
| 649 | break; |
| 650 | } else { |
| 651 | break; |
| 652 | } |
| 653 | } |
| 654 | retval = os.str (); |
| 655 | stream = ccn_fetch_close(stream); |
| 656 | } |
| 657 | fetch = ccn_fetch_destroy(fetch); |
| 658 | } |
| 659 | |
| 660 | ccn_charbuf_destroy (&name); |
| 661 | |
| 662 | ccn_disconnect (tmp_handle); |
| 663 | ccn_destroy (&tmp_handle); |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 664 | |
Zhenkai Zhu | cbbb888 | 2013-01-25 13:49:12 -0800 | [diff] [blame] | 665 | boost::algorithm::trim(retval); |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 666 | return Name(retval); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 667 | } |
| 668 | |
Zhenkai Zhu | 79264a4 | 2013-02-07 21:49:42 -0800 | [diff] [blame] | 669 | bool |
| 670 | CcnxWrapper::verifyPco(PcoPtr &pco) |
| 671 | { |
Zhenkai Zhu | 5957c0d | 2013-02-08 13:47:52 -0800 | [diff] [blame^] | 672 | bool verified = (ccn_verify_content(m_handle, pco->msg(), (ccn_parsed_ContentObject *)pco->pco()) == 0); |
Zhenkai Zhu | 79264a4 | 2013-02-07 21:49:42 -0800 | [diff] [blame] | 673 | pco->setVerified(verified); |
| 674 | return verified; |
| 675 | } |
| 676 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 677 | } |