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 | { |
Alexander Afanasyev | 17ae9e3 | 2013-02-19 18:09:08 -0800 | [diff] [blame] | 120 | if (m_handle != 0) { |
| 121 | ccn_disconnect (m_handle); |
Zhenkai Zhu | 3b82d43 | 2013-01-03 22:48:40 -0800 | [diff] [blame] | 122 | //ccn_destroy (&m_handle); |
Alexander Afanasyev | 17ae9e3 | 2013-02-19 18:09:08 -0800 | [diff] [blame] | 123 | } |
| 124 | else |
| 125 | { |
| 126 | m_handle = ccn_create (); |
| 127 | } |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 128 | |
Alexander Afanasyev | 17ae9e3 | 2013-02-19 18:09:08 -0800 | [diff] [blame] | 129 | UniqueRecLock lock(m_mutex); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 130 | if (ccn_connect(m_handle, NULL) < 0) |
| 131 | { |
| 132 | BOOST_THROW_EXCEPTION (CcnxOperationException() << errmsg_info_str("connection to ccnd failed")); |
| 133 | } |
| 134 | m_connected = true; |
| 135 | |
Alexander Afanasyev | 17ae9e3 | 2013-02-19 18:09:08 -0800 | [diff] [blame] | 136 | if (!m_registeredInterests.empty()) |
| 137 | { |
| 138 | for (map<Name, InterestCallback>::const_iterator it = m_registeredInterests.begin(); it != m_registeredInterests.end(); ++it) |
| 139 | { |
| 140 | clearInterestFilter(it->first, false); |
| 141 | setInterestFilter(it->first, it->second, false); |
| 142 | } |
| 143 | } |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | CcnxWrapper::~CcnxWrapper() |
| 147 | { |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 148 | shutdown (); |
| 149 | } |
| 150 | |
| 151 | void |
| 152 | CcnxWrapper::start () // called automatically in constructor |
| 153 | { |
| 154 | connectCcnd(); |
| 155 | m_thread = thread (&CcnxWrapper::ccnLoop, this); |
| 156 | m_executor->start(); |
| 157 | } |
| 158 | |
| 159 | void |
| 160 | CcnxWrapper::shutdown () // called in destructor, but can called manually |
| 161 | { |
| 162 | m_executor->shutdown(); |
| 163 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 164 | { |
Alexander Afanasyev | 20bc8e2 | 2013-01-17 10:56:04 -0800 | [diff] [blame] | 165 | UniqueRecLock lock(m_mutex); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 166 | m_running = false; |
| 167 | } |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 168 | |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 169 | _LOG_DEBUG ("+++++++++SHUTDOWN+++++++"); |
| 170 | if (m_connected) |
| 171 | { |
| 172 | m_thread.join (); |
Zhenkai Zhu | 1888f74 | 2013-01-28 12:47:33 -0800 | [diff] [blame] | 173 | |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 174 | ccn_disconnect (m_handle); |
| 175 | //ccn_destroy (&m_handle); |
| 176 | m_connected = false; |
| 177 | } |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | void |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 181 | CcnxWrapper::ccnLoop () |
| 182 | { |
| 183 | static boost::mt19937 randomGenerator (static_cast<unsigned int> (std::time (0))); |
| 184 | static boost::variate_generator<boost::mt19937&, boost::uniform_int<> > rangeUniformRandom (randomGenerator, uniform_int<> (0,1000)); |
| 185 | |
| 186 | while (m_running) |
| 187 | { |
| 188 | try |
| 189 | { |
| 190 | int res = 0; |
| 191 | { |
Alexander Afanasyev | 20bc8e2 | 2013-01-17 10:56:04 -0800 | [diff] [blame] | 192 | UniqueRecLock lock(m_mutex); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 193 | res = ccn_run (m_handle, 0); |
| 194 | } |
| 195 | |
| 196 | if (!m_running) break; |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 197 | |
Alexander Afanasyev | 9c9c301 | 2013-01-25 23:44:25 -0800 | [diff] [blame] | 198 | if (res < 0) { |
| 199 | _LOG_ERROR ("ccn_run returned negative status: " << res); |
Alexander Afanasyev | 17ae9e3 | 2013-02-19 18:09:08 -0800 | [diff] [blame] | 200 | |
| 201 | BOOST_THROW_EXCEPTION (CcnxOperationException() |
| 202 | << errmsg_info_str("ccn_run returned error")); |
Alexander Afanasyev | 9c9c301 | 2013-01-25 23:44:25 -0800 | [diff] [blame] | 203 | } |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 204 | |
| 205 | |
| 206 | pollfd pfds[1]; |
| 207 | { |
Alexander Afanasyev | 20bc8e2 | 2013-01-17 10:56:04 -0800 | [diff] [blame] | 208 | UniqueRecLock lock(m_mutex); |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 209 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 210 | pfds[0].fd = ccn_get_connection_fd (m_handle); |
| 211 | pfds[0].events = POLLIN; |
| 212 | if (ccn_output_is_pending (m_handle)) |
| 213 | pfds[0].events |= POLLOUT; |
| 214 | } |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 215 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 216 | int ret = poll (pfds, 1, 1); |
| 217 | if (ret < 0) |
| 218 | { |
| 219 | BOOST_THROW_EXCEPTION (CcnxOperationException() << errmsg_info_str("ccnd socket failed (probably ccnd got stopped)")); |
| 220 | } |
| 221 | } |
| 222 | catch (CcnxOperationException &e) |
| 223 | { |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 224 | m_connected = false; |
| 225 | // probably ccnd has been stopped |
| 226 | // try reconnect with sleep |
| 227 | int interval = 1; |
| 228 | int maxInterval = 32; |
| 229 | while (m_running) |
| 230 | { |
| 231 | try |
| 232 | { |
Alexander Afanasyev | 17ae9e3 | 2013-02-19 18:09:08 -0800 | [diff] [blame] | 233 | this_thread::sleep (boost::get_system_time () + boost::posix_time::seconds (interval) + boost::posix_time::milliseconds (rangeUniformRandom ())); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 234 | |
Alexander Afanasyev | 17ae9e3 | 2013-02-19 18:09:08 -0800 | [diff] [blame] | 235 | connectCcnd (); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 236 | _LOG_DEBUG("reconnect to ccnd succeeded"); |
| 237 | break; |
| 238 | } |
| 239 | catch (CcnxOperationException &e) |
| 240 | { |
Alexander Afanasyev | 17ae9e3 | 2013-02-19 18:09:08 -0800 | [diff] [blame] | 241 | this_thread::sleep (boost::get_system_time () + boost::posix_time::seconds (interval) + boost::posix_time::milliseconds (rangeUniformRandom ())); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 242 | |
| 243 | // do exponential backup for reconnect interval |
| 244 | if (interval < maxInterval) |
| 245 | { |
| 246 | interval *= 2; |
| 247 | } |
| 248 | } |
| 249 | } |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 250 | } |
| 251 | catch (const std::exception &exc) |
| 252 | { |
| 253 | // catch anything thrown within try block that derives from std::exception |
| 254 | std::cerr << exc.what(); |
| 255 | } |
| 256 | catch (...) |
| 257 | { |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 258 | cout << "UNKNOWN EXCEPTION !!!" << endl; |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 259 | } |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 260 | } |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 261 | } |
| 262 | |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame] | 263 | Bytes |
Zhenkai Zhu | 589be8e | 2013-02-24 20:51:12 -0800 | [diff] [blame] | 264 | CcnxWrapper::createContentObject(const Name &name, const void *buf, size_t len, int freshness, const Name &keyName) |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 265 | { |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 266 | { |
| 267 | UniqueRecLock lock(m_mutex); |
| 268 | if (!m_running || !m_connected) |
| 269 | { |
| 270 | _LOG_TRACE ("<< not running or connected"); |
| 271 | return Bytes (); |
| 272 | } |
| 273 | } |
| 274 | |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 275 | CcnxCharbufPtr ptr = name.toCcnxCharbuf(); |
| 276 | ccn_charbuf *pname = ptr->getBuf(); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 277 | ccn_charbuf *content = ccn_charbuf_create(); |
| 278 | |
Zhenkai Zhu | d9d03f6 | 2013-01-04 16:57:46 -0800 | [diff] [blame] | 279 | struct ccn_signing_params sp = CCN_SIGNING_PARAMS_INIT; |
| 280 | sp.freshness = freshness; |
| 281 | |
Zhenkai Zhu | 589be8e | 2013-02-24 20:51:12 -0800 | [diff] [blame] | 282 | if (keyName.size() > 0) |
| 283 | { |
| 284 | if (sp.template_ccnb == NULL) |
| 285 | { |
| 286 | sp.template_ccnb = ccn_charbuf_create(); |
| 287 | ccn_charbuf_append_tt(sp.template_ccnb, CCN_DTAG_SignedInfo, CCN_DTAG); |
| 288 | } |
| 289 | // no idea what the following 3 lines do, but it was there |
| 290 | else if (sp.template_ccnb->length > 0) { |
| 291 | sp.template_ccnb->length--; |
| 292 | } |
| 293 | ccn_charbuf_append_tt(sp.template_ccnb, CCN_DTAG_KeyLocator, CCN_DTAG); |
| 294 | ccn_charbuf_append_tt(sp.template_ccnb, CCN_DTAG_KeyName, CCN_DTAG); |
| 295 | CcnxCharbufPtr keyPtr = keyName.toCcnxCharbuf(); |
| 296 | ccn_charbuf *keyBuf = keyPtr->getBuf(); |
| 297 | ccn_charbuf_append(sp.template_ccnb, keyBuf->buf, keyBuf->length); |
| 298 | ccn_charbuf_append_closer(sp.template_ccnb); // </KeyName> |
| 299 | ccn_charbuf_append_closer(sp.template_ccnb); // </KeyLocator> |
| 300 | sp.sp_flags |= CCN_SP_TEMPL_KEY_LOCATOR; |
| 301 | ccn_charbuf_append_closer(sp.template_ccnb); // </SignedInfo> |
| 302 | } |
| 303 | |
Zhenkai Zhu | d9d03f6 | 2013-01-04 16:57:46 -0800 | [diff] [blame] | 304 | if (ccn_sign_content(m_handle, content, pname, &sp, buf, len) != 0) |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 305 | { |
Zhenkai Zhu | d9d03f6 | 2013-01-04 16:57:46 -0800 | [diff] [blame] | 306 | BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("sign content failed")); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 307 | } |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame] | 308 | |
| 309 | Bytes bytes; |
| 310 | readRaw(bytes, content->buf, content->length); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 311 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 312 | ccn_charbuf_destroy (&content); |
Zhenkai Zhu | 589be8e | 2013-02-24 20:51:12 -0800 | [diff] [blame] | 313 | if (sp.template_ccnb != NULL) |
| 314 | { |
| 315 | ccn_charbuf_destroy (&sp.template_ccnb); |
| 316 | } |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 317 | |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame] | 318 | return bytes; |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | int |
Zhenkai Zhu | bad089c | 2012-12-28 10:28:27 -0800 | [diff] [blame] | 322 | CcnxWrapper::putToCcnd (const Bytes &contentObject) |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 323 | { |
Alexander Afanasyev | d982666 | 2013-01-28 11:18:06 -0800 | [diff] [blame] | 324 | _LOG_TRACE (">> putToCcnd"); |
Alexander Afanasyev | 20bc8e2 | 2013-01-17 10:56:04 -0800 | [diff] [blame] | 325 | UniqueRecLock lock(m_mutex); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 326 | if (!m_running || !m_connected) |
Alexander Afanasyev | d982666 | 2013-01-28 11:18:06 -0800 | [diff] [blame] | 327 | { |
| 328 | _LOG_TRACE ("<< not running or connected"); |
| 329 | return -1; |
| 330 | } |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 331 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 332 | |
Zhenkai Zhu | 974c5a6 | 2012-12-28 14:15:30 -0800 | [diff] [blame] | 333 | if (ccn_put(m_handle, head(contentObject), contentObject.size()) < 0) |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 334 | { |
Alexander Afanasyev | d982666 | 2013-01-28 11:18:06 -0800 | [diff] [blame] | 335 | _LOG_ERROR ("ccn_put failed"); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 336 | // BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("ccnput failed")); |
| 337 | } |
Alexander Afanasyev | d982666 | 2013-01-28 11:18:06 -0800 | [diff] [blame] | 338 | else |
| 339 | { |
| 340 | _LOG_DEBUG ("<< putToCcnd"); |
| 341 | } |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 342 | |
| 343 | return 0; |
| 344 | } |
| 345 | |
| 346 | int |
Zhenkai Zhu | 589be8e | 2013-02-24 20:51:12 -0800 | [diff] [blame] | 347 | CcnxWrapper::publishData (const Name &name, const unsigned char *buf, size_t len, int freshness, const Name &keyName) |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 348 | { |
Zhenkai Zhu | 589be8e | 2013-02-24 20:51:12 -0800 | [diff] [blame] | 349 | Bytes co = createContentObject(name, buf, len, freshness, keyName); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 350 | return putToCcnd(co); |
| 351 | } |
| 352 | |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame] | 353 | int |
Zhenkai Zhu | 5acebd7 | 2013-02-07 19:59:25 -0800 | [diff] [blame] | 354 | CcnxWrapper::publishUnsignedData(const Name &name, const unsigned char *buf, size_t len, int freshness) |
| 355 | { |
| 356 | { |
| 357 | UniqueRecLock lock(m_mutex); |
| 358 | if (!m_running || !m_connected) |
| 359 | { |
| 360 | _LOG_TRACE ("<< not running or connected"); |
| 361 | return -1; |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | CcnxCharbufPtr ptr = name.toCcnxCharbuf(); |
| 366 | ccn_charbuf *pname = ptr->getBuf(); |
| 367 | ccn_charbuf *content = ccn_charbuf_create(); |
| 368 | ccn_charbuf *signed_info = ccn_charbuf_create(); |
| 369 | |
Alexander Afanasyev | 848a8e4 | 2013-02-07 21:19:18 -0800 | [diff] [blame] | 370 | static char fakeKey[PUBLISHER_KEY_SIZE]; |
Zhenkai Zhu | 5acebd7 | 2013-02-07 19:59:25 -0800 | [diff] [blame] | 371 | |
| 372 | int res = ccn_signed_info_create(signed_info, |
Alexander Afanasyev | 848a8e4 | 2013-02-07 21:19:18 -0800 | [diff] [blame] | 373 | fakeKey, PUBLISHER_KEY_SIZE, |
| 374 | NULL, |
| 375 | CCN_CONTENT_DATA, |
| 376 | freshness, |
| 377 | NULL, |
| 378 | NULL // ccnd is happy with absent key locator and key itself... ha ha |
| 379 | ); |
| 380 | ccn_pack_unsigned_ContentObject(content, pname, signed_info, buf, len); |
Zhenkai Zhu | 5acebd7 | 2013-02-07 19:59:25 -0800 | [diff] [blame] | 381 | |
| 382 | Bytes bytes; |
| 383 | readRaw(bytes, content->buf, content->length); |
| 384 | |
| 385 | ccn_charbuf_destroy (&content); |
| 386 | ccn_charbuf_destroy (&signed_info); |
| 387 | |
| 388 | return putToCcnd (bytes); |
| 389 | } |
| 390 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 391 | |
Alexander Afanasyev | 816251e | 2013-01-28 16:16:49 -0800 | [diff] [blame] | 392 | static void |
| 393 | deleterInInterestTuple (tuple<CcnxWrapper::InterestCallback *, ExecutorPtr> *tuple) |
| 394 | { |
| 395 | delete tuple->get<0> (); |
| 396 | delete tuple; |
| 397 | } |
| 398 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 399 | static ccn_upcall_res |
| 400 | incomingInterest(ccn_closure *selfp, |
| 401 | ccn_upcall_kind kind, |
| 402 | ccn_upcall_info *info) |
| 403 | { |
Alexander Afanasyev | 816251e | 2013-01-28 16:16:49 -0800 | [diff] [blame] | 404 | CcnxWrapper::InterestCallback *f; |
| 405 | ExecutorPtr executor; |
| 406 | tuple<CcnxWrapper::InterestCallback *, ExecutorPtr> *realData = reinterpret_cast< tuple<CcnxWrapper::InterestCallback *, ExecutorPtr>* > (selfp->data); |
| 407 | tie (f, executor) = *realData; |
| 408 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 409 | switch (kind) |
| 410 | { |
| 411 | case CCN_UPCALL_FINAL: // effective in unit tests |
Alexander Afanasyev | 816251e | 2013-01-28 16:16:49 -0800 | [diff] [blame] | 412 | // delete closure; |
| 413 | executor->execute (bind (deleterInInterestTuple, realData)); |
| 414 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 415 | delete selfp; |
Alexander Afanasyev | d982666 | 2013-01-28 11:18:06 -0800 | [diff] [blame] | 416 | _LOG_TRACE ("<< incomingInterest with CCN_UPCALL_FINAL"); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 417 | return CCN_UPCALL_RESULT_OK; |
| 418 | |
| 419 | case CCN_UPCALL_INTEREST: |
Alexander Afanasyev | 6aec3bf | 2013-02-05 11:25:52 -0800 | [diff] [blame] | 420 | _LOG_TRACE (">> incomingInterest upcall: " << Name(info->interest_ccnb, info->interest_comps)); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 421 | break; |
| 422 | |
| 423 | default: |
Alexander Afanasyev | 6aec3bf | 2013-02-05 11:25:52 -0800 | [diff] [blame] | 424 | _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] | 425 | return CCN_UPCALL_RESULT_OK; |
| 426 | } |
| 427 | |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 428 | Name interest(info->interest_ccnb, info->interest_comps); |
Zhenkai Zhu | ff4fa8a | 2013-01-28 22:02:40 -0800 | [diff] [blame] | 429 | Selectors selectors(info->pi); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 430 | |
Zhenkai Zhu | ff4fa8a | 2013-01-28 22:02:40 -0800 | [diff] [blame] | 431 | executor->execute (bind (*f, interest, selectors)); |
Zhenkai Zhu | 1888f74 | 2013-01-28 12:47:33 -0800 | [diff] [blame] | 432 | // this will be run in executor |
Alexander Afanasyev | 816251e | 2013-01-28 16:16:49 -0800 | [diff] [blame] | 433 | // (*f) (interest); |
| 434 | // closure->runInterestCallback(interest); |
Alexander Afanasyev | d982666 | 2013-01-28 11:18:06 -0800 | [diff] [blame] | 435 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 436 | return CCN_UPCALL_RESULT_OK; |
| 437 | } |
| 438 | |
Alexander Afanasyev | 816251e | 2013-01-28 16:16:49 -0800 | [diff] [blame] | 439 | static void |
Zhenkai Zhu | ff4fa8a | 2013-01-28 22:02:40 -0800 | [diff] [blame] | 440 | deleterInDataTuple (tuple<Closure *, ExecutorPtr, Selectors> *tuple) |
Alexander Afanasyev | 816251e | 2013-01-28 16:16:49 -0800 | [diff] [blame] | 441 | { |
| 442 | delete tuple->get<0> (); |
| 443 | delete tuple; |
| 444 | } |
| 445 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 446 | static ccn_upcall_res |
| 447 | incomingData(ccn_closure *selfp, |
| 448 | ccn_upcall_kind kind, |
| 449 | ccn_upcall_info *info) |
| 450 | { |
Alexander Afanasyev | 816251e | 2013-01-28 16:16:49 -0800 | [diff] [blame] | 451 | // Closure *cp = static_cast<Closure *> (selfp->data); |
| 452 | Closure *cp; |
| 453 | ExecutorPtr executor; |
Zhenkai Zhu | ff4fa8a | 2013-01-28 22:02:40 -0800 | [diff] [blame] | 454 | Selectors selectors; |
| 455 | tuple<Closure *, ExecutorPtr, Selectors> *realData = reinterpret_cast< tuple<Closure*, ExecutorPtr, Selectors>* > (selfp->data); |
| 456 | tie (cp, executor, selectors) = *realData; |
Alexander Afanasyev | 816251e | 2013-01-28 16:16:49 -0800 | [diff] [blame] | 457 | |
Zhenkai Zhu | dd1f14d | 2013-03-13 12:04:28 -0700 | [diff] [blame^] | 458 | bool checked = false; |
Zhenkai Zhu | 79264a4 | 2013-02-07 21:49:42 -0800 | [diff] [blame] | 459 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 460 | switch (kind) |
| 461 | { |
| 462 | case CCN_UPCALL_FINAL: // effecitve in unit tests |
Alexander Afanasyev | 816251e | 2013-01-28 16:16:49 -0800 | [diff] [blame] | 463 | executor->execute (bind (deleterInDataTuple, realData)); |
| 464 | |
Zhenkai Zhu | 19f81de | 2013-01-04 22:27:47 -0800 | [diff] [blame] | 465 | cp = NULL; |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 466 | delete selfp; |
Alexander Afanasyev | d982666 | 2013-01-28 11:18:06 -0800 | [diff] [blame] | 467 | _LOG_TRACE ("<< incomingData with CCN_UPCALL_FINAL"); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 468 | return CCN_UPCALL_RESULT_OK; |
| 469 | |
| 470 | case CCN_UPCALL_CONTENT: |
Zhenkai Zhu | dd1f14d | 2013-03-13 12:04:28 -0700 | [diff] [blame^] | 471 | checked = true; |
Alexander Afanasyev | 6aec3bf | 2013-02-05 11:25:52 -0800 | [diff] [blame] | 472 | _LOG_TRACE (">> incomingData content upcall: " << Name (info->content_ccnb, info->content_comps)); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 473 | break; |
| 474 | |
Zhenkai Zhu | 5acebd7 | 2013-02-07 19:59:25 -0800 | [diff] [blame] | 475 | case CCN_UPCALL_CONTENT_UNVERIFIED: |
| 476 | _LOG_TRACE (">> incomingData content unverified upcall: " << Name (info->content_ccnb, info->content_comps)); |
| 477 | break; |
| 478 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 479 | case CCN_UPCALL_INTEREST_TIMED_OUT: { |
Zhenkai Zhu | 8339e91 | 2013-01-18 18:10:17 -0800 | [diff] [blame] | 480 | if (cp != NULL) |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 481 | { |
Zhenkai Zhu | 8339e91 | 2013-01-18 18:10:17 -0800 | [diff] [blame] | 482 | Name interest(info->interest_ccnb, info->interest_comps); |
Alexander Afanasyev | 6aec3bf | 2013-02-05 11:25:52 -0800 | [diff] [blame] | 483 | _LOG_TRACE ("<< incomingData timeout: " << Name (info->interest_ccnb, info->interest_comps)); |
Zhenkai Zhu | ff4fa8a | 2013-01-28 22:02:40 -0800 | [diff] [blame] | 484 | executor->execute (bind (&Closure::runTimeoutCallback, cp, interest, *cp, selectors)); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 485 | } |
Alexander Afanasyev | d982666 | 2013-01-28 11:18:06 -0800 | [diff] [blame] | 486 | else |
| 487 | { |
Alexander Afanasyev | 6aec3bf | 2013-02-05 11:25:52 -0800 | [diff] [blame] | 488 | _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] | 489 | } |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 490 | return CCN_UPCALL_RESULT_OK; |
| 491 | } |
| 492 | |
| 493 | default: |
Zhenkai Zhu | 5acebd7 | 2013-02-07 19:59:25 -0800 | [diff] [blame] | 494 | _LOG_TRACE(">> unknown upcall type"); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 495 | return CCN_UPCALL_RESULT_OK; |
| 496 | } |
| 497 | |
Zhenkai Zhu | dd1f14d | 2013-03-13 12:04:28 -0700 | [diff] [blame^] | 498 | PcoPtr pco = make_shared<ParsedContentObject> (info->content_ccnb, info->pco->offset[CCN_PCO_E], checked); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 499 | |
Zhenkai Zhu | 1888f74 | 2013-01-28 12:47:33 -0800 | [diff] [blame] | 500 | // this will be run in executor |
Alexander Afanasyev | 816251e | 2013-01-28 16:16:49 -0800 | [diff] [blame] | 501 | executor->execute (bind (&Closure::runDataCallback, cp, pco->name (), pco)); |
Alexander Afanasyev | d982666 | 2013-01-28 11:18:06 -0800 | [diff] [blame] | 502 | _LOG_TRACE (">> incomingData"); |
| 503 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 504 | return CCN_UPCALL_RESULT_OK; |
| 505 | } |
| 506 | |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 507 | int CcnxWrapper::sendInterest (const Name &interest, const Closure &closure, const Selectors &selectors) |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 508 | { |
Alexander Afanasyev | 6aec3bf | 2013-02-05 11:25:52 -0800 | [diff] [blame] | 509 | _LOG_TRACE (">> sendInterest: " << interest); |
Zhenkai Zhu | 7599db4 | 2013-01-28 10:32:53 -0800 | [diff] [blame] | 510 | { |
| 511 | UniqueRecLock lock(m_mutex); |
| 512 | if (!m_running || !m_connected) |
Alexander Afanasyev | b3d4cd5 | 2013-01-28 11:20:52 -0800 | [diff] [blame] | 513 | { |
| 514 | _LOG_ERROR ("<< sendInterest: not running or connected"); |
| 515 | return -1; |
| 516 | } |
Zhenkai Zhu | 7599db4 | 2013-01-28 10:32:53 -0800 | [diff] [blame] | 517 | } |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 518 | |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 519 | CcnxCharbufPtr namePtr = interest.toCcnxCharbuf(); |
| 520 | ccn_charbuf *pname = namePtr->getBuf(); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 521 | ccn_closure *dataClosure = new ccn_closure; |
| 522 | |
Alexander Afanasyev | 816251e | 2013-01-28 16:16:49 -0800 | [diff] [blame] | 523 | // Closure *myClosure = new ExecutorClosure(closure, m_executor); |
| 524 | Closure *myClosure = closure.dup (); |
Zhenkai Zhu | ff4fa8a | 2013-01-28 22:02:40 -0800 | [diff] [blame] | 525 | dataClosure->data = new tuple<Closure*, ExecutorPtr, Selectors> (myClosure, m_executor, selectors); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 526 | |
| 527 | dataClosure->p = &incomingData; |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 528 | |
| 529 | CcnxCharbufPtr selectorsPtr = selectors.toCcnxCharbuf(); |
| 530 | ccn_charbuf *templ = NULL; |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 531 | if (selectorsPtr) |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 532 | { |
| 533 | templ = selectorsPtr->getBuf(); |
| 534 | } |
| 535 | |
Zhenkai Zhu | 7599db4 | 2013-01-28 10:32:53 -0800 | [diff] [blame] | 536 | UniqueRecLock lock(m_mutex); |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 537 | if (ccn_express_interest (m_handle, pname, dataClosure, templ) < 0) |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 538 | { |
Alexander Afanasyev | d982666 | 2013-01-28 11:18:06 -0800 | [diff] [blame] | 539 | _LOG_ERROR ("<< sendInterest: ccn_express_interest FAILED!!!"); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 540 | } |
| 541 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 542 | return 0; |
| 543 | } |
| 544 | |
Alexander Afanasyev | 17ae9e3 | 2013-02-19 18:09:08 -0800 | [diff] [blame] | 545 | int CcnxWrapper::setInterestFilter (const Name &prefix, const InterestCallback &interestCallback, bool record/* = true*/) |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 546 | { |
Alexander Afanasyev | d982666 | 2013-01-28 11:18:06 -0800 | [diff] [blame] | 547 | _LOG_TRACE (">> setInterestFilter"); |
Alexander Afanasyev | 20bc8e2 | 2013-01-17 10:56:04 -0800 | [diff] [blame] | 548 | UniqueRecLock lock(m_mutex); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 549 | if (!m_running || !m_connected) |
Zhenkai Zhu | 1888f74 | 2013-01-28 12:47:33 -0800 | [diff] [blame] | 550 | { |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 551 | return -1; |
Zhenkai Zhu | 7599db4 | 2013-01-28 10:32:53 -0800 | [diff] [blame] | 552 | } |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 553 | |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 554 | CcnxCharbufPtr ptr = prefix.toCcnxCharbuf(); |
| 555 | ccn_charbuf *pname = ptr->getBuf(); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 556 | ccn_closure *interestClosure = new ccn_closure; |
| 557 | |
Alexander Afanasyev | 816251e | 2013-01-28 16:16:49 -0800 | [diff] [blame] | 558 | // interestClosure->data = new ExecutorInterestClosure(interestCallback, m_executor); |
| 559 | |
| 560 | 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] | 561 | interestClosure->p = &incomingInterest; |
Zhenkai Zhu | 7599db4 | 2013-01-28 10:32:53 -0800 | [diff] [blame] | 562 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 563 | int ret = ccn_set_interest_filter (m_handle, pname, interestClosure); |
| 564 | if (ret < 0) |
| 565 | { |
Alexander Afanasyev | d982666 | 2013-01-28 11:18:06 -0800 | [diff] [blame] | 566 | _LOG_ERROR ("<< setInterestFilter: ccn_set_interest_filter FAILED"); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 567 | } |
| 568 | |
Alexander Afanasyev | 17ae9e3 | 2013-02-19 18:09:08 -0800 | [diff] [blame] | 569 | if (record) |
| 570 | { |
| 571 | m_registeredInterests.insert(pair<Name, InterestCallback>(prefix, interestCallback)); |
| 572 | } |
Alexander Afanasyev | 816251e | 2013-01-28 16:16:49 -0800 | [diff] [blame] | 573 | |
Alexander Afanasyev | d982666 | 2013-01-28 11:18:06 -0800 | [diff] [blame] | 574 | _LOG_TRACE ("<< setInterestFilter"); |
Alexander Afanasyev | dcfa963 | 2013-01-07 16:38:19 -0800 | [diff] [blame] | 575 | |
| 576 | return ret; |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | void |
Alexander Afanasyev | 17ae9e3 | 2013-02-19 18:09:08 -0800 | [diff] [blame] | 580 | CcnxWrapper::clearInterestFilter (const Name &prefix, bool record/* = true*/) |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 581 | { |
Alexander Afanasyev | d982666 | 2013-01-28 11:18:06 -0800 | [diff] [blame] | 582 | _LOG_TRACE (">> clearInterestFilter"); |
Alexander Afanasyev | 20bc8e2 | 2013-01-17 10:56:04 -0800 | [diff] [blame] | 583 | UniqueRecLock lock(m_mutex); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 584 | if (!m_running || !m_connected) |
| 585 | return; |
| 586 | |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 587 | CcnxCharbufPtr ptr = prefix.toCcnxCharbuf(); |
| 588 | ccn_charbuf *pname = ptr->getBuf(); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 589 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 590 | int ret = ccn_set_interest_filter (m_handle, pname, 0); |
| 591 | if (ret < 0) |
| 592 | { |
| 593 | } |
| 594 | |
Alexander Afanasyev | 17ae9e3 | 2013-02-19 18:09:08 -0800 | [diff] [blame] | 595 | if (record) |
| 596 | { |
| 597 | m_registeredInterests.erase(prefix); |
| 598 | } |
Alexander Afanasyev | d982666 | 2013-01-28 11:18:06 -0800 | [diff] [blame] | 599 | |
| 600 | _LOG_TRACE ("<< clearInterestFilter"); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 601 | } |
| 602 | |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 603 | Name |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 604 | CcnxWrapper::getLocalPrefix () |
| 605 | { |
| 606 | struct ccn * tmp_handle = ccn_create (); |
| 607 | int res = ccn_connect (tmp_handle, NULL); |
| 608 | if (res < 0) |
| 609 | { |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 610 | return Name(); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 611 | } |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 612 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 613 | string retval = ""; |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 614 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 615 | struct ccn_charbuf *templ = ccn_charbuf_create(); |
| 616 | ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG); |
| 617 | ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG); |
| 618 | ccn_charbuf_append_closer(templ); /* </Name> */ |
| 619 | // XXX - use pubid if possible |
| 620 | ccn_charbuf_append_tt(templ, CCN_DTAG_MaxSuffixComponents, CCN_DTAG); |
| 621 | ccnb_append_number(templ, 1); |
| 622 | ccn_charbuf_append_closer(templ); /* </MaxSuffixComponents> */ |
| 623 | ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", 2); |
| 624 | ccn_charbuf_append_closer(templ); /* </Interest> */ |
| 625 | |
| 626 | struct ccn_charbuf *name = ccn_charbuf_create (); |
| 627 | res = ccn_name_from_uri (name, "/local/ndn/prefix"); |
| 628 | if (res < 0) { |
| 629 | } |
| 630 | else |
| 631 | { |
| 632 | struct ccn_fetch *fetch = ccn_fetch_new (tmp_handle); |
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 | 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] | 635 | NULL, 4, CCN_V_HIGHEST, 0); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 636 | if (stream == NULL) { |
| 637 | } |
| 638 | else |
| 639 | { |
| 640 | ostringstream os; |
| 641 | |
| 642 | int counter = 0; |
| 643 | char buf[256]; |
| 644 | while (true) { |
| 645 | res = ccn_fetch_read (stream, buf, sizeof(buf)); |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 646 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 647 | if (res == 0) { |
| 648 | break; |
| 649 | } |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 650 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 651 | if (res > 0) { |
| 652 | os << string(buf, res); |
| 653 | } else if (res == CCN_FETCH_READ_NONE) { |
| 654 | if (counter < 2) |
| 655 | { |
| 656 | ccn_run(tmp_handle, 1000); |
| 657 | counter ++; |
| 658 | } |
| 659 | else |
| 660 | { |
| 661 | break; |
| 662 | } |
| 663 | } else if (res == CCN_FETCH_READ_END) { |
| 664 | break; |
| 665 | } else if (res == CCN_FETCH_READ_TIMEOUT) { |
| 666 | break; |
| 667 | } else { |
| 668 | break; |
| 669 | } |
| 670 | } |
| 671 | retval = os.str (); |
| 672 | stream = ccn_fetch_close(stream); |
| 673 | } |
| 674 | fetch = ccn_fetch_destroy(fetch); |
| 675 | } |
| 676 | |
| 677 | ccn_charbuf_destroy (&name); |
| 678 | |
| 679 | ccn_disconnect (tmp_handle); |
| 680 | ccn_destroy (&tmp_handle); |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 681 | |
Zhenkai Zhu | cbbb888 | 2013-01-25 13:49:12 -0800 | [diff] [blame] | 682 | boost::algorithm::trim(retval); |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 683 | return Name(retval); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 684 | } |
| 685 | |
Zhenkai Zhu | 79264a4 | 2013-02-07 21:49:42 -0800 | [diff] [blame] | 686 | bool |
Zhenkai Zhu | dd1f14d | 2013-03-13 12:04:28 -0700 | [diff] [blame^] | 687 | CcnxWrapper::checkPcoIntegrity(PcoPtr &pco) |
Zhenkai Zhu | 79264a4 | 2013-02-07 21:49:42 -0800 | [diff] [blame] | 688 | { |
Zhenkai Zhu | dd1f14d | 2013-03-13 12:04:28 -0700 | [diff] [blame^] | 689 | bool checked = (ccn_verify_content(m_handle, pco->msg(), (ccn_parsed_ContentObject *)pco->pco()) == 0); |
| 690 | pco->setIntegrityChecked(checked); |
| 691 | return checked; |
Zhenkai Zhu | 79264a4 | 2013-02-07 21:49:42 -0800 | [diff] [blame] | 692 | } |
| 693 | |
Jared Lindblom | f83ff94 | 2013-03-01 00:01:49 -0800 | [diff] [blame] | 694 | // This is needed just for get function implementation |
| 695 | struct GetState |
| 696 | { |
| 697 | GetState (double maxWait) |
| 698 | { |
| 699 | double intPart, fraction; |
| 700 | fraction = modf (std::abs(maxWait), &intPart); |
| 701 | |
| 702 | m_maxWait = date_time::second_clock<boost::posix_time::ptime>::universal_time() |
| 703 | + boost::posix_time::seconds (intPart) |
| 704 | + boost::posix_time::microseconds (fraction * 1000000); |
| 705 | } |
| 706 | |
| 707 | PcoPtr |
| 708 | WaitForResult () |
| 709 | { |
| 710 | boost::unique_lock<boost::mutex> lock (m_mutex); |
| 711 | m_cond.timed_wait (lock, m_maxWait); |
| 712 | |
| 713 | return m_retval; |
| 714 | } |
| 715 | |
| 716 | void |
| 717 | DataCallback (Name name, PcoPtr pco) |
| 718 | { |
| 719 | m_retval = pco; |
| 720 | |
| 721 | boost::unique_lock<boost::mutex> lock (m_mutex); |
| 722 | m_cond.notify_one (); |
| 723 | } |
| 724 | |
| 725 | void |
| 726 | TimeoutCallback (Name name) |
| 727 | { |
| 728 | boost::unique_lock<boost::mutex> lock (m_mutex); |
| 729 | m_cond.notify_one (); |
| 730 | } |
| 731 | |
| 732 | private: |
| 733 | boost::posix_time::ptime m_maxWait; |
| 734 | |
| 735 | boost::mutex m_mutex; |
| 736 | boost::condition_variable m_cond; |
| 737 | |
| 738 | PcoPtr m_retval; |
| 739 | }; |
| 740 | |
| 741 | |
| 742 | PcoPtr |
| 743 | CcnxWrapper::get(const Name &interest, const Selectors &selectors, double maxWait/* = 4.0*/) |
| 744 | { |
| 745 | _LOG_TRACE (">> get: " << interest); |
| 746 | { |
| 747 | UniqueRecLock lock(m_mutex); |
| 748 | if (!m_running || !m_connected) |
| 749 | { |
| 750 | _LOG_ERROR ("<< get: not running or connected"); |
| 751 | return PcoPtr (); |
| 752 | } |
| 753 | } |
| 754 | |
| 755 | GetState state (maxWait); |
| 756 | this->sendInterest (interest, Closure (boost::bind (&GetState::DataCallback, &state, _1, _2), |
| 757 | boost::bind (&GetState::TimeoutCallback, &state, _1)), |
| 758 | selectors); |
| 759 | return state.WaitForResult (); |
| 760 | } |
| 761 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 762 | } |